From 7957c0d4ddaee560a01110145dbf2809ffdb6663 Mon Sep 17 00:00:00 2001 From: "Vojtech Vitek (golang.cz)" Date: Fri, 21 Jun 2024 08:30:42 -0400 Subject: [PATCH] =?UTF-8?q?Revert=20"fix(middleware):=20Close=20created=20?= =?UTF-8?q?writer=20in=20the=20compressor=20middleware=20(#=E2=80=A6"=20(#?= =?UTF-8?q?924)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This reverts commit f10dc4a9cab53a47f600f518fcccaa044666e95f. --- middleware/compress.go | 2 +- middleware/compress_test.go | 46 ++----------------------------------- 2 files changed, 3 insertions(+), 45 deletions(-) diff --git a/middleware/compress.go b/middleware/compress.go index 7ba95fd5..28240c4b 100644 --- a/middleware/compress.go +++ b/middleware/compress.go @@ -371,7 +371,7 @@ func (cw *compressResponseWriter) Push(target string, opts *http.PushOptions) er } func (cw *compressResponseWriter) Close() error { - if c, ok := cw.w.(io.WriteCloser); ok { + if c, ok := cw.writer().(io.WriteCloser); ok { return c.Close() } return errors.New("chi/middleware: io.WriteCloser is unavailable on the writer") diff --git a/middleware/compress_test.go b/middleware/compress_test.go index 992f1d40..eaafc13b 100644 --- a/middleware/compress_test.go +++ b/middleware/compress_test.go @@ -26,13 +26,8 @@ func TestCompressor(t *testing.T) { return w }) - var sideEffect int - compressor.SetEncoder("test", func(w io.Writer, _ int) io.Writer { - return newSideEffectWriter(w, &sideEffect) - }) - - if len(compressor.encoders) != 2 { - t.Errorf("nop and test encoders should be stored in the encoders map") + if len(compressor.encoders) != 1 { + t.Errorf("nop encoder should be stored in the encoders map") } r.Use(compressor.Handler) @@ -52,11 +47,6 @@ func TestCompressor(t *testing.T) { w.Write([]byte("textstring")) }) - r.Get("/getimage", func(w http.ResponseWriter, r *http.Request) { - w.Header().Set("Content-Type", "image/png") - w.Write([]byte("textstring")) - }) - ts := httptest.NewServer(r) defer ts.Close() @@ -103,12 +93,6 @@ func TestCompressor(t *testing.T) { acceptedEncodings: []string{"nop, gzip, deflate"}, expectedEncoding: "nop", }, - { - name: "test is used and side effect is cleared after close", - path: "/getimage", - acceptedEncodings: []string{"test"}, - expectedEncoding: "", - }, } for _, tc := range tests { @@ -123,10 +107,7 @@ func TestCompressor(t *testing.T) { } }) - } - if sideEffect > 1 { - t.Errorf("side effect should be cleared after close") } } @@ -236,26 +217,3 @@ func decodeResponseBody(t *testing.T, resp *http.Response) string { return string(respBody) } - -type ( - sideEffectWriter struct { - w io.Writer - s *int - } -) - -func newSideEffectWriter(w io.Writer, sideEffect *int) io.Writer { - *sideEffect = *sideEffect + 1 - - return &sideEffectWriter{w: w, s: sideEffect} -} - -func (w *sideEffectWriter) Write(p []byte) (n int, err error) { - return w.w.Write(p) -} - -func (w *sideEffectWriter) Close() error { - *w.s = *w.s - 1 - - return nil -}