@@ -4,14 +4,57 @@ import (
4
4
"bytes"
5
5
"io"
6
6
"io/ioutil"
7
+ "net/http"
7
8
"os"
8
9
"strings"
10
+ "sync/atomic"
9
11
"testing"
10
12
11
13
"github.com/wabarc/helper"
12
14
)
13
15
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
+
14
53
func TestPinFile (t * testing.T ) {
54
+ httpClient , mux , server := helper .MockServer ()
55
+ mux .HandleFunc ("/" , handleResponse )
56
+ defer server .Close ()
57
+
15
58
content := []byte (helper .RandString (6 , "lower" ))
16
59
tmpfile , err := ioutil .TempFile ("" , "ipfs-pinner-" )
17
60
if err != nil {
@@ -23,13 +66,17 @@ func TestPinFile(t *testing.T) {
23
66
t .Fatal (err )
24
67
}
25
68
26
- inf := & Infura {}
69
+ inf := & Infura {projectID , projectSecret , httpClient }
27
70
if _ , err := inf .PinFile (tmpfile .Name ()); err != nil {
28
71
t .Error (err )
29
72
}
30
73
}
31
74
32
75
func TestPinWithReader (t * testing.T ) {
76
+ httpClient , mux , server := helper .MockServer ()
77
+ mux .HandleFunc ("/" , handleResponse )
78
+ defer server .Close ()
79
+
33
80
content := []byte (helper .RandString (6 , "lower" ))
34
81
tmpfile , err := ioutil .TempFile ("" , "ipfs-pinner-" )
35
82
if err != nil {
@@ -49,7 +96,7 @@ func TestPinWithReader(t *testing.T) {
49
96
{"bytes.Buffer" , bytes .NewBufferString (helper .RandString (6 , "lower" ))},
50
97
}
51
98
52
- inf := & Infura {}
99
+ inf := & Infura {projectID , projectSecret , httpClient }
53
100
for _ , test := range tests {
54
101
t .Run (test .name , func (t * testing.T ) {
55
102
file := test .file .(io.Reader )
@@ -61,18 +108,62 @@ func TestPinWithReader(t *testing.T) {
61
108
}
62
109
63
110
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 }
65
116
buf := []byte (helper .RandString (6 , "lower" ))
66
117
if _ , err := inf .PinWithBytes (buf ); err != nil {
67
118
t .Error (err )
68
119
}
69
120
}
70
121
71
122
func TestPinHash (t * testing.T ) {
123
+ httpClient , mux , server := helper .MockServer ()
124
+ mux .HandleFunc ("/" , handleResponse )
125
+ defer server .Close ()
126
+
72
127
hash := "Qmaisz6NMhDB51cCvNWa1GMS7LU1pAxdF4Ld6Ft9kZEP2a"
73
128
74
- inf := & Infura {}
129
+ inf := & Infura {projectID , projectSecret , httpClient }
75
130
if ok , err := inf .PinHash (hash ); ! ok || err != nil {
76
131
t .Error (err )
77
132
}
78
133
}
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