-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
69 lines (56 loc) · 1.79 KB
/
main_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
package main
import (
"io"
"net/http"
"net/http/httptest"
"testing"
"github.com/ipfs/go-blockservice"
offline "github.com/ipfs/go-ipfs-exchange-offline"
"github.com/ipfs/go-libipfs/blocks"
"github.com/ipfs/go-libipfs/examples/gateway/common"
"github.com/stretchr/testify/assert"
)
const (
HelloWorldCID = "bafkreifzjut3te2nhyekklss27nh3k72ysco7y32koao5eei66wof36n5e"
)
func newProxyGateway(t *testing.T, rs *httptest.Server) *httptest.Server {
blockStore := newProxyStore(rs.URL, nil)
blockService := blockservice.New(blockStore, offline.Exchange(blockStore))
routing := newProxyRouting(rs.URL, nil)
gateway, err := common.NewBlocksGateway(blockService, routing)
if err != nil {
t.Error(err)
}
handler := common.NewBlocksHandler(gateway, 0)
ts := httptest.NewServer(handler)
t.Cleanup(ts.Close)
return ts
}
func TestErrorOnInvalidContent(t *testing.T) {
rs := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("wrong data"))
}))
t.Cleanup(rs.Close)
ts := newProxyGateway(t, rs)
res, err := http.Get(ts.URL + "/ipfs/" + HelloWorldCID)
assert.Nil(t, err)
body, err := io.ReadAll(res.Body)
res.Body.Close()
assert.Nil(t, err)
assert.EqualValues(t, res.StatusCode, http.StatusInternalServerError)
assert.Contains(t, string(body), blocks.ErrWrongHash.Error())
}
func TestPassOnOnCorrectContent(t *testing.T) {
rs := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("hello world"))
}))
t.Cleanup(rs.Close)
ts := newProxyGateway(t, rs)
res, err := http.Get(ts.URL + "/ipfs/" + HelloWorldCID)
assert.Nil(t, err)
body, err := io.ReadAll(res.Body)
res.Body.Close()
assert.Nil(t, err)
assert.EqualValues(t, res.StatusCode, http.StatusOK)
assert.EqualValues(t, string(body), "hello world")
}