-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathtorrentapi_test.go
315 lines (288 loc) · 6.64 KB
/
torrentapi_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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
package torrentapi
import (
"fmt"
"net/http"
"net/http/httptest"
"strings"
"sync"
"testing"
"time"
"github.com/kylelemons/godebug/pretty"
)
func TestTokenIsValid(t *testing.T) {
testData := []struct {
desc string
token Token
want bool
}{
{
desc: "Valid Token",
token: Token{Token: "test", Expires: time.Now().Add(time.Second * 100)},
want: true,
},
{
desc: "Expired Token",
token: Token{Token: "test", Expires: time.Now().Add(time.Second * -100)},
want: false,
},
{
desc: "Empty Token",
token: Token{Token: "", Expires: time.Now().Add(time.Second * 100)},
want: false,
},
}
for i, tc := range testData {
got := tc.token.IsValid()
if got != tc.want {
t.Errorf("Test(%d) %s: (%+v).IsValid() => got %v, want %v", i, tc.desc, tc.token, got, tc.want)
}
}
}
func performQueryStringTest(api *API, expectedResult string) (result bool, text string) {
if !strings.HasSuffix(api.Query, expectedResult) {
text = fmt.Sprintf("api.Query doesn't ends with requested string: %s, Query: %s", expectedResult, api.Query)
return
}
return true, ""
}
func TestAPISearchString(t *testing.T) {
api := new(API)
api = api.SearchString("test")
result, text := performQueryStringTest(api, "&search_string=test")
if !result {
t.Error(text)
}
}
func TestAPICategory(t *testing.T) {
api := new(API)
api = api.Category(1)
if len(api.categories) != 1 {
t.Error("Categories not added")
}
}
func TestAPISearchTVDB(t *testing.T) {
api := new(API)
api = api.SearchTVDB("123")
result, text := performQueryStringTest(api, "&search_tvdb=123")
if !result {
t.Error(text)
}
}
func TestAPISearchIMDb(t *testing.T) {
api := new(API)
api = api.SearchIMDb("tt123")
result, text := performQueryStringTest(api, "&search_imdb=tt123")
if !result {
t.Error(text)
}
}
func TestAPISearchTheMovieDb(t *testing.T) {
api := new(API)
api = api.SearchTheMovieDb("123")
result, text := performQueryStringTest(api, "&search_themoviedb=123")
if !result {
t.Error(text)
}
}
func TestAPIFormat(t *testing.T) {
api := new(API)
api = api.Format("super_format")
result, text := performQueryStringTest(api, "&format=super_format")
if !result {
t.Error(text)
}
}
func TestAPILimit(t *testing.T) {
api := new(API)
api = api.Limit(100)
result, text := performQueryStringTest(api, "&limit=100")
if !result {
t.Error(text)
}
}
func TestAPISort(t *testing.T) {
api := new(API)
api = api.Sort("seeders")
result, text := performQueryStringTest(api, "&sort=seeders")
if !result {
t.Error(text)
}
}
func TestAPIRankedTrue(t *testing.T) {
api := new(API)
api = api.Ranked(true)
result, text := performQueryStringTest(api, "&ranked=1")
if !result {
t.Error(text)
}
}
func TestAPIRankedFalse(t *testing.T) {
api := new(API)
api = api.Ranked(false)
result, text := performQueryStringTest(api, "&ranked=0")
if !result {
t.Error(text)
}
}
func TestAPIRankedMinSeeders(t *testing.T) {
api := new(API)
api = api.MinSeeders(100)
result, text := performQueryStringTest(api, "&min_seeders=100")
if !result {
t.Error(text)
}
}
func TestAPIRankedMinLeechers(t *testing.T) {
api := new(API)
api = api.MinLeechers(100)
result, text := performQueryStringTest(api, "&min_leechers=100")
if !result {
t.Error(text)
}
}
type fakeHandler struct {
sync.Mutex
cnt int
handlers []http.HandlerFunc
}
func (f *fakeHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
f.Lock()
defer f.Unlock()
fn := f.handlers[f.cnt%len(f.handlers)]
f.cnt++
fn(w, r)
}
func (f *fakeHandler) setHandlers(h []http.HandlerFunc) {
f.Lock()
defer f.Unlock()
f.handlers = h
f.cnt = 0
}
func fakeTokenHandler(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, `{"token": "some_token"}`)
}
func error500(w http.ResponseWriter, r *http.Request) {
http.Error(w, "some error", http.StatusInternalServerError)
}
func errorTooMany(w http.ResponseWriter, r *http.Request) {
http.Error(w, "too many requests error", http.StatusTooManyRequests)
}
func errorTokenExpired(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, `{
"error": "token expired",
"error_code": 4
}`)
}
func errorNoResults(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, `{
"error": "no results",
"error_code": 20
}`)
}
func okResponse(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, `{"torrent_results": [{"title": "Movie"}]}`)
}
func garbage(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "garbage")
}
func TestAPI(t *testing.T) {
testData := []struct {
desc string
handlers []http.HandlerFunc
wantErr bool
want TorrentResults
}{
{
desc: "error - garbage data",
handlers: []http.HandlerFunc{garbage},
wantErr: true,
},
{
desc: "error 500",
handlers: []http.HandlerFunc{error500},
wantErr: true,
},
{
desc: "too many requests then 500",
handlers: []http.HandlerFunc{
errorTooMany,
error500,
},
wantErr: true,
},
{
desc: "token expired, then 500",
handlers: []http.HandlerFunc{
errorTokenExpired,
error500,
},
wantErr: true,
},
{
desc: "token expired, then no results",
handlers: []http.HandlerFunc{
errorTokenExpired,
fakeTokenHandler,
errorNoResults,
},
},
{
desc: "too many requests then OK",
handlers: []http.HandlerFunc{
errorTooMany,
okResponse,
},
want: TorrentResults{{Title: "Movie"}},
},
{
desc: "token expired then OK",
handlers: []http.HandlerFunc{
errorTokenExpired,
fakeTokenHandler,
okResponse,
},
want: TorrentResults{{Title: "Movie"}},
},
{
desc: "token expired then too many then OK",
handlers: []http.HandlerFunc{
errorTokenExpired,
errorTooMany,
fakeTokenHandler,
errorTooMany,
okResponse,
},
want: TorrentResults{{Title: "Movie"}},
},
{
desc: "OK",
handlers: []http.HandlerFunc{
okResponse,
},
want: TorrentResults{{Title: "Movie"}},
},
}
fh := &fakeHandler{handlers: []http.HandlerFunc{fakeTokenHandler}}
ts := httptest.NewServer(fh)
defer ts.Close()
for i, tc := range testData {
t.Run(fmt.Sprintf("Test (%d) %s", i, tc.desc), func(t *testing.T) {
// insert token first to the handlers as New always fetches the token.
fh.setHandlers(append([]http.HandlerFunc{fakeTokenHandler}, tc.handlers...))
a, err := New("test", APIURL(ts.URL), RequestDelay(0))
if err != nil {
t.Fatalf("Failed to start API: %v", err)
}
got, err := a.call()
if (err != nil) != tc.wantErr {
t.Errorf("unexpected error got (%v) want (%v)", err, tc.wantErr)
}
if err != nil {
return
}
if diff := pretty.Compare(tc.want, got); diff != "" {
t.Errorf("unexpected results: diff -want +got\n%s", diff)
}
})
}
}