| 1 |
package putio |
| 2 |
|
| 3 |
import ( |
| 4 |
"context" |
| 5 |
"fmt" |
| 6 |
"net/http" |
| 7 |
"testing" |
| 8 |
) |
| 9 |
|
| 10 |
func TestFriends_List(t *testing.T) { |
| 11 |
setup() |
| 12 |
defer teardown() |
| 13 |
|
| 14 |
fixture := ` |
| 15 |
{ |
| 16 |
"friends": [ |
| 17 |
{ |
| 18 |
"avatar_url": "", |
| 19 |
"id": 1, |
| 20 |
"name": "jet" |
| 21 |
}, |
| 22 |
{ |
| 23 |
"avatar_url": "", |
| 24 |
"id": 2, |
| 25 |
"name": "spike" |
| 26 |
}, |
| 27 |
{ |
| 28 |
"avatar_url": "", |
| 29 |
"id": 3, |
| 30 |
"name": "faye" |
| 31 |
}, |
| 32 |
{ |
| 33 |
"avatar_url": "", |
| 34 |
"id": 4, |
| 35 |
"name": "ein" |
| 36 |
}, |
| 37 |
{ |
| 38 |
"avatar_url": "", |
| 39 |
"id": 5, |
| 40 |
"name": "ed" |
| 41 |
} |
| 42 |
], |
| 43 |
"status": "OK", |
| 44 |
"total": 5 |
| 45 |
} |
| 46 |
` |
| 47 |
|
| 48 |
mux.HandleFunc("/v2/friends/list", func(w http.ResponseWriter, r *http.Request) { |
| 49 |
testMethod(t, r, "GET") |
| 50 |
fmt.Fprintln(w, fixture) |
| 51 |
}) |
| 52 |
|
| 53 |
friends, err := client.Friends.List(context.Background()) |
| 54 |
if err != nil { |
| 55 |
t.Error(err) |
| 56 |
} |
| 57 |
|
| 58 |
if len(friends) != 5 { |
| 59 |
t.Errorf("got: %v, want: 5", len(friends)) |
| 60 |
} |
| 61 |
|
| 62 |
if friends[0].ID != 1 { |
| 63 |
t.Errorf("got: %v, want: 1", 1) |
| 64 |
} |
| 65 |
|
| 66 |
if friends[1].Name != "spike" { |
| 67 |
t.Errorf("got: %v, want: spike", friends[1].Name) |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
func TestFriends_WaitingRequests(t *testing.T) { |
| 72 |
setup() |
| 73 |
defer teardown() |
| 74 |
|
| 75 |
fixture := ` |
| 76 |
{ |
| 77 |
"friends": [ |
| 78 |
{ |
| 79 |
"avatar_url": "", |
| 80 |
"id": 6, |
| 81 |
"name": "julia" |
| 82 |
}, |
| 83 |
{ |
| 84 |
"avatar_url": "", |
| 85 |
"id": 7, |
| 86 |
"name": "vicious" |
| 87 |
} |
| 88 |
], |
| 89 |
"status": "OK" |
| 90 |
} |
| 91 |
` |
| 92 |
|
| 93 |
mux.HandleFunc("/v2/friends/waiting-requests", func(w http.ResponseWriter, r *http.Request) { |
| 94 |
testMethod(t, r, "GET") |
| 95 |
fmt.Fprintln(w, fixture) |
| 96 |
}) |
| 97 |
|
| 98 |
friends, err := client.Friends.WaitingRequests(context.Background()) |
| 99 |
if err != nil { |
| 100 |
t.Error(err) |
| 101 |
} |
| 102 |
|
| 103 |
if len(friends) != 2 { |
| 104 |
t.Errorf("got: %v, want: 2", len(friends)) |
| 105 |
} |
| 106 |
|
| 107 |
if friends[0].ID != 6 { |
| 108 |
t.Errorf("got: %v, want: 6", friends[0].ID) |
| 109 |
} |
| 110 |
|
| 111 |
if friends[1].Name != "vicious" { |
| 112 |
t.Errorf("got: %v, want: vicious", friends[1].Name) |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
func TestFriends_Request(t *testing.T) { |
| 117 |
setup() |
| 118 |
defer teardown() |
| 119 |
|
| 120 |
mux.HandleFunc("/v2/friends/annie/request", func(w http.ResponseWriter, r *http.Request) { |
| 121 |
testMethod(t, r, "POST") |
| 122 |
testHeader(t, r, "Content-Type", "application/x-www-form-urlencoded") |
| 123 |
fmt.Fprintln(w, `{"status":"OK"}`) |
| 124 |
}) |
| 125 |
|
| 126 |
err := client.Friends.Request(context.Background(), "annie") |
| 127 |
if err != nil { |
| 128 |
t.Error(err) |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
err = client.Friends.Request(context.Background(), "") |
| 133 |
if err == nil { |
| 134 |
t.Error("empty username accepted") |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
func TestFriends_Approve(t *testing.T) { |
| 139 |
setup() |
| 140 |
defer teardown() |
| 141 |
|
| 142 |
mux.HandleFunc("/v2/friends/bob/approve", func(w http.ResponseWriter, r *http.Request) { |
| 143 |
testMethod(t, r, "POST") |
| 144 |
testHeader(t, r, "Content-Type", "application/x-www-form-urlencoded") |
| 145 |
fmt.Fprintln(w, `{"status":"OK"}`) |
| 146 |
}) |
| 147 |
|
| 148 |
err := client.Friends.Approve(context.Background(), "bob") |
| 149 |
if err != nil { |
| 150 |
t.Error(err) |
| 151 |
} |
| 152 |
|
| 153 |
|
| 154 |
err = client.Friends.Approve(context.Background(), "") |
| 155 |
if err == nil { |
| 156 |
t.Error("empty username accepted") |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
func TestFriends_Deny(t *testing.T) { |
| 161 |
setup() |
| 162 |
defer teardown() |
| 163 |
|
| 164 |
mux.HandleFunc("/v2/friends/andy/deny", func(w http.ResponseWriter, r *http.Request) { |
| 165 |
testMethod(t, r, "POST") |
| 166 |
testHeader(t, r, "Content-Type", "application/x-www-form-urlencoded") |
| 167 |
fmt.Fprintln(w, `{"status":"OK"}`) |
| 168 |
}) |
| 169 |
|
| 170 |
err := client.Friends.Deny(context.Background(), "andy") |
| 171 |
if err != nil { |
| 172 |
t.Error(err) |
| 173 |
} |
| 174 |
|
| 175 |
|
| 176 |
err = client.Friends.Deny(context.Background(), "") |
| 177 |
if err == nil { |
| 178 |
t.Error("empty username accepted") |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
func TestFriends_Unfriend(t *testing.T) { |
| 183 |
setup() |
| 184 |
defer teardown() |
| 185 |
|
| 186 |
mux.HandleFunc("/v2/friends/lin/unfriend", func(w http.ResponseWriter, r *http.Request) { |
| 187 |
testMethod(t, r, "POST") |
| 188 |
testHeader(t, r, "Content-Type", "application/x-www-form-urlencoded") |
| 189 |
fmt.Fprintln(w, `{"status":"OK"}`) |
| 190 |
}) |
| 191 |
|
| 192 |
err := client.Friends.Unfriend(context.Background(), "lin") |
| 193 |
if err != nil { |
| 194 |
t.Error(err) |
| 195 |
} |
| 196 |
|
| 197 |
|
| 198 |
err = client.Friends.Unfriend(context.Background(), "") |
| 199 |
if err == nil { |
| 200 |
t.Error("empty username accepted") |
| 201 |
} |
| 202 |
} |