-
Notifications
You must be signed in to change notification settings - Fork 3
/
client_test.go
190 lines (163 loc) · 3.85 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
package mapbox
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"net/url"
"sync"
"testing"
"time"
)
type roundTripperFunc func(*http.Request) (*http.Response, error)
func (rt roundTripperFunc) RoundTrip(req *http.Request) (*http.Response, error) {
return rt(req)
}
func mockClient(responses ...*http.Response) (*Client, chan *http.Request) {
ch := make(chan *http.Request)
i := 0
client := &Client{
httpClient: &http.Client{
Transport: roundTripperFunc(func(r *http.Request) (*http.Response, error) {
ch <- r
if i >= len(responses) {
return nil, errors.New("mockClient: not enough responses")
}
resp := responses[i]
i++
return resp, nil
}),
},
}
return client, ch
}
////////////////////////////////////////////////////////////////////////////////
func TestClient_raceCondition(t *testing.T) {
c, _ := NewClient(&MapboxConfig{
APIKey: "test",
})
rlc := &rateLimitingClient{}
c.httpClient = rlc
req := ReverseGeocodeRequest{
Coordinate: Coordinate{
Lat: 123.1,
Lng: 123.2,
},
Language: "en",
Limit: 1,
}
// Set limit, then run requests asynchronously until the limit is reset
n := 50
var wg sync.WaitGroup
wg.Add(n * 2)
rlc.rateLimiting = true
c.ReverseGeocode(context.Background(), &req)
rlc.rateLimiting = false
go func() {
for i := 0; i < n; i++ {
time.Sleep(50 * time.Millisecond)
c.ReverseGeocode(context.Background(), &req)
wg.Done()
}
}()
go func() {
for i := 0; i < n; i++ {
time.Sleep(50 * time.Millisecond)
c.ReverseGeocode(context.Background(), &req)
wg.Done()
}
}()
wg.Wait()
}
func TestClient_rateLimits(t *testing.T) {
c, _ := NewClient(&MapboxConfig{
APIKey: "test",
})
rlc := &rateLimitingClient{}
c.httpClient = rlc
req := ReverseGeocodeRequest{
Coordinate: Coordinate{
Lat: 123.1,
Lng: 123.2,
},
Language: "en",
Limit: 1,
}
// not rate limiting
_, err := c.ReverseGeocode(
context.Background(),
&req,
)
if err != nil {
t.Fatalf("expected no error, got %v", err)
}
// rate limiting
rlc.rateLimiting = true
_, err = c.ReverseGeocode(
context.Background(),
&req,
)
t.Logf("error: %v", err)
if err.Error() != "api error(429): Too Many Requests" {
t.Fatalf("Expected error, got none")
}
// Next request should be auto rate limited
_, err = c.ReverseGeocode(
context.Background(),
&req,
)
if err.Error() != "api error(429): Rate limiting geocoding requests" {
t.Fatalf("Expected error, got none")
}
// After reset, should be good to go again
time.Sleep(2 * time.Second)
rlc.rateLimiting = false
_, err = c.ReverseGeocode(
context.Background(),
&req,
)
if err != nil {
t.Fatalf("expected no error, got %v", err)
}
}
func TestClientReferer(t *testing.T) {
expectedReferer := "https://example.com/"
client, requests := mockClient()
client.Referer = expectedReferer
go client.do(context.Background(), "GET", "/", url.Values{}, nil)
httpReq := <-requests
actualReferer := httpReq.Referer()
if expectedReferer != actualReferer {
t.Errorf("expected referer: %q, got: %q", expectedReferer, actualReferer)
}
}
////////////////////////////////////////////////////////////////////////////////
type rateLimitingClient struct {
rateLimiting bool
reset time.Time //nolint:unused
}
func (rlc *rateLimitingClient) Do(req *http.Request) (*http.Response, error) {
if !rlc.rateLimiting {
return &http.Response{
StatusCode: 200,
Body: io.NopCloser(bytes.NewBufferString("{}")),
}, nil
}
rateLimitErr := ErrorResponse{
Message: "Too Many Requests",
Code: "too_many_requests",
}
resJson, _ := json.Marshal(rateLimitErr)
headers := http.Header{}
headers.Add("X-Rate-Limit-Reset", fmt.Sprintf("%v", time.Now().Add(1*time.Second).Unix()))
return &http.Response{
StatusCode: 429,
Body: io.NopCloser(
bytes.NewBuffer(resJson),
),
Header: headers,
}, nil
}