From 9fd3ab542666cf562a025b1816657c8f1a345d02 Mon Sep 17 00:00:00 2001 From: Laurent Senta Date: Fri, 7 Apr 2023 09:58:41 +0200 Subject: [PATCH] feat: add helpers lib --- tests/t0109_gateway_web_redirects_test.go | 82 +-------------------- tooling/helpers/subdomain.go | 90 +++++++++++++++++++++++ 2 files changed, 93 insertions(+), 79 deletions(-) create mode 100644 tooling/helpers/subdomain.go diff --git a/tests/t0109_gateway_web_redirects_test.go b/tests/t0109_gateway_web_redirects_test.go index b6c1e8e25..94b47df00 100644 --- a/tests/t0109_gateway_web_redirects_test.go +++ b/tests/t0109_gateway_web_redirects_test.go @@ -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" ) @@ -306,7 +307,7 @@ func TestRedirectsFileSupport(t *testing.T) { } if specs.SubdomainGateway.IsEnabled() { - Run(t, unwrapTests(t, tests)) + Run(t, helpers.UnwrapSubdomainTests(t, tests)) } else { t.Skip("subdomain gateway disabled") } @@ -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, - }, - } -} diff --git a/tooling/helpers/subdomain.go b/tooling/helpers/subdomain.go new file mode 100644 index 000000000..7049d3e3e --- /dev/null +++ b/tooling/helpers/subdomain.go @@ -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, + }, + } +}