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

Function TestRequestDoesNotDecodeSuccessfulResponses

api/request_test.go:141–167  ·  view source on GitHub ↗

REST treats 204 and 205 specially and decodes every other 2xx as JSON. Request must do neither, so that bodiless successes and non-JSON successes both work.

(t *testing.T)

Source from the content-addressed store, hash-verified

139// REST treats 204 and 205 specially and decodes every other 2xx as JSON. Request must do neither,
140// so that bodiless successes and non-JSON successes both work.
141func TestRequestDoesNotDecodeSuccessfulResponses(t *testing.T) {
142 tests := []struct {
143 name string
144 statusCode int
145 body string
146 }{
147 {name: "bodiless 200, as returned by HEAD", statusCode: 200, body: ""},
148 {name: "no content", statusCode: 204, body: ""},
149 {name: "non-JSON body", statusCode: 200, body: "d3f9c4a"},
150 }
151
152 for _, tt := range tests {
153 t.Run(tt.name, func(t *testing.T) {
154 reg := &httpmock.Registry{}
155 defer reg.Verify(t)
156 client := newTestClient(reg)
157
158 reg.Register(httpmock.MatchAny, httpmock.StatusStringResponse(tt.statusCode, tt.body))
159
160 resp, err := client.Request("github.com", http.MethodGet, "repos/OWNER/REPO", nil)
161 require.NoError(t, err)
162 defer resp.Body.Close()
163
164 assert.Equal(t, tt.statusCode, resp.StatusCode)
165 })
166 }
167}
168
169func TestRequestError(t *testing.T) {
170 reg := &httpmock.Registry{}

Callers

nothing calls this directly

Calls 8

VerifyMethod · 0.95
RegisterMethod · 0.95
StatusStringResponseFunction · 0.92
newTestClientFunction · 0.85
RequestMethod · 0.80
EqualMethod · 0.80
RunMethod · 0.65
CloseMethod · 0.65

Tested by

no test coverage detected