Skip to content

Commit

Permalink
feat: add helpers lib
Browse files Browse the repository at this point in the history
  • Loading branch information
laurentsenta committed Apr 7, 2023
1 parent cec3fda commit a40f2e0
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 78 deletions.
80 changes: 2 additions & 78 deletions tests/t0109_gateway_web_redirects_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"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"
)
Expand Down Expand Up @@ -398,85 +399,8 @@ func TestRedirectsFileSupportWithDNSLink(t *testing.T) {
}

if specs.DNSLinkResolver.IsEnabled() {
Run(t, unwrapTests(t, tests))
Run(t, helpers.UnwrapSubdomainTests(t, tests))
} else {
t.Skip("subdomain gateway disabled")
}
}

func unwrapTests(t *testing.T, tests SugarTests) SugarTests {
t.Helper()

var out SugarTests
for _, test := range tests {
out = append(out, unwrapTestForGateway(t, test)...)
}
return out
}

func unwrapTestForGateway(t *testing.T, test SugarTest) SugarTests {
t.Helper()

baseURL := test.Request.GetURL()
req := test.Request
expected := test.Response

u, err := url.Parse(baseURL)
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 URL we test for subdomain requests, it might be "dweb.link", "localhost", "example.com", etc.

// host is the hostname of the gateway we are testing, it might be `localhost` or `example.com`
host := u.Host

// raw url is the url but we replace the host with our local url, it might be `http://127.0.0.1/ipfs/something`
u.Host = GatewayHost
rawURL := u.String()

return SugarTests{
{
Name: fmt.Sprintf("%s (direct HTTP)", test.Name),
Hint: fmt.Sprintf("%s\n%s", test.Hint, "direct HTTP request (hostname in URL, raw IP in Host header)"),
Request: req.
URL(rawURL).
DoNotFollowRedirects().
Headers(
Header("Host", host),
),
Response: expected,
},
{
Name: fmt.Sprintf("%s (HTTP proxy)", test.Name),
Hint: fmt.Sprintf("%s\n%s", test.Hint, "HTTP proxy (hostname is passed via URL)"),
Request: req.
URL(baseURL).
Proxy(GatewayURL).
DoNotFollowRedirects(),
Response: expected,
},
{
Name: fmt.Sprintf("%s (HTTP proxy tunneling via CONNECT)", test.Name),
Hint: fmt.Sprintf("%s\n%s", test.Hint, `HTTP proxy
In HTTP/1.x, the pseudo-method CONNECT,
can be used to convert an HTTP connection into a tunnel to a remote host
https://tools.ietf.org/html/rfc7231#section-4.3.6
`),
Request: req.
URL(baseURL).
Proxy(GatewayURL).
WithProxyTunnel().
DoNotFollowRedirects().
Headers(
Header("Host", host),
),
Response: expected,
},
}
}
90 changes: 90 additions & 0 deletions tooling/helpers/subdomain.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package helpers

import (
"fmt"
"net/url"
"testing"

"github.com/ipfs/gateway-conformance/tooling/test"
)

/**
* UnwrapSubdomainTests takes a list of tests and returns a (larger) list of tests
* that will run on the subdomain gateway.
*/
func UnwrapSubdomainTests(t *testing.T, tests test.SugarTests) test.SugarTests {
t.Helper()

var out test.SugarTests
for _, test := range tests {
out = append(out, unwrapSubdomainTest(t, test)...)
}
return out
}

func unwrapSubdomainTest(t *testing.T, unwraped test.SugarTest) test.SugarTests {
t.Helper()

baseURL := unwraped.Request.GetURL()
req := unwraped.Request
expected := unwraped.Response

u, err := url.Parse(baseURL)
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 URL we test for subdomain requests, it might be "dweb.link", "localhost", "example.com", etc.

// host is the hostname of the gateway we are testing, it might be `localhost` or `example.com`
host := u.Host

// raw url is the url but we replace the host with our local url, it might be `http://127.0.0.1/ipfs/something`
u.Host = test.GatewayHost
rawURL := u.String()

return test.SugarTests{
{
Name: fmt.Sprintf("%s (direct HTTP)", unwraped.Name),
Hint: fmt.Sprintf("%s\n%s", unwraped.Hint, "direct HTTP request (hostname in URL, raw IP in Host header)"),
Request: req.
URL(rawURL).
DoNotFollowRedirects().
Headers(
test.Header("Host", host),
),
Response: expected,
},
{
Name: fmt.Sprintf("%s (HTTP proxy)", unwraped.Name),
Hint: fmt.Sprintf("%s\n%s", unwraped.Hint, "HTTP proxy (hostname is passed via URL)"),
Request: req.
URL(baseURL).
Proxy(test.GatewayURL).
DoNotFollowRedirects(),
Response: expected,
},
{
Name: fmt.Sprintf("%s (HTTP proxy tunneling via CONNECT)", unwraped.Name),
Hint: fmt.Sprintf("%s\n%s", unwraped.Hint, `HTTP proxy
In HTTP/1.x, the pseudo-method CONNECT,
can be used to convert an HTTP connection into a tunnel to a remote host
https://tools.ietf.org/html/rfc7231#section-4.3.6
`),
Request: req.
URL(baseURL).
Proxy(test.GatewayURL).
WithProxyTunnel().
DoNotFollowRedirects().
Headers(
test.Header("Host", host),
),
Response: expected,
},
}
}

0 comments on commit a40f2e0

Please sign in to comment.