-
Notifications
You must be signed in to change notification settings - Fork 12
/
handler_test.go
198 lines (163 loc) · 5.96 KB
/
handler_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package main
import (
"context"
"crypto/rand"
"io"
"net/http"
"testing"
"time"
bsnet "github.com/ipfs/boxo/bitswap/network"
bsserver "github.com/ipfs/boxo/bitswap/server"
"github.com/ipfs/go-metrics-interface"
"github.com/libp2p/go-libp2p"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestTrustless(t *testing.T) {
t.Parallel()
ts, gnd := mustTestServer(t, Config{
Bitswap: true,
TrustlessGatewayDomains: []string{"trustless.com"},
disableMetrics: true,
})
content := "hello world"
cid := mustAddFile(t, gnd, []byte(content))
url := ts.URL "/ipfs/" cid.String()
t.Run("Non-trustless request returns 406", func(t *testing.T) {
req, err := http.NewRequest(http.MethodGet, url, nil)
require.NoError(t, err)
req.Host = "trustless.com"
res, err := http.DefaultClient.Do(req)
assert.NoError(t, err)
assert.Equal(t, http.StatusNotAcceptable, res.StatusCode)
})
t.Run("Trustless request with query parameter returns 200", func(t *testing.T) {
req, err := http.NewRequest(http.MethodGet, url "?format=raw", nil)
require.NoError(t, err)
req.Host = "trustless.com"
res, err := http.DefaultClient.Do(req)
assert.NoError(t, err)
assert.Equal(t, http.StatusOK, res.StatusCode)
})
t.Run("Trustless request with accept header returns 200", func(t *testing.T) {
req, err := http.NewRequest(http.MethodGet, url, nil)
require.NoError(t, err)
req.Host = "trustless.com"
req.Header.Set("Accept", "application/vnd.ipld.raw")
res, err := http.DefaultClient.Do(req)
assert.NoError(t, err)
assert.Equal(t, http.StatusOK, res.StatusCode)
})
}
func TestNoBlockcacheHeader(t *testing.T) {
const authToken = "authorized"
const authHeader = "Authorization"
ts, gnd := mustTestServer(t, Config{
Bitswap: true,
TracingAuthToken: authToken,
disableMetrics: true,
})
content := make([]byte, 1024)
_, err := rand.Read(content)
require.NoError(t, err)
cid := mustAddFile(t, gnd, content)
url := ts.URL "/ipfs/" cid.String()
t.Run("Successful download of data with standard already cached in the node", func(t *testing.T) {
req, err := http.NewRequest(http.MethodGet, url, nil)
require.NoError(t, err)
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("When caching is explicitly skipped the data is not found", func(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
require.NoError(t, err)
// Both headers present, expect NoBlockcacheHeader to work
req.Header.Set(NoBlockcacheHeader, "true")
req.Header.Set(authHeader, authToken)
_, err = http.DefaultClient.Do(req)
assert.ErrorIs(t, err, context.DeadlineExceeded)
})
t.Run("When caching is explicitly skipped the data is found if a peer has it", func(t *testing.T) {
newHost, err := libp2p.New()
require.NoError(t, err)
ctx := context.Background()
// pacify metrics reporting code
ctx = metrics.CtxScope(ctx, "test.bsserver.host")
n := bsnet.NewFromIpfsHost(newHost, nil)
bs := bsserver.New(ctx, n, gnd.blockstore)
n.Start(bs)
defer bs.Close()
require.NoError(t, newHost.Connect(context.Background(), peer.AddrInfo{
ID: gnd.host.ID(),
Addrs: gnd.host.Addrs(),
}))
ctx, cancel := context.WithTimeout(ctx, time.Second*1)
defer cancel()
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
require.NoError(t, err)
// Both headers present, expect NoBlockcacheHeader to work
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)
// Both headers present, but NoBlockcacheHeader is not 'true'
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 header matches", func(t *testing.T) {
req, err := http.NewRequest(http.MethodGet, url, nil)
require.NoError(t, err)
// Authorization missing, expect NoBlockcacheHeader to be ignored
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)
})
t.Run("Skipping the cache only works when RAINBOW_TRACING_AUTH is set", func(t *testing.T) {
// Set up separate server without authToken set
ts2, gnd := mustTestServer(t, Config{
Bitswap: true,
TracingAuthToken: "", // simulate RAINBOW_TRACING_AUTH being not set
disableMetrics: true,
})
content := make([]byte, 1024)
_, err := rand.Read(content)
require.NoError(t, err)
cid2 := mustAddFile(t, gnd, content)
url := ts2.URL "/ipfs/" cid2.String()
req, err := http.NewRequest(http.MethodGet, url, nil)
require.NoError(t, err)
// Authorization missing, expect NoBlockcacheHeader to be ignored
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)
})
}