-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrequest_test.go
181 lines (168 loc) · 3.77 KB
/
request_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
package poeapi
import (
"net/http"
"sync"
"testing"
)
func TestGetJSON(t *testing.T) {
var (
c = client{
host: testHost,
limiter: newRateLimiter(UnlimitedRate, UnlimitedRate),
httpClient: testClient,
}
url = c.formatURL(leaguesEndpoint)
)
_, err := c.getJSON(url)
if err != nil {
t.Fatalf("failed to get json: %v", err)
}
}
func TestGetStashsJSON(t *testing.T) {
var (
c = client{
host: testHost,
limiter: newRateLimiter(UnlimitedRate, UnlimitedRate),
httpClient: testClient,
}
url = c.formatURL(stashTabsEndpoint)
)
_, err := c.getJSON(url)
if err != nil {
t.Fatalf("failed to get stash json: %v", err)
}
}
func TestGetJSONWithInvalidProtocol(t *testing.T) {
var (
c = client{
host: testHost,
limiter: newRateLimiter(UnlimitedRate, UnlimitedRate),
httpClient: testClient,
}
url = "htps://127.0.0.1:8000"
)
_, err := c.getJSON(url)
if err == nil {
t.Fatal("failed to detect invalid http protocol")
}
}
func TestGetJSONRateLimit(t *testing.T) {
type errorCollector struct {
set []error
lock sync.Mutex
}
var (
c = client{
host: testHost,
limiter: newRateLimiter(50, UnlimitedRate),
httpClient: testClient,
}
url = c.formatURL(rateLimitEndpoint)
errs = errorCollector{
set: make([]error, 0),
}
)
var wg sync.WaitGroup
for i := 0; i < 50; i++ {
wg.Add(1)
go func() {
_, err := c.getJSON(url)
errs.lock.Lock()
errs.set = append(errs.set, err)
errs.lock.Unlock()
wg.Done()
}()
}
wg.Wait()
rateLimited := false
for _, e := range errs.set {
if e == ErrRateLimited {
rateLimited = true
break
}
}
if !rateLimited {
t.Fatal("failed to handle rate-limited responses")
}
}
func TestHandleServerError(t *testing.T) {
c := client{
host: testHost,
limiter: newRateLimiter(UnlimitedRate, UnlimitedRate),
httpClient: testClient,
}
_, err := c.getJSON(c.formatURL(failureEndpoint))
if err != ErrServerFailure {
t.Fatal("failed to handle server error")
}
}
func TestWithRateLimit(t *testing.T) {
var (
c = client{
host: testHost,
limiter: newRateLimiter(DefaultRateLimit, UnlimitedRate),
httpClient: testClient,
}
url = "https://127.0.0.1:8000"
fn = func(s string) (string, error) { return s, nil }
)
_ = c.withRateLimit(url, fn)
}
func TestWithStashRateLimit(t *testing.T) {
var (
c = client{
host: testHost,
useSSL: true,
limiter: newRateLimiter(UnlimitedRate, DefaultStashRateLimit),
httpClient: testClient,
}
url = "https://127.0.0.1:8000/public-stash-tabs"
fn = func(s string) (string, error) { return s, nil }
)
_ = c.withRateLimit(url, fn)
}
func TestParseCodeBadRequest(t *testing.T) {
if err := parseError(http.StatusBadRequest); err != ErrBadRequest {
t.Fatal("failed to detect bad request")
}
}
func TestParseCodeUnknownFailure(t *testing.T) {
if err := parseError(http.StatusTeapot); err != ErrUnknownFailure {
t.Fatal("failed to detect unknown error")
}
}
func TestCacheHelperWithStashURL(t *testing.T) {
var (
c = client{
host: testHost,
useCache: true,
}
url = c.formatURL(stashTabsEndpoint)
fn = func(s string) (string, error) {
return "", nil
}
)
if _, err := c.withCache(url, fn); err != nil {
t.Fatal("failed to bypass cache in decorated function")
}
}
func TestCacheHelperWithErrorResult(t *testing.T) {
cache, err := newResponseCache(10)
if err != nil {
t.Fatalf("failed to create cache: %v", err)
}
var (
c = client{
host: testHost,
useCache: true,
cache: cache,
}
url = c.formatURL(leaguesEndpoint)
fn = func(s string) (string, error) {
return "", ErrUnknownFailure
}
)
if _, err := c.withCache(url, fn); err != ErrUnknownFailure {
t.Fatal("failed to detect error in decorated function")
}
}