Skip to content

Commit

Permalink
test: percent-encoded filename and _redirects with If-None-Match on D…
Browse files Browse the repository at this point in the history
…NSLink
  • Loading branch information
laurentsenta authored Sep 19, 2023
2 parents 512e0b4 + f97bbf8 commit 4dc579c
Show file tree
Hide file tree
Showing 9 changed files with 121 additions and 4 deletions.
13 changes: 13 additions & 0 deletions fixtures/path_gateway_unixfs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,16 @@ ipfs files ls -l
# Manually CID of "dir-with-files" and then...
ipfs dag export $CID
```

### [dir-with-percent-encoded-filename.car](./dir-with-percent-encoded-name-file.car)

```sh
ipfs version
# ipfs version 0.22.0
CID=$(echo "hello from a percent encoded filename" | ipfs add --cid-version=1 -q)
ipfs files mkdir -p --cid-version 1 /dir-with-percent-encoded-filename
ipfs files cp /ipfs/$CID "/dir-with-percent-encoded-filename/Portugal%2C+España=Peninsula Ibérica.txt"
ipfs files ls -l
# Manually CID of "dir-with-percent-encoded-filename" and then...
ipfs dag export $CID
```
Binary file not shown.
22 changes: 21 additions & 1 deletion fixtures/redirects_file/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/)
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
```
9 changes: 6 additions & 3 deletions fixtures/redirects_file/dnslink.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
# cid of ./redirects.car:/examples/
path: /ipfs/QmYBhLYDwVFvxos9h8CGU2ibaY66QNgv8hpfewxaQrPiZj
dnslink-spa:
subdomain: dnslink-enabled-with-spa
# cid of ./redirects-spa.car
path: /ipfs/bafybeib5lboymwd6p2eo4qb2lkueaine577flvsjjeuevmp2nlio72xv5q
Binary file added fixtures/redirects_file/redirects-spa.car
Binary file not shown.
Binary file removed fixtures/t0400-api-no-gateway.car
Binary file not shown.
17 changes: 17 additions & 0 deletions tests/path_gateway_unixfs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -577,3 +577,20 @@ func TestGatewayUnixFSFileRanges(t *testing.T) {

RunWithSpecs(t, tests, specs.PathGatewayUnixFS)
}

func TestPathGatewayMiscellaneous(t *testing.T) {
fixture := car.MustOpenUnixfsCar("path_gateway_unixfs/dir-with-percent-encoded-filename.car")
rootDirCID := fixture.MustGetCid()

tests := SugarTests{
{
Name: "GET for /ipfs/ file whose filename contains percentage-encoded characters works",
Request: Request().
Path("/ipfs/{{CID}}/Portugal%252C+España=Peninsula%20Ibérica.txt", rootDirCID),
Response: Expect().
Body(fixture.MustGetRawData("Portugal%2C+España=Peninsula Ibérica.txt")),
},
}

RunWithSpecs(t, tests, specs.PathGatewayUnixFS)
}
52 changes: 52 additions & 0 deletions tests/redirects_file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,3 +283,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)
}
12 changes: 12 additions & 0 deletions tests/subdomain_gateway_ipfs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,9 @@ func TestGatewaySubdomains(t *testing.T) {
CIDv1_TOO_LONG := fixture.MustGetCid("hello-CIDv1_TOO_LONG")
CIDWikipedia := "QmXoypizjW3WknFiJnKLwHCnL72vedxjQkDDP1mXWo6uco"

dirWithPercentEncodedFilename := car.MustOpenUnixfsCar("path_gateway_unixfs/dir-with-percent-encoded-filename.car")
dirWithPercentEncodedFilenameCID := dirWithPercentEncodedFilename.MustGetCid()

tests := SugarTests{}

// We're going to run the same test against multiple gateways (localhost, and a subdomain gateway)
Expand Down Expand Up @@ -150,6 +153,15 @@ func TestGatewaySubdomains(t *testing.T) {
Contains("{{scheme}}://{{cid}}.ipfs.{{host}}/", u.Scheme, CIDv1, u.Host),
),
},
{
Name: "request for example.com/ipfs/{CIDv1}/{filename with percent encoding} redirects to subdomain",
Request: Request().URL("{{url}}/ipfs/{{cid}}/Portugal%252C+España=Peninsula%20Ibérica.txt", gatewayURL, dirWithPercentEncodedFilenameCID),
Response: Expect().
Status(301).
Headers(
Header("Location").Equals("{{scheme}}://{{cid}}.ipfs.{{host}}/Portugal%252C+Espa%C3%B1a=Peninsula%20Ib%C3%A9rica.txt", u.Scheme, dirWithPercentEncodedFilenameCID, u.Host),
),
},
{
Name: "request for example.com/ipfs/{DirCID} redirects to subdomain",
Hint: `
Expand Down

0 comments on commit 4dc579c

Please sign in to comment.