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

Function Test_readBodyAsError

codersdk/client_internal_test.go:203–346  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

201}
202
203func Test_readBodyAsError(t *testing.T) {
204 t.Parallel()
205
206 exampleURL := "http://example.com"
207 simpleResponse := Response{
208 Message: "test",
209 Detail: "hi",
210 }
211
212 longResponse := ""
213 for i := 0; i < 4000; i++ {
214 longResponse += "a"
215 }
216
217 unexpectedJSON := marshal(map[string]any{
218 "hello": "world",
219 "foo": "bar",
220 })
221
222 //nolint:bodyclose
223 tests := []struct {
224 name string
225 req *http.Request
226 res *http.Response
227 assert func(t *testing.T, err error)
228 }{
229 {
230 name: "JSONWithRequest",
231 req: httptest.NewRequest(http.MethodGet, exampleURL, nil),
232 res: newResponse(http.StatusNotFound, jsonCT, marshal(simpleResponse)),
233 assert: func(t *testing.T, err error) {
234 sdkErr := assertSDKError(t, err)
235
236 assert.Equal(t, simpleResponse, sdkErr.Response)
237 assert.ErrorContains(t, err, sdkErr.Response.Message)
238 assert.ErrorContains(t, err, sdkErr.Response.Detail)
239
240 assert.Equal(t, http.StatusNotFound, sdkErr.StatusCode())
241 assert.ErrorContains(t, err, strconv.Itoa(sdkErr.StatusCode()))
242
243 assert.Equal(t, http.MethodGet, sdkErr.method)
244 assert.ErrorContains(t, err, sdkErr.method)
245
246 assert.Equal(t, exampleURL, sdkErr.url)
247 assert.ErrorContains(t, err, sdkErr.url)
248
249 assert.Empty(t, sdkErr.Helper)
250 },
251 },
252 {
253 name: "JSONWithoutRequest",
254 req: nil,
255 res: newResponse(http.StatusNotFound, jsonCT, marshal(simpleResponse)),
256 assert: func(t *testing.T, err error) {
257 sdkErr := assertSDKError(t, err)
258
259 assert.Equal(t, simpleResponse, sdkErr.Response)
260 assert.Equal(t, http.StatusNotFound, sdkErr.StatusCode())

Callers

nothing calls this directly

Calls 10

marshalFunction · 0.85
newResponseFunction · 0.85
assertSDKErrorFunction · 0.85
ReadBodyAsErrorFunction · 0.85
RunMethod · 0.65
EqualMethod · 0.45
StatusCodeMethod · 0.45
EmptyMethod · 0.45
ContainsMethod · 0.45
ErrorMethod · 0.45

Tested by

no test coverage detected