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

Function TestHTTPClientVerboseLogSanitizesTextBody

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

Source from the content-addressed store, hash-verified

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