registry/api/v2: checkTestRouter(): use sub-tests, t.Parallel()

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn 2023-04-30 15:38:30 +02:00
parent 2444c3282d
commit f4acd98865
No known key found for this signature in database
GPG Key ID: 76698F39D527CE8C
1 changed files with 62 additions and 54 deletions

View File

@ -29,6 +29,7 @@ type routeTestCase struct {
// //
// This may go away as the application structure comes together. // This may go away as the application structure comes together.
func TestRouter(t *testing.T) { func TestRouter(t *testing.T) {
t.Parallel()
tests := []routeTestCase{ tests := []routeTestCase{
{ {
RouteName: RouteNameBase, RouteName: RouteNameBase,
@ -177,6 +178,7 @@ func TestRouter(t *testing.T) {
} }
func TestRouterWithPathTraversals(t *testing.T) { func TestRouterWithPathTraversals(t *testing.T) {
t.Parallel()
tests := []routeTestCase{ tests := []routeTestCase{
{ {
RouteName: RouteNameBlobUploadChunk, RouteName: RouteNameBlobUploadChunk,
@ -198,6 +200,7 @@ func TestRouterWithPathTraversals(t *testing.T) {
} }
func TestRouterWithBadCharacters(t *testing.T) { func TestRouterWithBadCharacters(t *testing.T) {
t.Parallel()
if testing.Short() { if testing.Short() {
tests := []routeTestCase{ tests := []routeTestCase{
{ {
@ -253,67 +256,72 @@ func checkTestRouter(t *testing.T, tests []routeTestCase, prefix string, deeplyE
server := httptest.NewServer(router) server := httptest.NewServer(router)
for _, tc := range tests { for _, tc := range tests {
tc.RequestURI = strings.TrimSuffix(prefix, "/") + tc.RequestURI tc := tc
// Register the endpoint requestURI := strings.TrimSuffix(prefix, "/") + tc.RequestURI
route := router.GetRoute(tc.RouteName) t.Run("("+tc.RouteName+")"+requestURI, func(t *testing.T) {
if route == nil { t.Parallel()
t.Fatalf("route for name %q not found", tc.RouteName) tc.RequestURI = requestURI
} // Register the endpoint
route := router.GetRoute(tc.RouteName)
if route == nil {
t.Fatalf("route for name %q not found", tc.RouteName)
}
route.Handler(testHandler) route.Handler(testHandler)
u := server.URL + tc.RequestURI u := server.URL + tc.RequestURI
resp, err := http.Get(u) resp, err := http.Get(u)
if err != nil { if err != nil {
t.Fatalf("error issuing get request: %v", err) t.Fatalf("error issuing get request: %v", err)
} }
if tc.StatusCode == 0 { if tc.StatusCode == 0 {
// Override default, zero-value // Override default, zero-value
tc.StatusCode = http.StatusOK tc.StatusCode = http.StatusOK
} }
if tc.ExpectedURI == "" { if tc.ExpectedURI == "" {
// Override default, zero-value // Override default, zero-value
tc.ExpectedURI = tc.RequestURI tc.ExpectedURI = tc.RequestURI
} }
if resp.StatusCode != tc.StatusCode { if resp.StatusCode != tc.StatusCode {
t.Fatalf("unexpected status for %s: %v %v", u, resp.Status, resp.StatusCode) t.Fatalf("unexpected status for %s: %v %v", u, resp.Status, resp.StatusCode)
} }
if tc.StatusCode != http.StatusOK {
resp.Body.Close()
// We don't care about json response.
return
}
dec := json.NewDecoder(resp.Body)
var actualRouteInfo routeTestCase
if err := dec.Decode(&actualRouteInfo); err != nil {
t.Fatalf("error reading json response: %v", err)
}
// Needs to be set out of band
actualRouteInfo.StatusCode = resp.StatusCode
if actualRouteInfo.RequestURI != tc.ExpectedURI {
t.Fatalf("URI %v incorrectly parsed, expected %v", actualRouteInfo.RequestURI, tc.ExpectedURI)
}
if actualRouteInfo.RouteName != tc.RouteName {
t.Fatalf("incorrect route %q matched, expected %q", actualRouteInfo.RouteName, tc.RouteName)
}
// when testing deep equality, the actualRouteInfo has an empty ExpectedURI, we don't want
// that to make the comparison fail. We're otherwise done with the testcase so empty the
// testcase.ExpectedURI
tc.ExpectedURI = ""
if deeplyEqual && !reflect.DeepEqual(actualRouteInfo, tc) {
t.Fatalf("actual does not equal expected: %#v != %#v", actualRouteInfo, tc)
}
if tc.StatusCode != http.StatusOK {
resp.Body.Close() resp.Body.Close()
// We don't care about json response. })
continue
}
dec := json.NewDecoder(resp.Body)
var actualRouteInfo routeTestCase
if err := dec.Decode(&actualRouteInfo); err != nil {
t.Fatalf("error reading json response: %v", err)
}
// Needs to be set out of band
actualRouteInfo.StatusCode = resp.StatusCode
if actualRouteInfo.RequestURI != tc.ExpectedURI {
t.Fatalf("URI %v incorrectly parsed, expected %v", actualRouteInfo.RequestURI, tc.ExpectedURI)
}
if actualRouteInfo.RouteName != tc.RouteName {
t.Fatalf("incorrect route %q matched, expected %q", actualRouteInfo.RouteName, tc.RouteName)
}
// when testing deep equality, the actualRouteInfo has an empty ExpectedURI, we don't want
// that to make the comparison fail. We're otherwise done with the testcase so empty the
// testcase.ExpectedURI
tc.ExpectedURI = ""
if deeplyEqual && !reflect.DeepEqual(actualRouteInfo, tc) {
t.Fatalf("actual does not equal expected: %#v != %#v", actualRouteInfo, tc)
}
resp.Body.Close()
} }
} }