diff --git a/app/proxy/cache_control_test.go b/app/proxy/cache_control_test.go index d387080..6e16eec 100644 --- a/app/proxy/cache_control_test.go +++ b/app/proxy/cache_control_test.go @@ -39,7 +39,6 @@ func TestCacheControl_MiddlewareDisabled(t *testing.T) { } func TestCacheControl_MiddlewareMime(t *testing.T) { - cc := NewCacheControl(time.Hour) cc.AddMime("text/html", time.Hour*2) cc.AddMime("image/png", time.Hour*10) @@ -47,41 +46,41 @@ func TestCacheControl_MiddlewareMime(t *testing.T) { w.Write([]byte("something")) })) - { + t.Run("match on html", func(t *testing.T) { req := httptest.NewRequest("GET", "/file.html", http.NoBody) w := httptest.NewRecorder() h.ServeHTTP(w, req) resp := w.Result() assert.Equal(t, http.StatusOK, resp.StatusCode) assert.Equal(t, "public, max-age=7200", resp.Header.Get("Cache-Control"), "match on .html") - } + }) - { + t.Run("match on png", func(t *testing.T) { req := httptest.NewRequest("GET", "/xyz/file.png?something=blah", http.NoBody) w := httptest.NewRecorder() h.ServeHTTP(w, req) resp := w.Result() assert.Equal(t, http.StatusOK, resp.StatusCode) assert.Equal(t, "public, max-age=36000", resp.Header.Get("Cache-Control"), "match on png") - } + }) - { + t.Run("no match, default", func(t *testing.T) { req := httptest.NewRequest("GET", "/xyz/file.gif?something=blah", http.NoBody) w := httptest.NewRecorder() h.ServeHTTP(w, req) resp := w.Result() assert.Equal(t, http.StatusOK, resp.StatusCode) assert.Equal(t, "public, max-age=3600", resp.Header.Get("Cache-Control"), "no match, default") - } + }) - { + t.Run("no match, empty", func(t *testing.T) { req := httptest.NewRequest("GET", "/xyz/", http.NoBody) w := httptest.NewRecorder() h.ServeHTTP(w, req) resp := w.Result() assert.Equal(t, http.StatusOK, resp.StatusCode) assert.Equal(t, "public, max-age=7200", resp.Header.Get("Cache-Control"), "match on empty (index)") - } + }) } func TestMakeCacheControl(t *testing.T) { diff --git a/app/proxy/handlers_test.go b/app/proxy/handlers_test.go index 174e4b5..0b48ff0 100644 --- a/app/proxy/handlers_test.go +++ b/app/proxy/handlers_test.go @@ -92,7 +92,7 @@ func Test_maxReqSizeHandler(t *testing.T) { } func Test_signatureHandler(t *testing.T) { - { + t.Run("with signature", func(t *testing.T) { wr := httptest.NewRecorder() handler := signatureHandler(true, "v0.0.1")(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { t.Logf("req: %v", r) @@ -104,8 +104,9 @@ func Test_signatureHandler(t *testing.T) { assert.Equal(t, "reproxy", wr.Result().Header.Get("App-Name"), wr.Result().Header) assert.Equal(t, "umputun", wr.Result().Header.Get("Author"), wr.Result().Header) assert.Equal(t, "v0.0.1", wr.Result().Header.Get("App-Version"), wr.Result().Header) - } - { + }) + + t.Run("without signature", func(t *testing.T) { wr := httptest.NewRecorder() handler := signatureHandler(false, "v0.0.1")(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { t.Logf("req: %v", r) @@ -117,7 +118,7 @@ func Test_signatureHandler(t *testing.T) { assert.Equal(t, "", wr.Result().Header.Get("App-Name"), wr.Result().Header) assert.Equal(t, "", wr.Result().Header.Get("Author"), wr.Result().Header) assert.Equal(t, "", wr.Result().Header.Get("App-Version"), wr.Result().Header) - } + }) } func Test_limiterSystemHandler(t *testing.T) { diff --git a/app/proxy/proxy.go b/app/proxy/proxy.go index ccdb6b5..b1618bb 100644 --- a/app/proxy/proxy.go +++ b/app/proxy/proxy.go @@ -80,7 +80,7 @@ type Reporter interface { // LBSelector defines load balancer strategy type LBSelector interface { - Select(len int) int // return index of picked server + Select(size int) int // return index of picked server } // Timeouts consolidate timeouts for both server and transport diff --git a/app/proxy/proxy_test.go b/app/proxy/proxy_test.go index 0fdcc4c..bee5843 100644 --- a/app/proxy/proxy_test.go +++ b/app/proxy/proxy_test.go @@ -543,7 +543,8 @@ func TestHttp_DoWithAssetRules(t *testing.T) { time.Sleep(150 * time.Millisecond) client := http.Client{} - { + + t.Run("web2 spa not found page", func(t *testing.T) { resp, err := client.Get("http://localhost:" + strconv.Itoa(port) + "/web2/nop.html") require.NoError(t, err) defer resp.Body.Close() @@ -556,9 +557,9 @@ func TestHttp_DoWithAssetRules(t *testing.T) { assert.Equal(t, "", resp.Header.Get("App-Method")) assert.Equal(t, "", resp.Header.Get("h1")) assert.Equal(t, "public, max-age=43200", resp.Header.Get("Cache-Control")) - } + }) - { + t.Run("api call on 127.0.0.1", func(t *testing.T) { req, err := http.NewRequest("GET", "http://127.0.0.1:"+strconv.Itoa(port)+"/api/something", http.NoBody) require.NoError(t, err) resp, err := client.Do(req) @@ -572,9 +573,9 @@ func TestHttp_DoWithAssetRules(t *testing.T) { assert.Equal(t, "response /567/something", string(body)) assert.Equal(t, "", resp.Header.Get("App-Method")) assert.Equal(t, "v1", resp.Header.Get("h1")) - } + }) - { + t.Run("web call on localhost", func(t *testing.T) { resp, err := client.Get("http://localhost:" + strconv.Itoa(port) + "/web/1.html") require.NoError(t, err) defer resp.Body.Close() @@ -587,15 +588,14 @@ func TestHttp_DoWithAssetRules(t *testing.T) { assert.Equal(t, "", resp.Header.Get("App-Method")) assert.Equal(t, "", resp.Header.Get("h1")) assert.Equal(t, "public, max-age=43200", resp.Header.Get("Cache-Control")) - } + }) - { + t.Run("web call on localhost, not found", func(t *testing.T) { resp, err := client.Get("http://localhost:" + strconv.Itoa(port) + "/web/nop.html") require.NoError(t, err) defer resp.Body.Close() assert.Equal(t, http.StatusNotFound, resp.StatusCode) - } - + }) } func TestHttp_DoWithRedirects(t *testing.T) { @@ -631,7 +631,7 @@ func TestHttp_DoWithRedirects(t *testing.T) { }, } - { + t.Run("localhost to example.com", func(t *testing.T) { req, err := http.NewRequest("GET", "http://localhost:"+strconv.Itoa(port)+"/api/something", http.NoBody) require.NoError(t, err) resp, err := client.Do(req) @@ -640,9 +640,9 @@ func TestHttp_DoWithRedirects(t *testing.T) { assert.Equal(t, http.StatusMovedPermanently, resp.StatusCode) t.Logf("%+v", resp.Header) assert.Equal(t, "http://example.com/123/something", resp.Header.Get("Location")) - } + }) - { + t.Run("127.0.0.1 to example.com", func(t *testing.T) { req, err := http.NewRequest("GET", "http://127.0.0.1:"+strconv.Itoa(port)+"/api/something", http.NoBody) require.NoError(t, err) resp, err := client.Do(req) @@ -651,7 +651,7 @@ func TestHttp_DoWithRedirects(t *testing.T) { assert.Equal(t, http.StatusFound, resp.StatusCode) t.Logf("%+v", resp.Header) assert.Equal(t, "http://example.com/567/something", resp.Header.Get("Location")) - } + }) } func TestHttp_DoLimitedReq(t *testing.T) { @@ -690,7 +690,7 @@ func TestHttp_DoLimitedReq(t *testing.T) { client := http.Client{} - { + t.Run("allowed request size", func(t *testing.T) { req, err := http.NewRequest("POST", "http://127.0.0.1:"+strconv.Itoa(port)+"/api/something", bytes.NewBufferString("abcdefg")) require.NoError(t, err) resp, err := client.Do(req) @@ -706,16 +706,16 @@ func TestHttp_DoLimitedReq(t *testing.T) { assert.Equal(t, "v1", resp.Header.Get("h1")) assert.Equal(t, "vv1", resp.Header.Get("hh1")) assert.Equal(t, "vv2", resp.Header.Get("hh2")) - } + }) - { + t.Run("request size too large", func(t *testing.T) { req, err := http.NewRequest("POST", "http://127.0.0.1:"+strconv.Itoa(port)+"/api/something", bytes.NewBufferString("abcdefg1234567")) require.NoError(t, err) resp, err := client.Do(req) require.NoError(t, err) defer resp.Body.Close() assert.Equal(t, http.StatusRequestEntityTooLarge, resp.StatusCode) - } + }) } func TestHttp_health(t *testing.T) { @@ -755,35 +755,33 @@ func TestHttp_health(t *testing.T) { client := http.Client{} - { - req, err := http.NewRequest("POST", "http://127.0.0.1:"+strconv.Itoa(port)+"/api/something", bytes.NewBufferString("abcdefg")) - require.NoError(t, err) - resp, err := client.Do(req) - require.NoError(t, err) - defer resp.Body.Close() - assert.Equal(t, http.StatusOK, resp.StatusCode) - t.Logf("%+v", resp.Header) + // api call + req, err := http.NewRequest("POST", "http://127.0.0.1:"+strconv.Itoa(port)+"/api/something", bytes.NewBufferString("abcdefg")) + require.NoError(t, err) + resp, err := client.Do(req) + require.NoError(t, err) + defer resp.Body.Close() + assert.Equal(t, http.StatusOK, resp.StatusCode) + t.Logf("%+v", resp.Header) - body, err := io.ReadAll(resp.Body) - require.NoError(t, err) - assert.Equal(t, "response /567/something", string(body)) - assert.Equal(t, "reproxy", resp.Header.Get("App-Name")) - assert.Equal(t, "v1", resp.Header.Get("h1")) - assert.Equal(t, "vv1", resp.Header.Get("hh1")) - assert.Equal(t, "vv2", resp.Header.Get("hh2")) - } + body, err := io.ReadAll(resp.Body) + require.NoError(t, err) + assert.Equal(t, "response /567/something", string(body)) + assert.Equal(t, "reproxy", resp.Header.Get("App-Name")) + assert.Equal(t, "v1", resp.Header.Get("h1")) + assert.Equal(t, "vv1", resp.Header.Get("hh1")) + assert.Equal(t, "vv2", resp.Header.Get("hh2")) - { - req, err := http.NewRequest("GET", "http://127.0.0.1:"+strconv.Itoa(port)+"/health", http.NoBody) - require.NoError(t, err) - resp, err := client.Do(req) - require.NoError(t, err) - defer resp.Body.Close() - assert.Equal(t, http.StatusOK, resp.StatusCode) - body, err := io.ReadAll(resp.Body) - require.NoError(t, err) - assert.Equal(t, `{"status": "ok", "services": 2}`, string(body)) - } + // health check + req, err = http.NewRequest("GET", "http://127.0.0.1:"+strconv.Itoa(port)+"/health", http.NoBody) + require.NoError(t, err) + resp, err = client.Do(req) + require.NoError(t, err) + defer resp.Body.Close() + assert.Equal(t, http.StatusOK, resp.StatusCode) + body, err = io.ReadAll(resp.Body) + require.NoError(t, err) + assert.Equal(t, `{"status": "ok", "services": 2}`, string(body)) } func TestHttp_withBasicAuth(t *testing.T) {