refactor: extract whitelist package from internal/imgcache
All checks were successful
check / check (push) Successful in 57s
All checks were successful
check / check (push) Successful in 57s
Move HostWhitelist, NewHostWhitelist, IsWhitelisted, IsEmpty, and Count from internal/imgcache into the new internal/whitelist package. The whitelist package is completely self-contained, depending only on net/url and strings from the standard library. Updated import in internal/imgcache/service.go to use the new package. Tests moved and adapted to external test style (package whitelist_test). Part of #39
This commit is contained in:
@@ -12,6 +12,7 @@ import (
|
|||||||
|
|
||||||
"github.com/dustin/go-humanize"
|
"github.com/dustin/go-humanize"
|
||||||
"sneak.berlin/go/pixa/internal/imageprocessor"
|
"sneak.berlin/go/pixa/internal/imageprocessor"
|
||||||
|
"sneak.berlin/go/pixa/internal/whitelist"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Service implements the ImageCache interface, orchestrating cache, fetcher, and processor.
|
// Service implements the ImageCache interface, orchestrating cache, fetcher, and processor.
|
||||||
@@ -20,7 +21,7 @@ type Service struct {
|
|||||||
fetcher Fetcher
|
fetcher Fetcher
|
||||||
processor *imageprocessor.ImageProcessor
|
processor *imageprocessor.ImageProcessor
|
||||||
signer *Signer
|
signer *Signer
|
||||||
whitelist *HostWhitelist
|
whitelist *whitelist.HostWhitelist
|
||||||
log *slog.Logger
|
log *slog.Logger
|
||||||
allowHTTP bool
|
allowHTTP bool
|
||||||
maxResponseSize int64
|
maxResponseSize int64
|
||||||
@@ -85,7 +86,7 @@ func NewService(cfg *ServiceConfig) (*Service, error) {
|
|||||||
fetcher: fetcher,
|
fetcher: fetcher,
|
||||||
processor: imageprocessor.New(imageprocessor.Params{MaxInputBytes: maxResponseSize}),
|
processor: imageprocessor.New(imageprocessor.Params{MaxInputBytes: maxResponseSize}),
|
||||||
signer: signer,
|
signer: signer,
|
||||||
whitelist: NewHostWhitelist(cfg.Whitelist),
|
whitelist: whitelist.NewHostWhitelist(cfg.Whitelist),
|
||||||
log: log,
|
log: log,
|
||||||
allowHTTP: allowHTTP,
|
allowHTTP: allowHTTP,
|
||||||
maxResponseSize: maxResponseSize,
|
maxResponseSize: maxResponseSize,
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
package imgcache
|
// Package whitelist provides host-based URL whitelisting for the image proxy.
|
||||||
|
package whitelist
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"net/url"
|
"net/url"
|
||||||
@@ -1,8 +1,10 @@
|
|||||||
package imgcache
|
package whitelist_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"net/url"
|
"net/url"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"sneak.berlin/go/pixa/internal/whitelist"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestHostWhitelist_IsWhitelisted(t *testing.T) {
|
func TestHostWhitelist_IsWhitelisted(t *testing.T) {
|
||||||
@@ -94,7 +96,7 @@ func TestHostWhitelist_IsWhitelisted(t *testing.T) {
|
|||||||
|
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
w := NewHostWhitelist(tt.patterns)
|
w := whitelist.NewHostWhitelist(tt.patterns)
|
||||||
|
|
||||||
var u *url.URL
|
var u *url.URL
|
||||||
if tt.testURL != "" {
|
if tt.testURL != "" {
|
||||||
@@ -143,7 +145,7 @@ func TestHostWhitelist_IsEmpty(t *testing.T) {
|
|||||||
|
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
w := NewHostWhitelist(tt.patterns)
|
w := whitelist.NewHostWhitelist(tt.patterns)
|
||||||
if got := w.IsEmpty(); got != tt.want {
|
if got := w.IsEmpty(); got != tt.want {
|
||||||
t.Errorf("IsEmpty() = %v, want %v", got, tt.want)
|
t.Errorf("IsEmpty() = %v, want %v", got, tt.want)
|
||||||
}
|
}
|
||||||
@@ -181,7 +183,7 @@ func TestHostWhitelist_Count(t *testing.T) {
|
|||||||
|
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
w := NewHostWhitelist(tt.patterns)
|
w := whitelist.NewHostWhitelist(tt.patterns)
|
||||||
if got := w.Count(); got != tt.want {
|
if got := w.Count(); got != tt.want {
|
||||||
t.Errorf("Count() = %v, want %v", got, tt.want)
|
t.Errorf("Count() = %v, want %v", got, tt.want)
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user