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

Function TestNewExternalHTTPClient

api/http_client_test.go:629–676  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

627}
628
629func TestNewExternalHTTPClient(t *testing.T) {
630 tests := []struct {
631 name string
632 url string
633 }{
634 {
635 name: "third-party host",
636 url: "https://example.com/path",
637 },
638 {
639 // Even when talking to GitHub, the external client must not set
640 // authorization or any GitHub-specific headers.
641 name: "github.com host",
642 url: "https://api.github.com/repos/cli/cli",
643 },
644 }
645
646 for _, tt := range tests {
647 t.Run(tt.name, func(t *testing.T) {
648 var gotReq *http.Request
649 transport := &funcTripper{roundTrip: func(req *http.Request) (*http.Response, error) {
650 gotReq = req
651 return &http.Response{StatusCode: 204, Body: io.NopCloser(strings.NewReader(""))}, nil
652 }}
653
654 client, err := NewExternalHTTPClient(ExternalHTTPClientOptions{
655 AppVersion: "v1.2.3",
656 Transport: transport,
657 })
658 require.NoError(t, err)
659
660 req, err := http.NewRequest("GET", tt.url, nil)
661 require.NoError(t, err)
662
663 res, err := client.Do(req)
664 require.NoError(t, err)
665 assert.Equal(t, 204, res.StatusCode)
666
667 // No headers should be set by default, except for User-Agent which should include the app version.
668 assert.Equal(t, []string{"GitHub CLI v1.2.3"}, gotReq.Header.Values("user-agent"))
669 assert.Empty(t, gotReq.Header.Values("authorization"))
670 assert.Empty(t, gotReq.Header.Values("x-github-api-version"))
671 assert.Empty(t, gotReq.Header.Values("accept"))
672 assert.Empty(t, gotReq.Header.Values("content-type"))
673 assert.Empty(t, gotReq.Header.Values("time-zone"))
674 })
675 }
676}
677
678type fakeTelemetryDisabler struct {
679 disabled bool

Callers

nothing calls this directly

Calls 5

NewExternalHTTPClientFunction · 0.85
EqualMethod · 0.80
EmptyMethod · 0.80
RunMethod · 0.65
DoMethod · 0.65

Tested by

no test coverage detected