Skip to content

Commit 7b39e10

Browse files
authored
fix: test error and decompressMiddleware doesn't work (#20)
* fix: test error and decompressMiddleware doesn't work * fix: return error in ClientMiddleware * chore: optimize license header
1 parent 56f7ab9 commit 7b39e10

File tree

8 files changed

+21
-13
lines changed

8 files changed

+21
-13
lines changed

client_middleware.go

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2023 CloudWeGo Authors
2+
* Copyright 2024 CloudWeGo Authors
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -82,10 +82,14 @@ func (g *gzipClientMiddleware) ClientMiddleware(next client.Endpoint) client.End
8282

8383
err = next(ctx, req, resp)
8484
if err != nil {
85-
return
85+
return err
8686
}
8787
if fn := g.DecompressFnForClient; fn != nil && strings.EqualFold(resp.Header.Get("Content-Encoding"), "gzip") {
88-
fn(next)
88+
f := fn(next)
89+
err = f(ctx, req, resp)
90+
if err != nil {
91+
return err
92+
}
8993
}
9094
return nil
9195
}

example/standard/main.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2023 CloudWeGo Authors
2+
* Copyright 2024 CloudWeGo Authors
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.

example/stream/main.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2023 CloudWeGo Authors
2+
* Copyright 2024 CloudWeGo Authors
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.

gzip.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2023 CloudWeGo Authors
2+
* Copyright 2024 CloudWeGo Authors
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.

gzip_test.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2023 CloudWeGo Authors
2+
* Copyright 2024 CloudWeGo Authors
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -405,11 +405,12 @@ func TestNoGzipForClient(t *testing.T) {
405405
func TestDecompressGzipForClient(t *testing.T) {
406406
h := server.Default(server.WithHostPorts("127.0.0.1:2338"))
407407

408+
h.Use(Gzip(DefaultCompression, WithDecompressFn(DefaultDecompressHandle)))
408409
h.GET("/", func(ctx context.Context, c *app.RequestContext) {
409410
c.Header("Content-Length", strconv.Itoa(len(testResponse)))
410411
c.String(200, testResponse)
411412
})
412-
h.Use(Gzip(DefaultCompression, WithDecompressFn(DefaultDecompressHandle)))
413+
413414
go h.Spin()
414415

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

426427
req.SetBodyString("bar")
427428
req.SetRequestURI("http://127.0.0.1:2338/")
429+
req.SetHeader("Accept-Encoding", "gzip")
428430

429431
err = cli.Do(context.Background(), req, res)
430432
if err != nil {

options.go

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2023 CloudWeGo Authors
2+
* Copyright 2024 CloudWeGo Authors
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -41,6 +41,7 @@
4141
package gzip
4242

4343
import (
44+
"bytes"
4445
"context"
4546
"net/http"
4647
"regexp"
@@ -216,7 +217,8 @@ func DefaultDecompressMiddlewareForClient(next client.Endpoint) client.Endpoint
216217
}
217218
resp.Header.DelBytes([]byte("Content-Encoding"))
218219
resp.Header.DelBytes([]byte("Content-Length"))
219-
resp.SetBody(gunzipBytes)
220-
return next(ctx, req, resp)
220+
resp.Header.DelBytes([]byte("Vary"))
221+
resp.SetBodyStream(bytes.NewBuffer(gunzipBytes), len(gunzipBytes))
222+
return nil
221223
}
222224
}

srv_middleware.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2023 CloudWeGo Authors
2+
* Copyright 2024 CloudWeGo Authors
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.

srv_stream_middleware.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2023 CloudWeGo Authors
2+
* Copyright 2024 CloudWeGo Authors
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.

0 commit comments

Comments
 (0)