Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions imagor.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,6 @@ func (app *Imagor) ServeHTTP(w http.ResponseWriter, r *http.Request) {
return
}
blob, err := checkBlob(app.Do(r, p))
if !isBlobEmpty(blob) {
w.Header().Set("Content-Type", blob.ContentType())
}
if err != nil {
if errors.Is(err, context.Canceled) {
w.WriteHeader(499)
Expand All @@ -178,7 +175,8 @@ func (app *Imagor) ServeHTTP(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(e.Code)
return
}
if !isBlobEmpty(blob) {
if !isBlobEmpty(blob) && !p.Meta {
w.Header().Set("Content-Type", blob.ContentType())
reader, size, _ := blob.NewReader()
if reader != nil {
w.WriteHeader(e.Code)
Expand All @@ -194,6 +192,7 @@ func (app *Imagor) ServeHTTP(w http.ResponseWriter, r *http.Request) {
return
}
reader, size, _ := blob.NewReader()
w.Header().Set("Content-Type", blob.ContentType())
setCacheHeaders(w, app.CacheHeaderTTL, app.CacheHeaderSWR)
writeBody(w, r, reader, size)
return
Expand Down
9 changes: 9 additions & 0 deletions imagor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -528,18 +528,21 @@ func TestWithLoadersStoragesProcessors(t *testing.T) {
w := httptest.NewRecorder()
app.ServeHTTP(w, httptest.NewRequest(
http.MethodGet, "https://example.com/unsafe/foo", nil))
time.Sleep(time.Millisecond * 10)
assert.Equal(t, 200, w.Code)
assert.Equal(t, "bark", w.Body.String())

w = httptest.NewRecorder()
app.ServeHTTP(w, httptest.NewRequest(
http.MethodGet, "https://example.com/unsafe/bar", nil))
time.Sleep(time.Millisecond * 10)
assert.Equal(t, 200, w.Code)
assert.Equal(t, "bar", w.Body.String())

w = httptest.NewRecorder()
app.ServeHTTP(w, httptest.NewRequest(
http.MethodGet, "https://example.com/unsafe/ping", nil))
time.Sleep(time.Millisecond * 10)
assert.Equal(t, 200, w.Code)
assert.Equal(t, "pong", w.Body.String())
time.Sleep(time.Millisecond * 10) // make sure storage reached
Expand All @@ -552,13 +555,15 @@ func TestWithLoadersStoragesProcessors(t *testing.T) {
w := httptest.NewRecorder()
app.ServeHTTP(w, httptest.NewRequest(
http.MethodGet, "https://example.com/unsafe/", nil))
time.Sleep(time.Millisecond * 10)
assert.Equal(t, 404, w.Code)
assert.Equal(t, jsonStr(ErrNotFound), w.Body.String())
})
t.Run(fmt.Sprintf("empty %d", i), func(t *testing.T) {
w := httptest.NewRecorder()
app.ServeHTTP(w, httptest.NewRequest(
http.MethodGet, "https://example.com/unsafe/empty", nil))
time.Sleep(time.Millisecond * 10)
assert.Equal(t, 404, w.Code)
assert.Equal(t, jsonStr(ErrNotFound), w.Body.String())
assert.Nil(t, store.Map["empty"])
Expand All @@ -567,13 +572,15 @@ func TestWithLoadersStoragesProcessors(t *testing.T) {
w := httptest.NewRecorder()
app.ServeHTTP(w, httptest.NewRequest(
http.MethodGet, "https://example.com/unsafe/boooo", nil))
time.Sleep(time.Millisecond * 10)
assert.Equal(t, 404, w.Code)
assert.Equal(t, jsonStr(ErrNotFound), w.Body.String())
})
t.Run(fmt.Sprintf("unexpected error %d", i), func(t *testing.T) {
w := httptest.NewRecorder()
app.ServeHTTP(w, httptest.NewRequest(
http.MethodGet, "https://example.com/unsafe/boom", nil))
time.Sleep(time.Millisecond * 10)
assert.Equal(t, 500, w.Code)
assert.Equal(t, jsonStr(NewError("unexpected error", 500)), w.Body.String())
assert.Nil(t, store.Map["boom"])
Expand All @@ -582,6 +589,7 @@ func TestWithLoadersStoragesProcessors(t *testing.T) {
w := httptest.NewRecorder()
app.ServeHTTP(w, httptest.NewRequest(
http.MethodGet, "https://example.com/unsafe/dood", nil))
time.Sleep(time.Millisecond * 10)
assert.Equal(t, 500, w.Code)
assert.Equal(t, "dood", w.Body.String())
assert.Nil(t, store.Map["dood"])
Expand All @@ -590,6 +598,7 @@ func TestWithLoadersStoragesProcessors(t *testing.T) {
w := httptest.NewRecorder()
app.ServeHTTP(w, httptest.NewRequest(
http.MethodGet, "https://example.com/unsafe/poop", nil))
time.Sleep(time.Millisecond * 10)
assert.Equal(t, ErrUnsupportedFormat.Code, w.Code)
assert.Equal(t, "poop", w.Body.String())
assert.Nil(t, store.Map["poop"])
Expand Down
6 changes: 6 additions & 0 deletions vips/processor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,12 @@ func TestProcessor(t *testing.T) {
buf, err := ioutil.ReadFile(testDataDir + "/../README.md")
require.NoError(t, err)
assert.Equal(t, buf, w.Body.Bytes(), "should return original file")

w = httptest.NewRecorder()
app.ServeHTTP(w, httptest.NewRequest(
http.MethodGet, "/unsafe/meta/README.md", nil))
assert.Equal(t, 406, w.Code)
assert.Equal(t, "application/json", w.Header().Get("Content-Type"))
})
t.Run("resolution exceeded", func(t *testing.T) {
app := imagor.New(
Expand Down