package httpapi import ( "io" "log" "net/http" "path/filepath" "testing" "time" "scratchbox/internal/config" "scratchbox/internal/storage" ) var testStorageKey = []byte("0123456789abcdef0123456789abcdef") type testServerOptions struct { maxUploadBytes int64 defaultTTL time.Duration rateLimitEnable bool } func newTestHandler(t *testing.T, opts testServerOptions) http.Handler { t.Helper() handler, _ := newTestHandlerAndStore(t, opts) return handler } func newTestHandlerAndStore(t *testing.T, opts testServerOptions) (http.Handler, *storage.FilesystemStore) { t.Helper() dataDir := filepath.Join(t.TempDir(), "data") store, err := storage.NewFilesystemStore(dataDir, testStorageKey) if err != nil { t.Fatalf("NewFilesystemStore() error = %v", err) } cfg := config.Config{ Server: config.ServerConfig{ ListenAddr: ":0", }, Limits: config.LimitsConfig{ MaxUploadSizeBytes: opts.maxUploadBytes, DefaultTTLDuration: opts.defaultTTL, RawCacheMaxBytes: 64 * 1024 * 1024, RawCacheMaxEntries: 32, }, Storage: config.StorageConfig{ DataDir: dataDir, }, Security: config.SecurityConfig{ RateLimitUI: config.RateLimitConfig{ Enabled: opts.rateLimitEnable, RequestsPerMinute: 120, Burst: 1, }, RateLimitAPIRead: config.RateLimitConfig{ Enabled: opts.rateLimitEnable, RequestsPerMinute: 120, Burst: 1, }, RateLimitAPIWrite: config.RateLimitConfig{ Enabled: opts.rateLimitEnable, RequestsPerMinute: 120, Burst: 1, }, HSTSEnabled: true, }, } srv := &Server{ cfg: cfg, store: store, logger: log.New(io.Discard, "", 0), rawCache: newRawContentCache(cfg.Limits.RawCacheMaxEntries, cfg.Limits.RawCacheMaxBytes), } return srv.Routes(), store }