(t *testing.T)
| 15 | ) |
| 16 | |
| 17 | func TestHandleResponseErrorWithHTTPError(t *testing.T) { |
| 18 | examples := []struct { |
| 19 | HTTPError *HTTPError |
| 20 | APIVersion string |
| 21 | ExpectedBody string |
| 22 | }{ |
| 23 | { |
| 24 | HTTPError: apierrors.NewBadRequestError(apierrors.ErrorCodeBadJSON, "Unable to parse JSON"), |
| 25 | APIVersion: "", |
| 26 | ExpectedBody: "{\"code\":400,\"error_code\":\"" + apierrors.ErrorCodeBadJSON + "\",\"msg\":\"Unable to parse JSON\"}", |
| 27 | }, |
| 28 | { |
| 29 | HTTPError: apierrors.NewBadRequestError(apierrors.ErrorCodeBadJSON, "Unable to parse JSON"), |
| 30 | APIVersion: "2023-12-31", |
| 31 | ExpectedBody: "{\"code\":400,\"error_code\":\"" + apierrors.ErrorCodeBadJSON + "\",\"msg\":\"Unable to parse JSON\"}", |
| 32 | }, |
| 33 | { |
| 34 | HTTPError: apierrors.NewBadRequestError(apierrors.ErrorCodeBadJSON, "Unable to parse JSON"), |
| 35 | APIVersion: "2024-01-01", |
| 36 | ExpectedBody: "{\"code\":\"" + apierrors.ErrorCodeBadJSON + "\",\"message\":\"Unable to parse JSON\"}", |
| 37 | }, |
| 38 | { |
| 39 | HTTPError: &HTTPError{ |
| 40 | HTTPStatus: http.StatusBadRequest, |
| 41 | Message: "Uncoded failure", |
| 42 | }, |
| 43 | APIVersion: "2024-01-01", |
| 44 | ExpectedBody: "{\"code\":\"" + apierrors.ErrorCodeUnknown + "\",\"message\":\"Uncoded failure\"}", |
| 45 | }, |
| 46 | { |
| 47 | HTTPError: &HTTPError{ |
| 48 | HTTPStatus: http.StatusInternalServerError, |
| 49 | Message: "Unexpected failure", |
| 50 | }, |
| 51 | APIVersion: "2024-01-01", |
| 52 | ExpectedBody: "{\"code\":\"" + apierrors.ErrorCodeUnexpectedFailure + "\",\"message\":\"Unexpected failure\"}", |
| 53 | }, |
| 54 | } |
| 55 | |
| 56 | for _, example := range examples { |
| 57 | rec := httptest.NewRecorder() |
| 58 | req, err := http.NewRequest(http.MethodPost, "http://example.com", nil) |
| 59 | require.NoError(t, err) |
| 60 | |
| 61 | if example.APIVersion != "" { |
| 62 | req.Header.Set(APIVersionHeaderName, example.APIVersion) |
| 63 | } |
| 64 | |
| 65 | HandleResponseError(example.HTTPError, rec, req) |
| 66 | |
| 67 | require.Equal(t, example.HTTPError.HTTPStatus, rec.Code) |
| 68 | require.Equal(t, example.ExpectedBody, rec.Body.String()) |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | func TestRecoverer(t *testing.T) { |
| 73 | var logBuffer bytes.Buffer |
nothing calls this directly
no test coverage detected
searching dependent graphs…