Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(346)

Side by Side Diff: common/lhttp/client_test.go

Issue 2984913002: Add HTTP error annotation. (Closed)
Patch Set: Created 3 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « common/lhttp/client.go ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2015 The LUCI Authors. 1 // Copyright 2015 The LUCI Authors.
2 // 2 //
3 // Licensed under the Apache License, Version 2.0 (the "License"); 3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License. 4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at 5 // You may obtain a copy of the License at
6 // 6 //
7 // http://www.apache.org/licenses/LICENSE-2.0 7 // http://www.apache.org/licenses/LICENSE-2.0
8 // 8 //
9 // Unless required by applicable law or agreed to in writing, software 9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS, 10 // distributed under the License is distributed on an "AS IS" BASIS,
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after
133 defer ts.Close() 133 defer ts.Close()
134 134
135 httpReq := httpReqGen("GET", ts.URL, nil) 135 httpReq := httpReqGen("GET", ts.URL, nil)
136 136
137 clientReq := NewRequest(ctx, http.DefaultClient, fast, httpReq, func(resp *http.Response) error { 137 clientReq := NewRequest(ctx, http.DefaultClient, fast, httpReq, func(resp *http.Response) error {
138 t.Fail() 138 t.Fail()
139 return nil 139 return nil
140 }, nil) 140 }, nil)
141 141
142 status, err := clientReq() 142 status, err := clientReq()
143 » » So(err.Error(), ShouldResemble, "http request failed: Internal S erver Error (HTTP 500) (attempts: 4)") 143 » » So(err.Error(), ShouldResemble, "gave up after 4 attempts: http request failed: Internal Server Error (HTTP 500)")
144 So(status, ShouldResemble, 500) 144 So(status, ShouldResemble, 500)
145 }) 145 })
146 } 146 }
147 147
148 func TestGetJSON(t *testing.T) { 148 func TestGetJSON(t *testing.T) {
149 Convey(`HTTP GET JSON requests should be handled correctly.`, t, func(c C) { 149 Convey(`HTTP GET JSON requests should be handled correctly.`, t, func(c C) {
150 ctx := context.Background() 150 ctx := context.Background()
151 151
152 // First call returns HTTP 500, second succeeds. 152 // First call returns HTTP 500, second succeeds.
153 serverCalls := 0 153 serverCalls := 0
(...skipping 26 matching lines...) Expand all
180 ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWr iter, r *http.Request) { 180 ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWr iter, r *http.Request) {
181 serverCalls++ 181 serverCalls++
182 w.Header().Set("Content-Type", jsonContentType) 182 w.Header().Set("Content-Type", jsonContentType)
183 _, err := io.WriteString(w, "yo") 183 _, err := io.WriteString(w, "yo")
184 c.So(err, ShouldBeNil) 184 c.So(err, ShouldBeNil)
185 })) 185 }))
186 defer ts.Close() 186 defer ts.Close()
187 187
188 actual := map[string]string{} 188 actual := map[string]string{}
189 status, err := GetJSON(ctx, fast, http.DefaultClient, ts.URL, &a ctual) 189 status, err := GetJSON(ctx, fast, http.DefaultClient, ts.URL, &a ctual)
190 » » So(err.Error(), ShouldResemble, "bad response "+ts.URL+": invali d character 'y' looking for beginning of value (attempts: 4)") 190 » » So(err.Error(), ShouldResemble, "gave up after 4 attempts: bad r esponse "+ts.URL+": invalid character 'y' looking for beginning of value")
191 So(status, ShouldResemble, 200) 191 So(status, ShouldResemble, 200)
192 So(actual, ShouldResemble, map[string]string{}) 192 So(actual, ShouldResemble, map[string]string{})
193 }) 193 })
194 } 194 }
195 195
196 func TestGetJSONBadResultIgnore(t *testing.T) { 196 func TestGetJSONBadResultIgnore(t *testing.T) {
197 Convey(`Bad results from GET JSON requests should be handled correctly.` , t, func(c C) { 197 Convey(`Bad results from GET JSON requests should be handled correctly.` , t, func(c C) {
198 ctx := context.Background() 198 ctx := context.Background()
199 199
200 serverCalls := 0 200 serverCalls := 0
201 ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWr iter, r *http.Request) { 201 ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWr iter, r *http.Request) {
202 serverCalls++ 202 serverCalls++
203 w.Header().Set("Content-Type", jsonContentType) 203 w.Header().Set("Content-Type", jsonContentType)
204 _, err := io.WriteString(w, "yo") 204 _, err := io.WriteString(w, "yo")
205 c.So(err, ShouldBeNil) 205 c.So(err, ShouldBeNil)
206 })) 206 }))
207 defer ts.Close() 207 defer ts.Close()
208 208
209 status, err := GetJSON(ctx, fast, http.DefaultClient, ts.URL, ni l) 209 status, err := GetJSON(ctx, fast, http.DefaultClient, ts.URL, ni l)
210 » » So(err.Error(), ShouldResemble, "bad response "+ts.URL+": invali d character 'y' looking for beginning of value (attempts: 4)") 210 » » So(err.Error(), ShouldResemble, "gave up after 4 attempts: bad r esponse "+ts.URL+": invalid character 'y' looking for beginning of value")
211 So(status, ShouldResemble, 200) 211 So(status, ShouldResemble, 200)
212 }) 212 })
213 } 213 }
214 214
215 func TestGetJSONBadContentTypeIgnore(t *testing.T) { 215 func TestGetJSONBadContentTypeIgnore(t *testing.T) {
216 Convey(`Invalid content in bad GET JSON requests should be handled corre ctly`, t, func(c C) { 216 Convey(`Invalid content in bad GET JSON requests should be handled corre ctly`, t, func(c C) {
217 ctx := context.Background() 217 ctx := context.Background()
218 218
219 ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWr iter, r *http.Request) { 219 ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWr iter, r *http.Request) {
220 _, err := io.WriteString(w, "{}") 220 _, err := io.WriteString(w, "{}")
221 c.So(err, ShouldBeNil) 221 c.So(err, ShouldBeNil)
222 })) 222 }))
223 defer ts.Close() 223 defer ts.Close()
224 224
225 status, err := GetJSON(ctx, fast, http.DefaultClient, ts.URL, ni l) 225 status, err := GetJSON(ctx, fast, http.DefaultClient, ts.URL, ni l)
226 » » So(err.Error(), ShouldResemble, "unexpected Content-Type, expect ed \"application/json\", got \"text/plain; charset=utf-8\" (attempts: 4)") 226 » » So(err.Error(), ShouldResemble, "gave up after 4 attempts: unexp ected Content-Type, expected \"application/json\", got \"text/plain; charset=utf -8\"")
227 So(status, ShouldResemble, 200) 227 So(status, ShouldResemble, 200)
228 }) 228 })
229 } 229 }
230 230
231 func TestPostJSON(t *testing.T) { 231 func TestPostJSON(t *testing.T) {
232 Convey(`HTTP POST JSON requests should be handled correctly.`, t, func(c C) { 232 Convey(`HTTP POST JSON requests should be handled correctly.`, t, func(c C) {
233 ctx := context.Background() 233 ctx := context.Background()
234 234
235 // First call returns HTTP 500, second succeeds. 235 // First call returns HTTP 500, second succeeds.
236 serverCalls := 0 236 serverCalls := 0
(...skipping 205 matching lines...) Expand 10 before | Expand all | Expand 10 after
442 out := handler(r.Body) 442 out := handler(r.Body)
443 if out == nil { 443 if out == nil {
444 w.WriteHeader(500) 444 w.WriteHeader(500)
445 } else { 445 } else {
446 w.Header().Set("Content-Type", jsonContentType) 446 w.Header().Set("Content-Type", jsonContentType)
447 So(json.NewEncoder(w).Encode(out), ShouldBeNil) 447 So(json.NewEncoder(w).Encode(out), ShouldBeNil)
448 } 448 }
449 }) 449 })
450 }) 450 })
451 } 451 }
OLDNEW
« no previous file with comments | « common/lhttp/client.go ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698