Skip to content

Commit

Permalink
Fix Content-Type bug in FS
Browse files Browse the repository at this point in the history
Fixes #417
  • Loading branch information
erikdubbelboer committed Sep 20, 2018
1 parent da9ba61 commit d53df67
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
3 changes: 2 additions & 1 deletion fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -823,7 +823,8 @@ func (h *fsHandler) handleRequest(ctx *RequestCtx) {
}
}
}
if len(ctx.Response.Header.ContentType()) == 0 {
hdr.noDefaultContentType = true
if len(hdr.ContentType()) == 0 {
ctx.SetContentType(ff.contentType)
}
ctx.SetStatusCode(statusCode)
Expand Down
22 changes: 22 additions & 0 deletions fs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -682,3 +682,25 @@ func testFileExtension(t *testing.T, path string, compressed bool, compressedFil
t.Fatalf("unexpected file extension for file %q: %q. Expecting %q", path, ext, expectedExt)
}
}

func TestServeFileContentType(t *testing.T) {
var ctx RequestCtx
var req Request
req.Header.SetMethod("GET")
req.SetRequestURI("http://foobar.com/baz")
ctx.Init(&req, nil, nil)

ServeFile(&ctx, "testdata/test.png")

var resp Response
s := ctx.Response.String()
br := bufio.NewReader(bytes.NewBufferString(s))
if err := resp.Read(br); err != nil {
t.Fatalf("unexpected error: %s", err)
}

expected := []byte("image/png")
if !bytes.Equal(resp.Header.ContentType(), expected) {
t.Fatalf("Unexpected Content-Type, expected: %q got %q", expected, resp.Header.ContentType())
}
}
1 change: 1 addition & 0 deletions testdata/test.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit d53df67

Please sign in to comment.