Skip to content

Commit 1976fd3

Browse files
Handle testing for infura using mock server
1 parent e32f203 commit 1976fd3

File tree

2 files changed

+100
-7
lines changed

2 files changed

+100
-7
lines changed

pkg/infura/infura.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ const api = "https://ipfs.infura.io:5001"
2222
type Infura struct {
2323
ProjectID string
2424
ProjectSecret string
25+
26+
client *http.Client
2527
}
2628

2729
// PinFile alias to *Infura.PinFile, the purpose is to be backwards
@@ -107,7 +109,7 @@ func (inf *Infura) PinWithBytes(buf []byte) (string, error) {
107109

108110
func (inf *Infura) pinFile(r *io.PipeReader, m *multipart.Writer) (string, error) {
109111
endpoint := api + "/api/v0/add"
110-
client := httpretry.NewClient(nil)
112+
client := httpretry.NewClient(inf.client)
111113
// client.Timeout = 30 * time.Second
112114

113115
req, err := http.NewRequest(http.MethodPost, endpoint, r)
@@ -170,7 +172,7 @@ func (inf *Infura) PinHash(hash string) (bool, error) {
170172
if inf.ProjectID != "" && inf.ProjectSecret != "" {
171173
req.Header.Add("Authorization", "Basic "+basicAuth(inf.ProjectID, inf.ProjectSecret))
172174
}
173-
client := httpretry.NewClient(nil)
175+
client := httpretry.NewClient(inf.client)
174176
resp, err := client.Do(req)
175177
if err != nil {
176178
return false, err
@@ -196,7 +198,7 @@ func (inf *Infura) PinHash(hash string) (bool, error) {
196198
return false, err
197199
}
198200

199-
if h, ok := dat["Pins"].([]interface{}); ok {
201+
if h, ok := dat["Pins"].([]interface{}); ok && len(h) > 0 {
200202
return h[0] == hash, nil
201203
}
202204

pkg/infura/infura_test.go

Lines changed: 95 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,57 @@ import (
44
"bytes"
55
"io"
66
"io/ioutil"
7+
"net/http"
78
"os"
89
"strings"
10+
"sync/atomic"
911
"testing"
1012

1113
"github.com/wabarc/helper"
1214
)
1315

16+
var (
17+
projectID = "fake-project-id"
18+
projectSecret = "fake-project-secret"
19+
addJSON = `{
20+
"Bytes": 0,
21+
"Hash": "Qmaisz6NMhDB51cCvNWa1GMS7LU1pAxdF4Ld6Ft9kZEP2a",
22+
"Name": "name",
23+
"Size": "string"
24+
}`
25+
pinHashJSON = `{
26+
"Pins": [
27+
"Qmaisz6NMhDB51cCvNWa1GMS7LU1pAxdF4Ld6Ft9kZEP2a"
28+
],
29+
"Progress": 0
30+
}`
31+
badRequestJSON = `{}`
32+
unauthorizedJSON = `{}`
33+
tooManyRequestsJSON = `{}`
34+
)
35+
36+
func handleResponse(w http.ResponseWriter, r *http.Request) {
37+
authorization := r.Header.Get("Authorization")
38+
if len(authorization) < 10 {
39+
w.WriteHeader(http.StatusUnauthorized)
40+
_, _ = w.Write([]byte(unauthorizedJSON))
41+
return
42+
}
43+
switch r.URL.Path {
44+
case "/api/v0/add":
45+
_, _ = w.Write([]byte(addJSON))
46+
case "/api/v0/pin/add":
47+
_, _ = w.Write([]byte(pinHashJSON))
48+
default:
49+
_, _ = w.Write([]byte(badRequestJSON))
50+
}
51+
}
52+
1453
func TestPinFile(t *testing.T) {
54+
httpClient, mux, server := helper.MockServer()
55+
mux.HandleFunc("/", handleResponse)
56+
defer server.Close()
57+
1558
content := []byte(helper.RandString(6, "lower"))
1659
tmpfile, err := ioutil.TempFile("", "ipfs-pinner-")
1760
if err != nil {
@@ -23,13 +66,17 @@ func TestPinFile(t *testing.T) {
2366
t.Fatal(err)
2467
}
2568

26-
inf := &Infura{}
69+
inf := &Infura{projectID, projectSecret, httpClient}
2770
if _, err := inf.PinFile(tmpfile.Name()); err != nil {
2871
t.Error(err)
2972
}
3073
}
3174

3275
func TestPinWithReader(t *testing.T) {
76+
httpClient, mux, server := helper.MockServer()
77+
mux.HandleFunc("/", handleResponse)
78+
defer server.Close()
79+
3380
content := []byte(helper.RandString(6, "lower"))
3481
tmpfile, err := ioutil.TempFile("", "ipfs-pinner-")
3582
if err != nil {
@@ -49,7 +96,7 @@ func TestPinWithReader(t *testing.T) {
4996
{"bytes.Buffer", bytes.NewBufferString(helper.RandString(6, "lower"))},
5097
}
5198

52-
inf := &Infura{}
99+
inf := &Infura{projectID, projectSecret, httpClient}
53100
for _, test := range tests {
54101
t.Run(test.name, func(t *testing.T) {
55102
file := test.file.(io.Reader)
@@ -61,18 +108,62 @@ func TestPinWithReader(t *testing.T) {
61108
}
62109

63110
func TestPinWithBytes(t *testing.T) {
64-
inf := &Infura{}
111+
httpClient, mux, server := helper.MockServer()
112+
mux.HandleFunc("/", handleResponse)
113+
defer server.Close()
114+
115+
inf := &Infura{projectID, projectSecret, httpClient}
65116
buf := []byte(helper.RandString(6, "lower"))
66117
if _, err := inf.PinWithBytes(buf); err != nil {
67118
t.Error(err)
68119
}
69120
}
70121

71122
func TestPinHash(t *testing.T) {
123+
httpClient, mux, server := helper.MockServer()
124+
mux.HandleFunc("/", handleResponse)
125+
defer server.Close()
126+
72127
hash := "Qmaisz6NMhDB51cCvNWa1GMS7LU1pAxdF4Ld6Ft9kZEP2a"
73128

74-
inf := &Infura{}
129+
inf := &Infura{projectID, projectSecret, httpClient}
75130
if ok, err := inf.PinHash(hash); !ok || err != nil {
76131
t.Error(err)
77132
}
78133
}
134+
135+
func TestRateLimit(t *testing.T) {
136+
if testing.Short() {
137+
t.Skip("skip in short mode")
138+
}
139+
140+
httpClient, mux, server := helper.MockServer()
141+
var retries int32
142+
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
143+
// Retry one times
144+
if retries < 1 {
145+
atomic.AddInt32(&retries, 1)
146+
w.WriteHeader(http.StatusTooManyRequests)
147+
_, _ = w.Write([]byte(tooManyRequestsJSON))
148+
} else {
149+
_, _ = w.Write([]byte(addJSON))
150+
}
151+
})
152+
defer server.Close()
153+
154+
content := []byte(helper.RandString(6, "lower"))
155+
tmpfile, err := ioutil.TempFile("", "ipfs-pinner-")
156+
if err != nil {
157+
t.Fatal(err)
158+
}
159+
defer os.Remove(tmpfile.Name())
160+
161+
if _, err := tmpfile.Write(content); err != nil {
162+
t.Fatal(err)
163+
}
164+
165+
inf := &Infura{projectID, projectSecret, httpClient}
166+
if _, err := inf.PinFile(tmpfile.Name()); err != nil {
167+
t.Error(err)
168+
}
169+
}

0 commit comments

Comments
 (0)