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

console logger HTTP status code bug fixed #3453

Conversation

alirezaeftekhari
Copy link
Contributor

@manucorporat @thinkerou @appleboy
Hello Dears.
It looks like the console logger HTTP response code is not equal to the client(browser) HTTP status code in the case we set the response status code twice or more by mistake or whatever.
It would make some problems when we want to monitor our network traffic because the status code which we have is not right.
Please consider the following code:

	router := gin.Default()

	router.GET("/", func(c *gin.Context) {
		c.String(http.StatusOK, "Status OK")
		c.String(http.StatusUnauthorized, "Status Unauthorized")
	})

	router.Run()

If we make an HTTP request by curl:

curl -v 127.0.0.1:8080

We'll get 200 status by curl, but the console logger prints 401, and its not true.

I fixed this problem by preventing rewriting the HTTP status code in the response writer and It's working fine now.

Regards.

@appleboy
Copy link
Member

@alirezaeftekhari Please add unit testing.

@appleboy appleboy added this to the v1.9 milestone Dec 29, 2022
@alirezaeftekhari
Copy link
Contributor Author

@appleboy Done

@codecov
Copy link

codecov bot commented Jan 1, 2023

Codecov Report

Merging #3453 (20bf2f7) into master (8659ab5) will increase coverage by 0.00%.
The diff coverage is 100.00%.

@@           Coverage Diff           @@
##           master    #3453   +/-   ##
=======================================
  Coverage   98.63%   98.63%           
=======================================
  Files          42       42           
  Lines        3139     3140    +1     
=======================================
+ Hits         3096     3097    +1     
  Misses         29       29           
  Partials       14       14           
Flag Coverage Δ
98.63% <100.00%> (+<0.01%) ⬆️
go-1.16 ∅ <ø> (∅)
go-1.17 98.53% <100.00%> (+<0.01%) ⬆️
go-1.18 98.53% <100.00%> (+<0.01%) ⬆️
go-1.19 98.63% <100.00%> (+<0.01%) ⬆️
macos-latest 98.63% <100.00%> (+<0.01%) ⬆️
ubuntu-latest 98.63% <100.00%> (+<0.01%) ⬆️

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

Impacted Files Coverage Δ
response_writer.go 93.61% <100.00%> (+0.13%) ⬆️

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

@thinkerou thinkerou merged commit 41f2669 into gin-gonic:master Jan 2, 2023
@alirezaeftekhari alirezaeftekhari deleted the console-logger-http-status-patch branch January 2, 2023 21:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants