Skip to content

Commit

Permalink
feat: test for filenames with percent-encoding
Browse files Browse the repository at this point in the history
closes #115
  • Loading branch information
hacdias committed Sep 8, 2023
1 parent f6d2dc7 commit 432c89a
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 0 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.
17 changes: 17 additions & 0 deletions tests/path_gateway_unixfs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -564,3 +564,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)
}
12 changes: 12 additions & 0 deletions tests/subdomain_gateway_ipfs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,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 @@ -145,6 +148,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ña=Peninsula%20Ibérica.txt", u.Scheme, dirWithPercentEncodedFilenameCID, u.Host),
),
},
{
Name: "request for example.com/ipfs/{DirCID} redirects to subdomain",
Hint: `
Expand Down

0 comments on commit 432c89a

Please sign in to comment.