Skip to content

Commit

Permalink
fix: override header for processed blob (#420)
Browse files Browse the repository at this point in the history
* fix: fix override header for processed blob

* fix: fix override header for processed blob
  • Loading branch information
cshum authored Mar 17, 2024
1 parent dc32cdb commit fe944a0
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
3 changes: 3 additions & 0 deletions imagor.go
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,9 @@ func (app *Imagor) Do(r *http.Request, p imagorpath.Params) (blob *Blob, err err
for _, processor := range app.Processors {
b, e := checkBlob(processor.Process(ctx, blob, forwardP, load))
if !isBlobEmpty(b) {
if blob != nil && blob.Header != nil && b.Header == nil {
b.Header = blob.Header // forward blob Header
}
blob = b // forward Blob to next processor if exists
}
if e == nil {
Expand Down
10 changes: 7 additions & 3 deletions imagor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -345,13 +345,17 @@ func TestWithOverrideHeader(t *testing.T) {
blob.Header.Set("asdf", "fghj")
return blob, nil
})),
WithProcessors(processorFunc(func(ctx context.Context, blob *Blob, p imagorpath.Params, load LoadFunc) (*Blob, error) {
out := NewBlobFromBytes([]byte("processed"))
out.SetContentType("boom")
return out, nil
})),
)
w := httptest.NewRecorder()
app.ServeHTTP(w, httptest.NewRequest(
http.MethodGet, "https://example.com/unsafe/filters:fill(red):raw()/gopher.png", nil))
http.MethodGet, "https://example.com/unsafe/filters:fill(red)/gopher.png", nil))
assert.Equal(t, 200, w.Code)
assert.Equal(t, "foo", w.Body.String())
assert.Equal(t, "script-src 'none'", w.Header().Get("Content-Security-Policy"))
assert.Equal(t, "processed", w.Body.String())
assert.Equal(t, "tada", w.Header().Get("Content-Type"))
assert.Equal(t, "fghj", w.Header().Get("ASDF"))
}
Expand Down

0 comments on commit fe944a0

Please sign in to comment.