Skip to content

Commit

Permalink
fix: improve stability of ratelimit tests (#1641)
Browse files Browse the repository at this point in the history
* fix: improve stability of ratelimit tests

Signed-off-by: zhaonan <[email protected]>

* adjust order

Signed-off-by: zhaonan <[email protected]>

---------

Signed-off-by: zhaonan <[email protected]>
  • Loading branch information
tmsnan authored Jul 11, 2023
1 parent 634ccdb commit 2c501d4
Showing 1 changed file with 22 additions and 7 deletions.
29 changes: 22 additions & 7 deletions test/e2e/tests/ratelimit.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,18 @@ var RateLimitTest = suite.ConformanceTest{
routeNN := types.NamespacedName{Name: "http-ratelimit", Namespace: ns}
gwNN := types.NamespacedName{Name: "same-namespace", Namespace: ns}
gwAddr := kubernetes.GatewayAndHTTPRoutesMustBeAccepted(t, suite.Client, suite.TimeoutConfig, suite.ControllerName, kubernetes.NewGatewayRef(gwNN), routeNN)

ratelimitHeader := make(map[string]string)
expectOkResp := http.ExpectedResponse{
Request: http.Request{
Path: "/",
},
Response: http.Response{
StatusCode: 200,
Headers: ratelimitHeader,
},
Namespace: ns,
}
expectOkResp.Response.Headers["X-Ratelimit-Limit"] = "3, 3;w=3600"
expectOkReq := http.MakeRequest(t, &expectOkResp, gwAddr, "HTTP", "http")

expectLimitResp := http.ExpectedResponse{
Expand Down Expand Up @@ -104,18 +106,26 @@ var RateLimitBasedJwtClaimsTest = suite.ConformanceTest{
}

// Just to construct the request that carries a jwt token that can be limited
ratelimitHeader := make(map[string]string)
TokenHeader := make(map[string]string)
JwtOkResp := http.ExpectedResponse{
Request: http.Request{
Path: "/foo",
Headers: TokenHeader,
},
ExpectedRequest: &http.ExpectedRequest{
Request: http.Request{
Path: "/foo",
},
},
Response: http.Response{
StatusCode: 200,
Headers: ratelimitHeader,
},
Namespace: ns,
}
JwtOkResp.Request.Headers["Authorization"] = "Bearer " + "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWUsImlhdCI6MTUxNjIzOTAyMn0.NHVaYe26MbtOYhSKkoKYdFVomg4i8ZJd8_-RU8VNbftc4TSMb4bXP3l3YlNWACwyXPGffz5aXHc6lty1Y2t4SWRqGteragsVdZufDn5BlnJl9pdR_kdVFUsra2rWKEofkZeIC4yWytE58sMIihvo9H1ScmmVwBcQP6XETqYd0aSHp1gOa9RdUPDvoXQ5oqygTqVtxaDr6wUFKrKItgBMzWIdNZ6y7O9E0DhEPTbE9rfBo6KTFsHAZnMg4k68CDp2woYIaXbmYTWcvbzIuHO7_37GT79XdIwkm95QJ7hYC9RiwrV7mesbY4PAahERJawntho0my942XheVLmGwLMBkQ"
JwtOkResp.Response.Headers["X-Ratelimit-Limit"] = "3, 3;w=3600"

JwtReq := http.MakeRequest(t, &JwtOkResp, gwAddr, "HTTP", "http")

Expand Down Expand Up @@ -149,18 +159,23 @@ var RateLimitBasedJwtClaimsTest = suite.ConformanceTest{
// keep sending requests till get 200 first to make sure the gateway is available
http.MakeRequestAndExpectEventuallyConsistentResponse(t, suite.RoundTripper, suite.TimeoutConfig, gwAddr, OkResp)

// Carrying different jwt claims will not be limited
if err := GotExactNExpectedResponse(t, 4, suite.RoundTripper, difJwtReq, expectOkResp); err != nil {
t.Errorf("fail to get expected response at first four request: %v", err)
}

// should just send exactly 4 requests, and expect 429
if err := GotExactNExpectedResponse(t, 3, suite.RoundTripper, JwtReq, expectOkResp); err != nil {

// keep sending requests till get 200 first, that will cost one 200
http.MakeRequestAndExpectEventuallyConsistentResponse(t, suite.RoundTripper, suite.TimeoutConfig, gwAddr, JwtOkResp)

// fire the rest request
if err := GotExactNExpectedResponse(t, 2, suite.RoundTripper, JwtReq, JwtOkResp); err != nil {
t.Errorf("fail to get expected response at three request: %v", err)
}
if err := GotExactNExpectedResponse(t, 1, suite.RoundTripper, JwtReq, expectLimitResp); err != nil {
t.Errorf("fail to get expected response at last fourth request: %v", err)
}

// Carrying different jwt claims will not be limited
if err := GotExactNExpectedResponse(t, 4, suite.RoundTripper, difJwtReq, expectOkResp); err != nil {
t.Errorf("fail to get expected response at first four request: %v", err)
}
})
},
}
Expand Down

0 comments on commit 2c501d4

Please sign in to comment.