forked from zmb3/spotify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspotify_test.go
102 lines (93 loc) · 2.62 KB
/
spotify_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
package spotify
import (
"io"
"net/http"
"net/http/httptest"
"os"
"strings"
"testing"
)
func testClient(code int, body io.Reader, validators ...func(*http.Request)) (*Client, *httptest.Server) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
for _, v := range validators {
v(r)
}
w.WriteHeader(code)
io.Copy(w, body)
r.Body.Close()
if closer, ok := body.(io.Closer); ok {
closer.Close()
}
}))
client := &Client{
http: http.DefaultClient,
baseURL: server.URL + "/",
}
return client, server
}
// Returns a client whose requests will always return
// the specified status code and body.
func testClientString(code int, body string, validators ...func(*http.Request)) (*Client, *httptest.Server) {
return testClient(code, strings.NewReader(body))
}
// Returns a client whose requests will always return
// a response with the specified status code and a body
// that is read from the specified file.
func testClientFile(code int, filename string, validators ...func(*http.Request)) (*Client, *httptest.Server) {
f, err := os.Open(filename)
if err != nil {
panic(err)
}
return testClient(code, f)
}
func TestNewReleases(t *testing.T) {
c, s := testClientFile(http.StatusOK, "test_data/new_releases.txt")
defer s.Close()
r, err := c.NewReleases()
if err != nil {
t.Fatal(err)
}
if r.Albums[0].ID != "60mvULtYiNSRmpVvoa3RE4" {
t.Error("Invalid data:", r.Albums[0].ID)
}
if r.Albums[0].Name != "We Are One (Ole Ola) [The Official 2014 FIFA World Cup Song]" {
t.Error("Invalid data", r.Albums[0].Name)
}
}
func TestNewReleasesRateLimitExceeded(t *testing.T) {
t.Parallel()
handlers := []http.HandlerFunc{
// first attempt fails
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Retry-After", "2")
w.WriteHeader(rateLimitExceededStatusCode)
io.WriteString(w, `{ "error": { "message": "slow down", "status": 429 } }`)
}),
// next attempt succeeds
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
f, err := os.Open("test_data/new_releases.txt")
if err != nil {
t.Fatal(err)
}
defer f.Close()
_, err = io.Copy(w, f)
if err != nil {
t.Fatal(err)
}
}),
}
i := 0
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
handlers[i](w, r)
i++
}))
defer server.Close()
client := &Client{http: http.DefaultClient, baseURL: server.URL + "/", AutoRetry: true}
releases, err := client.NewReleases()
if err != nil {
t.Fatal(err)
}
if releases.Albums[0].ID != "60mvULtYiNSRmpVvoa3RE4" {
t.Error("Invalid data:", releases.Albums[0].ID)
}
}