Skip to content

Commit 97fbfe8

Browse files
authored
feat: add User-Agent header to API request debug output (#432)
feat: log User-Agent header in API request debug output Add the User-Agent header to the debug log output for API requests, making it easier to verify CLI version propagation during debugging.
1 parent fae6821 commit 97fbfe8

2 files changed

Lines changed: 53 additions & 0 deletions

File tree

internal/api/debug.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ func (c *Client) printRequest(ctx context.Context, req *http.Request, skipDebugL
4242
var output string
4343
// HTTP Request
4444
outputLines = append(outputLines, fmt.Sprintf("HTTP Request: %v %v %v", req.Method, req.URL, req.Proto))
45+
// HTTP User-Agent
46+
outputLines = append(outputLines, fmt.Sprintf("HTTP Request User-Agent: %s", req.Header.Get("User-Agent")))
4547
// HTTP Body
4648
outputLines = append(outputLines, fmt.Sprintf("HTTP Request Body:\n%s", reqBody))
4749
output = strings.Join(outputLines, "\n")

internal/api/debug_test.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,64 @@
1515
package api
1616

1717
import (
18+
"fmt"
19+
"net/http"
1820
"os"
1921
"testing"
2022

23+
"github.com/slackapi/slack-cli/internal/config"
2124
"github.com/slackapi/slack-cli/internal/goutils"
25+
"github.com/slackapi/slack-cli/internal/iostreams"
26+
"github.com/slackapi/slack-cli/internal/slackcontext"
27+
"github.com/slackapi/slack-cli/internal/slackdeps"
28+
"github.com/stretchr/testify/mock"
2229
"github.com/stretchr/testify/require"
2330
)
2431

32+
func Test_printRequest(t *testing.T) {
33+
tests := map[string]struct {
34+
userAgent string
35+
expected string
36+
}{
37+
"includes User-Agent header in output": {
38+
userAgent: "slack-cli/v1.2.3 (os: darwin)",
39+
expected: "HTTP Request User-Agent: slack-cli/v1.2.3 (os: darwin)",
40+
},
41+
"includes empty User-Agent when header is not set": {
42+
userAgent: "",
43+
expected: "HTTP Request User-Agent: ",
44+
},
45+
}
46+
for name, tc := range tests {
47+
t.Run(name, func(t *testing.T) {
48+
ctx := slackcontext.MockContext(t.Context())
49+
fs := slackdeps.NewFsMock()
50+
osMock := slackdeps.NewOsMock()
51+
osMock.AddDefaultMocks()
52+
cfg := config.NewConfig(fs, osMock)
53+
cfg.DebugEnabled = true
54+
ioMock := iostreams.NewIOStreamsMock(cfg, fs, osMock)
55+
ioMock.On("PrintDebug", mock.Anything, mock.Anything, mock.MatchedBy(func(args ...any) bool { return true }))
56+
57+
c := &Client{io: ioMock}
58+
req, _ := http.NewRequest("GET", "https://slack.com/api/test", nil)
59+
if tc.userAgent != "" {
60+
req.Header.Set("User-Agent", tc.userAgent)
61+
}
62+
63+
c.printRequest(ctx, req, false)
64+
65+
var output string
66+
for _, call := range ioMock.Calls {
67+
if call.Method == "PrintDebug" {
68+
output = fmt.Sprintf(call.Arguments[1].(string), call.Arguments[2].([]any)...)
69+
}
70+
}
71+
require.Contains(t, output, tc.expected)
72+
})
73+
}
74+
}
75+
2576
func Test_RedactPII(t *testing.T) {
2677
home, _ := os.UserHomeDir()
2778
tests := map[string]struct {

0 commit comments

Comments
 (0)