Skip to content
This repository was archived by the owner on Nov 8, 2024. It is now read-only.

Commit ed81843

Browse files
fix: returns validation error on unknown combination of real/expected body content types
1 parent 9b42238 commit ed81843

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

lib/next/test/unit/units/validateBody.test.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,49 @@ describe('validateBody', () => {
1919
});
2020

2121
describe('when given supported body type', () => {
22+
describe('in a combination without related validator', () => {
23+
const result = validateBody(
24+
{
25+
headers: { 'content-type': 'application/json' },
26+
body: '{ "foo": "bar" }'
27+
},
28+
{
29+
headers: { 'content-type': 'text/plain' },
30+
body: ''
31+
}
32+
);
33+
34+
it('has no validator', () => {
35+
assert.isNull(result.validator);
36+
});
37+
38+
it('has "application/json" real type', () => {
39+
assert.propertyVal(result, 'realType', 'application/json');
40+
});
41+
42+
it('has "text/plain" expected type', () => {
43+
assert.propertyVal(result, 'expectedType', 'text/plain');
44+
});
45+
46+
describe('produces validation error', () => {
47+
it('exactly one error', () => {
48+
assert.lengthOf(result.results, 1);
49+
});
50+
51+
it('has "error" severity', () => {
52+
assert.propertyVal(result.results[0], 'severity', 'error');
53+
});
54+
55+
it('has explanatory message', () => {
56+
assert.propertyVal(
57+
result.results[0],
58+
'message',
59+
`Can't validate real media type 'application/json' against expected media type 'text/plain'.`
60+
);
61+
});
62+
});
63+
});
64+
2265
describe('with explicit "Content-Type" header', () => {
2366
describe('application/json', () => {
2467
describe('with matching body type', () => {

lib/next/units/validateBody.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,13 @@ function getBodyValidator(realType, expectedType) {
141141
return predicate(realType, expectedType);
142142
});
143143

144+
if (!validator) {
145+
const error = `Can't validate real media type '${mediaTyper.format(
146+
realType
147+
)}' against expected media type '${mediaTyper.format(expectedType)}'.`;
148+
return [error, null];
149+
}
150+
144151
return [null, validator[0]];
145152
}
146153

0 commit comments

Comments
 (0)