Skip to content

Commit

Permalink
refactor: make sugar tests the default (#32)
Browse files Browse the repository at this point in the history
* refactor: make sugar tests the default

* fix: check.Not

* fix: clean up calls to Build

* chore: go fmt

* fix: content type header check in TestDAgPbConversion
  • Loading branch information
galargh authored Apr 6, 2023
1 parent 70219de commit 8b224e1
Show file tree
Hide file tree
Showing 11 changed files with 189 additions and 277 deletions.
2 changes: 1 addition & 1 deletion tests/t0109_gateway_web_redirects_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ func TestRedirectsFileSupport(t *testing.T) {
}

if specs.SubdomainGateway.IsEnabled() {
Run(t, unwrapTests(t, tests).Build())
Run(t, unwrapTests(t, tests))
} else {
t.Skip("subdomain gateway disabled")
}
Expand Down
2 changes: 1 addition & 1 deletion tests/t0113_gateway_symlink_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,5 @@ func TestGatewaySymlink(t *testing.T) {
},
}

test.Run(t, tests.Build())
test.Run(t, tests)
}
10 changes: 5 additions & 5 deletions tests/t0114_gateway_subdomains_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ func TestGatewaySubdomains(t *testing.T) {
CIDv1_TOO_LONG := fixture.MustGetCid("hello-CIDv1_TOO_LONG")
CIDWikipedia := "QmXoypizjW3WknFiJnKLwHCnL72vedxjQkDDP1mXWo6uco"

tests := []CTest{}
tests := SugarTests{}

// sugar: readable way to add more tests
with := func(moreTests []CTest) {
with := func(moreTests SugarTests) {
tests = append(tests, moreTests...)
}

Expand Down Expand Up @@ -64,7 +64,7 @@ func TestGatewaySubdomains(t *testing.T) {
We return body with HTTP 301 so existing cli scripts that use path-based
gateway do not break (curl doesn't auto-redirect without passing -L; wget
does not span across hostnames by default)
Context: https://github.com/ipfs/go-ipfs/issues/6975
Context: https://github.com/ipfs/go-ipfs/issues/6975
`,
IsEqual("hello\n"),
),
Expand Down Expand Up @@ -376,7 +376,7 @@ func TestGatewaySubdomains(t *testing.T) {
}
}

func testGatewayWithManyProtocols(t *testing.T, label string, hint string, reqURL interface{}, expected ExpectBuilder) []CTest {
func testGatewayWithManyProtocols(t *testing.T, label string, hint string, reqURL interface{}, expected ExpectBuilder) SugarTests {
t.Helper()

baseURL := ""
Expand Down Expand Up @@ -449,5 +449,5 @@ func testGatewayWithManyProtocols(t *testing.T, label string, hint string, reqUR
),
Response: expected,
},
}.Build()
}
}
212 changes: 106 additions & 106 deletions tests/t0116_gateway_cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,116 +197,116 @@ func TestGatewayCache(t *testing.T) {
},
// The tests below require `ipnsId` to be set.
/*
{
Name: "GET for /ipns/ unixfs dir listing succeeds",
Request: Request().
Path("ipns/%s/root2/root3/", ipnsId),
Response: Expect().
Status(200).
Headers(
Header("Cache-Control").
IsEmpty(),
Header("X-Ipfs-Path").
Equals("/ipns/%s/root2/root3", ipnsId),
Header("X-Ipfs-Roots").
Equals("%s,%s,%s", fixture.MustGetCid(), fixture.MustGetCid("root2"), fixture.MustGetCid("root2", "root3")),
Header("Etag").
Matches("DirIndex-.*_CID-%s", fixture.MustGetCid("root2", "root3")),
),
},
{
Name: "GET for /ipns/ unixfs dir with index.html succeeds",
Request: Request().
Path("ipns/%s/root2/root3/root4/", ipnsId),
Response: Expect().
Status(200).
Headers(
Header("Cache-Control").
IsEmpty(),
Header("X-Ipfs-Path").
Equals("/ipns/%s/root2/root3/root4/", ipnsId),
Header("X-Ipfs-Roots").
Equals("%s,%s,%s,%s", fixture.MustGetCid(), fixture.MustGetCid("root2"), fixture.MustGetCid("root2", "root3"), fixture.MustGetCid("root2", "root3", "root4")),
Header("Etag").
Matches("DirIndex-.*_CID-%s", fixture.MustGetCid("root2", "root3", "root4")),
),
},
{
Name: "GET for /ipns/ unixfs file succeeds",
Request: Request().
Path("ipns/%s/root2/root3/root4/index.html", ipnsId),
Response: Expect().
Status(200).
Headers(
Header("Cache-Control").
Equals("public, max-age=29030400, immutable"),
Header("X-Ipfs-Path").
Equals("/ipns/%s/root2/root3/root4/index.html", ipnsId),
Header("X-Ipfs-Roots").
Equals("%s,%s,%s,%s,%s", fixture.MustGetCid(), fixture.MustGetCid("root2"), fixture.MustGetCid("root2", "root3"), fixture.MustGetCid("root2", "root3", "root4"), fixture.MustGetCid("root2", "root3", "root4", "index.html")),
Header("Etag").
Equals("\"%s\"", fixture.MustGetCid("root2", "root3", "root4", "index.html")),
),
},
{
Name: "GET for /ipns/ unixfs dir as DAG-JSON succeeds",
Request: Request().
Path("ipns/%s/root2/root3/root4/", ipnsId).
Query("format", "dag-json"),
Response: Expect().
Status(200).
Headers(
Header("Cache-Control").
Equals("public, max-age=29030400, immutable"),
),
},
{
Name: "GET for /ipns/ unixfs dir as JSON succeeds",
Request: Request().
Path("ipns/%s/root2/root3/root4/", ipnsId).
Query("format", "json"),
Response: Expect().
Status(200).
Headers(
Header("Cache-Control").
Equals("public, max-age=29030400, immutable"),
),
},
{
Name: "GET for /ipns/ file with matching Etag in If-None-Match returns 304 Not Modified",
Request: Request().
Path("ipns/%s/root2/root3/root4/index.html", ipnsId).
Headers(
Header("If-None-Match", fmt.Sprintf("\"%s\"", fixture.MustGetCid("root2", "root3", "root4", "index.html"))),
),
Response: Expect().
Status(304),
},
{
Name: "GET for /ipns/ unixfs dir listing succeeds",
Request: Request().
Path("ipns/%s/root2/root3/", ipnsId),
Response: Expect().
Status(200).
Headers(
Header("Cache-Control").
IsEmpty(),
Header("X-Ipfs-Path").
Equals("/ipns/%s/root2/root3", ipnsId),
Header("X-Ipfs-Roots").
Equals("%s,%s,%s", fixture.MustGetCid(), fixture.MustGetCid("root2"), fixture.MustGetCid("root2", "root3")),
Header("Etag").
Matches("DirIndex-.*_CID-%s", fixture.MustGetCid("root2", "root3")),
),
},
{
Name: "GET for /ipns/ unixfs dir with index.html succeeds",
Request: Request().
Path("ipns/%s/root2/root3/root4/", ipnsId),
Response: Expect().
Status(200).
Headers(
Header("Cache-Control").
IsEmpty(),
Header("X-Ipfs-Path").
Equals("/ipns/%s/root2/root3/root4/", ipnsId),
Header("X-Ipfs-Roots").
Equals("%s,%s,%s,%s", fixture.MustGetCid(), fixture.MustGetCid("root2"), fixture.MustGetCid("root2", "root3"), fixture.MustGetCid("root2", "root3", "root4")),
Header("Etag").
Matches("DirIndex-.*_CID-%s", fixture.MustGetCid("root2", "root3", "root4")),
),
},
{
Name: "GET for /ipns/ unixfs file succeeds",
Request: Request().
Path("ipns/%s/root2/root3/root4/index.html", ipnsId),
Response: Expect().
Status(200).
Headers(
Header("Cache-Control").
Equals("public, max-age=29030400, immutable"),
Header("X-Ipfs-Path").
Equals("/ipns/%s/root2/root3/root4/index.html", ipnsId),
Header("X-Ipfs-Roots").
Equals("%s,%s,%s,%s,%s", fixture.MustGetCid(), fixture.MustGetCid("root2"), fixture.MustGetCid("root2", "root3"), fixture.MustGetCid("root2", "root3", "root4"), fixture.MustGetCid("root2", "root3", "root4", "index.html")),
Header("Etag").
Equals("\"%s\"", fixture.MustGetCid("root2", "root3", "root4", "index.html")),
),
},
{
Name: "GET for /ipns/ unixfs dir as DAG-JSON succeeds",
Request: Request().
Path("ipns/%s/root2/root3/root4/", ipnsId).
Query("format", "dag-json"),
Response: Expect().
Status(200).
Headers(
Header("Cache-Control").
Equals("public, max-age=29030400, immutable"),
),
},
{
Name: "GET for /ipns/ unixfs dir as JSON succeeds",
Request: Request().
Path("ipns/%s/root2/root3/root4/", ipnsId).
Query("format", "json"),
Response: Expect().
Status(200).
Headers(
Header("Cache-Control").
Equals("public, max-age=29030400, immutable"),
),
},
{
Name: "GET for /ipns/ file with matching Etag in If-None-Match returns 304 Not Modified",
Request: Request().
Path("ipns/%s/root2/root3/root4/index.html", ipnsId).
Headers(
Header("If-None-Match", fmt.Sprintf("\"%s\"", fixture.MustGetCid("root2", "root3", "root4", "index.html"))),
),
Response: Expect().
Status(304),
},
*/
// The tests below require `etag` to be set.
/*
{
Name: "GET for /ipfs/ dir listing with matching strong Etag in If-None-Match returns 304 Not Modified",
Request: Request().
Path("ipfs/%s/root2/root3/", fixture.MustGetCid()).
Headers(
Header("If-None-Match", fmt.Sprintf("\"%s\"", etag)),
),
Response: Expect().
Status(304),
},
{
Name: "GET for /ipfs/ dir listing with matching strong Etag in If-None-Match returns 304 Not Modified",
Request: Request().
Path("ipfs/%s/root2/root3/", fixture.MustGetCid()).
Headers(
Header("If-None-Match", fmt.Sprintf("W/\"%s\"", etag)),
),
Response: Expect().
Status(304),
},
{
Name: "GET for /ipfs/ dir listing with matching strong Etag in If-None-Match returns 304 Not Modified",
Request: Request().
Path("ipfs/%s/root2/root3/", fixture.MustGetCid()).
Headers(
Header("If-None-Match", fmt.Sprintf("\"%s\"", etag)),
),
Response: Expect().
Status(304),
},
{
Name: "GET for /ipfs/ dir listing with matching strong Etag in If-None-Match returns 304 Not Modified",
Request: Request().
Path("ipfs/%s/root2/root3/", fixture.MustGetCid()).
Headers(
Header("If-None-Match", fmt.Sprintf("W/\"%s\"", etag)),
),
Response: Expect().
Status(304),
},
*/
}.Build()
}

Run(t, tests)
}
4 changes: 2 additions & 2 deletions tests/t0117_gateway_block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (

func TestGatewayBlock(t *testing.T) {
fixture := car.MustOpenUnixfsCar("t0117-gateway-block.car")

tests := SugarTests{
{
Name: "GET with format=raw param returns a raw block",
Expand Down Expand Up @@ -118,7 +118,7 @@ func TestGatewayBlock(t *testing.T) {
}),
),
},
}.Build()
}

Run(t, tests)
}
2 changes: 1 addition & 1 deletion tests/t0118_gateway_car_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func TestGatewayCar(t *testing.T) {
Equals("none"),
),
},
}.Build()
}

Run(t, tests)
}
2 changes: 1 addition & 1 deletion tests/t0122_gateway_tar_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,5 +185,5 @@ func TestGatewayTar(t *testing.T) {
},
}

Run(t, tests.Build())
Run(t, tests)
}
16 changes: 9 additions & 7 deletions tests/t0123_gateway_json_cbor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func TestGatewayJsonCbor(t *testing.T) {
},
}

test.Run(t, tests.Build())
test.Run(t, tests)
}

// ## Reading UnixFS (data encoded with dag-pb codec) as DAG-CBOR and DAG-JSON
Expand Down Expand Up @@ -210,8 +210,9 @@ func TestDAgPbConversion(t *testing.T) {
Response: Expect().
Status(200).
Headers(
// NOTE: kubo gateway returns "text/plain; charset=utf-8" for example
Header("Content-Type").
Equals("text/plain"),
Contains("text/plain"),
Header("Content-Type").
Not().Contains("application/%s", row.Format),
Header("Content-Type").
Expand Down Expand Up @@ -240,8 +241,9 @@ func TestDAgPbConversion(t *testing.T) {
Response: Expect().
Status(200).
Headers(
// NOTE: kubo gateway returns "text/plain; charset=utf-8" for example
Header("Content-Type").
Equals("text/plain"),
Contains("text/plain"),
Header("Content-Type").
Not().Contains("application/%s", row.Format),
Header("Content-Type").
Expand All @@ -252,7 +254,7 @@ func TestDAgPbConversion(t *testing.T) {
},
}

test.Run(t, tests.Build())
test.Run(t, tests)
}
}

Expand Down Expand Up @@ -403,7 +405,7 @@ func TestPlainCodec(t *testing.T) {
},
}

test.Run(t, tests.Build())
test.Run(t, tests)
}
}

Expand Down Expand Up @@ -492,7 +494,7 @@ func TestPathing(t *testing.T) {
},
}

test.Run(t, tests.Build())
test.Run(t, tests)
}

// ## NATIVE TESTS for DAG-JSON (0x0129) and DAG-CBOR (0x71):
Expand Down Expand Up @@ -854,6 +856,6 @@ func TestNativeDag(t *testing.T) {
*/
}

test.Run(t, tests.Build())
test.Run(t, tests)
}
}
4 changes: 2 additions & 2 deletions tooling/test/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ type ReportInput struct {
Req *http.Request
Res *http.Response
Err error
Test CTest
Test SugarTest
}

const TEMPLATE = `
Expand All @@ -35,7 +35,7 @@ Actual Response:
{{.Res | dump}}
`

func report(t *testing.T, test CTest, req *http.Request, res *http.Response, err error) {
func report(t *testing.T, test SugarTest, req *http.Request, res *http.Response, err error) {
input := ReportInput{
Req: req,
Res: res,
Expand Down
Loading

0 comments on commit 8b224e1

Please sign in to comment.