From 728b965f378564332b6aea515ee584528e85f170 Mon Sep 17 00:00:00 2001 From: Henrique Dias Date: Fri, 8 Sep 2023 15:24:53 +0200 Subject: [PATCH] feat: test for filenames with percent-encoding --- fixtures/path_gateway_unixfs/README.md | 13 +++++++++++++ .../dir-with-percent-encoded-filename.car | Bin 0 -> 261 bytes tests/path_gateway_unixfs_test.go | 17 +++++++++++++++++ tests/subdomain_gateway_ipfs_test.go | 12 ++++++++++++ 4 files changed, 42 insertions(+) create mode 100644 fixtures/path_gateway_unixfs/dir-with-percent-encoded-filename.car diff --git a/fixtures/path_gateway_unixfs/README.md b/fixtures/path_gateway_unixfs/README.md index 6f13750a7..9fa94a574 100644 --- a/fixtures/path_gateway_unixfs/README.md +++ b/fixtures/path_gateway_unixfs/README.md @@ -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 +``` \ No newline at end of file diff --git a/fixtures/path_gateway_unixfs/dir-with-percent-encoded-filename.car b/fixtures/path_gateway_unixfs/dir-with-percent-encoded-filename.car new file mode 100644 index 0000000000000000000000000000000000000000..559f4d338705995920cebb6ba5375dab4b418fbe GIT binary patch literal 261 zcmcColvu7>b|)04Ieb0Sz9fZ zeRMy)EVZaOGe3{9ju_QKAzUhqp+X8z6P78w3|T#GhETta@}k*+&$H)sJ!^Mex?@5o2sdYEDkRLRwLNu0o##YF=`FN@|KiT4qjaUSe)47XUn1aHIeL literal 0 HcmV?d00001 diff --git a/tests/path_gateway_unixfs_test.go b/tests/path_gateway_unixfs_test.go index ae3705d82..1af34c203 100644 --- a/tests/path_gateway_unixfs_test.go +++ b/tests/path_gateway_unixfs_test.go @@ -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) +} diff --git a/tests/subdomain_gateway_ipfs_test.go b/tests/subdomain_gateway_ipfs_test.go index 8fd11a682..c3304566a 100644 --- a/tests/subdomain_gateway_ipfs_test.go +++ b/tests/subdomain_gateway_ipfs_test.go @@ -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) @@ -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: `