| 1 | package putio |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "fmt" |
| 6 | "net/http" |
| 7 | "testing" |
| 8 | ) |
| 9 | |
| 10 | func TestZips_Get(t *testing.T) { |
| 11 | setup() |
| 12 | defer teardown() |
| 13 | |
| 14 | fixture := ` |
| 15 | { |
| 16 | "missing_files": [], |
| 17 | "size": 27039611973, |
| 18 | "status": "OK", |
| 19 | "url": "https://some-valid-storage-url.com/12345" |
| 20 | } |
| 21 | ` |
| 22 | mux.HandleFunc("/v2/zips/1", func(w http.ResponseWriter, r *http.Request) { |
| 23 | testMethod(t, r, "GET") |
| 24 | fmt.Fprintln(w, fixture) |
| 25 | }) |
| 26 | |
| 27 | zip, err := client.Zips.Get(context.Background(), 1) |
| 28 | if err != nil { |
| 29 | t.Error(err) |
| 30 | } |
| 31 | |
| 32 | if zip.URL != "https://some-valid-storage-url.com/12345" { |
| 33 | t.Errorf("got: %v, want: https://some-valid-storage-url.com/12345", zip.URL) |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | func TestZips_List(t *testing.T) { |
| 38 | setup() |
| 39 | defer teardown() |
| 40 | |
| 41 | fixture := ` |
| 42 | { |
| 43 | "status": "OK", |
| 44 | "zips": [ |
| 45 | { |
| 46 | "created_at": "2016-07-15T10:42:12", |
| 47 | "id": 4177262 |
| 48 | } |
| 49 | ] |
| 50 | } |
| 51 | ` |
| 52 | mux.HandleFunc("/v2/zips/list", func(w http.ResponseWriter, r *http.Request) { |
| 53 | testMethod(t, r, "GET") |
| 54 | fmt.Fprintln(w, fixture) |
| 55 | }) |
| 56 | |
| 57 | zips, err := client.Zips.List(context.Background()) |
| 58 | if err != nil { |
| 59 | t.Error(err) |
| 60 | } |
| 61 | |
| 62 | if len(zips) != 1 { |
| 63 | t.Errorf("got: %v, want: 1", len(zips)) |
| 64 | } |
| 65 | |
| 66 | if zips[0].ID != 4177262 { |
| 67 | t.Errorf("got: %v, want: 4177262", zips[0].ID) |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | func TestZips_Create(t *testing.T) { |
| 72 | setup() |
| 73 | defer teardown() |
| 74 | |
| 75 | fixture := ` |
| 76 | { |
| 77 | "status": "OK", |
| 78 | "zip_id": 4177264 |
| 79 | } |
| 80 | ` |
| 81 | mux.HandleFunc("/v2/zips/create", func(w http.ResponseWriter, r *http.Request) { |
| 82 | testMethod(t, r, "POST") |
| 83 | testHeader(t, r, "Content-Type", "application/x-www-form-urlencoded") |
| 84 | fmt.Fprintln(w, fixture) |
| 85 | }) |
| 86 | |
| 87 | id, err := client.Zips.Create(context.Background(), 666) |
| 88 | if err != nil { |
| 89 | t.Error(err) |
| 90 | } |
| 91 | |
| 92 | if id != 4177264 { |
| 93 | t.Errorf("got: %v, want 4177264", id) |
| 94 | } |
| 95 | |
| 96 | _, err = client.Zips.Create(context.Background()) |
| 97 | if err == nil { |
| 98 | t.Errorf("empty params accepted") |
| 99 | } |
| 100 | } |