From 955f550d2907130d25103adf7445ae56640538fb Mon Sep 17 00:00:00 2001 From: Adin Schmahmann Date: Tue, 23 May 2023 20:53:54 -0400 Subject: [PATCH] feat: move car test helpers to tooling/helpers --- tests/t0118_gateway_car_test.go | 75 +------------------------------ tooling/helpers/car.go | 79 +++++++++++++++++++++++++++++++++ 2 files changed, 81 insertions(+), 73 deletions(-) create mode 100644 tooling/helpers/car.go diff --git a/tests/t0118_gateway_car_test.go b/tests/t0118_gateway_car_test.go index 4c17e0592..590250bb5 100644 --- a/tests/t0118_gateway_car_test.go +++ b/tests/t0118_gateway_car_test.go @@ -1,7 +1,7 @@ package tests import ( - "fmt" + "github.com/ipfs/gateway-conformance/tooling/helpers" "testing" "github.com/ipfs/gateway-conformance/tooling/car" @@ -116,77 +116,6 @@ func TestGatewayCar(t *testing.T) { }, } - transformedTests := standardCARTestTransforms(t, tests) + transformedTests := helpers.StandardCARTestTransforms(t, tests) Run(t, transformedTests) } - -func standardCARTestTransforms(t *testing.T, tests SugarTests) SugarTests { - t.Helper() - - var out SugarTests - for _, test := range tests { - out = append(out, checkBothFormatAndAcceptHeaderCAR(t, applyStandardCarResponseHeaders(t, test))...) - } - return out -} - -func applyStandardCarResponseHeaders(t *testing.T, test SugarTest) SugarTest { - test.Response = test.Response.Headers( - Header("Content-Length"). - Hint("CAR is streamed, gateway may not have the entire thing, unable to calculate total size"). - IsEmpty(), - Header("X-Content-Type-Options"). - Hint("CAR is streamed, gateway may not have the entire thing, unable to calculate total size"). - Equals("nosniff"), - Header("Accept-Ranges"). - Hint("CAR is streamed, gateway may not have the entire thing, unable to support range-requests. Partial downloads and resumes should be handled using IPLD selectors: https://github.com/ipfs/go-ipfs/issues/8769"). - Equals("none"), - ) - return test -} - -func checkBothFormatAndAcceptHeaderCAR(t *testing.T, testWithFormatParam SugarTest) SugarTests { - t.Helper() - - formatParamReq := testWithFormatParam.Request - expected := testWithFormatParam.Response - - carFormatQueryParams, found := formatParamReq.Query_["format"] - if !found { - t.Fatal("could not find 'format' query parameter") - } - - if len(carFormatQueryParams) != 1 { - t.Fatal("only using a single format parameter is supported") - } - carFormatQueryParam := carFormatQueryParams[0] - - acceptHeaderReq := formatParamReq.Clone() - delete(acceptHeaderReq.Query_, "format") - - return SugarTests{ - { - Name: fmt.Sprintf("%s (format=car)", testWithFormatParam.Name), - Hint: fmt.Sprintf("%s\n%s", testWithFormatParam.Hint, "Request using format=car"), - Request: formatParamReq, - Response: expected, - }, - { - Name: fmt.Sprintf("%s (Accept Header)", testWithFormatParam.Name), - Hint: fmt.Sprintf("%s\n%s", testWithFormatParam.Hint, "Request using an Accept header"), - Request: acceptHeaderReq. - Headers( - Header("Accept", transformCARFormatParameterToAcceptHeader(t, carFormatQueryParam)), - ), - Response: expected, - }, - } -} - -func transformCARFormatParameterToAcceptHeader(t *testing.T, param string) string { - if param == "car" { - return "application/vnd.ipld.car" - } - t.Fatalf("can only convert the CAR format parameter to an accept header. Got %q", param) - return "" -} diff --git a/tooling/helpers/car.go b/tooling/helpers/car.go new file mode 100644 index 000000000..52d78998e --- /dev/null +++ b/tooling/helpers/car.go @@ -0,0 +1,79 @@ +package helpers + +import ( + "fmt" + "testing" + + "github.com/ipfs/gateway-conformance/tooling/test" +) + +func StandardCARTestTransforms(t *testing.T, sts test.SugarTests) test.SugarTests { + t.Helper() + + var out test.SugarTests + for _, st := range sts { + out = append(out, checkBothFormatAndAcceptHeaderCAR(t, applyStandardCarResponseHeaders(t, st))...) + } + return out +} + +func applyStandardCarResponseHeaders(t *testing.T, st test.SugarTest) test.SugarTest { + st.Response = st.Response.Headers( + test.Header("Content-Length"). + Hint("CAR is streamed, gateway may not have the entire thing, unable to calculate total size"). + IsEmpty(), + test.Header("X-Content-Type-Options"). + Hint("CAR is streamed, gateway may not have the entire thing, unable to calculate total size"). + Equals("nosniff"), + test.Header("Accept-Ranges"). + Hint("CAR is streamed, gateway may not have the entire thing, unable to support range-requests. Partial downloads and resumes should be handled using IPLD selectors: https://github.com/ipfs/go-ipfs/issues/8769"). + Equals("none"), + ) + return st +} + +func checkBothFormatAndAcceptHeaderCAR(t *testing.T, testWithFormatParam test.SugarTest) test.SugarTests { + t.Helper() + + formatParamReq := testWithFormatParam.Request + expected := testWithFormatParam.Response + + carFormatQueryParams, found := formatParamReq.Query_["format"] + if !found { + t.Fatal("could not find 'format' query parameter") + } + + if len(carFormatQueryParams) != 1 { + t.Fatal("only using a single format parameter is supported") + } + carFormatQueryParam := carFormatQueryParams[0] + + acceptHeaderReq := formatParamReq.Clone() + delete(acceptHeaderReq.Query_, "format") + + return test.SugarTests{ + { + Name: fmt.Sprintf("%s (format=car)", testWithFormatParam.Name), + Hint: fmt.Sprintf("%s\n%s", testWithFormatParam.Hint, "Request using format=car"), + Request: formatParamReq, + Response: expected, + }, + { + Name: fmt.Sprintf("%s (Accept Header)", testWithFormatParam.Name), + Hint: fmt.Sprintf("%s\n%s", testWithFormatParam.Hint, "Request using an Accept header"), + Request: acceptHeaderReq. + Headers( + test.Header("Accept", transformCARFormatParameterToAcceptHeader(t, carFormatQueryParam)), + ), + Response: expected, + }, + } +} + +func transformCARFormatParameterToAcceptHeader(t *testing.T, param string) string { + if param == "car" { + return "application/vnd.ipld.car" + } + t.Fatalf("can only convert the CAR format parameter to an accept header. Got %q", param) + return "" +}