MCPcopy Index your code
hub / github.com/coder/coder / TestWrapTransportWithUserAgentHeader

Function TestWrapTransportWithUserAgentHeader

cli/root_test.go:447–501  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

445}
446
447func TestWrapTransportWithUserAgentHeader(t *testing.T) {
448 t.Parallel()
449
450 testCases := []struct {
451 name string
452 cmdArgs []string
453 cmdEnv map[string]string
454 expectedUserAgentHeader string
455 }{
456 {
457 name: "top-level command",
458 cmdArgs: []string{"login"},
459 expectedUserAgentHeader: fmt.Sprintf("coder-cli/%s (%s/%s; coder login)", buildinfo.Version(), runtime.GOOS, runtime.GOARCH),
460 },
461 {
462 name: "nested commands",
463 cmdArgs: []string{"templates", "list"},
464 expectedUserAgentHeader: fmt.Sprintf("coder-cli/%s (%s/%s; coder templates list)", buildinfo.Version(), runtime.GOOS, runtime.GOARCH),
465 },
466 {
467 name: "does not include positional args, flags, or env",
468 cmdArgs: []string{"templates", "push", "my-template", "-d", "/path/to/template", "--yes", "--var", "myvar=myvalue"},
469 cmdEnv: map[string]string{"SECRET_KEY": "secret_value"},
470 expectedUserAgentHeader: fmt.Sprintf("coder-cli/%s (%s/%s; coder templates push)", buildinfo.Version(), runtime.GOOS, runtime.GOARCH),
471 },
472 }
473
474 for _, tc := range testCases {
475 t.Run(tc.name, func(t *testing.T) {
476 t.Parallel()
477
478 ch := make(chan string, 1)
479 srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
480 select {
481 case ch <- r.Header.Get("User-Agent"):
482 default: // already sent
483 }
484 }))
485 t.Cleanup(srv.Close)
486
487 args := append([]string{}, tc.cmdArgs...)
488 inv, _ := clitest.New(t, args...)
489 inv.Environ.Set("CODER_URL", srv.URL)
490 for k, v := range tc.cmdEnv {
491 inv.Environ.Set(k, v)
492 }
493
494 ctx := testutil.Context(t, testutil.WaitShort)
495 _ = inv.WithContext(ctx).Run() // Ignore error as we only care about headers.
496
497 actual := testutil.RequireReceive(ctx, t, ch)
498 require.Equal(t, tc.expectedUserAgentHeader, actual, "User-Agent should match expected format exactly")
499 })
500 }
501}

Callers

nothing calls this directly

Calls 10

VersionFunction · 0.92
NewFunction · 0.92
ContextFunction · 0.92
RequireReceiveFunction · 0.92
WithContextMethod · 0.80
RunMethod · 0.65
GetMethod · 0.65
CleanupMethod · 0.65
SetMethod · 0.65
EqualMethod · 0.45

Tested by

no test coverage detected