(t *testing.T)
| 469 | } |
| 470 | |
| 471 | func TestNewExternalHTTPClient(t *testing.T) { |
| 472 | tests := []struct { |
| 473 | name string |
| 474 | url string |
| 475 | }{ |
| 476 | { |
| 477 | name: "third-party host", |
| 478 | url: "https://example.com/path", |
| 479 | }, |
| 480 | { |
| 481 | // Even when talking to GitHub, the external client must not set |
| 482 | // authorization or any GitHub-specific headers. |
| 483 | name: "github.com host", |
| 484 | url: "https://api.github.com/repos/cli/cli", |
| 485 | }, |
| 486 | } |
| 487 | |
| 488 | for _, tt := range tests { |
| 489 | t.Run(tt.name, func(t *testing.T) { |
| 490 | var gotReq *http.Request |
| 491 | transport := &funcTripper{roundTrip: func(req *http.Request) (*http.Response, error) { |
| 492 | gotReq = req |
| 493 | return &http.Response{StatusCode: 204, Body: io.NopCloser(strings.NewReader(""))}, nil |
| 494 | }} |
| 495 | |
| 496 | client, err := NewExternalHTTPClient(ExternalHTTPClientOptions{ |
| 497 | AppVersion: "v1.2.3", |
| 498 | Transport: transport, |
| 499 | }) |
| 500 | require.NoError(t, err) |
| 501 | |
| 502 | req, err := http.NewRequest("GET", tt.url, nil) |
| 503 | require.NoError(t, err) |
| 504 | |
| 505 | res, err := client.Do(req) |
| 506 | require.NoError(t, err) |
| 507 | assert.Equal(t, 204, res.StatusCode) |
| 508 | |
| 509 | // No headers should be set by default, except for User-Agent which should include the app version. |
| 510 | assert.Equal(t, []string{"GitHub CLI v1.2.3"}, gotReq.Header.Values("user-agent")) |
| 511 | assert.Empty(t, gotReq.Header.Values("authorization")) |
| 512 | assert.Empty(t, gotReq.Header.Values("x-github-api-version")) |
| 513 | assert.Empty(t, gotReq.Header.Values("accept")) |
| 514 | assert.Empty(t, gotReq.Header.Values("content-type")) |
| 515 | assert.Empty(t, gotReq.Header.Values("time-zone")) |
| 516 | }) |
| 517 | } |
| 518 | } |
| 519 | |
| 520 | type fakeTelemetryDisabler struct { |
| 521 | disabled bool |
nothing calls this directly
no test coverage detected