The transport built by NewHTTPClient supplies default Accept, Content-Type and User-Agent headers. Per-request headers are only useful if they win against that real transport, so this exercises it rather than a bare test tripper.
(t *testing.T)
| 347 | // headers. Per-request headers are only useful if they win against that real transport, so this |
| 348 | // exercises it rather than a bare test tripper. |
| 349 | func TestRequestHeadersAgainstRealTransport(t *testing.T) { |
| 350 | var gotReq *http.Request |
| 351 | ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 352 | gotReq = r |
| 353 | w.WriteHeader(http.StatusOK) |
| 354 | })) |
| 355 | defer ts.Close() |
| 356 | |
| 357 | ios, _, _, _ := iostreams.Test() |
| 358 | httpClient, err := NewHTTPClient(HTTPClientOptions{ |
| 359 | AppVersion: "v1.2.3", |
| 360 | Config: tinyConfig{serverHostname(t, ts.URL) + ":oauth_token": "MYTOKEN"}, |
| 361 | Log: ios.ErrOut, |
| 362 | }) |
| 363 | require.NoError(t, err) |
| 364 | client := NewClientFromHTTP(httpClient) |
| 365 | |
| 366 | resp, err := client.Request(ts.URL, http.MethodGet, ts.URL+"/user/repos", nil, |
| 367 | WithHeader("Accept", "application/vnd.github.raw"), |
| 368 | WithHeader("Content-Type", "application/zip")) |
| 369 | require.NoError(t, err) |
| 370 | defer resp.Body.Close() |
| 371 | |
| 372 | assert.Equal(t, "application/vnd.github.raw", gotReq.Header.Get("Accept")) |
| 373 | assert.Equal(t, "application/zip", gotReq.Header.Get("Content-Type")) |
| 374 | |
| 375 | // Headers the caller did not override must still be supplied by the transport. |
| 376 | assert.Equal(t, "token MYTOKEN", gotReq.Header.Get("Authorization")) |
| 377 | assert.Equal(t, "GitHub CLI v1.2.3", gotReq.Header.Get("User-Agent")) |
| 378 | } |
| 379 | |
| 380 | func TestGraphQLWithHeader(t *testing.T) { |
| 381 | reg := &httpmock.Registry{} |
nothing calls this directly
no test coverage detected