|
| 1 | +// SPDX-License-Identifier: MIT |
| 2 | +// SPDX-FileCopyrightText: © 2015 LabStack LLC and Echo contributors |
| 3 | + |
| 4 | +package echo |
| 5 | + |
| 6 | +import ( |
| 7 | + "errors" |
| 8 | + "fmt" |
| 9 | + "log/slog" |
| 10 | + "net/http" |
| 11 | + "net/http/httptest" |
| 12 | + "testing" |
| 13 | + |
| 14 | + "github.com/stretchr/testify/assert" |
| 15 | +) |
| 16 | + |
| 17 | +type customProblemErrorer struct { |
| 18 | + pe *ProblemError |
| 19 | +} |
| 20 | + |
| 21 | +func (ce *customProblemErrorer) Error() string { |
| 22 | + return "custom problem errorer" |
| 23 | +} |
| 24 | + |
| 25 | +func (ce *customProblemErrorer) ProblemError() *ProblemError { |
| 26 | + return ce.pe |
| 27 | +} |
| 28 | + |
| 29 | +func TestProblemDetailsHTTPErrorHandler(t *testing.T) { |
| 30 | + var testCases = []struct { |
| 31 | + whenError error |
| 32 | + name string |
| 33 | + whenMethod string |
| 34 | + expectBody string |
| 35 | + expectStatus int |
| 36 | + givenExposeError bool |
| 37 | + }{ |
| 38 | + { |
| 39 | + name: "ok, expose error = true, HTTPError, no wrapped err", |
| 40 | + givenExposeError: true, |
| 41 | + whenError: &HTTPError{Code: http.StatusTeapot, Message: "my_error"}, |
| 42 | + expectStatus: http.StatusTeapot, |
| 43 | + expectBody: `{"type":"about:blank","title":"I'm a teapot","status":418,"detail":"my_error"}` + "\n", |
| 44 | + }, |
| 45 | + { |
| 46 | + name: "ok, expose error = true, HTTPError + wrapped error", |
| 47 | + givenExposeError: true, |
| 48 | + whenError: HTTPError{Code: http.StatusTeapot, Message: "my_error"}.Wrap(errors.New("internal_error")), |
| 49 | + expectStatus: http.StatusTeapot, |
| 50 | + expectBody: `{"type":"about:blank","title":"I'm a teapot","status":418,"detail":"my_error: internal_error"}` + "\n", |
| 51 | + }, |
| 52 | + { |
| 53 | + name: "ok, expose error = true, HTTPError + wrapped HTTPError", |
| 54 | + givenExposeError: true, |
| 55 | + whenError: HTTPError{Code: http.StatusTeapot, Message: "my_error"}.Wrap(&HTTPError{Code: http.StatusTeapot, Message: "early_error"}), |
| 56 | + expectStatus: http.StatusTeapot, |
| 57 | + expectBody: `{"type":"about:blank","title":"I'm a teapot","status":418,"detail":"my_error: code=418, message=early_error"}` + "\n", |
| 58 | + }, |
| 59 | + { |
| 60 | + name: "ok, expose error = false, HTTPError + wrapped error", |
| 61 | + whenError: HTTPError{Code: http.StatusTeapot, Message: "my_error"}.Wrap(errors.New("internal_error")), |
| 62 | + expectStatus: http.StatusTeapot, |
| 63 | + expectBody: `{"type":"about:blank","title":"I'm a teapot","status":418,"detail":"my_error"}` + "\n", |
| 64 | + }, |
| 65 | + { |
| 66 | + name: "ok, expose error = false, HTTPError", |
| 67 | + whenError: &HTTPError{Code: http.StatusTeapot, Message: "my_error"}, |
| 68 | + expectStatus: http.StatusTeapot, |
| 69 | + expectBody: `{"type":"about:blank","title":"I'm a teapot","status":418,"detail":"my_error"}` + "\n", |
| 70 | + }, |
| 71 | + { |
| 72 | + name: "ok, expose error = true, HTTPError, no message, wrapped error", |
| 73 | + givenExposeError: true, |
| 74 | + whenError: HTTPError{Code: http.StatusTeapot, Message: ""}.Wrap(errors.New("internal_error")), |
| 75 | + expectStatus: http.StatusTeapot, |
| 76 | + expectBody: `{"type":"about:blank","title":"I'm a teapot","status":418,"detail":"internal_error"}` + "\n", |
| 77 | + }, |
| 78 | + { |
| 79 | + name: "ok, expose error = false, HTTPError, no message", |
| 80 | + whenError: &HTTPError{Code: http.StatusTeapot, Message: ""}, |
| 81 | + expectStatus: http.StatusTeapot, |
| 82 | + expectBody: `{"type":"about:blank","title":"I'm a teapot","status":418}` + "\n", |
| 83 | + }, |
| 84 | + { |
| 85 | + name: "ok, expose error = true, plain error", |
| 86 | + givenExposeError: true, |
| 87 | + whenError: fmt.Errorf("my errors wraps: %w", errors.New("internal_error")), |
| 88 | + expectStatus: http.StatusInternalServerError, |
| 89 | + expectBody: `{"type":"about:blank","title":"Internal Server Error","status":500,"detail":"my errors wraps: internal_error"}` + "\n", |
| 90 | + }, |
| 91 | + { |
| 92 | + name: "ok, expose error = false, plain error", |
| 93 | + whenError: fmt.Errorf("my errors wraps: %w", errors.New("internal_error")), |
| 94 | + expectStatus: http.StatusInternalServerError, |
| 95 | + expectBody: `{"type":"about:blank","title":"Internal Server Error","status":500}` + "\n", |
| 96 | + }, |
| 97 | + { |
| 98 | + name: "ok, http.HEAD, expose error = true, plain error", |
| 99 | + givenExposeError: true, |
| 100 | + whenMethod: http.MethodHead, |
| 101 | + whenError: fmt.Errorf("my errors wraps: %w", errors.New("internal_error")), |
| 102 | + expectStatus: http.StatusInternalServerError, |
| 103 | + expectBody: ``, |
| 104 | + }, |
| 105 | + { |
| 106 | + name: "ok, error is *ProblemError, used as-is", |
| 107 | + whenMethod: http.MethodGet, |
| 108 | + whenError: &ProblemError{Type: "https://example.com/probs/out-of-credit", Title: "You do not have enough credit.", Status: http.StatusForbidden, Detail: "Your current balance is 30, but that costs 50.", Instance: "/account/12345/msgs/abc"}, |
| 109 | + expectStatus: http.StatusForbidden, |
| 110 | + expectBody: `{"type":"https://example.com/probs/out-of-credit","title":"You do not have enough credit.","status":403,"detail":"Your current balance is 30, but that costs 50.","instance":"/account/12345/msgs/abc"}` + "\n", |
| 111 | + }, |
| 112 | + { |
| 113 | + name: "ok, error is *ProblemError with zero fields, defaults are filled", |
| 114 | + whenMethod: http.MethodGet, |
| 115 | + whenError: &ProblemError{}, |
| 116 | + expectStatus: http.StatusInternalServerError, |
| 117 | + expectBody: `{"type":"about:blank","title":"Internal Server Error","status":500}` + "\n", |
| 118 | + }, |
| 119 | + { |
| 120 | + name: "ok, custom error implements ProblemErrorer", |
| 121 | + whenMethod: http.MethodGet, |
| 122 | + whenError: &customProblemErrorer{pe: &ProblemError{Status: http.StatusConflict, Detail: "already exists"}}, |
| 123 | + expectStatus: http.StatusConflict, |
| 124 | + expectBody: `{"type":"about:blank","title":"Conflict","status":409,"detail":"already exists"}` + "\n", |
| 125 | + }, |
| 126 | + { |
| 127 | + name: "ok, custom error implements ProblemErrorer, returns nil", |
| 128 | + whenMethod: http.MethodGet, |
| 129 | + whenError: &customProblemErrorer{pe: nil}, |
| 130 | + expectStatus: http.StatusInternalServerError, |
| 131 | + expectBody: `{"type":"about:blank","title":"Internal Server Error","status":500}` + "\n", |
| 132 | + }, |
| 133 | + } |
| 134 | + |
| 135 | + for _, tc := range testCases { |
| 136 | + t.Run(tc.name, func(t *testing.T) { |
| 137 | + e := New() |
| 138 | + e.Logger = slog.New(slog.DiscardHandler) |
| 139 | + e.Any("/path", func(c *Context) error { |
| 140 | + return tc.whenError |
| 141 | + }) |
| 142 | + |
| 143 | + e.HTTPErrorHandler = ProblemDetailsHTTPErrorHandler(tc.givenExposeError) |
| 144 | + |
| 145 | + method := http.MethodGet |
| 146 | + if tc.whenMethod != "" { |
| 147 | + method = tc.whenMethod |
| 148 | + } |
| 149 | + req := httptest.NewRequest(method, "/path", nil) |
| 150 | + rec := httptest.NewRecorder() |
| 151 | + e.ServeHTTP(rec, req) |
| 152 | + |
| 153 | + assert.Equal(t, tc.expectStatus, rec.Code) |
| 154 | + assert.Equal(t, tc.expectBody, rec.Body.String()) |
| 155 | + assert.Equal(t, MIMEApplicationProblemJSON, rec.Header().Get(HeaderContentType)) |
| 156 | + }) |
| 157 | + } |
| 158 | +} |
| 159 | + |
| 160 | +func TestProblemDetailsHTTPErrorHandler_CommittedResponse(t *testing.T) { |
| 161 | + e := New() |
| 162 | + req := httptest.NewRequest(http.MethodGet, "/", nil) |
| 163 | + resp := httptest.NewRecorder() |
| 164 | + c := e.NewContext(req, resp) |
| 165 | + |
| 166 | + c.orgResponse.Committed = true |
| 167 | + errHandler := ProblemDetailsHTTPErrorHandler(false) |
| 168 | + |
| 169 | + errHandler(c, errors.New("my_error")) |
| 170 | + assert.Equal(t, http.StatusOK, resp.Code) |
| 171 | + assert.Equal(t, "", resp.Header().Get(HeaderContentType)) |
| 172 | +} |
| 173 | + |
| 174 | +func TestProblemError_Error(t *testing.T) { |
| 175 | + pe := &ProblemError{Status: http.StatusTeapot, Title: "I'm a teapot"} |
| 176 | + assert.Equal(t, "code=418, message=I'm a teapot", pe.Error()) |
| 177 | + |
| 178 | + pe.Detail = "brewing" |
| 179 | + assert.Equal(t, "code=418, message=I'm a teapot: brewing", pe.Error()) |
| 180 | +} |
| 181 | + |
| 182 | +func TestProblemError_StatusCode(t *testing.T) { |
| 183 | + pe := &ProblemError{Status: http.StatusTeapot} |
| 184 | + assert.Equal(t, http.StatusTeapot, pe.StatusCode()) |
| 185 | +} |
0 commit comments