Many changes
This commit is contained in:
+14
-53
@@ -16,10 +16,10 @@ import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"gopkg.in/yaml.v3"
|
||||
"scratchbox/internal/config"
|
||||
httpapi "scratchbox/internal/http"
|
||||
"scratchbox/internal/storage"
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
func repoRoot(t *testing.T) string {
|
||||
@@ -125,44 +125,6 @@ security:
|
||||
}
|
||||
}
|
||||
|
||||
func TestMainProcessHTTPInitFails(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
tmp := t.TempDir()
|
||||
cfgPath := filepath.Join(tmp, "config.yaml")
|
||||
cfg := `
|
||||
server:
|
||||
listen_addr: "127.0.0.1:0"
|
||||
limits:
|
||||
max_upload_size: "1MB"
|
||||
default_ttl: "1h"
|
||||
storage:
|
||||
data_dir: "` + filepath.Join(tmp, "data") + `"
|
||||
cleanup_interval: "10s"
|
||||
security:
|
||||
allowed_ips: []
|
||||
trust_proxy_headers: false
|
||||
rate_limit:
|
||||
enabled: true
|
||||
requests_per_minute: 10
|
||||
burst: 1
|
||||
`
|
||||
if err := os.WriteFile(cfgPath, []byte(cfg), 0o644); err != nil {
|
||||
t.Fatalf("WriteFile() error = %v", err)
|
||||
}
|
||||
|
||||
cmd := exec.Command(os.Args[0], "-test.run=TestMainHelperProcess")
|
||||
cmd.Dir = repoRoot(t)
|
||||
cmd.Env = append(os.Environ(),
|
||||
"GO_WANT_MAIN_HELPER=1",
|
||||
"MAIN_HELPER_MODE=http-fail",
|
||||
"MAIN_HELPER_CONFIG="+cfgPath,
|
||||
)
|
||||
if err := cmd.Run(); err == nil {
|
||||
t.Fatalf("expected helper process failure for http init")
|
||||
}
|
||||
}
|
||||
|
||||
func TestMainHelperProcess(t *testing.T) {
|
||||
if os.Getenv("GO_WANT_MAIN_HELPER") != "1" {
|
||||
return
|
||||
@@ -193,14 +155,6 @@ func TestMainHelperProcess(t *testing.T) {
|
||||
os.Args = []string{"scratchbox", "server", "--config", cfgPath}
|
||||
main()
|
||||
os.Exit(0)
|
||||
case "http-fail":
|
||||
cfgPath := os.Getenv("MAIN_HELPER_CONFIG")
|
||||
if err := os.Chdir(t.TempDir()); err != nil {
|
||||
os.Exit(2)
|
||||
}
|
||||
os.Args = []string{"scratchbox", "server", "--config", cfgPath}
|
||||
main()
|
||||
os.Exit(0)
|
||||
default:
|
||||
os.Exit(2)
|
||||
}
|
||||
@@ -222,17 +176,24 @@ func TestGenerateConfigCommandWritesDefaultYAML(t *testing.T) {
|
||||
if out.Len() == 0 {
|
||||
t.Fatalf("generate-config output is empty")
|
||||
}
|
||||
output := out.String()
|
||||
if !strings.Contains(output, "# HTTP listener settings.") {
|
||||
t.Fatalf("generate-config output missing expected section comment")
|
||||
}
|
||||
if !strings.Contains(output, "# Maximum upload request body size. Supports B, KB, MB, GB and KiB, MiB, GiB (case-insensitive). Defaults to bytes when unit is omitted (example: 1048576).") {
|
||||
t.Fatalf("generate-config output missing expected field comment")
|
||||
}
|
||||
|
||||
var cfg config.Config
|
||||
if err := yaml.Unmarshal(out.Bytes(), &cfg); err != nil {
|
||||
if err := yaml.Unmarshal([]byte(output), &cfg); err != nil {
|
||||
t.Fatalf("yaml.Unmarshal() error = %v", err)
|
||||
}
|
||||
|
||||
if cfg.Server.ListenAddr != ":8080" {
|
||||
t.Fatalf("server.listen_addr = %q, want %q", cfg.Server.ListenAddr, ":8080")
|
||||
}
|
||||
if cfg.Limits.MaxUploadSize != "100MB" {
|
||||
t.Fatalf("limits.max_upload_size = %q, want %q", cfg.Limits.MaxUploadSize, "100MB")
|
||||
if cfg.Limits.MaxUploadSize != "100MiB" {
|
||||
t.Fatalf("limits.max_upload_size = %q, want %q", cfg.Limits.MaxUploadSize, "100MiB")
|
||||
}
|
||||
if cfg.Storage.DataDir != "./data" {
|
||||
t.Fatalf("storage.data_dir = %q, want %q", cfg.Storage.DataDir, "./data")
|
||||
@@ -349,7 +310,7 @@ security:
|
||||
t.Fatalf("create payload missing id")
|
||||
}
|
||||
|
||||
rawResp, err := client.Get(baseURL + "/raw/" + id)
|
||||
rawResp, err := client.Get(baseURL + "/api/raw/" + id)
|
||||
if err != nil {
|
||||
t.Fatalf("client.Get(raw) error = %v", err)
|
||||
}
|
||||
@@ -377,7 +338,7 @@ security:
|
||||
if err != nil {
|
||||
t.Fatalf("ReadAll(view) error = %v", err)
|
||||
}
|
||||
if !strings.Contains(string(viewBody), "Scratch "+id) {
|
||||
t.Fatalf("view body missing scratch id")
|
||||
if !strings.Contains(string(viewBody), `id="app"`) {
|
||||
t.Fatalf("view body missing SPA mount point")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user