MCPcopy Create free account
hub / github.com/cli/cli / TestHTTPClientVerboseLogSanitizesTextBody

Function TestHTTPClientVerboseLogSanitizesTextBody

api/http_client_test.go:504–548  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

502}
503
504func TestHTTPClientVerboseLogSanitizesTextBody(t *testing.T) {
505 const textBody = "\x1b]0;title\x1b\\\x1b[31mred\x1b[0m"
506 ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
507 w.Header().Set("Content-Type", "text/plain")
508 fmt.Fprint(w, textBody)
509 }))
510 defer ts.Close()
511
512 tests := []struct {
513 name string
514 newClient func(log io.Writer) (*http.Client, error)
515 }{
516 {
517 name: "GitHub client",
518 newClient: func(log io.Writer) (*http.Client, error) {
519 return NewHTTPClient(HTTPClientOptions{Log: log, LogColorize: true, LogVerboseHTTP: true})
520 },
521 },
522 {
523 name: "external client under GH_DEBUG=api",
524 newClient: func(log io.Writer) (*http.Client, error) {
525 t.Setenv("GH_DEBUG", "api")
526 return NewExternalHTTPClient(ExternalHTTPClientOptions{Log: log, LogColorize: true})
527 },
528 },
529 }
530
531 for _, tt := range tests {
532 t.Run(tt.name, func(t *testing.T) {
533 var log strings.Builder
534 client, err := tt.newClient(&log)
535 require.NoError(t, err)
536
537 res, err := client.Get(ts.URL)
538 require.NoError(t, err)
539 body, err := io.ReadAll(res.Body)
540 res.Body.Close()
541 require.NoError(t, err)
542
543 assert.Equal(t, textBody, string(body))
544 assert.NotContains(t, log.String(), "\x1b")
545 assert.Contains(t, log.String(), "^[]0;title^[\\^[[31mred^[[0m")
546 })
547 }
548}
549
550func TestNewHTTPClientVerboseWithoutLog(t *testing.T) {
551 ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {

Callers

nothing calls this directly

Calls 9

NewHTTPClientFunction · 0.85
NewExternalHTTPClientFunction · 0.85
EqualMethod · 0.80
ContainsMethod · 0.80
SetMethod · 0.65
CloseMethod · 0.65
RunMethod · 0.65
GetMethod · 0.65
StringMethod · 0.65

Tested by

no test coverage detected