Skip to content

#98 Bug/incorrect response code for empty body #111

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
4 changes: 4 additions & 0 deletions handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ func (g *gzipHandler) Handle(c *gin.Context) {
if c.Writer.Size() < 0 {
// do not write gzip footer when nothing is written to the response body
gz.Reset(io.Discard)

// revert back to the original gin ResponseWriter to allow gin to handle the empty response (i.e. 404)
c.Writer = c.Writer.(*gzipWriter).ResponseWriter
c.Header(headerContentEncoding, "")
}
_ = gz.Close()
if c.Writer.Size() > -1 {
Expand Down
21 changes: 20 additions & 1 deletion handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,39 @@ func TestHandleGzip(t *testing.T) {
acceptEncoding string
expectedContentEncoding string
expectedBody string
expectedStatus int
}{
{
name: "Gzip compression",
path: "/",
acceptEncoding: "gzip",
expectedContentEncoding: "gzip",
expectedBody: "Gzip Test Response",
expectedStatus: http.StatusOK,
},
{
name: "No compression",
path: "/",
acceptEncoding: "",
expectedContentEncoding: "",
expectedBody: "Gzip Test Response",
expectedStatus: http.StatusOK,
},
{
name: "Bad path, no compression",
path: "/bad-path",
acceptEncoding: "",
expectedContentEncoding: "",
expectedBody: "404 page not found",
expectedStatus: http.StatusNotFound,
},
{
name: "Bad path, with compression",
path: "/bad-path",
acceptEncoding: "gzip",
expectedContentEncoding: "",
expectedBody: "404 page not found",
expectedStatus: http.StatusNotFound,
},
}

Expand All @@ -53,7 +72,7 @@ func TestHandleGzip(t *testing.T) {
w := httptest.NewRecorder()
router.ServeHTTP(w, req)

assert.Equal(t, http.StatusOK, w.Code)
assert.Equal(t, tt.expectedStatus, w.Code)
assert.Equal(t, tt.expectedContentEncoding, w.Header().Get("Content-Encoding"))

if tt.expectedContentEncoding == "gzip" {
Expand Down