Skip to content

Commit

Permalink
fix(wip): avoid mangling Host header handling
Browse files Browse the repository at this point in the history
- splitting into subdomain and dnslink gateway variants
- towards avoiding running everything three times
- keep custom Host header when defined
  • Loading branch information
lidel committed May 24, 2024
1 parent 3e74a1a commit 27c5303
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 48 deletions.
8 changes: 2 additions & 6 deletions .github/workflows/test-kubo-e2e.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,7 @@ jobs:
- name: Provision Kubo Gateway
run: |
# Import car files
cars=$(find ./fixtures -name '*.car')
for car in $cars
do
ipfs dag import --pin-roots=false --stats "$car"
done
find ./fixtures -name '*.car' -exec ipfs dag import --pin-roots=false {} \;
# Import ipns records
records=$(find ./fixtures -name '*.ipns-record')
Expand All @@ -63,7 +59,7 @@ jobs:
uses: ./gateway-conformance/.github/actions/test
with:
gateway-url: http://127.0.0.1:8080
subdomain-url: http://example.com
subdomain-url: http://example.com:8080
json: output.json
xml: output.xml
html: output.html
Expand Down
37 changes: 11 additions & 26 deletions tests/dnslink_gateway_test.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
package tests

import (
"net/url"
"testing"

"github.com/ipfs/gateway-conformance/tooling"
"github.com/ipfs/gateway-conformance/tooling/car"
. "github.com/ipfs/gateway-conformance/tooling/check"
"github.com/ipfs/gateway-conformance/tooling/dnslink"
"github.com/ipfs/gateway-conformance/tooling/helpers"
"github.com/ipfs/gateway-conformance/tooling/specs"
. "github.com/ipfs/gateway-conformance/tooling/test"
"github.com/ipfs/gateway-conformance/tooling/tmpl"
)

func TestDNSLinkGatewayUnixFSDirectoryListing(t *testing.T) {
Expand All @@ -20,28 +17,15 @@ func TestDNSLinkGatewayUnixFSDirectoryListing(t *testing.T) {
fixture := car.MustOpenUnixfsCar("dir_listing/fixtures.car")
file := fixture.MustGetNode("ą", "ę", "file-źł.txt")

// DNSLink domain and fixture we will be using for Host headerthis test
dnsLinkHostname := "dnslink-website.example.org"
dnsLinks := dnslink.MustOpenDNSLink("dir_listing/dnslink.yml")
dnsLink := dnsLinks.MustGet("dir-listing-website")

tests := SugarTests{}

// Sent requests to endpoint defined by --gateway-url
u, err := url.Parse(GatewayURL)
if err != nil {
t.Fatal(err)
}

// Host header should use dnslink domain with the same scheme as --gateway-url
hostHdr := tmpl.Fmt("{{scheme}}://{{dnslink}}", u.Scheme, dnsLink)

tests = append(tests, SugarTests{
tests := SugarTests{
{
Name: "Backlink on root CID should be hidden (TODO: cleanup Kubo-specifics)",
Request: Request().
Header("Host", hostHdr).
URL(GatewayURL),
Path("/").
Header("Host", dnsLink),
Response: Expect().
Body(
And(
Expand All @@ -53,8 +37,8 @@ func TestDNSLinkGatewayUnixFSDirectoryListing(t *testing.T) {
{
Name: "Redirect dir listing to URL with trailing slash",
Request: Request().
Header("Host", hostHdr).
URL(GatewayURL + "/ą/ę"),
Path("/ą/ę").
Header("Host", dnsLink),
Response: Expect().
Status(301).
Headers(
Expand All @@ -64,8 +48,8 @@ func TestDNSLinkGatewayUnixFSDirectoryListing(t *testing.T) {
{
Name: "Regular dir listing (TODO: cleanup Kubo-specifics)",
Request: Request().
Header("Host", hostHdr).
URL(GatewayURL + "/ą/ę"),
Path("/ą/ę").
Header("Host", dnsLink),
Response: Expect().
Headers(
Header("Etag").Contains(`"DirIndex-`),
Expand All @@ -81,13 +65,14 @@ func TestDNSLinkGatewayUnixFSDirectoryListing(t *testing.T) {
And(
Contains("Index of"),
Contains(`<a href="/%C4%85/%C4%99/..">..</a>`),
Contains(`/ipns/<a href="//{{hostname}}/">{{hostname}}</a>/<a href="//{{hostname}}/%C4%85">ą</a>/<a href="//{{hostname}}/%C4%85/%C4%99">ę</a>`, dnsLinkHostname),
Contains(`/ipns/<a href="//{{hostname}}/">{{hostname}}</a>/<a href="//{{hostname}}/%C4%85">ą</a>/<a href="//{{hostname}}/%C4%85/%C4%99">ę</a>`, dnsLink),
Contains(`<a href="/%C4%85/%C4%99/file-%C5%BA%C5%82.txt">file-źł.txt</a>`),
Contains(`<a class="ipfs-hash" translate="no" href="https://cid.ipfs.tech/#{{cid}}" target="_blank" rel="noreferrer noopener">`, file.Cid()),
),
),
},
}...)
}

RunWithSpecs(t, helpers.UnwrapSubdomainTests(t, tests), specs.DNSLinkGateway)
// TODO: add explicit proxy tests for specs.DNSLinkGateway
RunWithSpecs(t, tests, specs.DNSLinkGateway)
}
50 changes: 44 additions & 6 deletions tests/redirects_file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,12 @@ func TestRedirectsFileSupport(t *testing.T) {

redirectDirBaseURL := Fmt("{{scheme}}://{{cid}}.ipfs.{{host}}", u.Scheme, redirectDirCID, u.Host)

// TODO hostHdr := Fmt("{{cid}}.ipfs.{{host}}", redirectDirCID, u.Host)

tests = append(tests, SugarTests{
{
Name: "request for $REDIRECTS_DIR_HOSTNAME/redirect-one redirects with default of 301, per _redirects file",
Request: Request().
Header("Host", u.Host).
URL("{{url}}/redirect-one", redirectDirBaseURL),
Response: Expect().
Status(301).
Expand Down Expand Up @@ -288,15 +289,52 @@ func TestRedirectsFileWithIfNoneMatchHeader(t *testing.T) {
t.Fatal(err)
}

pageURL := Fmt("{{scheme}}://{{dnslink}}.{{host}}/missing-page", u.Scheme, dnsLink, u.Host)
subdomainPageURL := Fmt("{{scheme}}://{{dnslink}}.ipns.{{host}}/missing-page", u.Scheme, dnslink.InlineDNS(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",
Name: "request for //{{dnslink}}.ipns.{{subdomain-gw}}/missing-page returns body of index.html as per _redirects",
Request: Request().
URL(subdomainPageURL).
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.SubdomainGatewayIPNS, specs.RedirectsFile)

RunWithSpecs(t, helpers.UnwrapSubdomainTests(t, SugarTests{
{
Name: "request for //{{dnslink}}.ipns.{{subdomain-gw}}/missing-page with If-None-Match returns 304",
Request: Request().
URL(subdomainPageURL).
Headers(
Header("Accept", "text/html"),
Header("If-None-Match", etag),
),
Response: Expect().
Status(304),
},
}), specs.SubdomainGatewayIPNS, specs.RedirectsFile)

dnslinkPageURL := Fmt("{{scheme}}://{{dnslink}}/missing-page", u.Scheme, dnsLink)

RunWithSpecs(t, helpers.UnwrapSubdomainTests(t, SugarTests{
{
Name: "request for //{{dnslink}/missing-page returns body of index.html as per _redirects",
Request: Request().
URL(pageURL).
URL(dnslinkPageURL).
Headers(
Header("Accept", "text/html"),
),
Expand All @@ -315,9 +353,9 @@ func TestRedirectsFileWithIfNoneMatchHeader(t *testing.T) {

RunWithSpecs(t, helpers.UnwrapSubdomainTests(t, SugarTests{
{
Name: "request for $DNSLINK_FQDN/missing-page with If-None-Match returns 301",
Name: "request for $DNSLINK_FQDN/missing-page with If-None-Match returns 304",
Request: Request().
URL(pageURL).
URL(dnslinkPageURL).
Headers(
Header("Accept", "text/html"),
Header("If-None-Match", etag),
Expand Down
22 changes: 12 additions & 10 deletions tooling/helpers/subdomain.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,21 +33,23 @@ func unwrapSubdomainTest(t *testing.T, unwraped test.SugarTest) test.SugarTests
if err != nil {
t.Fatal(err)
}
// Because you might be testing an IPFS node in CI, or on your local machine, the test are designed
// to test the subdomain behavior (querying http://{CID}.my-subdomain-gateway.io/) even if the node is
// actually living on http://127.0.0.1:8080 or somewhere else.
//
// The test knows two addresses:
// - GatewayURL: the URL we connect to, it might be "dweb.link", "127.0.0.1:8080", etc.
// - SubdomainGatewayURL: the origin that informs value in Host HTTP header used for subdomain requests, it might be "dweb.link", "localhost", "example.com", etc.

// host is the subdomain ggateway origin we are testing, it might be `localhost` or `example.com`
host := u.Host
host := unwraped.Request.GetHeader("Host")
if host == "" {
// if no custom Host header was defined, create explicit one based on the Request.GetURL()
host = u.Host
}

// rawURL is the low level HTTP endpoint that is supposed to understand Host header (it might be `http://127.0.0.1/ipfs/something`)
// change the the low level HTTP endpoint to one defined via --gateway-url
u.Host = test.GatewayHost

rawURL := u.String()

// TODO: this mangles tests based on Host, and make it difficult to grasp hwo much is broken, because number of errors
// is multiplied x3
// We should have simple standalone test in subdomain gateway or dnslink test suite
// and remove it from here

return test.SugarTests{
{
Name: fmt.Sprintf("%s (direct HTTP)", unwraped.Name),
Expand Down
10 changes: 10 additions & 0 deletions tooling/test/sugar.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,16 @@ func (r RequestBuilder) GetURL() string {
return r.URL_
}

func (r RequestBuilder) GetHeader(hdr string) string {
if r.Headers_ != nil {
v, ok := r.Headers_[hdr]
if ok {
return v
}
}
return ""
}

func (r RequestBuilder) Proxy(path string, args ...any) RequestBuilder {
r.Proxy_ = tmpl.Fmt(path, args...)
return r
Expand Down

0 comments on commit 27c5303

Please sign in to comment.