From 9e144e854af29352a529d78aee575f0fd7334417 Mon Sep 17 00:00:00 2001 From: Dmitry Pokidov <124110+dooman87@users.noreply.github.com> Date: Sun, 2 Feb 2025 21:02:15 +1100 Subject: [PATCH] Fixed test when context is nil --- img/service.go | 4 ++++ img/service_test.go | 12 +++++++++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/img/service.go b/img/service.go index 0a86dab..2beda35 100644 --- a/img/service.go +++ b/img/service.go @@ -409,6 +409,10 @@ func NewContextWithHeaders(ctx context.Context, headers *http.Header) context.Co } func HeaderFromContext(ctx context.Context) (*http.Header, bool) { + if ctx == nil { + return nil, false + } + header, ok := ctx.Value(headersKey(0)).(*http.Header) return header, ok } diff --git a/img/service_test.go b/img/service_test.go index b7d6b73..74280d5 100644 --- a/img/service_test.go +++ b/img/service_test.go @@ -697,7 +697,7 @@ func FuzzService_ResizeUrl(f *testing.F) { }) } -func TestHeaderFromContext(t *testing.T) { +func TestHeaderFromContext_NoHeader(t *testing.T) { header, ok := img.HeaderFromContext(context.Background()) if header != nil { t.Errorf("expected nil header") @@ -707,6 +707,16 @@ func TestHeaderFromContext(t *testing.T) { } } +func TestHeaderFromContext_NoContext(t *testing.T) { + header, ok := img.HeaderFromContext(nil) + if header != nil { + t.Errorf("expected nil header") + } + if ok { + t.Errorf("expected ok to be false") + } +} + func createService(t *testing.T) *img.Service { img.CacheTTL = 86400 s, err := img.NewService(&loaderMock{}, &resizerMock{}, 1)