MCPcopy Create free account
hub / github.com/cli/cli / TestRequestHeadersAgainstRealTransport

Function TestRequestHeadersAgainstRealTransport

api/request_test.go:349–378  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

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.
349func 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
380func TestGraphQLWithHeader(t *testing.T) {
381 reg := &httpmock.Registry{}

Callers

nothing calls this directly

Calls 9

TestFunction · 0.92
NewHTTPClientFunction · 0.85
serverHostnameFunction · 0.85
NewClientFromHTTPFunction · 0.85
RequestMethod · 0.80
EqualMethod · 0.80
WithHeaderFunction · 0.70
CloseMethod · 0.65
GetMethod · 0.65

Tested by

no test coverage detected