diff --git a/sdk/user_agent.go b/sdk/user_agent.go index e7d4fb2..5a0acaf 100644 --- a/sdk/user_agent.go +++ b/sdk/user_agent.go @@ -4,7 +4,7 @@ import ( "net/http" ) -const proxyVersion = "0.2.3" +const proxyVersion = "0.2.4" type userAgentInterceptor struct { http.RoundTripper diff --git a/web/mware/compress.go b/web/mware/compress.go index 888027d..6a4382f 100644 --- a/web/mware/compress.go +++ b/web/mware/compress.go @@ -28,9 +28,11 @@ func GZip(next http.HandlerFunc) http.HandlerFunc { w.Header().Set("Vary", "Accept-Encoding") writer := pool.Get().(*gzip.Writer) writer.Reset(w) - gz := gzipWriter{writer: writer, ResponseWriter: w} - defer gz.close() - next(&gz, r) + gz := &gzipWriter{writer: writer, ResponseWriter: w} + defer func() { + _ = gz.Close() + }() + next(gz, r) } else { next(w, r) } @@ -47,18 +49,17 @@ func (w *gzipWriter) Write(b []byte) (int, error) { if !w.headersDone { w.WriteHeader(http.StatusOK) } - i, e := w.writer.Write(b) - _ = w.writer.Flush() - return i, e + return w.writer.Write(b) } func (w *gzipWriter) Flush() { + _ = w.writer.Flush() if flusher, ok := w.ResponseWriter.(http.Flusher); ok { flusher.Flush() } } -func (w *gzipWriter) close() { - w.writer.Reset(io.Discard) - pool.Put(w.writer) +func (w *gzipWriter) Close() error { + defer pool.Put(w.writer) + return w.writer.Close() } diff --git a/web/mware/compress_test.go b/web/mware/compress_test.go index 50d89c1..401c158 100644 --- a/web/mware/compress_test.go +++ b/web/mware/compress_test.go @@ -1,7 +1,6 @@ package mware import ( - "bytes" "compress/gzip" "github.com/stretchr/testify/assert" "io" @@ -15,25 +14,21 @@ func TestGZip(t *testing.T) { _, _ = writer.Write([]byte("test")) }) srv := httptest.NewServer(handler) - client := http.Client{} t.Run("with gzip", func(t *testing.T) { req, _ := http.NewRequest(http.MethodGet, srv.URL, http.NoBody) req.Header.Set("Accept-Encoding", "gzip") - resp, _ := client.Do(req) + resp, _ := http.DefaultClient.Do(req) assert.Equal(t, http.StatusOK, resp.StatusCode) - body, _ := io.ReadAll(resp.Body) - _ = resp.Body.Close() assert.Equal(t, "gzip", resp.Header.Get("Content-Encoding")) - var buf bytes.Buffer - wr := gzip.NewWriter(&buf) - _, _ = wr.Write([]byte("test")) - _ = wr.Flush() - assert.Equal(t, buf.Bytes(), body) + gzipReader, err := gzip.NewReader(resp.Body) + assert.NoError(t, err) + body, _ := io.ReadAll(gzipReader) + assert.Equal(t, "test", string(body)) }) t.Run("without gzip", func(t *testing.T) { req, _ := http.NewRequest(http.MethodGet, srv.URL, http.NoBody) - resp, _ := client.Do(req) + resp, _ := http.DefaultClient.Do(req) assert.Equal(t, http.StatusOK, resp.StatusCode) body, _ := io.ReadAll(resp.Body) _ = resp.Body.Close() diff --git a/web/router_api_test.go b/web/router_api_test.go index b6bd3d4..8dad804 100644 --- a/web/router_api_test.go +++ b/web/router_api_test.go @@ -1,7 +1,6 @@ package web import ( - "bytes" "compress/gzip" "fmt" "github.com/configcat/configcat-proxy/config" @@ -20,11 +19,10 @@ func TestAPI_Eval(t *testing.T) { router := newAPIRouter(t, config.ApiConfig{Enabled: true, CORS: config.CORSConfig{Enabled: true}, Headers: map[string]string{"h1": "v1"}, AuthHeaders: map[string]string{"X-AUTH": "key"}}) srv := httptest.NewServer(router.Handler()) path := fmt.Sprintf("%s/api/test/eval", srv.URL) - client := http.Client{} t.Run("options cors", func(t *testing.T) { req, _ := http.NewRequest(http.MethodOptions, path, http.NoBody) - resp, _ := client.Do(req) + resp, _ := http.DefaultClient.Do(req) assert.Equal(t, http.StatusNoContent, resp.StatusCode) assert.Equal(t, "POST,OPTIONS", resp.Header.Get("Access-Control-Allow-Methods")) @@ -37,7 +35,7 @@ func TestAPI_Eval(t *testing.T) { }) t.Run("missing auth", func(t *testing.T) { req, _ := http.NewRequest(http.MethodPost, path, http.NoBody) - resp, _ := client.Do(req) + resp, _ := http.DefaultClient.Do(req) assert.Equal(t, http.StatusUnauthorized, resp.StatusCode) assert.Equal(t, "*", resp.Header.Get("Access-Control-Allow-Origin")) assert.Equal(t, "Content-Length,ETag,Date,Content-Encoding,h1", resp.Header.Get("Access-Control-Expose-Headers")) @@ -46,7 +44,7 @@ func TestAPI_Eval(t *testing.T) { t.Run("ok", func(t *testing.T) { req, _ := http.NewRequest(http.MethodPost, path, strings.NewReader(`{"key":"flag"}`)) req.Header.Set("X-AUTH", "key") - resp, _ := client.Do(req) + resp, _ := http.DefaultClient.Do(req) assert.Equal(t, http.StatusOK, resp.StatusCode) body, _ := io.ReadAll(resp.Body) _ = resp.Body.Close() @@ -59,38 +57,35 @@ func TestAPI_Eval(t *testing.T) { req, _ := http.NewRequest(http.MethodPost, path, strings.NewReader(`{"key":"flag"}`)) req.Header.Set("X-AUTH", "key") req.Header.Set("Accept-Encoding", "gzip") - resp, _ := client.Do(req) + resp, _ := http.DefaultClient.Do(req) assert.Equal(t, http.StatusOK, resp.StatusCode) - body, _ := io.ReadAll(resp.Body) - _ = resp.Body.Close() assert.Equal(t, "gzip", resp.Header.Get("Content-Encoding")) - var buf bytes.Buffer - wr := gzip.NewWriter(&buf) - _, _ = wr.Write([]byte(`{"value":true,"variationId":"v_flag"}`)) - _ = wr.Flush() - assert.Equal(t, buf.Bytes(), body) + gzipReader, err := gzip.NewReader(resp.Body) + assert.NoError(t, err) + body, _ := io.ReadAll(gzipReader) + assert.Equal(t, `{"value":true,"variationId":"v_flag"}`, string(body)) assert.Equal(t, "*", resp.Header.Get("Access-Control-Allow-Origin")) assert.Equal(t, "Content-Length,ETag,Date,Content-Encoding,h1", resp.Header.Get("Access-Control-Expose-Headers")) assert.Equal(t, "v1", resp.Header.Get("h1")) }) t.Run("get not allowed", func(t *testing.T) { req, _ := http.NewRequest(http.MethodGet, path, http.NoBody) - resp, _ := client.Do(req) + resp, _ := http.DefaultClient.Do(req) assert.Equal(t, http.StatusMethodNotAllowed, resp.StatusCode) }) t.Run("put not allowed", func(t *testing.T) { req, _ := http.NewRequest(http.MethodPut, path, http.NoBody) - resp, _ := client.Do(req) + resp, _ := http.DefaultClient.Do(req) assert.Equal(t, http.StatusMethodNotAllowed, resp.StatusCode) }) t.Run("patch not allowed", func(t *testing.T) { req, _ := http.NewRequest(http.MethodPatch, path, http.NoBody) - resp, _ := client.Do(req) + resp, _ := http.DefaultClient.Do(req) assert.Equal(t, http.StatusMethodNotAllowed, resp.StatusCode) }) t.Run("delete not allowed", func(t *testing.T) { req, _ := http.NewRequest(http.MethodDelete, path, http.NoBody) - resp, _ := client.Do(req) + resp, _ := http.DefaultClient.Do(req) assert.Equal(t, http.StatusMethodNotAllowed, resp.StatusCode) }) } @@ -99,11 +94,10 @@ func TestAPI_Eval_Headers(t *testing.T) { router := newAPIRouter(t, config.ApiConfig{Enabled: true, CORS: config.CORSConfig{Enabled: false}, AuthHeaders: map[string]string{"X-AUTH": "key"}}) srv := httptest.NewServer(router.Handler()) path := fmt.Sprintf("%s/api/test/eval", srv.URL) - client := http.Client{} t.Run("options", func(t *testing.T) { req, _ := http.NewRequest(http.MethodOptions, path, http.NoBody) - resp, _ := client.Do(req) + resp, _ := http.DefaultClient.Do(req) assert.Equal(t, http.StatusNoContent, resp.StatusCode) assert.Empty(t, resp.Header.Get("Access-Control-Allow-Methods")) assert.Empty(t, resp.Header.Get("Access-Control-Allow-Credentials")) @@ -116,7 +110,7 @@ func TestAPI_Eval_Headers(t *testing.T) { t.Run("ok", func(t *testing.T) { req, _ := http.NewRequest(http.MethodPost, path, strings.NewReader(`{"key":"flag"}`)) req.Header.Set("X-AUTH", "key") - resp, _ := client.Do(req) + resp, _ := http.DefaultClient.Do(req) assert.Equal(t, http.StatusOK, resp.StatusCode) body, _ := io.ReadAll(resp.Body) _ = resp.Body.Close() @@ -131,11 +125,10 @@ func TestAPI_EvalAll(t *testing.T) { router := newAPIRouter(t, config.ApiConfig{Enabled: true, CORS: config.CORSConfig{Enabled: true}, Headers: map[string]string{"h1": "v1"}, AuthHeaders: map[string]string{"X-AUTH": "key"}}) srv := httptest.NewServer(router.Handler()) path := fmt.Sprintf("%s/api/test/eval-all", srv.URL) - client := http.Client{} t.Run("options cors", func(t *testing.T) { req, _ := http.NewRequest(http.MethodOptions, path, http.NoBody) - resp, _ := client.Do(req) + resp, _ := http.DefaultClient.Do(req) assert.Equal(t, http.StatusNoContent, resp.StatusCode) assert.Equal(t, "POST,OPTIONS", resp.Header.Get("Access-Control-Allow-Methods")) @@ -148,7 +141,7 @@ func TestAPI_EvalAll(t *testing.T) { }) t.Run("missing auth", func(t *testing.T) { req, _ := http.NewRequest(http.MethodPost, path, http.NoBody) - resp, _ := client.Do(req) + resp, _ := http.DefaultClient.Do(req) assert.Equal(t, http.StatusUnauthorized, resp.StatusCode) assert.Equal(t, "*", resp.Header.Get("Access-Control-Allow-Origin")) assert.Equal(t, "Content-Length,ETag,Date,Content-Encoding,h1", resp.Header.Get("Access-Control-Expose-Headers")) @@ -157,7 +150,7 @@ func TestAPI_EvalAll(t *testing.T) { t.Run("ok", func(t *testing.T) { req, _ := http.NewRequest(http.MethodPost, path, strings.NewReader(`{"key":"flag"}`)) req.Header.Set("X-AUTH", "key") - resp, _ := client.Do(req) + resp, _ := http.DefaultClient.Do(req) assert.Equal(t, http.StatusOK, resp.StatusCode) body, _ := io.ReadAll(resp.Body) _ = resp.Body.Close() @@ -170,38 +163,35 @@ func TestAPI_EvalAll(t *testing.T) { req, _ := http.NewRequest(http.MethodPost, path, strings.NewReader(`{"key":"flag"}`)) req.Header.Set("X-AUTH", "key") req.Header.Set("Accept-Encoding", "gzip") - resp, _ := client.Do(req) + resp, _ := http.DefaultClient.Do(req) assert.Equal(t, http.StatusOK, resp.StatusCode) - body, _ := io.ReadAll(resp.Body) - _ = resp.Body.Close() assert.Equal(t, "gzip", resp.Header.Get("Content-Encoding")) - var buf bytes.Buffer - wr := gzip.NewWriter(&buf) - _, _ = wr.Write([]byte(`{"flag":{"value":true,"variationId":"v_flag"}}`)) - _ = wr.Flush() - assert.Equal(t, buf.Bytes(), body) + gzipReader, err := gzip.NewReader(resp.Body) + assert.NoError(t, err) + body, _ := io.ReadAll(gzipReader) + assert.Equal(t, `{"flag":{"value":true,"variationId":"v_flag"}}`, string(body)) assert.Equal(t, "*", resp.Header.Get("Access-Control-Allow-Origin")) assert.Equal(t, "Content-Length,ETag,Date,Content-Encoding,h1", resp.Header.Get("Access-Control-Expose-Headers")) assert.Equal(t, "v1", resp.Header.Get("h1")) }) t.Run("get not allowed", func(t *testing.T) { req, _ := http.NewRequest(http.MethodGet, path, http.NoBody) - resp, _ := client.Do(req) + resp, _ := http.DefaultClient.Do(req) assert.Equal(t, http.StatusMethodNotAllowed, resp.StatusCode) }) t.Run("put not allowed", func(t *testing.T) { req, _ := http.NewRequest(http.MethodPut, path, http.NoBody) - resp, _ := client.Do(req) + resp, _ := http.DefaultClient.Do(req) assert.Equal(t, http.StatusMethodNotAllowed, resp.StatusCode) }) t.Run("patch not allowed", func(t *testing.T) { req, _ := http.NewRequest(http.MethodPatch, path, http.NoBody) - resp, _ := client.Do(req) + resp, _ := http.DefaultClient.Do(req) assert.Equal(t, http.StatusMethodNotAllowed, resp.StatusCode) }) t.Run("delete not allowed", func(t *testing.T) { req, _ := http.NewRequest(http.MethodDelete, path, http.NoBody) - resp, _ := client.Do(req) + resp, _ := http.DefaultClient.Do(req) assert.Equal(t, http.StatusMethodNotAllowed, resp.StatusCode) }) } @@ -210,11 +200,10 @@ func TestAPI_EvalAll_Headers(t *testing.T) { router := newAPIRouter(t, config.ApiConfig{Enabled: true, CORS: config.CORSConfig{Enabled: false}, AuthHeaders: map[string]string{"X-AUTH": "key"}}) srv := httptest.NewServer(router.Handler()) path := fmt.Sprintf("%s/api/test/eval-all", srv.URL) - client := http.Client{} t.Run("options", func(t *testing.T) { req, _ := http.NewRequest(http.MethodOptions, path, http.NoBody) - resp, _ := client.Do(req) + resp, _ := http.DefaultClient.Do(req) assert.Equal(t, http.StatusNoContent, resp.StatusCode) assert.Empty(t, resp.Header.Get("Access-Control-Allow-Methods")) assert.Empty(t, resp.Header.Get("Access-Control-Allow-Credentials")) @@ -227,7 +216,7 @@ func TestAPI_EvalAll_Headers(t *testing.T) { t.Run("ok", func(t *testing.T) { req, _ := http.NewRequest(http.MethodPost, path, strings.NewReader(`{"key":"flag"}`)) req.Header.Set("X-AUTH", "key") - resp, _ := client.Do(req) + resp, _ := http.DefaultClient.Do(req) assert.Equal(t, http.StatusOK, resp.StatusCode) body, _ := io.ReadAll(resp.Body) _ = resp.Body.Close() @@ -242,11 +231,10 @@ func TestAPI_Keys(t *testing.T) { router := newAPIRouter(t, config.ApiConfig{Enabled: true, CORS: config.CORSConfig{Enabled: true}, Headers: map[string]string{"h1": "v1"}, AuthHeaders: map[string]string{"X-AUTH": "key"}}) srv := httptest.NewServer(router.Handler()) path := fmt.Sprintf("%s/api/test/keys", srv.URL) - client := http.Client{} t.Run("options cors", func(t *testing.T) { req, _ := http.NewRequest(http.MethodOptions, path, http.NoBody) - resp, _ := client.Do(req) + resp, _ := http.DefaultClient.Do(req) assert.Equal(t, http.StatusNoContent, resp.StatusCode) assert.Equal(t, "GET,OPTIONS", resp.Header.Get("Access-Control-Allow-Methods")) @@ -259,7 +247,7 @@ func TestAPI_Keys(t *testing.T) { }) t.Run("missing auth", func(t *testing.T) { req, _ := http.NewRequest(http.MethodGet, path, http.NoBody) - resp, _ := client.Do(req) + resp, _ := http.DefaultClient.Do(req) assert.Equal(t, http.StatusUnauthorized, resp.StatusCode) assert.Equal(t, "*", resp.Header.Get("Access-Control-Allow-Origin")) assert.Equal(t, "Content-Length,ETag,Date,Content-Encoding,h1", resp.Header.Get("Access-Control-Expose-Headers")) @@ -268,7 +256,7 @@ func TestAPI_Keys(t *testing.T) { t.Run("ok", func(t *testing.T) { req, _ := http.NewRequest(http.MethodGet, path, http.NoBody) req.Header.Set("X-AUTH", "key") - resp, _ := client.Do(req) + resp, _ := http.DefaultClient.Do(req) assert.Equal(t, http.StatusOK, resp.StatusCode) body, _ := io.ReadAll(resp.Body) _ = resp.Body.Close() @@ -281,38 +269,35 @@ func TestAPI_Keys(t *testing.T) { req, _ := http.NewRequest(http.MethodGet, path, http.NoBody) req.Header.Set("X-AUTH", "key") req.Header.Set("Accept-Encoding", "gzip") - resp, _ := client.Do(req) + resp, _ := http.DefaultClient.Do(req) assert.Equal(t, http.StatusOK, resp.StatusCode) - body, _ := io.ReadAll(resp.Body) - _ = resp.Body.Close() assert.Equal(t, "gzip", resp.Header.Get("Content-Encoding")) - var buf bytes.Buffer - wr := gzip.NewWriter(&buf) - _, _ = wr.Write([]byte(`{"keys":["flag"]}`)) - _ = wr.Flush() - assert.Equal(t, buf.Bytes(), body) + gzipReader, err := gzip.NewReader(resp.Body) + assert.NoError(t, err) + body, _ := io.ReadAll(gzipReader) + assert.Equal(t, `{"keys":["flag"]}`, string(body)) assert.Equal(t, "*", resp.Header.Get("Access-Control-Allow-Origin")) assert.Equal(t, "Content-Length,ETag,Date,Content-Encoding,h1", resp.Header.Get("Access-Control-Expose-Headers")) assert.Equal(t, "v1", resp.Header.Get("h1")) }) t.Run("post not allowed", func(t *testing.T) { req, _ := http.NewRequest(http.MethodPost, path, http.NoBody) - resp, _ := client.Do(req) + resp, _ := http.DefaultClient.Do(req) assert.Equal(t, http.StatusMethodNotAllowed, resp.StatusCode) }) t.Run("put not allowed", func(t *testing.T) { req, _ := http.NewRequest(http.MethodPut, path, http.NoBody) - resp, _ := client.Do(req) + resp, _ := http.DefaultClient.Do(req) assert.Equal(t, http.StatusMethodNotAllowed, resp.StatusCode) }) t.Run("patch not allowed", func(t *testing.T) { req, _ := http.NewRequest(http.MethodPatch, path, http.NoBody) - resp, _ := client.Do(req) + resp, _ := http.DefaultClient.Do(req) assert.Equal(t, http.StatusMethodNotAllowed, resp.StatusCode) }) t.Run("delete not allowed", func(t *testing.T) { req, _ := http.NewRequest(http.MethodDelete, path, http.NoBody) - resp, _ := client.Do(req) + resp, _ := http.DefaultClient.Do(req) assert.Equal(t, http.StatusMethodNotAllowed, resp.StatusCode) }) } @@ -321,11 +306,10 @@ func TestAPI_Keys_Headers(t *testing.T) { router := newAPIRouter(t, config.ApiConfig{Enabled: true, CORS: config.CORSConfig{Enabled: false}, AuthHeaders: map[string]string{"X-AUTH": "key"}}) srv := httptest.NewServer(router.Handler()) path := fmt.Sprintf("%s/api/test/keys", srv.URL) - client := http.Client{} t.Run("options", func(t *testing.T) { req, _ := http.NewRequest(http.MethodOptions, path, http.NoBody) - resp, _ := client.Do(req) + resp, _ := http.DefaultClient.Do(req) assert.Equal(t, http.StatusNoContent, resp.StatusCode) assert.Empty(t, resp.Header.Get("Access-Control-Allow-Methods")) assert.Empty(t, resp.Header.Get("Access-Control-Allow-Credentials")) @@ -338,7 +322,7 @@ func TestAPI_Keys_Headers(t *testing.T) { t.Run("ok", func(t *testing.T) { req, _ := http.NewRequest(http.MethodGet, path, http.NoBody) req.Header.Set("X-AUTH", "key") - resp, _ := client.Do(req) + resp, _ := http.DefaultClient.Do(req) assert.Equal(t, http.StatusOK, resp.StatusCode) body, _ := io.ReadAll(resp.Body) _ = resp.Body.Close() @@ -353,11 +337,10 @@ func TestAPI_Refresh(t *testing.T) { router := newAPIRouter(t, config.ApiConfig{Enabled: true, CORS: config.CORSConfig{Enabled: true}, Headers: map[string]string{"h1": "v1"}, AuthHeaders: map[string]string{"X-AUTH": "key"}}) srv := httptest.NewServer(router.Handler()) path := fmt.Sprintf("%s/api/test/refresh", srv.URL) - client := http.Client{} t.Run("options cors", func(t *testing.T) { req, _ := http.NewRequest(http.MethodOptions, path, http.NoBody) - resp, _ := client.Do(req) + resp, _ := http.DefaultClient.Do(req) assert.Equal(t, http.StatusNoContent, resp.StatusCode) assert.Equal(t, "POST,OPTIONS", resp.Header.Get("Access-Control-Allow-Methods")) @@ -370,7 +353,7 @@ func TestAPI_Refresh(t *testing.T) { }) t.Run("missing auth", func(t *testing.T) { req, _ := http.NewRequest(http.MethodPost, path, http.NoBody) - resp, _ := client.Do(req) + resp, _ := http.DefaultClient.Do(req) assert.Equal(t, http.StatusUnauthorized, resp.StatusCode) assert.Equal(t, "*", resp.Header.Get("Access-Control-Allow-Origin")) assert.Equal(t, "Content-Length,ETag,Date,Content-Encoding,h1", resp.Header.Get("Access-Control-Expose-Headers")) @@ -379,7 +362,7 @@ func TestAPI_Refresh(t *testing.T) { t.Run("ok", func(t *testing.T) { req, _ := http.NewRequest(http.MethodPost, path, http.NoBody) req.Header.Set("X-AUTH", "key") - resp, _ := client.Do(req) + resp, _ := http.DefaultClient.Do(req) assert.Equal(t, http.StatusOK, resp.StatusCode) body, _ := io.ReadAll(resp.Body) _ = resp.Body.Close() @@ -390,22 +373,22 @@ func TestAPI_Refresh(t *testing.T) { }) t.Run("get not allowed", func(t *testing.T) { req, _ := http.NewRequest(http.MethodGet, path, http.NoBody) - resp, _ := client.Do(req) + resp, _ := http.DefaultClient.Do(req) assert.Equal(t, http.StatusMethodNotAllowed, resp.StatusCode) }) t.Run("put not allowed", func(t *testing.T) { req, _ := http.NewRequest(http.MethodPut, path, http.NoBody) - resp, _ := client.Do(req) + resp, _ := http.DefaultClient.Do(req) assert.Equal(t, http.StatusMethodNotAllowed, resp.StatusCode) }) t.Run("patch not allowed", func(t *testing.T) { req, _ := http.NewRequest(http.MethodPatch, path, http.NoBody) - resp, _ := client.Do(req) + resp, _ := http.DefaultClient.Do(req) assert.Equal(t, http.StatusMethodNotAllowed, resp.StatusCode) }) t.Run("delete not allowed", func(t *testing.T) { req, _ := http.NewRequest(http.MethodDelete, path, http.NoBody) - resp, _ := client.Do(req) + resp, _ := http.DefaultClient.Do(req) assert.Equal(t, http.StatusMethodNotAllowed, resp.StatusCode) }) } @@ -414,11 +397,10 @@ func TestAPI_Refresh_Headers(t *testing.T) { router := newAPIRouter(t, config.ApiConfig{Enabled: true, CORS: config.CORSConfig{Enabled: false}, AuthHeaders: map[string]string{"X-AUTH": "key"}}) srv := httptest.NewServer(router.Handler()) path := fmt.Sprintf("%s/api/test/refresh", srv.URL) - client := http.Client{} t.Run("options", func(t *testing.T) { req, _ := http.NewRequest(http.MethodOptions, path, http.NoBody) - resp, _ := client.Do(req) + resp, _ := http.DefaultClient.Do(req) assert.Equal(t, http.StatusNoContent, resp.StatusCode) assert.Empty(t, resp.Header.Get("Access-Control-Allow-Methods")) assert.Empty(t, resp.Header.Get("Access-Control-Allow-Credentials")) @@ -431,7 +413,7 @@ func TestAPI_Refresh_Headers(t *testing.T) { t.Run("ok", func(t *testing.T) { req, _ := http.NewRequest(http.MethodPost, path, http.NoBody) req.Header.Set("X-AUTH", "key") - resp, _ := client.Do(req) + resp, _ := http.DefaultClient.Do(req) assert.Equal(t, http.StatusOK, resp.StatusCode) body, _ := io.ReadAll(resp.Body) _ = resp.Body.Close() diff --git a/web/router_cdnproxy_test.go b/web/router_cdnproxy_test.go index 1570e9d..dc3d980 100644 --- a/web/router_cdnproxy_test.go +++ b/web/router_cdnproxy_test.go @@ -1,7 +1,6 @@ package web import ( - "bytes" "compress/gzip" "fmt" "github.com/configcat/configcat-proxy/config" @@ -126,19 +125,15 @@ func TestCDNProxy_Get_Body(t *testing.T) { func TestCDNProxy_Get_Body_GZip(t *testing.T) { router := newCDNProxyRouter(t, config.CdnProxyConfig{Enabled: true, CORS: config.CORSConfig{Enabled: true}, Headers: map[string]string{"h1": "v1"}}) srv := httptest.NewServer(router.Handler()) - client := http.Client{} req, _ := http.NewRequest(http.MethodGet, fmt.Sprintf("%s/configuration-files/test/config_v5.json", srv.URL), http.NoBody) req.Header.Set("Accept-Encoding", "gzip") - resp, _ := client.Do(req) + resp, _ := http.DefaultClient.Do(req) assert.Equal(t, http.StatusOK, resp.StatusCode) - body, _ := io.ReadAll(resp.Body) - _ = resp.Body.Close() assert.Equal(t, "gzip", resp.Header.Get("Content-Encoding")) - var buf bytes.Buffer - wr := gzip.NewWriter(&buf) - _, _ = wr.Write([]byte(`{"f":{"flag":{"i":"v_flag","v":true,"t":0,"r":[],"p":null}},"p":null}`)) - _ = wr.Flush() - assert.Equal(t, buf.Bytes(), body) + gzipReader, err := gzip.NewReader(resp.Body) + assert.NoError(t, err) + body, _ := io.ReadAll(gzipReader) + assert.Equal(t, `{"f":{"flag":{"i":"v_flag","v":true,"t":0,"r":[],"p":null}},"p":null}`, string(body)) assert.Equal(t, "v1", resp.Header.Get("h1")) }