Skip to content

Commit 7957c0d

Browse files
authoredJun 21, 2024··
Revert "fix(middleware): Close created writer in the compressor middleware (#…" (#924)
This reverts commit f10dc4a.
1 parent f728a1c commit 7957c0d

File tree

2 files changed

+3
-45
lines changed

2 files changed

+3
-45
lines changed
 

‎middleware/compress.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,7 @@ func (cw *compressResponseWriter) Push(target string, opts *http.PushOptions) er
371371
}
372372

373373
func (cw *compressResponseWriter) Close() error {
374-
if c, ok := cw.w.(io.WriteCloser); ok {
374+
if c, ok := cw.writer().(io.WriteCloser); ok {
375375
return c.Close()
376376
}
377377
return errors.New("chi/middleware: io.WriteCloser is unavailable on the writer")

‎middleware/compress_test.go

+2-44
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,8 @@ func TestCompressor(t *testing.T) {
2626
return w
2727
})
2828

29-
var sideEffect int
30-
compressor.SetEncoder("test", func(w io.Writer, _ int) io.Writer {
31-
return newSideEffectWriter(w, &sideEffect)
32-
})
33-
34-
if len(compressor.encoders) != 2 {
35-
t.Errorf("nop and test encoders should be stored in the encoders map")
29+
if len(compressor.encoders) != 1 {
30+
t.Errorf("nop encoder should be stored in the encoders map")
3631
}
3732

3833
r.Use(compressor.Handler)
@@ -52,11 +47,6 @@ func TestCompressor(t *testing.T) {
5247
w.Write([]byte("textstring"))
5348
})
5449

55-
r.Get("/getimage", func(w http.ResponseWriter, r *http.Request) {
56-
w.Header().Set("Content-Type", "image/png")
57-
w.Write([]byte("textstring"))
58-
})
59-
6050
ts := httptest.NewServer(r)
6151
defer ts.Close()
6252

@@ -103,12 +93,6 @@ func TestCompressor(t *testing.T) {
10393
acceptedEncodings: []string{"nop, gzip, deflate"},
10494
expectedEncoding: "nop",
10595
},
106-
{
107-
name: "test is used and side effect is cleared after close",
108-
path: "/getimage",
109-
acceptedEncodings: []string{"test"},
110-
expectedEncoding: "",
111-
},
11296
}
11397

11498
for _, tc := range tests {
@@ -123,10 +107,7 @@ func TestCompressor(t *testing.T) {
123107
}
124108

125109
})
126-
}
127110

128-
if sideEffect > 1 {
129-
t.Errorf("side effect should be cleared after close")
130111
}
131112
}
132113

@@ -236,26 +217,3 @@ func decodeResponseBody(t *testing.T, resp *http.Response) string {
236217

237218
return string(respBody)
238219
}
239-
240-
type (
241-
sideEffectWriter struct {
242-
w io.Writer
243-
s *int
244-
}
245-
)
246-
247-
func newSideEffectWriter(w io.Writer, sideEffect *int) io.Writer {
248-
*sideEffect = *sideEffect + 1
249-
250-
return &sideEffectWriter{w: w, s: sideEffect}
251-
}
252-
253-
func (w *sideEffectWriter) Write(p []byte) (n int, err error) {
254-
return w.w.Write(p)
255-
}
256-
257-
func (w *sideEffectWriter) Close() error {
258-
*w.s = *w.s - 1
259-
260-
return nil
261-
}

0 commit comments

Comments
 (0)
Please sign in to comment.