Add mock fetcher and service tests for imgcache

Introduces Fetcher interface, mock implementation for testing,
and ApplyMigrations helper for test database setup.
This commit is contained in:
2026-01-08 07:39:18 -08:00
parent 1f0ec59eb5
commit 2cbafe374c
5 changed files with 843 additions and 6 deletions

View File

@@ -14,7 +14,7 @@ import (
// Service implements the ImageCache interface, orchestrating cache, fetcher, and processor.
type Service struct {
cache *Cache
fetcher *HTTPFetcher
fetcher Fetcher
processor Processor
signer *Signer
whitelist *HostWhitelist
@@ -25,8 +25,10 @@ type Service struct {
type ServiceConfig struct {
// Cache is the cache instance
Cache *Cache
// FetcherConfig configures the upstream fetcher
// FetcherConfig configures the upstream fetcher (ignored if Fetcher is set)
FetcherConfig *FetcherConfig
// Fetcher is an optional custom fetcher (for testing)
Fetcher Fetcher
// SigningKey is the HMAC signing key (empty disables signing)
SigningKey string
// Whitelist is the list of hosts that don't require signatures
@@ -41,9 +43,16 @@ func NewService(cfg *ServiceConfig) (*Service, error) {
return nil, errors.New("cache is required")
}
fetcherCfg := cfg.FetcherConfig
if fetcherCfg == nil {
fetcherCfg = DefaultFetcherConfig()
// Use custom fetcher if provided, otherwise create HTTP fetcher
var fetcher Fetcher
if cfg.Fetcher != nil {
fetcher = cfg.Fetcher
} else {
fetcherCfg := cfg.FetcherConfig
if fetcherCfg == nil {
fetcherCfg = DefaultFetcherConfig()
}
fetcher = NewHTTPFetcher(fetcherCfg)
}
var signer *Signer
@@ -58,7 +67,7 @@ func NewService(cfg *ServiceConfig) (*Service, error) {
return &Service{
cache: cfg.Cache,
fetcher: NewHTTPFetcher(fetcherCfg),
fetcher: fetcher,
processor: NewImageProcessor(),
signer: signer,
whitelist: NewHostWhitelist(cfg.Whitelist),