(t *testing.T)
| 382 | } |
| 383 | |
| 384 | func TestNewExternalHTTPClient(t *testing.T) { |
| 385 | tests := []struct { |
| 386 | name string |
| 387 | url string |
| 388 | }{ |
| 389 | { |
| 390 | name: "third-party host", |
| 391 | url: "https://example.com/path", |
| 392 | }, |
| 393 | { |
| 394 | // Even when talking to GitHub, the external client must not set |
| 395 | // authorization or any GitHub-specific headers. |
| 396 | name: "github.com host", |
| 397 | url: "https://api.github.com/repos/cli/cli", |
| 398 | }, |
| 399 | } |
| 400 | |
| 401 | for _, tt := range tests { |
| 402 | t.Run(tt.name, func(t *testing.T) { |
| 403 | var gotReq *http.Request |
| 404 | transport := &funcTripper{roundTrip: func(req *http.Request) (*http.Response, error) { |
| 405 | gotReq = req |
| 406 | return &http.Response{StatusCode: 204, Body: io.NopCloser(strings.NewReader(""))}, nil |
| 407 | }} |
| 408 | |
| 409 | client, err := NewExternalHTTPClient(ExternalHTTPClientOptions{ |
| 410 | AppVersion: "v1.2.3", |
| 411 | Transport: transport, |
| 412 | }) |
| 413 | require.NoError(t, err) |
| 414 | |
| 415 | req, err := http.NewRequest("GET", tt.url, nil) |
| 416 | require.NoError(t, err) |
| 417 | |
| 418 | res, err := client.Do(req) |
| 419 | require.NoError(t, err) |
| 420 | assert.Equal(t, 204, res.StatusCode) |
| 421 | |
| 422 | // No headers should be set by default, except for User-Agent which should include the app version. |
| 423 | assert.Equal(t, []string{"GitHub CLI v1.2.3"}, gotReq.Header.Values("user-agent")) |
| 424 | assert.Empty(t, gotReq.Header.Values("authorization")) |
| 425 | assert.Empty(t, gotReq.Header.Values("x-github-api-version")) |
| 426 | assert.Empty(t, gotReq.Header.Values("accept")) |
| 427 | assert.Empty(t, gotReq.Header.Values("content-type")) |
| 428 | assert.Empty(t, gotReq.Header.Values("time-zone")) |
| 429 | }) |
| 430 | } |
| 431 | } |
| 432 | |
| 433 | type fakeTelemetryDisabler struct { |
| 434 | disabled bool |
nothing calls this directly
no test coverage detected