Debian Codegraph @

putio/transfers_test.go

raw symbols SCIP
1 package putio
2
3 import (
4 "context"
5 "fmt"
6 "net/http"
7 "strings"
8 "testing"
9 )
10
11 func TestTransfers_Get(t *testing.T) {
12 setup()
13 defer teardown()
14
15 fixture := `
16 {
17 "status": "OK",
18 "transfer": {
19 "availability": null,
20 "callback_url": null,
21 "client_ip": null,
22 "created_at": "2016-07-15T09:45:15",
23 "created_torrent": false,
24 "current_ratio": 0.00,
25 "down_speed": 0,
26 "download_id": 20448117,
27 "downloaded": 0,
28 "error_message": null,
29 "estimated_time": null,
30 "extract": false,
31 "file_id": 415107363,
32 "finished_at": "2016-07-15T09:45:16",
33 "id": 1,
34 "is_private": false,
35 "magneturi": "magnet:?xt=urn:btih:4344503b7e797ebf31582327a5baae35b11bda01&dn=ubuntu-16.04-desktop-amd64.iso",
36 "name": "ubuntu-16.04-desktop-amd64.iso",
37 "peers_connected": 0,
38 "peers_getting_from_us": 0,
39 "peers_sending_to_us": 0,
40 "percent_done": 100,
41 "save_parent_id": 400004368,
42 "seconds_seeding": 0,
43 "simulated": true,
44 "size": 1485881344,
45 "source": "http://releases.ubuntu.com/16.04/ubuntu-16.04-desktop-amd64.iso.torrent",
46 "status": "COMPLETED",
47 "status_message": "Completed 4 mins ago.",
48 "subscription_id": null,
49 "torrent_link": "/v2/transfers/36003178/torrent",
50 "tracker_message": null,
51 "trackers": null,
52 "type": "TORRENT",
53 "up_speed": 0,
54 "uploaded": 0
55 }
56 }
57 `
58 mux.HandleFunc("/v2/transfers/1", func(w http.ResponseWriter, r *http.Request) {
59 testMethod(t, r, "GET")
60 fmt.Fprintln(w, fixture)
61 })
62
63 transfer, err := client.Transfers.Get(context.Background(), 1)
64 if err != nil {
65 t.Error(err)
66 }
67
68 if transfer.ID != 1 {
69 t.Errorf("got: %v, want: 1", transfer.ID)
70 }
71 }
72
73 func TestTransfers_List(t *testing.T) {
74 setup()
75 defer teardown()
76
77 fixture := `
78 {
79 "status": "OK",
80 "transfers": [
81 {
82 "availability": null,
83 "callback_url": null,
84 "client_ip": null,
85 "created_at": "2016-07-15T09:45:15",
86 "created_torrent": false,
87 "current_ratio": 0.00,
88 "down_speed": 0,
89 "download_id": 20448117,
90 "downloaded": 0,
91 "error_message": null,
92 "estimated_time": null,
93 "extract": false,
94 "file_id": 415107363,
95 "finished_at": "2016-07-15T09:45:16",
96 "id": 36003178,
97 "is_private": false,
98 "links": [
99 {
100 "label": "ud_logs",
101 "url": "https://papertrailapp.com/events?q=user_download_36003178"
102 },
103 {
104 "label": "d_logs",
105 "url": "https://papertrailapp.com/events?q=download_id_20448117"
106 },
107 {
108 "label": "torrent",
109 "url": "/v2/transfers/36003178/torrent"
110 },
111 {
112 "label": "magnet",
113 "url": "magnet:?xt=urn:btih:4344503b7e797ebf31582327a5baae35b11bda01&dn=ubuntu-16.04-desktop-amd64.iso"
114 }
115 ],
116 "magneturi": "magnet:?xt=urn:btih:4344503b7e797ebf31582327a5baae35b11bda01&dn=ubuntu-16.04-desktop-amd64.iso",
117 "name": "ubuntu-16.04-desktop-amd64.iso",
118 "peers_connected": 0,
119 "peers_getting_from_us": 0,
120 "peers_sending_to_us": 0,
121 "percent_done": 100,
122 "save_parent_id": 400004368,
123 "seconds_seeding": 0,
124 "simulated": true,
125 "size": 1485881344,
126 "source": "http://releases.ubuntu.com/16.04/ubuntu-16.04-desktop-amd64.iso.torrent",
127 "status": "COMPLETED",
128 "status_message": "Completed 29 mins ago.",
129 "subscription_id": null,
130 "torrent_link": "/v2/transfers/36003178/torrent",
131 "tracker_message": null,
132 "trackers": null,
133 "type": "TORRENT",
134 "up_speed": 0,
135 "uploaded": 0
136 }]
137 }
138 `
139
140 mux.HandleFunc("/v2/transfers/list", func(w http.ResponseWriter, r *http.Request) {
141 testMethod(t, r, "GET")
142 fmt.Fprintln(w, fixture)
143 })
144
145 transfers, err := client.Transfers.List(context.Background())
146 if err != nil {
147 t.Error(err)
148 }
149
150 if len(transfers) != 1 {
151 t.Errorf("got: %v, want: 1", len(transfers))
152 }
153
154 if transfers[0].ID != 36003178 {
155 t.Errorf("got: %v, want: 36003178", transfers[0].ID)
156 }
157 }
158
159 func TestTransfers_Add(t *testing.T) {
160 setup()
161 defer teardown()
162
163 fixture := `
164 {
165 "status": "OK",
166 "transfer": {
167 "availability": null,
168 "callback_url": null,
169 "client_ip": null,
170 "created_at": "2016-07-15T09:45:15",
171 "created_torrent": false,
172 "current_ratio": 0.00,
173 "down_speed": 0,
174 "download_id": null,
175 "downloaded": 0,
176 "error_message": null,
177 "estimated_time": null,
178 "extract": false,
179 "file_id": null,
180 "finished_at": null,
181 "id": 3600317,
182 "is_private": false,
183 "magneturi": "magnet:?xt=urn:btih:4344503b7e797ebf31582327a5baae35b11bda01&dn=ubuntu-16.04-desktop-amd64.iso",
184 "name": "ubuntu-16.04-desktop-amd64.iso",
185 "peers_connected": 0,
186 "peers_getting_from_us": 0,
187 "peers_sending_to_us": 0,
188 "percent_done": 0,
189 "save_parent_id": 400004368,
190 "seconds_seeding": 0,
191 "simulated": false,
192 "size": 1485881344,
193 "source": "http://releases.ubuntu.com/16.04/ubuntu-16.04-desktop-amd64.iso.torrent",
194 "status": "IN_QUEUE",
195 "status_message": "In queue...",
196 "subscription_id": null,
197 "torrent_link": "/v2/transfers/36003178/torrent",
198 "tracker_message": null,
199 "trackers": null,
200 "type": "TORRENT",
201 "up_speed": 0,
202 "uploaded": 0
203 }
204 }
205 `
206 mux.HandleFunc("/v2/transfers/add", func(w http.ResponseWriter, r *http.Request) {
207 testMethod(t, r, "POST")
208 testHeader(t, r, "Content-Type", "application/x-www-form-urlencoded")
209
210 // form values
211 u := r.FormValue("url")
212 if u == "" {
213 w.WriteHeader(http.StatusBadRequest)
214 return
215 }
216
217 fmt.Fprintln(w, fixture)
218 })
219
220 transfer, err := client.Transfers.Add(context.Background(), "http://releases.ubuntu.com/16.04/ubuntu-16.04-desktop-amd64.iso.torrent", 0, "")
221 if err != nil {
222 t.Error(err)
223 }
224
225 if transfer.ID != 3600317 {
226 t.Errorf("got: %v, want: 3100317", transfer.ID)
227 }
228
229 // empty URL
230 _, err = client.Transfers.Add(context.Background(), "", 0, "")
231 if err == nil {
232 t.Errorf("empty URL accepted")
233 }
234
235 // negative parent folder means use the user's prefered download folder.
236 _, err = client.Transfers.Add(context.Background(), "filepath", -1, "")
237 if err != nil {
238 t.Error(err)
239 }
240
241 // callback-url
242 _, err = client.Transfers.Add(context.Background(), "filepath", -1, "https://some-valid-endpoint-for-post-hook.com")
243 if err != nil {
244 t.Error(err)
245 }
246 }
247
248 func TestTransfers_Retry(t *testing.T) {
249 setup()
250 defer teardown()
251
252 fixture := `
253 {
254 "status": "OK",
255 "transfer": {
256 "availability": null,
257 "callback_url": null,
258 "client_ip": null,
259 "created_at": "2016-07-15T09:45:15",
260 "created_torrent": false,
261 "current_ratio": 0.00,
262 "down_speed": 0,
263 "download_id": null,
264 "downloaded": 0,
265 "error_message": null,
266 "estimated_time": null,
267 "extract": false,
268 "file_id": null,
269 "finished_at": null,
270 "id": 1,
271 "is_private": false,
272 "magneturi": "magnet:?xt=urn:btih:4344503b7e797ebf31582327a5baae35b11bda01&dn=ubuntu-16.04-desktop-amd64.iso",
273 "name": "ubuntu-16.04-desktop-amd64.iso",
274 "peers_connected": 0,
275 "peers_getting_from_us": 0,
276 "peers_sending_to_us": 0,
277 "percent_done": 0,
278 "save_parent_id": 400004368,
279 "seconds_seeding": 0,
280 "simulated": false,
281 "size": 1485881344,
282 "source": "http://releases.ubuntu.com/16.04/ubuntu-16.04-desktop-amd64.iso.torrent",
283 "status": "IN_QUEUE",
284 "status_message": "In queue...",
285 "subscription_id": null,
286 "torrent_link": "/v2/transfers/36003178/torrent",
287 "tracker_message": null,
288 "trackers": null,
289 "type": "TORRENT",
290 "up_speed": 0,
291 "uploaded": 0
292 }
293 }
294 `
295 mux.HandleFunc("/v2/transfers/retry", func(w http.ResponseWriter, r *http.Request) {
296 testMethod(t, r, "POST")
297 testHeader(t, r, "Content-Type", "application/x-www-form-urlencoded")
298
299 id := r.FormValue("id")
300 if id != "1" {
301 http.NotFound(w, r)
302 return
303 }
304
305 fmt.Fprintln(w, fixture)
306 })
307
308 transfer, err := client.Transfers.Retry(context.Background(), 1)
309 if err != nil {
310 t.Error(err)
311 }
312
313 if transfer.ID != 1 {
314 t.Errorf("got: %v, want: %v", transfer.ID, 1)
315 }
316
317 // non-existent tranfer iD
318 _, err = client.Transfers.Retry(context.Background(), 2)
319 if err == nil {
320 t.Fatal("must not return nil")
321 }
322 if err, ok := err.(*ErrorResponse); ok && err.Response.StatusCode != 404 {
323 t.Errorf("error: %s, excepted: 404", err)
324 }
325 }
326
327 func TestTransfers_Cancel(t *testing.T) {
328 setup()
329 defer teardown()
330
331 contains := func(s []string, i string) bool {
332 for _, v := range s {
333 if i == v {
334 return true
335 }
336 }
337 return false
338 }
339
340 mux.HandleFunc("/v2/transfers/cancel", func(w http.ResponseWriter, r *http.Request) {
341 testMethod(t, r, "POST")
342 testHeader(t, r, "Content-Type", "application/x-www-form-urlencoded")
343
344 transferIdsStr := r.FormValue("transfer_ids")
345 if transferIdsStr == "" {
346 w.WriteHeader(http.StatusBadRequest)
347 return
348 }
349
350 validIds := []string{"1", "2", "3"}
351 ids := strings.Split(transferIdsStr, ",")
352
353 for _, id := range ids {
354 if !contains(validIds, id) {
355 http.NotFound(w, r)
356 return
357 }
358 }
359
360 fmt.Fprintln(w, `{"status":"OK"}`)
361 })
362
363 err := client.Transfers.Cancel(context.Background(), 1)
364 if err != nil {
365 t.Error(err)
366 }
367
368 // empty params
369 err = client.Transfers.Cancel(context.Background())
370 if err == nil {
371 t.Errorf("no parameters given and accepted")
372 }
373
374 // non-existent transfer
375 err = client.Transfers.Cancel(context.Background(), 1, 2, 3, 4)
376 if err == nil {
377 t.Fatal("must not return nil")
378 }
379 if err, ok := err.(*ErrorResponse); ok && err.Response.StatusCode != 404 {
380 t.Errorf("error: %s, excepted: 404", err)
381 }
382 }
383
384 func TestTransfers_Clean(t *testing.T) {
385 setup()
386 defer teardown()
387
388 mux.HandleFunc("/v2/transfers/clean", func(w http.ResponseWriter, r *http.Request) {
389 testMethod(t, r, "POST")
390 testHeader(t, r, "Content-Type", "application/x-www-form-urlencoded")
391 fmt.Fprintln(w, `{"status":"OK"}`)
392 })
393
394 err := client.Transfers.Clean(context.Background())
395 if err != nil {
396 t.Error(err)
397 }
398 }