(t *testing.T)
| 567 | } |
| 568 | |
| 569 | func TestNewExternalHTTPClient(t *testing.T) { |
| 570 | tests := []struct { |
| 571 | name string |
| 572 | url string |
| 573 | }{ |
| 574 | { |
| 575 | name: "third-party host", |
| 576 | url: "https://example.com/path", |
| 577 | }, |
| 578 | { |
| 579 | // Even when talking to GitHub, the external client must not set |
| 580 | // authorization or any GitHub-specific headers. |
| 581 | name: "github.com host", |
| 582 | url: "https://api.github.com/repos/cli/cli", |
| 583 | }, |
| 584 | } |
| 585 | |
| 586 | for _, tt := range tests { |
| 587 | t.Run(tt.name, func(t *testing.T) { |
| 588 | var gotReq *http.Request |
| 589 | transport := &funcTripper{roundTrip: func(req *http.Request) (*http.Response, error) { |
| 590 | gotReq = req |
| 591 | return &http.Response{StatusCode: 204, Body: io.NopCloser(strings.NewReader(""))}, nil |
| 592 | }} |
| 593 | |
| 594 | client, err := NewExternalHTTPClient(ExternalHTTPClientOptions{ |
| 595 | AppVersion: "v1.2.3", |
| 596 | Transport: transport, |
| 597 | }) |
| 598 | require.NoError(t, err) |
| 599 | |
| 600 | req, err := http.NewRequest("GET", tt.url, nil) |
| 601 | require.NoError(t, err) |
| 602 | |
| 603 | res, err := client.Do(req) |
| 604 | require.NoError(t, err) |
| 605 | assert.Equal(t, 204, res.StatusCode) |
| 606 | |
| 607 | // No headers should be set by default, except for User-Agent which should include the app version. |
| 608 | assert.Equal(t, []string{"GitHub CLI v1.2.3"}, gotReq.Header.Values("user-agent")) |
| 609 | assert.Empty(t, gotReq.Header.Values("authorization")) |
| 610 | assert.Empty(t, gotReq.Header.Values("x-github-api-version")) |
| 611 | assert.Empty(t, gotReq.Header.Values("accept")) |
| 612 | assert.Empty(t, gotReq.Header.Values("content-type")) |
| 613 | assert.Empty(t, gotReq.Header.Values("time-zone")) |
| 614 | }) |
| 615 | } |
| 616 | } |
| 617 | |
| 618 | type fakeTelemetryDisabler struct { |
| 619 | disabled bool |
nothing calls this directly
no test coverage detected