test(internal/authflow): assert user-agent header is not modified/added
Signed-off-by: Babak K. Shandiz <babakks@github.com>
This commit is contained in:
parent
4e8aa562a3
commit
2bf528ccc7
1 changed files with 11 additions and 17 deletions
|
|
@ -10,34 +10,28 @@ import (
|
|||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func Test_getViewer_preservesUserAgent(t *testing.T) {
|
||||
func Test_getViewer_leavesUserAgent(t *testing.T) {
|
||||
var receivedUA string
|
||||
var receivedAuth string
|
||||
|
||||
// Outer transport sets User-Agent, simulating the factory-built client's header middleware.
|
||||
// Inner transport captures headers as-received to verify they survived the wrapping.
|
||||
plainClient := &http.Client{
|
||||
Transport: &roundTripper{roundTrip: func(req *http.Request) (*http.Response, error) {
|
||||
req.Header.Set("User-Agent", "GitHub CLI 1.2.3 Agent/copilot-cli")
|
||||
return (&http.Client{
|
||||
Transport: &roundTripper{roundTrip: func(req *http.Request) (*http.Response, error) {
|
||||
receivedUA = req.Header.Get("User-Agent")
|
||||
receivedAuth = req.Header.Get("Authorization")
|
||||
return &http.Response{
|
||||
StatusCode: 200,
|
||||
Header: http.Header{"Content-Type": []string{"application/json"}},
|
||||
Body: io.NopCloser(bytes.NewBufferString(`{"data":{"viewer":{"login":"monalisa"}}}`)),
|
||||
Request: req,
|
||||
}, nil
|
||||
}},
|
||||
}).Transport.RoundTrip(req)
|
||||
receivedUA = req.Header.Get("User-Agent")
|
||||
receivedAuth = req.Header.Get("Authorization")
|
||||
|
||||
return &http.Response{
|
||||
StatusCode: 200,
|
||||
Header: http.Header{"Content-Type": []string{"application/json"}},
|
||||
Body: io.NopCloser(bytes.NewBufferString(`{"data":{"viewer":{"login":"monalisa"}}}`)),
|
||||
Request: req,
|
||||
}, nil
|
||||
}},
|
||||
}
|
||||
|
||||
login, err := getViewer(plainClient, "github.com", "test-token")
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, "monalisa", login)
|
||||
assert.Equal(t, "GitHub CLI 1.2.3 Agent/copilot-cli", receivedUA)
|
||||
assert.Empty(t, receivedUA, "User-Agent header should be left unset so that downstream transports can set it")
|
||||
assert.Equal(t, "token test-token", receivedAuth)
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue