* refactor(context): use http.StatusContinue constant instead of magic number 100
Replace magic number 100 with http.StatusContinue constant for better
code clarity and maintainability in bodyAllowedForStatus function.
Also fix typo in logger_test.go: colorForLantency -> colorForLatency
Fixes#4489
* test: improve coverage to meet 99% threshold
- Add TestContextGetRawDataNilBody to cover nil body error case
- Add SameSiteDefaultMode test case in SetCookieData
- Add 400ms latency test case for LatencyColor
- Add TestMarshalXMLforHSuccess for successful XML marshaling
Coverage improved from 99.1% to 99.2%
* fix: use require for error assertions (testifylint)
Use http.StatusContinue and http.StatusOK instead of hardcoded 100 and
199 for the 1xx informational status range check, consistent with the
pattern already used in logger.go.
Fixes#4489
* fix: correct typos and improve documentation clarity
- Fix typo "Oupps" to "Oops" in recovery test panic messages
- Fix confusing documentation in Bind() and ShouldBind() methods
that incorrectly stated "JSON or XML as a JSON input"
- Remove double period in StaticFileFS documentation comment
- Remove unused ErrorTypeNu constant that had duplicate comment
with ErrorTypeAny and was never used in the codebase
* tech: Fix the pull request routing link
- Update linting configuration to exclude G115 gosec check instead of including specific checks
- Add the safeInt8 helper for safer type conversions and use it to prevent int8 overflow in middleware handler execution
- Group related constants and variables together for better organization in gin.go
- Refactor HTTP server instantiation to use a dedicated http.Server object for all Run methods
- Add the safeUint16 helper and use it to safely handle conversions in tree node functions to prevent uint16 overflow
Signed-off-by: appleboy <appleboy.tw@gmail.com>
- Added detailed example for ShouldBindJSON
- Added consistent descriptive comments for ShouldBindXML, ShouldBindQuery, ShouldBindYAML, ShouldBindTOML, ShouldBindPlain, ShouldBindHeader, ShouldBindUri
- Makes binding method usage clearer for new users
* refactor(context): refactor keys to `map[any]any`
Signed-off-by: Flc゛ <four_leaf_clover@foxmail.com>
* refactor(context): refactor keys to `map[any]any`
Signed-off-by: Flc゛ <four_leaf_clover@foxmail.com>
* style(context): remove empty lines before GetInt16, GetIntSlice, and GetStringMapString methods
- Remove unnecessary empty lines in the context.go file
- Improve code readability and consistency
Signed-off-by: flc1125 <four_leaf_clover@foxmail.com>
* refactor(context): simplify GetStringSlice function
- Replace manual type assertion with generic getTyped function
- Reduce code duplication and improve type safety
Signed-off-by: flc1125 <four_leaf_clover@foxmail.com>
* test(context): improve context.Set and context.Get tests
- Split existing test into separate functions for different scenarios
- Add test for setting and getting values with any key type
- Add test for handling non-comparable keys
- Improve assertions to check for key existence and value correctness
Signed-off-by: flc1125 <four_leaf_clover@foxmail.com>
* refactor(context): replace fmt.Errorf with fmt.Sprintf in panic message
* test(context): remove trailing hyphen from context_test.go
* refactor(context): improve error message for missing key in context
- Remove unnecessary quotes around the key in the error message
- Simplify the error message format for better readability
* test(context): improve panic test message for non-existent key
---------
Signed-off-by: Flc゛ <four_leaf_clover@foxmail.com>
Signed-off-by: flc1125 <four_leaf_clover@foxmail.com>
* Bind: return 413 status code when error is `http.MaxBytesError`
The Go standard library includes a method `http.MaxBytesReader` that allows limiting the request body. For example, users can create a middleware like:
```go
func MiddlewareMaxBodySize(c *gin.Context) {
// Limit request body to 100 bytes
c.Request.Body = http.MaxBytesReader(c.Writer, c.Request.Body, 100)
c.Next()
}
```
When the body exceeds the limit, reading from the request body returns an error of type `http.MaxBytesError`.
This PR makes sure that when the error is of kind `http.MaxBytesError`, Gin returns the correct status code 413 (Request Entity Too Large) instead of a generic 400 (Bad Request).
* Disable test when using sonic
* Fix
* Disable for go-json too
* Add references to GitHub issues
* Test that the response is 400 for sonic and go-json
* enhance code imported by #3413
if it needs to check if the handler is nil, tie c.index shall
always ++
* test: refactor test context initialization and handler logic
- Remove an empty line in `TestContextInitQueryCache`
- Add `TestContextNext` function with tests for `Next` method behavior with no handlers, one handler, and multiple handlers
Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
---------
Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
Co-authored-by: zjj <zhong2plus@gmail.com>
This PR introduces a generic function, getTyped[T any], to simplify value retrieval in the Context struct. It replaces repetitive type assertions in the GetString GetBool etc. methods.
Co-authored-by: Maksim Konovalov <maksim.konovalov@vk.team>
Use assert.InDelta for float comparison with tolerance in TestContextGetFloat32
Remove unnecessary blank line in TestContextInitQueryCache
Replace anonymous struct with named contextKey type in TestContextWithFallbackValueFromRequestContext
Update context key handling in TestContextWithFallbackValueFromRequestContext to use contextKey type
* fixed#3404 2022-11-23
* up 2022-11-23
* refactor: refactor context handling and nil checks
- Refactor nil checks to improve readability in `context.go`
- Modify the control flow in `HandlerNames` and `Next` methods to continue on nil values before appending or invoking handlers in `context.go`
Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
* test: refactor context_test.go for clarity and efficiency
- Insert a `nil` value into the `HandlersChain` array in `context_test.go`
- Remove empty test functions in `context_test.go`
Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
---------
Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
Co-authored-by: Bo-Yi Wu <appleboy.tw@gmail.com>
* feat: ShouldBindBodyWith shortcut and change doc
* fix: yaml can parse json test case
* style: fix new test case in context_test.go
* chore: modify the code style to specify binding type
* chroe: gofmt modifies the code format
* Optimize the Copy method of the Context struct: using 'make' to initialize the map('cp.Keys') with a length of 'c.Keys'; avoiding repeatedly assiging the 'params' to 'context'.
* Using temporary variables to save c.Keys and c.Params to prevent them from changing during the copying process.
---------
Co-authored-by: huangzw <huangzw@hsmap.com>
* fix lack of escaping of filename in Content-Disposition
* add test for Content-Disposition filename escaping process
* fix filename escape bypass problem
fix backslashes before backquotes were not properly escaped problem.