Skip to content

Commit

Permalink
fix: test error and decompressMiddleware doesn't work (#20)
Browse files Browse the repository at this point in the history
* fix: test error and decompressMiddleware doesn't work

* fix: return error in ClientMiddleware

* chore: optimize license header
  • Loading branch information
jkskj authored Jul 10, 2024
1 parent 56f7ab9 commit 7b39e10
Show file tree
Hide file tree
Showing 8 changed files with 21 additions and 13 deletions.
10 changes: 7 additions & 3 deletions client_middleware.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2023 CloudWeGo Authors
* Copyright 2024 CloudWeGo Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -82,10 +82,14 @@ func (g *gzipClientMiddleware) ClientMiddleware(next client.Endpoint) client.End

err = next(ctx, req, resp)
if err != nil {
return
return err
}
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
2 changes: 1 addition & 1 deletion example/standard/main.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2023 CloudWeGo Authors
* Copyright 2024 CloudWeGo Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion example/stream/main.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2023 CloudWeGo Authors
* Copyright 2024 CloudWeGo Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion gzip.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2023 CloudWeGo Authors
* Copyright 2024 CloudWeGo Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
6 changes: 4 additions & 2 deletions gzip_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2023 CloudWeGo Authors
* Copyright 2024 CloudWeGo Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down 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
8 changes: 5 additions & 3 deletions options.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2023 CloudWeGo Authors
* Copyright 2024 CloudWeGo Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down 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
}
}
2 changes: 1 addition & 1 deletion srv_middleware.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2023 CloudWeGo Authors
* Copyright 2024 CloudWeGo Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion srv_stream_middleware.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2023 CloudWeGo Authors
* Copyright 2024 CloudWeGo Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down

0 comments on commit 7b39e10

Please sign in to comment.