diff --git a/handler_test.go b/handler_test.go index 31c9686..9866487 100644 --- a/handler_test.go +++ b/handler_test.go @@ -25,7 +25,7 @@ func TestTrustless(t *testing.T) { Bitswap: true, TrustlessGatewayDomains: []string{"trustless.com"}, disableMetrics: true, - }) + }, "") content := "hello world" cid := mustAddFile(t, gnd, []byte(content)) @@ -64,9 +64,11 @@ func TestTrustless(t *testing.T) { } func TestNoBlockcacheHeader(t *testing.T) { + const authToken = "authorized" + const authHeader = "Authorization" ts, gnd := mustTestServer(t, Config{ Bitswap: true, - }) + }, authToken) content := make([]byte, 1024) _, err := rand.Read(content) @@ -93,6 +95,7 @@ func TestNoBlockcacheHeader(t *testing.T) { require.NoError(t, err) req.Header.Set(NoBlockcacheHeader, "true") + req.Header.Set(authHeader, authToken) _, err = http.DefaultClient.Do(req) assert.ErrorIs(t, err, context.DeadlineExceeded) }) @@ -120,6 +123,7 @@ func TestNoBlockcacheHeader(t *testing.T) { require.NoError(t, err) req.Header.Set(NoBlockcacheHeader, "true") + req.Header.Set(authHeader, authToken) res, err := http.DefaultClient.Do(req) require.NoError(t, err) assert.Equal(t, http.StatusOK, res.StatusCode) @@ -127,4 +131,31 @@ func TestNoBlockcacheHeader(t *testing.T) { assert.NoError(t, err) assert.Equal(t, content, responseBody) }) + + t.Run("Skipping the cache only works when 'true' is passed", func(t *testing.T) { + req, err := http.NewRequest(http.MethodGet, url, nil) + require.NoError(t, err) + + req.Header.Set(NoBlockcacheHeader, "1") + req.Header.Set(authHeader, authToken) + res, err := http.DefaultClient.Do(req) + assert.NoError(t, err) + assert.Equal(t, http.StatusOK, res.StatusCode) + responseBody, err := io.ReadAll(res.Body) + assert.NoError(t, err) + assert.Equal(t, content, responseBody) + }) + + t.Run("Skipping the cache only works when the authorization field matches", func(t *testing.T) { + req, err := http.NewRequest(http.MethodGet, url, nil) + require.NoError(t, err) + + req.Header.Set(NoBlockcacheHeader, "true") + res, err := http.DefaultClient.Do(req) + assert.NoError(t, err) + assert.Equal(t, http.StatusOK, res.StatusCode) + responseBody, err := io.ReadAll(res.Body) + assert.NoError(t, err) + assert.Equal(t, content, responseBody) + }) } diff --git a/main_test.go b/main_test.go index a4eb39b..db512d4 100644 --- a/main_test.go +++ b/main_test.go @@ -68,10 +68,10 @@ func mustTestNodeWithKey(t *testing.T, cfg Config, sk ic.PrivKey) *Node { return nd } -func mustTestServer(t *testing.T, cfg Config) (*httptest.Server, *Node) { +func mustTestServer(t *testing.T, cfg Config, tracingAuth string) (*httptest.Server, *Node) { nd := mustTestNode(t, cfg) - handler, err := setupGatewayHandler(cfg, nd, "") + handler, err := setupGatewayHandler(cfg, nd, tracingAuth) if err != nil { require.NoError(t, err) }