Skip to content

Commit

Permalink
refactor: refactor WithDecompressOnly function and update related t…
Browse files Browse the repository at this point in the history
…ests

- Remove the boolean parameter from `WithDecompressOnly` function
- Update `WithDecompressOnly` function to always set `DecompressOnly` to true
- Update test cases to use the modified `WithDecompressOnly` function
- Improve the documentation for `WithDecompressOnly` function

Signed-off-by: appleboy <[email protected]>
  • Loading branch information
appleboy committed Jan 12, 2025
1 parent 969f96e commit 48d0307
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
4 changes: 2 additions & 2 deletions gzip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ func TestDecompressOnly(t *testing.T) {
req.Header.Add("Content-Encoding", "gzip")

router := gin.New()
router.Use(Gzip(NoCompression, WithDecompressOnly(true), WithDecompressFn(DefaultDecompressHandle)))
router.Use(Gzip(NoCompression, WithDecompressOnly(), WithDecompressFn(DefaultDecompressHandle)))
router.POST("/", func(c *gin.Context) {
if v := c.Request.Header.Get("Content-Encoding"); v != "" {
t.Errorf("unexpected `Content-Encoding`: %s header", v)
Expand Down Expand Up @@ -300,7 +300,7 @@ func TestGzipWithDecompressOnly(t *testing.T) {
req.Header.Add("Accept-Encoding", "gzip")

r := gin.New()
r.Use(Gzip(NoCompression, WithDecompressOnly(true), WithDecompressFn(DefaultDecompressHandle)))
r.Use(Gzip(NoCompression, WithDecompressOnly(), WithDecompressFn(DefaultDecompressHandle)))
r.POST("/", func(c *gin.Context) {
assert.Equal(t, c.Request.Header.Get("Content-Encoding"), "")
assert.Equal(t, c.Request.Header.Get("Content-Length"), "")
Expand Down
9 changes: 6 additions & 3 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,13 @@ func WithDecompressFn(decompressFn func(c *gin.Context)) Option {
}
}

// disable compression, only decompress incoming request
func WithDecompressOnly(decompressOnly bool) Option {
// WithDecompressOnly is an option that configures the gzip middleware to only
// decompress incoming requests without compressing the responses. When this
// option is enabled, the middleware will set the DecompressOnly field of the
// Options struct to true.
func WithDecompressOnly() Option {
return func(o *Options) {
o.DecompressOnly = decompressOnly
o.DecompressOnly = true
}
}

Expand Down

0 comments on commit 48d0307

Please sign in to comment.