From c07604c685a5be04d1403138b5bc56641c48128f Mon Sep 17 00:00:00 2001 From: Henrique Dias Date: Fri, 8 Sep 2023 15:59:39 +0200 Subject: [PATCH] feat: test redirects file and if-none-match header --- fixtures/redirects_file/README.md | 22 ++++++++- fixtures/redirects_file/dnslink.yml | 9 ++-- fixtures/redirects_file/redirects-spa.car | Bin 0 -> 360 bytes tests/redirects_file_test.go | 52 ++++++++++++++++++++++ 4 files changed, 79 insertions(+), 4 deletions(-) create mode 100644 fixtures/redirects_file/redirects-spa.car diff --git a/fixtures/redirects_file/README.md b/fixtures/redirects_file/README.md index 9d56fb5cf..fb1613739 100644 --- a/fixtures/redirects_file/README.md +++ b/fixtures/redirects_file/README.md @@ -9,4 +9,24 @@ See comments in the yml file. ### [redirects.car](./redirects.car) -Fixtures based on [specs.ipfs.tech/http-gateways/web-redirects-file/#test-fixtures](https://specs.ipfs.tech/http-gateways/web-redirects-file/#test-fixtures) and [IPIP-0002](https://specs.ipfs.tech/ipips/ipip-0002/) \ No newline at end of file +Fixtures based on [specs.ipfs.tech/http-gateways/web-redirects-file/#test-fixtures](https://specs.ipfs.tech/http-gateways/web-redirects-file/#test-fixtures) and [IPIP-0002](https://specs.ipfs.tech/ipips/ipip-0002/) + +### [redirects-spa.car](./redirects-spa.car) + +```sh +ipfs version +# ipfs version 0.22.0 +REDIRECTS=$(cat <<-EOF +# Map SPA routes to the main index HTML file. +/* /index.html 200 +EOF +) +REDIRECTS_CID=$(echo $REDIRECTS | ipfs add --cid-version=1 -q) +HELLO_CID=$(echo "hello world" | ipfs add --cid-version=1 -q) +ipfs files mkdir -p --cid-version 1 /redirects-spa +ipfs files cp /ipfs/$REDIRECTS_CID "/redirects-spa/_redirects" +ipfs files cp /ipfs/$HELLO_CID "/redirects-spa/index.html" +ipfs files ls -l +# Manually CID of "redirects-spa" and then... +ipfs dag export $CID +``` diff --git a/fixtures/redirects_file/dnslink.yml b/fixtures/redirects_file/dnslink.yml index 15be77219..f5fb2cde9 100644 --- a/fixtures/redirects_file/dnslink.yml +++ b/fixtures/redirects_file/dnslink.yml @@ -2,6 +2,9 @@ dnslinks: custom-dnslink: subdomain: dnslink-enabled-on-fqdn - # this is the cid of the folder - # ./redirects.car:/examples/ - path: /ipfs/QmYBhLYDwVFvxos9h8CGU2ibaY66QNgv8hpfewxaQrPiZj \ No newline at end of file + # cid of ./redirects.car:/examples/ + path: /ipfs/QmYBhLYDwVFvxos9h8CGU2ibaY66QNgv8hpfewxaQrPiZj + dnslink-spa: + subdomain: dnslink-enabled-with-spa + # cid of ./redirects-spa.car + path: /ipfs/bafybeib5lboymwd6p2eo4qb2lkueaine577flvsjjeuevmp2nlio72xv5q diff --git a/fixtures/redirects_file/redirects-spa.car b/fixtures/redirects_file/redirects-spa.car new file mode 100644 index 0000000000000000000000000000000000000000..b92b253991c300332a456410a770b9355972bca9 GIT binary patch literal 360 zcmcColv%#k2 zU*Dvcr4|)u=I1d^VI)SmkO`LxW2lgV)iJIn3I2n>3cqYs`NaH8<#v`+tM856a*vLM z&cZ^S)$V=6J0DJ%FU7AOP@CfnsQAo?oN!8=h*HX|&SfgNMV8CSnvW1Z2GE#GL@)gSSi*i!90HeZ;ssI20 literal 0 HcmV?d00001 diff --git a/tests/redirects_file_test.go b/tests/redirects_file_test.go index af1c85c9a..b2af62f24 100644 --- a/tests/redirects_file_test.go +++ b/tests/redirects_file_test.go @@ -278,3 +278,55 @@ func TestRedirectsFileSupportWithDNSLink(t *testing.T) { RunWithSpecs(t, helpers.UnwrapSubdomainTests(t, tests), specs.DNSLinkGateway, specs.RedirectsFile) } + +func TestRedirectsFileWithIfNoneMatchHeader(t *testing.T) { + fixture := car.MustOpenUnixfsCar("redirects_file/redirects-spa.car") + + dnsLinks := dnslink.MustOpenDNSLink("redirects_file/dnslink.yml") + dnsLink := dnsLinks.MustGet("dnslink-spa") + + gatewayURL := SubdomainGatewayURL + u, err := url.Parse(gatewayURL) + if err != nil { + t.Fatal(err) + } + + pageURL := Fmt("{{scheme}}://{{dnslink}}.{{host}}/missing-page", u.Scheme, dnsLink, u.Host) + + var etag string + + RunWithSpecs(t, helpers.UnwrapSubdomainTests(t, SugarTests{ + { + Name: "request for $DNSLINK_FQDN/missing-page returns body of index.html as per _redirects", + Request: Request(). + URL(pageURL). + Headers( + Header("Accept", "text/html"), + ), + Response: Expect(). + Status(200). + Headers( + Header("Etag"). + Checks(func(v string) bool { + etag = v + return v != "" + }), + ). + Body(fixture.MustGetRawData("index.html")), + }, + }), specs.DNSLinkGateway, specs.RedirectsFile) + + RunWithSpecs(t, helpers.UnwrapSubdomainTests(t, SugarTests{ + { + Name: "request for $DNSLINK_FQDN/missing-page with If-None-Match returns 301", + Request: Request(). + URL(pageURL). + Headers( + Header("Accept", "text/html"), + Header("If-None-Match", etag), + ), + Response: Expect(). + Status(304), + }, + }), specs.DNSLinkGateway, specs.RedirectsFile) +}