Skip to content

Commit

Permalink
Fixed test when context is nil
Browse files Browse the repository at this point in the history
  • Loading branch information
dooman87 committed Feb 2, 2025
1 parent 92921a1 commit 9e144e8
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
4 changes: 4 additions & 0 deletions img/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
12 changes: 11 additions & 1 deletion img/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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)
Expand Down

0 comments on commit 9e144e8

Please sign in to comment.