(t *testing.T)
| 194 | } |
| 195 | |
| 196 | func TestResponseEncoder_Encode_ErrorResponse(t *testing.T) { |
| 197 | var ( |
| 198 | serviceError = goa.NewServiceError(errors.New("foo"), "foo", false, false, false) |
| 199 | defaultXMLName = ErrorResponseXMLName |
| 200 | backwardCompatibleXMLName = xml.Name{Local: "ErrorResponse"} // Compatible with v3.13.2 and earlier. |
| 201 | ) |
| 202 | |
| 203 | cases := []struct { |
| 204 | acceptType string |
| 205 | xmlName xml.Name |
| 206 | encoded string |
| 207 | }{ |
| 208 | {"application/json", defaultXMLName, fmt.Sprintf(`{"name":"foo","id":"%s","message":"foo","temporary":false,"timeout":false,"fault":false}`, serviceError.ID)}, |
| 209 | {"application/json", backwardCompatibleXMLName, fmt.Sprintf(`{"name":"foo","id":"%s","message":"foo","temporary":false,"timeout":false,"fault":false}`, serviceError.ID)}, |
| 210 | {"application/xml", defaultXMLName, fmt.Sprintf(`<error><name>foo</name><id>%s</id><message>foo</message><temporary>false</temporary><timeout>false</timeout><fault>false</fault></error>`, serviceError.ID)}, |
| 211 | {"application/xml", backwardCompatibleXMLName, fmt.Sprintf(`<ErrorResponse><name>foo</name><id>%s</id><message>foo</message><temporary>false</temporary><timeout>false</timeout><fault>false</fault></ErrorResponse>`, serviceError.ID)}, |
| 212 | } |
| 213 | |
| 214 | for _, c := range cases { |
| 215 | name := c.acceptType |
| 216 | if c.xmlName.Local != "" { |
| 217 | name += "/" + c.xmlName.Local |
| 218 | } |
| 219 | t.Run(name, func(t *testing.T) { |
| 220 | ctx := context.Background() |
| 221 | ctx = context.WithValue(ctx, AcceptTypeKey, c.acceptType) |
| 222 | w := httptest.NewRecorder() |
| 223 | ErrorResponseXMLName = c.xmlName |
| 224 | encoder := ResponseEncoder(ctx, w) |
| 225 | |
| 226 | err := encoder.Encode(NewErrorResponse(ctx, serviceError)) |
| 227 | |
| 228 | assert.NoError(t, err) |
| 229 | body := strings.TrimSpace(w.Body.String()) |
| 230 | assert.Equal(t, c.encoded, body) |
| 231 | }) |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | func TestResponseDecoder(t *testing.T) { |
| 236 | cases := []struct { |
nothing calls this directly
no test coverage detected