-
Notifications
You must be signed in to change notification settings - Fork 13
/
client_test.go
222 lines (185 loc) · 4.74 KB
/
client_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
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
package radiko
import (
"context"
"net/http"
"net/http/cookiejar"
"strings"
"testing"
"time"
)
func TestNew(t *testing.T) {
_, err := New("")
if err != nil {
t.Fatalf("Failed to construct client: %s", err)
}
}
func TestNew_EmptyHTTPClient(t *testing.T) {
var c *http.Client
SetHTTPClient(c)
defer teardownHTTPClient()
client, err := New("")
if err == nil {
t.Errorf(
"Should detect that HTTPClient is nil.\nclient: %v", client)
}
}
func TestNewRequest(t *testing.T) {
client, err := New("")
if err != nil {
t.Fatalf("Failed to construct client: %s", err)
}
ctx := context.Background()
_, err = client.newRequest(ctx, "GET", "", &Params{})
if err != nil {
t.Error(err)
}
}
func TestNewRequest_WithAuthToken(t *testing.T) {
const expected = "auth_token"
client, err := New(expected)
if err != nil {
t.Errorf("Failed to construct client: %s", err)
}
req, err := client.newRequest(context.Background(), "GET", "", &Params{
setAuthToken: true,
})
if err != nil {
t.Error(err)
}
if actual := req.Header.Get(radikoAuthTokenHeader); actual != expected {
t.Errorf("expected %s, but %s.", expected, actual)
}
}
func TestNewRequest_WithContext(t *testing.T) {
client, err := New("")
if err != nil {
t.Fatalf("Failed to construct client: %s", err)
}
timeout := 100 * time.Millisecond
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
_, err = client.newRequest(ctx, "GET", "", &Params{})
if err != nil {
t.Error(err)
}
select {
case <-time.After(3 * time.Second):
t.Fatalf("context: %v", ctx)
case <-ctx.Done():
}
if ctx.Err() == nil {
t.Errorf("Shoud detect the context deadline exceeded.\n%v", ctx)
}
}
func TestNewRequest_WithEmptyContext(t *testing.T) {
client, err := New("")
if err != nil {
t.Fatalf("Failed to construct client: %s", err)
}
var ctx context.Context
_, err = client.newRequest(ctx, "GET", "", &Params{})
if err == nil {
t.Error("Should detect an empty context.")
}
}
func TestClient_AreaID(t *testing.T) {
client, err := New("")
if err != nil {
t.Errorf("Failed to construct client: %s", err)
}
areaID := client.AreaID()
if areaID == "" {
t.Error("httpClient.AreaID is empty.")
}
if !(strings.HasPrefix(areaID, "JP") || areaID == "OUT") {
t.Errorf("Invalid area id.\nAreaID: %s", areaID)
}
}
func TestClient_SetAreaID(t *testing.T) {
client, err := New("")
if err != nil {
t.Errorf("Failed to construct client: %s", err)
}
const expected = "JP13"
client.SetAreaID(expected)
if actual := client.AreaID(); expected != actual {
t.Errorf("expected %v, but %v.", expected, actual)
}
}
func TestClient_SetJar(t *testing.T) {
client, err := New("")
if err != nil {
t.Errorf("Failed to construct client: %s", err)
}
expected, err := cookiejar.New(&cookiejar.Options{})
if err != nil {
t.Fatal(err)
}
client.SetJar(expected)
defer teardownHTTPClient()
if actual := client.Jar(); expected != actual {
t.Errorf("expected %v, but %v.", expected, actual)
}
}
func TestClient_SetAuthTokenHeader(t *testing.T) {
client, err := New("")
if err != nil {
t.Errorf("Failed to construct client: %s", err)
}
const expected = "test_token"
client.setAuthTokenHeader(expected)
if actual := client.AuthToken(); expected != actual {
t.Errorf("expected %s, but %s", expected, actual)
}
}
func TestDo(t *testing.T) {
client, err := New("")
if err != nil {
t.Errorf("Failed to construct client: %s", err)
}
ctx := context.Background()
req, err := client.newRequest(ctx, "GET", "", &Params{})
if err != nil {
t.Error(err)
}
resp, err := client.Do(req)
if err != nil {
t.Error(err)
}
defer resp.Body.Close()
const expected = 200
if actual := resp.StatusCode; actual != expected {
t.Errorf("expected %d, but StatusCode is %d.", expected, actual)
}
}
func TestSetHTTPClient(t *testing.T) {
const expected = 1 * time.Second
SetHTTPClient(&http.Client{Timeout: expected})
defer teardownHTTPClient()
client, err := New("")
if err != nil {
t.Errorf("Failed to construct client: %s", err)
}
if client.httpClient.Timeout != expected {
t.Errorf("expected %d, but %d", expected, client.httpClient.Timeout)
}
}
func TestSetUserAgent(t *testing.T) {
const expected = "test-user-agent"
SetUserAgent(expected)
if expected != userAgent {
t.Errorf("expected %s, but %s", expected, userAgent)
}
}
func TestAPIPath(t *testing.T) {
const path = "test"
var apiEndpoint string
apiEndpoint = apiPath(apiV2, path)
if !(strings.HasPrefix(apiEndpoint, apiV2+"/") && strings.HasSuffix(apiEndpoint, "/"+path)) {
t.Errorf("invalid apiEndpoint: %s", apiEndpoint)
}
apiEndpoint = apiPath(apiV3, path)
if !(strings.HasPrefix(apiEndpoint, apiV3+"/") && strings.HasSuffix(apiEndpoint, "/"+path)) {
t.Errorf("invalid apiEndpoint: %s", apiEndpoint)
}
}