-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: move car test helpers to tooling/helpers
- Loading branch information
1 parent
2fbdd10
commit 955f550
Showing
2 changed files
with
81 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 "" | ||
} |