| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
package binding |
| 17 |
|
| 18 |
import ( |
| 19 |
"io" |
| 20 |
"net/http" |
| 21 |
"net/http/httptest" |
| 22 |
"testing" |
| 23 |
|
| 24 |
"github.com/stretchr/testify/assert" |
| 25 |
"github.com/stretchr/testify/require" |
| 26 |
) |
| 27 |
|
| 28 |
var errorTestCases = []errorTestCase{ |
| 29 |
{ |
| 30 |
description: "No errors", |
| 31 |
errors: Errors{}, |
| 32 |
expected: errorTestResult{ |
| 33 |
statusCode: http.StatusOK, |
| 34 |
}, |
| 35 |
}, |
| 36 |
{ |
| 37 |
description: "Deserialization error", |
| 38 |
errors: Errors{ |
| 39 |
{ |
| 40 |
Classification: ERR_DESERIALIZATION, |
| 41 |
Message: "Some parser error here", |
| 42 |
}, |
| 43 |
}, |
| 44 |
expected: errorTestResult{ |
| 45 |
statusCode: http.StatusBadRequest, |
| 46 |
contentType: jsonContentType, |
| 47 |
body: `[{"classification":"DeserializationError","message":"Some parser error here"}]`, |
| 48 |
}, |
| 49 |
}, |
| 50 |
{ |
| 51 |
description: "Content-Type error", |
| 52 |
errors: Errors{ |
| 53 |
{ |
| 54 |
Classification: ERR_CONTENT_TYPE, |
| 55 |
Message: "Empty Content-Type", |
| 56 |
}, |
| 57 |
}, |
| 58 |
expected: errorTestResult{ |
| 59 |
statusCode: http.StatusUnsupportedMediaType, |
| 60 |
contentType: jsonContentType, |
| 61 |
body: `[{"classification":"ContentTypeError","message":"Empty Content-Type"}]`, |
| 62 |
}, |
| 63 |
}, |
| 64 |
{ |
| 65 |
description: "Requirement error", |
| 66 |
errors: Errors{ |
| 67 |
{ |
| 68 |
FieldNames: []string{"some_field"}, |
| 69 |
Classification: ERR_REQUIRED, |
| 70 |
Message: "Required", |
| 71 |
}, |
| 72 |
}, |
| 73 |
expected: errorTestResult{ |
| 74 |
statusCode: http.StatusUnprocessableEntity, |
| 75 |
contentType: jsonContentType, |
| 76 |
body: `[{"fieldNames":["some_field"],"classification":"RequiredError","message":"Required"}]`, |
| 77 |
}, |
| 78 |
}, |
| 79 |
{ |
| 80 |
description: "Bad header error", |
| 81 |
errors: Errors{ |
| 82 |
{ |
| 83 |
Classification: "HeaderError", |
| 84 |
Message: "The X-Something header must be specified", |
| 85 |
}, |
| 86 |
}, |
| 87 |
expected: errorTestResult{ |
| 88 |
statusCode: http.StatusUnprocessableEntity, |
| 89 |
contentType: jsonContentType, |
| 90 |
body: `[{"classification":"HeaderError","message":"The X-Something header must be specified"}]`, |
| 91 |
}, |
| 92 |
}, |
| 93 |
{ |
| 94 |
description: "Custom field error", |
| 95 |
errors: Errors{ |
| 96 |
{ |
| 97 |
FieldNames: []string{"month", "year"}, |
| 98 |
Classification: "DateError", |
| 99 |
Message: "The month and year must be in the future", |
| 100 |
}, |
| 101 |
}, |
| 102 |
expected: errorTestResult{ |
| 103 |
statusCode: http.StatusUnprocessableEntity, |
| 104 |
contentType: jsonContentType, |
| 105 |
body: `[{"fieldNames":["month","year"],"classification":"DateError","message":"The month and year must be in the future"}]`, |
| 106 |
}, |
| 107 |
}, |
| 108 |
{ |
| 109 |
description: "Multiple errors", |
| 110 |
errors: Errors{ |
| 111 |
{ |
| 112 |
FieldNames: []string{"foo"}, |
| 113 |
Classification: ERR_REQUIRED, |
| 114 |
Message: "Required", |
| 115 |
}, |
| 116 |
{ |
| 117 |
FieldNames: []string{"foo"}, |
| 118 |
Classification: "LengthError", |
| 119 |
Message: "The length of the 'foo' field is too short", |
| 120 |
}, |
| 121 |
}, |
| 122 |
expected: errorTestResult{ |
| 123 |
statusCode: http.StatusUnprocessableEntity, |
| 124 |
contentType: jsonContentType, |
| 125 |
body: `[{"fieldNames":["foo"],"classification":"RequiredError","message":"Required"},{"fieldNames":["foo"],"classification":"LengthError","message":"The length of the 'foo' field is too short"}]`, |
| 126 |
}, |
| 127 |
}, |
| 128 |
} |
| 129 |
|
| 130 |
func Test_ErrorHandler(t *testing.T) { |
| 131 |
for _, testCase := range errorTestCases { |
| 132 |
performErrorTest(t, testCase) |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
func performErrorTest(t *testing.T, testCase errorTestCase) { |
| 137 |
resp := httptest.NewRecorder() |
| 138 |
|
| 139 |
errorHandler(testCase.errors, resp) |
| 140 |
|
| 141 |
assert.Equal(t, testCase.expected.statusCode, resp.Code) |
| 142 |
assert.Equal(t, testCase.expected.contentType, resp.Header().Get("Content-Type")) |
| 143 |
|
| 144 |
actualBody, err := io.ReadAll(resp.Body) |
| 145 |
require.NoError(t, err) |
| 146 |
assert.Equal(t, testCase.expected.body, string(actualBody)) |
| 147 |
} |
| 148 |
|
| 149 |
type ( |
| 150 |
errorTestCase struct { |
| 151 |
description string |
| 152 |
errors Errors |
| 153 |
expected errorTestResult |
| 154 |
} |
| 155 |
|
| 156 |
errorTestResult struct { |
| 157 |
statusCode int |
| 158 |
contentType string |
| 159 |
body string |
| 160 |
} |
| 161 |
) |