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

Function TestNewCachedHTTPClientWithStatuses

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

Source from the content-addressed store, hash-verified

727}
728
729func TestNewCachedHTTPClientWithStatuses(t *testing.T) {
730 tests := []struct {
731 name string
732 statuses []int
733 wantStatuses string
734 }{
735 {
736 name: "with statuses",
737 statuses: []int{http.StatusForbidden, http.StatusNotFound},
738 wantStatuses: "403,404",
739 },
740 {
741 name: "without statuses",
742 wantStatuses: "",
743 },
744 }
745
746 for _, tt := range tests {
747 t.Run(tt.name, func(t *testing.T) {
748 var gotHeader http.Header
749 httpClient := &http.Client{Transport: funcTripper{roundTrip: func(req *http.Request) (*http.Response, error) {
750 gotHeader = req.Header
751 return &http.Response{StatusCode: http.StatusOK, Body: http.NoBody, Request: req}, nil
752 }}}
753
754 client := NewCachedHTTPClientWithStatuses(httpClient, time.Hour, tt.statuses...)
755 res, err := client.Get("https://api.github.com/user")
756 require.NoError(t, err)
757 res.Body.Close()
758
759 assert.Equal(t, "1h0m0s", gotHeader.Get("X-GH-CACHE-TTL"))
760 if tt.wantStatuses != "" {
761 assert.Equal(t, tt.wantStatuses, gotHeader.Get("X-GH-CACHE-STATUSES"))
762 } else {
763 // We can't use the Get method because it'll return the same value for both empty and non-existent
764 // headers and we want to make sure the header is actually not present.
765 assert.Empty(t, gotHeader.Values("X-GH-CACHE-STATUSES"))
766 }
767 })
768 }
769}

Callers

nothing calls this directly

Calls 6

EqualMethod · 0.80
EmptyMethod · 0.80
RunMethod · 0.65
GetMethod · 0.65
CloseMethod · 0.65

Tested by

no test coverage detected