@@ -123,7 +123,7 @@ func (co capturedOutput) reset() {
123
123
Err = co .oldErr
124
124
}
125
125
126
- func errorResponse (contentType string , body string ) * http.Response {
126
+ func errorResponse418 (contentType string , body string ) * http.Response {
127
127
response := & http.Response {
128
128
Status : "418 I'm a teapot" ,
129
129
StatusCode : 418 ,
@@ -135,35 +135,57 @@ func errorResponse(contentType string, body string) *http.Response {
135
135
return response
136
136
}
137
137
138
+ func errorResponse429 (retryAfter string ) * http.Response {
139
+ body := ""
140
+ response := & http.Response {
141
+ Status : "429 Too Many Requests" ,
142
+ StatusCode : 429 ,
143
+ Header : make (http.Header ),
144
+ Body : ioutil .NopCloser (strings .NewReader (body )),
145
+ ContentLength : int64 (len (body )),
146
+ }
147
+ response .Header .Set ("Content-Type" , "text/plain" )
148
+ response .Header .Set ("Retry-After" , retryAfter )
149
+ return response
150
+ }
151
+
138
152
func TestDecodeErrorResponse (t * testing.T ) {
139
153
testCases := []struct {
140
154
response * http.Response
141
155
wantMessage string
142
156
}{
143
157
{
144
- response : errorResponse ("text/html" , "Time for tea" ),
158
+ response : errorResponse418 ("text/html" , "Time for tea" ),
145
159
wantMessage : `expected response with Content-Type "application/json" but got status "418 I'm a teapot" with Content-Type "text/html"` ,
146
160
},
147
161
{
148
- response : errorResponse ("application/json" , `{"error": {"type": "json", "valid": no}}` ),
162
+ response : errorResponse418 ("application/json" , `{"error": {"type": "json", "valid": no}}` ),
149
163
wantMessage : "failed to parse API error response: invalid character 'o' in literal null (expecting 'u')" ,
150
164
},
151
165
{
152
- response : errorResponse ("application/json" , `{"error": {"type": "track_ambiguous", "message": "message", "possible_track_ids": ["a", "b"]}}` ),
166
+ response : errorResponse418 ("application/json" , `{"error": {"type": "track_ambiguous", "message": "message", "possible_track_ids": ["a", "b"]}}` ),
153
167
wantMessage : "message: a, b" ,
154
168
},
155
169
{
156
- response : errorResponse ("application/json" , `{"error": {"message": "message"}}` ),
170
+ response : errorResponse418 ("application/json" , `{"error": {"message": "message"}}` ),
157
171
wantMessage : "message" ,
158
172
},
159
173
{
160
- response : errorResponse ("application/problem+json" , `{"error": {"message": "new json format"}}` ),
174
+ response : errorResponse418 ("application/problem+json" , `{"error": {"message": "new json format"}}` ),
161
175
wantMessage : "new json format" ,
162
176
},
163
177
{
164
- response : errorResponse ("application/json" , `{"error": {}}` ),
178
+ response : errorResponse418 ("application/json" , `{"error": {}}` ),
165
179
wantMessage : "unexpected API response: 418" ,
166
180
},
181
+ {
182
+ response : errorResponse429 ("30" ),
183
+ wantMessage : "request failed with status 429 Too Many Requests; please try again after 30 seconds" ,
184
+ },
185
+ {
186
+ response : errorResponse429 ("Wed, 21 Oct 2015 07:28:00 GMT" ),
187
+ wantMessage : "request failed with status 429 Too Many Requests; please try again after Wed, 21 Oct 2015 07:28:00 GMT" ,
188
+ },
167
189
}
168
190
tc := testCases [0 ]
169
191
got := decodedAPIError (tc .response )
0 commit comments