Skip to content

Commit

Permalink
feat: move car test helpers to tooling/helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
aschmahmann committed May 24, 2023
1 parent 2fbdd10 commit 955f550
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 73 deletions.
75 changes: 2 additions & 73 deletions tests/t0118_gateway_car_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package tests

import (
"fmt"
"github.com/ipfs/gateway-conformance/tooling/helpers"
"testing"

"github.com/ipfs/gateway-conformance/tooling/car"
Expand Down Expand Up @@ -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 ""
}
79 changes: 79 additions & 0 deletions tooling/helpers/car.go
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 ""
}

0 comments on commit 955f550

Please sign in to comment.