Skip to content

Commit

Permalink
fix: test error and decompressMiddleware doesn't work
Browse files Browse the repository at this point in the history
  • Loading branch information
jkskj committed Jul 2, 2024
1 parent 56f7ab9 commit a136d45
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 4 deletions.
6 changes: 5 additions & 1 deletion client_middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,11 @@ func (g *gzipClientMiddleware) ClientMiddleware(next client.Endpoint) client.End
return
}
if fn := g.DecompressFnForClient; fn != nil && strings.EqualFold(resp.Header.Get("Content-Encoding"), "gzip") {
fn(next)
f := fn(next)
err = f(ctx, req, resp)
if err != nil {
return err
}
}
return nil
}
Expand Down
4 changes: 3 additions & 1 deletion gzip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -405,11 +405,12 @@ func TestNoGzipForClient(t *testing.T) {
func TestDecompressGzipForClient(t *testing.T) {
h := server.Default(server.WithHostPorts("127.0.0.1:2338"))

h.Use(Gzip(DefaultCompression, WithDecompressFn(DefaultDecompressHandle)))
h.GET("/", func(ctx context.Context, c *app.RequestContext) {
c.Header("Content-Length", strconv.Itoa(len(testResponse)))
c.String(200, testResponse)
})
h.Use(Gzip(DefaultCompression, WithDecompressFn(DefaultDecompressHandle)))

go h.Spin()

time.Sleep(time.Second)
Expand All @@ -425,6 +426,7 @@ func TestDecompressGzipForClient(t *testing.T) {

req.SetBodyString("bar")
req.SetRequestURI("http://127.0.0.1:2338/")
req.SetHeader("Accept-Encoding", "gzip")

err = cli.Do(context.Background(), req, res)
if err != nil {
Expand Down
6 changes: 4 additions & 2 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
package gzip

import (
"bytes"
"context"
"net/http"
"regexp"
Expand Down Expand Up @@ -216,7 +217,8 @@ func DefaultDecompressMiddlewareForClient(next client.Endpoint) client.Endpoint
}
resp.Header.DelBytes([]byte("Content-Encoding"))
resp.Header.DelBytes([]byte("Content-Length"))
resp.SetBody(gunzipBytes)
return next(ctx, req, resp)
resp.Header.DelBytes([]byte("Vary"))
resp.SetBodyStream(bytes.NewBuffer(gunzipBytes), len(gunzipBytes))
return nil
}
}

0 comments on commit a136d45

Please sign in to comment.