Skip to content
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

Fix bug: the result will be incorrect when call gin.Context.Writer.Status() in our custom gin middleware used after this middleware #46

Merged
merged 2 commits into from
Nov 24, 2023
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
32 changes: 32 additions & 0 deletions timeout_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"context"
"net/http"
"net/http/httptest"
"strconv"
"sync"
"testing"
"time"
Expand Down Expand Up @@ -102,6 +103,37 @@
assert.Equal(t, "", w.Body.String())
}

func TestWriter_Status(t *testing.T) {
r := gin.New()

r.Use(New(
WithTimeout(1*time.Second),
WithHandler(func(c *gin.Context) {
c.Next()
}),
WithResponse(testResponse),
))

r.Use(func(c *gin.Context) {
c.Next()
statusInMW := c.Writer.Status()
c.Request.Header.Set("X-Status-Code-MW-Set", strconv.Itoa(statusInMW))
t.Logf("[%s] %s %s %d\n", time.Now().Format(time.RFC3339), c.Request.Method, c.Request.URL, statusInMW)
})

r.GET("/test", func(c *gin.Context) {
c.Writer.WriteHeader(http.StatusInternalServerError)
})

w := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodGet, "/test", nil)

r.ServeHTTP(w, req)

assert.Equal(t, w.Result().StatusCode, http.StatusInternalServerError)

Check failure on line 133 in timeout_test.go

View workflow job for this annotation

GitHub Actions / lint

response body must be closed (bodyclose)
assert.Equal(t, strconv.Itoa(http.StatusInternalServerError), req.Header.Get("X-Status-Code-MW-Set"))
}

func TestDeadlineExceeded(t *testing.T) {
var wg sync.WaitGroup
wg.Add(1)
Expand Down
10 changes: 10 additions & 0 deletions writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,16 @@ func (w *Writer) writeHeader(code int) {
w.code = code
}

// Status we must override Status func here,
// or the http status code returned by gin.Context.Writer.Status()
// will always be 200 in other custom gin middlewares.
func (w *Writer) Status() int {
if w.code == 0 || w.timeout {
return w.ResponseWriter.Status()
}
return w.code
}

// Header will get response headers
func (w *Writer) Header() http.Header {
return w.headers
Expand Down
Loading