Skip to content

Commit

Permalink
refactor subtests
Browse files Browse the repository at this point in the history
  • Loading branch information
umputun committed Nov 27, 2024
1 parent ad84e95 commit 92bd228
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 60 deletions.
17 changes: 8 additions & 9 deletions app/proxy/cache_control_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,49 +39,48 @@ 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)
h := cc.Middleware(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
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) {
Expand Down
9 changes: 5 additions & 4 deletions app/proxy/handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion app/proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
90 changes: 44 additions & 46 deletions app/proxy/proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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)
Expand All @@ -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()
Expand All @@ -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) {
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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) {
Expand Down Expand Up @@ -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)
Expand All @@ -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) {
Expand Down Expand Up @@ -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)

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))
}
// 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"))

// 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) {
Expand Down

0 comments on commit 92bd228

Please sign in to comment.