registry/api/v2: use consistent names for test-tables

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

View file

@ -29,7 +29,7 @@ type routeTestCase struct {
//
// This may go away as the application structure comes together.
func TestRouter(t *testing.T) {
testCases := []routeTestCase{
tests := []routeTestCase{
{
RouteName: RouteNameBase,
RequestURI: "/v2/",
@ -172,12 +172,12 @@ func TestRouter(t *testing.T) {
},
}
checkTestRouter(t, testCases, "", true)
checkTestRouter(t, testCases, "/prefix/", true)
checkTestRouter(t, tests, "", true)
checkTestRouter(t, tests, "/prefix/", true)
}
func TestRouterWithPathTraversals(t *testing.T) {
testCases := []routeTestCase{
tests := []routeTestCase{
{
RouteName: RouteNameBlobUploadChunk,
RequestURI: "/v2/foo/../../blobs/uploads/D95306FA-FAD3-4E36-8D41-CF1C93EF8286",
@ -194,12 +194,12 @@ func TestRouterWithPathTraversals(t *testing.T) {
},
},
}
checkTestRouter(t, testCases, "", false)
checkTestRouter(t, tests, "", false)
}
func TestRouterWithBadCharacters(t *testing.T) {
if testing.Short() {
testCases := []routeTestCase{
tests := []routeTestCase{
{
RouteName: RouteNameBlobUploadChunk,
RequestURI: "/v2/foo/blobs/uploads/不95306FA-FAD3-4E36-8D41-CF1C93EF8286",
@ -212,26 +212,26 @@ func TestRouterWithBadCharacters(t *testing.T) {
StatusCode: http.StatusNotFound,
},
}
checkTestRouter(t, testCases, "", true)
checkTestRouter(t, tests, "", true)
} else {
// in the long version we're going to fuzz the router
// with random UTF8 characters not in the 128 bit ASCII range.
// These are not valid characters for the router and we expect
// 404s on every test.
rand.Seed(time.Now().UTC().UnixNano())
testCases := make([]routeTestCase, 1000)
for idx := range testCases {
testCases[idx] = routeTestCase{
tests := make([]routeTestCase, 1000)
for idx := range tests {
tests[idx] = routeTestCase{
RouteName: RouteNameTags,
RequestURI: fmt.Sprintf("/v2/%v/%v/tags/list", randomString(10), randomString(10)),
StatusCode: http.StatusNotFound,
}
}
checkTestRouter(t, testCases, "", true)
checkTestRouter(t, tests, "", true)
}
}
func checkTestRouter(t *testing.T, testCases []routeTestCase, prefix string, deeplyEqual bool) {
func checkTestRouter(t *testing.T, tests []routeTestCase, prefix string, deeplyEqual bool) {
router := RouterWithPrefix(prefix)
testHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
@ -252,37 +252,37 @@ func checkTestRouter(t *testing.T, testCases []routeTestCase, prefix string, dee
// Startup test server
server := httptest.NewServer(router)
for _, testcase := range testCases {
testcase.RequestURI = strings.TrimSuffix(prefix, "/") + testcase.RequestURI
for _, tc := range tests {
tc.RequestURI = strings.TrimSuffix(prefix, "/") + tc.RequestURI
// Register the endpoint
route := router.GetRoute(testcase.RouteName)
route := router.GetRoute(tc.RouteName)
if route == nil {
t.Fatalf("route for name %q not found", testcase.RouteName)
t.Fatalf("route for name %q not found", tc.RouteName)
}
route.Handler(testHandler)
u := server.URL + testcase.RequestURI
u := server.URL + tc.RequestURI
resp, err := http.Get(u)
if err != nil {
t.Fatalf("error issuing get request: %v", err)
}
if testcase.StatusCode == 0 {
if tc.StatusCode == 0 {
// Override default, zero-value
testcase.StatusCode = http.StatusOK
tc.StatusCode = http.StatusOK
}
if testcase.ExpectedURI == "" {
if tc.ExpectedURI == "" {
// Override default, zero-value
testcase.ExpectedURI = testcase.RequestURI
tc.ExpectedURI = tc.RequestURI
}
if resp.StatusCode != testcase.StatusCode {
if resp.StatusCode != tc.StatusCode {
t.Fatalf("unexpected status for %s: %v %v", u, resp.Status, resp.StatusCode)
}
if testcase.StatusCode != http.StatusOK {
if tc.StatusCode != http.StatusOK {
resp.Body.Close()
// We don't care about json response.
continue
@ -297,20 +297,20 @@ func checkTestRouter(t *testing.T, testCases []routeTestCase, prefix string, dee
// Needs to be set out of band
actualRouteInfo.StatusCode = resp.StatusCode
if actualRouteInfo.RequestURI != testcase.ExpectedURI {
t.Fatalf("URI %v incorrectly parsed, expected %v", actualRouteInfo.RequestURI, testcase.ExpectedURI)
if actualRouteInfo.RequestURI != tc.ExpectedURI {
t.Fatalf("URI %v incorrectly parsed, expected %v", actualRouteInfo.RequestURI, tc.ExpectedURI)
}
if actualRouteInfo.RouteName != testcase.RouteName {
t.Fatalf("incorrect route %q matched, expected %q", actualRouteInfo.RouteName, testcase.RouteName)
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
testcase.ExpectedURI = ""
if deeplyEqual && !reflect.DeepEqual(actualRouteInfo, testcase) {
t.Fatalf("actual does not equal expected: %#v != %#v", actualRouteInfo, testcase)
tc.ExpectedURI = ""
if deeplyEqual && !reflect.DeepEqual(actualRouteInfo, tc) {
t.Fatalf("actual does not equal expected: %#v != %#v", actualRouteInfo, tc)
}
resp.Body.Close()

View file

@ -138,23 +138,23 @@ func TestURLBuilder(t *testing.T) {
t.Fatalf("unexpected error creating urlbuilder: %v", err)
}
for _, testCase := range makeURLBuilderTestCases(urlBuilder) {
url, err := testCase.build()
expectedErr := testCase.expectedErr
for _, tc := range makeURLBuilderTestCases(urlBuilder) {
buildURL, err := tc.build()
expectedErr := tc.expectedErr
if !reflect.DeepEqual(expectedErr, err) {
t.Fatalf("%s: Expecting %v but got error %v", testCase.description, expectedErr, err)
t.Fatalf("%s: Expecting %v but got error %v", tc.description, expectedErr, err)
}
if expectedErr != nil {
continue
}
expectedURL := testCase.expectedPath
expectedURL := tc.expectedPath
if !relative {
expectedURL = root + expectedURL
}
if url != expectedURL {
t.Fatalf("%s: %q != %q", testCase.description, url, expectedURL)
if buildURL != expectedURL {
t.Fatalf("%s: %q != %q", tc.description, buildURL, expectedURL)
}
}
}
@ -178,22 +178,22 @@ func TestURLBuilderWithPrefix(t *testing.T) {
t.Fatalf("unexpected error creating urlbuilder: %v", err)
}
for _, testCase := range makeURLBuilderTestCases(urlBuilder) {
url, err := testCase.build()
expectedErr := testCase.expectedErr
for _, tc := range makeURLBuilderTestCases(urlBuilder) {
buildURL, err := tc.build()
expectedErr := tc.expectedErr
if !reflect.DeepEqual(expectedErr, err) {
t.Fatalf("%s: Expecting %v but got error %v", testCase.description, expectedErr, err)
t.Fatalf("%s: Expecting %v but got error %v", tc.description, expectedErr, err)
}
if expectedErr != nil {
continue
}
expectedURL := testCase.expectedPath
expectedURL := tc.expectedPath
if !relative {
expectedURL = root[0:len(root)-1] + expectedURL
}
if url != expectedURL {
t.Fatalf("%s: %q != %q", testCase.description, url, expectedURL)
if buildURL != expectedURL {
t.Fatalf("%s: %q != %q", tc.description, buildURL, expectedURL)
}
}
}
@ -424,23 +424,23 @@ func TestBuilderFromRequest(t *testing.T) {
builder = NewURLBuilderFromRequest(tr.request, relative)
}
for _, testCase := range makeURLBuilderTestCases(builder) {
buildURL, err := testCase.build()
expectedErr := testCase.expectedErr
for _, tc := range makeURLBuilderTestCases(builder) {
buildURL, err := tc.build()
expectedErr := tc.expectedErr
if !reflect.DeepEqual(expectedErr, err) {
t.Fatalf("%s: Expecting %v but got error %v", testCase.description, expectedErr, err)
t.Fatalf("%s: Expecting %v but got error %v", tc.description, expectedErr, err)
}
if expectedErr != nil {
continue
}
expectedURL := testCase.expectedPath
expectedURL := tc.expectedPath
if !relative {
expectedURL = tr.base + expectedURL
}
if buildURL != expectedURL {
t.Errorf("[relative=%t, request=%q, case=%q]: %q != %q", relative, tr.name, testCase.description, buildURL, expectedURL)
t.Errorf("[relative=%t, request=%q, case=%q]: %q != %q", relative, tr.name, tc.description, buildURL, expectedURL)
}
}
}
@ -497,11 +497,11 @@ func TestBuilderFromRequestWithPrefix(t *testing.T) {
builder = NewURLBuilderFromRequest(tr.request, false)
}
for _, testCase := range makeURLBuilderTestCases(builder) {
buildURL, err := testCase.build()
expectedErr := testCase.expectedErr
for _, tc := range makeURLBuilderTestCases(builder) {
buildURL, err := tc.build()
expectedErr := tc.expectedErr
if !reflect.DeepEqual(expectedErr, err) {
t.Fatalf("%s: Expecting %v but got error %v", testCase.description, expectedErr, err)
t.Fatalf("%s: Expecting %v but got error %v", tc.description, expectedErr, err)
}
if expectedErr != nil {
continue
@ -510,7 +510,7 @@ func TestBuilderFromRequestWithPrefix(t *testing.T) {
var expectedURL string
proto, ok := tr.request.Header["X-Forwarded-Proto"]
if !ok {
expectedURL = testCase.expectedPath
expectedURL = tc.expectedPath
if !relative {
expectedURL = tr.base[0:len(tr.base)-1] + expectedURL
}
@ -520,7 +520,7 @@ func TestBuilderFromRequestWithPrefix(t *testing.T) {
t.Fatal(err)
}
urlBase.Scheme = proto[0]
expectedURL = testCase.expectedPath
expectedURL = tc.expectedPath
if !relative {
expectedURL = urlBase.String()[0:len(urlBase.String())-1] + expectedURL
}
@ -528,7 +528,7 @@ func TestBuilderFromRequestWithPrefix(t *testing.T) {
}
if buildURL != expectedURL {
t.Fatalf("%s: %q != %q", testCase.description, buildURL, expectedURL)
t.Fatalf("%s: %q != %q", tc.description, buildURL, expectedURL)
}
}
}