-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfetchWebAPI_test.go
More file actions
240 lines (201 loc) · 5.76 KB
/
fetchWebAPI_test.go
File metadata and controls
240 lines (201 loc) · 5.76 KB
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
package main
import (
"fmt"
"net/http"
"net/http/httptest"
"testing"
)
func TestFetchWebAPI(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Println(r.URL)
switch r.URL.Path {
case "/v1/me": //get profile info
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, `{
"display_name": "niko",
"id": "417",
"uri": "spotify:417",
"product": "free",
"images": [
{
"url": "https://iida.com/images/1234567"
}
]
}`)
case "/v1/me/playlists": //get current user's playlists
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, `{
"items": [
{
"name": "Groovy!",
"uri": "spotify:572",
"public": false,
"images": [
{
"url": "https://yamashitarocks.jp"
}
],
"id": "572"
}
]
}`)
case "/v1/playlists/playlist_id/tracks":
if r.Method == http.MethodGet { // get items from playlist
w.WriteHeader(http.StatusOK)
w.Write([]byte(`{
"items": [
{
"name": "Groovy!",
"uri": "spotify:572",
"public": false,
"track":
{
"uri": "https://yamashitarocks.jp"
},
"id": "572"
}
]
}`))
} else if r.Method == http.MethodPost { //add to items playlist
w.WriteHeader(http.StatusCreated)
w.Write([]byte(`{
"snapshot_id": "abc"
}`))
}
case "/v1/users/user_id/playlists": //create a playlist for user
w.WriteHeader(http.StatusCreated)
w.Write([]byte(`{
"external_urls": {
"spotify": "shufflify.app"
}
}`))
default:
w.WriteHeader(http.StatusBadRequest)
}
}))
defer server.Close()
client := server.Client()
t.Run("Checking GET profile info call", func(t *testing.T) {
type ImageData struct {
URL string
}
type ProfileInfo struct {
Display_name string `json:"display_name"`
ID string `json:"id"`
URI string `json:"uri"`
Product string `json:"product"`
Images []ImageData `json:"images"`
}
profileInfo := ProfileInfo{}
_, err := FetchWebAPI("GET", fmt.Sprintf("%s/v1/me", server.URL), nil, &profileInfo, client)
if err != nil {
t.Error(err)
}
if profileInfo.Display_name != "niko" {
t.Errorf("Expected 'niko', got %q\n", profileInfo.Display_name)
}
if profileInfo.ID != "417" {
t.Errorf("Expected '417', got %v\n", profileInfo.ID)
}
if profileInfo.Images[0].URL != "https://iida.com/images/1234567" {
t.Errorf("Expected 'https://iida.com/images/1234567', got %s\n", profileInfo.Images[0].URL)
}
if profileInfo.URI != "spotify:417" {
t.Errorf("Expected 'spotify:417', got %q\n", profileInfo.URI)
}
if profileInfo.Product != "free" {
t.Errorf("Expected 'free', got %q\n", profileInfo.Product)
}
fmt.Println(profileInfo)
fmt.Println("")
})
t.Run("Checking GET user's playlists", func(t *testing.T) {
type ImageData struct {
URL string
}
type Playlist struct {
Name string `json:"name"`
URI string `json:"uri"`
Public bool `json:"public"`
Images []ImageData `json:"images"`
ID int `json:"id,string"`
}
type PlaylistItems struct {
Items []Playlist `json:"items"`
}
playlistItems := PlaylistItems{}
_, err := FetchWebAPI("GET", fmt.Sprintf("%s/v1/me/playlists", server.URL), nil, &playlistItems, client)
if err != nil {
t.Error(err)
}
fmt.Println(playlistItems)
fmt.Println("")
if playlistItems.Items[0].Name != "Groovy!" {
t.Errorf("Expected 'Groovy!', got %q\n", playlistItems.Items[0].Name)
}
if playlistItems.Items[0].URI != "spotify:572" {
t.Errorf("Expected 'spotify:572', got %q\n", playlistItems.Items[0].URI)
}
if playlistItems.Items[0].Public != false {
t.Errorf("Expected %t, got %v", false, playlistItems.Items[0].Public)
}
if playlistItems.Items[0].ID != 572 {
t.Errorf("Expected '572', got %v", playlistItems.Items[0].ID)
}
if playlistItems.Items[0].Images[0].URL != "https://yamashitarocks.jp" {
t.Errorf("Expected 'https://yamashitarocks.jp', got %v", playlistItems.Items[0].Images[0].URL)
}
})
t.Run("Checking GET items from generic user's playlist", func(t *testing.T) {
type Track struct {
URI string `json:"uri"`
}
type TrackObject struct {
Track Track `json:"track"`
}
type TrackItems struct {
Items []TrackObject `json:"items"`
}
response := TrackItems{}
status, err := FetchWebAPI(http.MethodGet, fmt.Sprintf("%s/v1/playlists/playlist_id/tracks", server.URL), nil, &response, client)
if err != nil {
t.Error(err)
}
fmt.Println(response)
fmt.Println("")
if status != http.StatusOK {
t.Errorf("Expected %v, got %v", http.StatusOK, status)
}
if response.Items[0].Track.URI != "https://yamashitarocks.jp" {
t.Errorf("Expected 'https://yamashitarocks.jp', got %q\n", response.Items[0].Track.URI)
}
})
t.Run("Checking POST items to user's playlist", func(t *testing.T) {
var response struct {
Snapshot_id string `json:"snapshot_id"`
}
FetchWebAPI("POST", fmt.Sprintf("%s/v1/playlists/playlist_id/tracks", server.URL), nil, &response, client)
fmt.Println(response)
fmt.Println("")
if response.Snapshot_id != "abc" {
t.Errorf("Expected 'abc', got %v", response.Snapshot_id)
}
})
t.Run("Checking POST playlist to user's account", func(t *testing.T) {
type ExternalURLs struct {
Spotify string `json:"spotify"`
}
type Response struct {
External_urls ExternalURLs `json:"external_urls"`
}
var response Response
{
}
FetchWebAPI("POST", fmt.Sprintf("%s/v1/users/user_id/playlists", server.URL), nil, &response, client)
fmt.Println(response)
fmt.Println("")
if response.External_urls.Spotify != "shufflify.app" {
t.Errorf("Expected 'shufflify.app', got %v", response.External_urls.Spotify)
}
})
}