Skip to content

Commit

Permalink
fix: returns validation error on unknown combination of real/expected…
Browse files Browse the repository at this point in the history
… body content types
  • Loading branch information
artem-zakharchenko committed May 21, 2019
1 parent 9b42238 commit ed81843
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
43 changes: 43 additions & 0 deletions lib/next/test/unit/units/validateBody.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,49 @@ describe('validateBody', () => {
});

describe('when given supported body type', () => {
describe('in a combination without related validator', () => {
const result = validateBody(
{
headers: { 'content-type': 'application/json' },
body: '{ "foo": "bar" }'
},
{
headers: { 'content-type': 'text/plain' },
body: ''
}
);

it('has no validator', () => {
assert.isNull(result.validator);
});

it('has "application/json" real type', () => {
assert.propertyVal(result, 'realType', 'application/json');
});

it('has "text/plain" expected type', () => {
assert.propertyVal(result, 'expectedType', 'text/plain');
});

describe('produces validation error', () => {
it('exactly one error', () => {
assert.lengthOf(result.results, 1);
});

it('has "error" severity', () => {
assert.propertyVal(result.results[0], 'severity', 'error');
});

it('has explanatory message', () => {
assert.propertyVal(
result.results[0],
'message',
`Can't validate real media type 'application/json' against expected media type 'text/plain'.`
);
});
});
});

describe('with explicit "Content-Type" header', () => {
describe('application/json', () => {
describe('with matching body type', () => {
Expand Down
7 changes: 7 additions & 0 deletions lib/next/units/validateBody.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,13 @@ function getBodyValidator(realType, expectedType) {
return predicate(realType, expectedType);
});

if (!validator) {
const error = `Can't validate real media type '${mediaTyper.format(
realType
)}' against expected media type '${mediaTyper.format(expectedType)}'.`;
return [error, null];
}

return [null, validator[0]];
}

Expand Down

0 comments on commit ed81843

Please sign in to comment.