| 198 | } |
| 199 | |
| 200 | func Test_requestErrors(t *testing.T) { |
| 201 | ctx := context.Background() |
| 202 | testCases := []struct { |
| 203 | data any |
| 204 | method string |
| 205 | authFn authFunc |
| 206 | expected error |
| 207 | requester Requester |
| 208 | }{ |
| 209 | { |
| 210 | data: make(chan int), |
| 211 | expected: fmt.Errorf("cannot marshal request: json: unsupported type: chan int"), |
| 212 | }, |
| 213 | { |
| 214 | data: 42, |
| 215 | authFn: func(*http.Request) error { return fmt.Errorf("boom") }, |
| 216 | expected: fmt.Errorf("boom"), |
| 217 | }, |
| 218 | { |
| 219 | data: 42, |
| 220 | method: "Ω", |
| 221 | expected: fmt.Errorf(`cannot create request: net/http: invalid method "Ω"`), |
| 222 | }, |
| 223 | { |
| 224 | data: 42, |
| 225 | authFn: func(*http.Request) error { return nil }, |
| 226 | requester: &requester{err: fmt.Errorf("boom")}, |
| 227 | expected: fmt.Errorf("cannot perform request: boom"), |
| 228 | }, |
| 229 | { |
| 230 | data: 42, |
| 231 | authFn: func(*http.Request) error { return nil }, |
| 232 | requester: &requester{code: 400, body: io.NopCloser(strings.NewReader(`{"detail":"boom"}`))}, |
| 233 | expected: fmt.Errorf("boom"), |
| 234 | }, |
| 235 | { |
| 236 | data: 42, |
| 237 | authFn: func(*http.Request) error { return nil }, |
| 238 | requester: &requester{code: 200, body: io.NopCloser(badReader{})}, |
| 239 | expected: fmt.Errorf("cannot read response: boom"), |
| 240 | }, |
| 241 | } |
| 242 | |
| 243 | for _, tc := range testCases { |
| 244 | c := &Client{requester: tc.requester} |
| 245 | _, err := c.request(ctx, tc.method, endpoint{url: &url.URL{}, query: url.Values{}}, tc.data, tc.authFn) |
| 246 | require.Error(t, err) |
| 247 | assert.Equal(t, tc.expected.Error(), err.Error()) |
| 248 | } |
| 249 | } |