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

Conversation

zhyee
Copy link
Contributor

@zhyee zhyee commented Feb 16, 2023

consider this code:

func Timeout() gin.HandlerFunc {
	return timeout.New(timeout.WithTimeout(time.Second*1),
		timeout.WithHandler(func(ctx *gin.Context) {
			ctx.Next()
		}),
		timeout.WithResponse(func(ctx *gin.Context) {
			ctx.String(http.StatusRequestTimeout, "timeout")
		}),
	)
}

func Logger() gin.HandlerFunc {
	return func(c *gin.Context) {
		c.Next()
		// access the status we are sending
		status := c.Writer.Status() // Bug: in any middleware used after this middleware, the result will always be 200.
		log.Println(status)
	}
}

func main() {
	r := gin.New()
	r.Use(Timeout())
	r.Use(Logger())

	r.GET("/test", func(c *gin.Context) {
		c.Status(http.StatusInternalServerError)
	})
	// Listen and serve on 0.0.0.0:8080
	r.Run(":8080")
}

@zhyee
Copy link
Contributor Author

zhyee commented Feb 16, 2023

issue: #47

@codecov-commenter
Copy link

codecov-commenter commented Mar 9, 2023

Codecov Report

Attention: 3 lines in your changes are missing coverage. Please review.

Comparison is base (e7c3a7b) 94.91% compared to head (fe2c126) 92.91%.
Report is 1 commits behind head on master.

Files Patch % Lines
writer.go 40.00% 2 Missing and 1 partial ⚠️

❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@            Coverage Diff             @@
##           master      #46      +/-   ##
==========================================
- Coverage   94.91%   92.91%   -2.01%     
==========================================
  Files           4        4              
  Lines         118      127       +9     
==========================================
+ Hits          112      118       +6     
- Misses          4        6       +2     
- Partials        2        3       +1     
Flag Coverage Δ
go- ?
go-1.18 ?
go-1.19 92.91% <70.00%> (+3.08%) ⬆️
go-1.20 ?
go-1.21 ?
macos-latest ?
ubuntu-latest 92.91% <70.00%> (-2.01%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@bramsvs
Copy link

bramsvs commented Jul 7, 2023

@zhyee @appleboy any chance this can be reviewed & merged? :)

@millotp
Copy link

millotp commented Nov 16, 2023

Hey, this would solve a bug for us too, can we get an update on this ?

…r.Status() is incorrect in other custom gin middlewares.
@zhyee
Copy link
Contributor Author

zhyee commented Nov 24, 2023

@appleboy Hi, I added a unit test to cover this bug fix, hope to be reviewed.

@appleboy appleboy added the bug Something isn't working label Nov 24, 2023
@appleboy appleboy merged commit c0a749e into gin-contrib:master Nov 24, 2023
3 of 8 checks passed
@appleboy
Copy link
Member

can't pass the testing @zhyee

@appleboy
Copy link
Member

I created the PR #61

appleboy added a commit that referenced this pull request Nov 25, 2023
* refactor: remove unused code related to writer status

- Remove the `strconv` import in `timeout_test.go`
- Remove the `TestWriter_Status` test function in `timeout_test.go`
- Remove the `Status` method in `writer.go`

Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>

* style: remove empty line in timeout_test.go

- Remove an empty line in `timeout_test.go`

Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>

---------

Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
@zhyee zhyee deleted the bugfix branch August 29, 2024 05:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants