Skip to content

Commit

Permalink
feat: add more cache skipping tests
Browse files Browse the repository at this point in the history
  • Loading branch information
aschmahmann committed Jun 25, 2024
1 parent 9f87182 commit c0447ab
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 4 deletions.
35 changes: 33 additions & 2 deletions handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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)
Expand All @@ -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)
})
Expand Down Expand Up @@ -120,11 +123,39 @@ 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)
responseBody, err := io.ReadAll(res.Body)
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)
})
}
4 changes: 2 additions & 2 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down

0 comments on commit c0447ab

Please sign in to comment.