Skip to content

Commit

Permalink
Merge 15c47eb into a6b65a4
Browse files Browse the repository at this point in the history
  • Loading branch information
artem-zakharchenko committed Aug 5, 2019
2 parents a6b65a4 + 15c47eb commit 0536780
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 27 deletions.
52 changes: 27 additions & 25 deletions lib/units/validateBody.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ function isJsonContentType(contentType) {
* on the given body and normalized headers.
* @param {string} body
* @param {string} contentType
* @param {'real'|'expected'} httpMessageOrigin
* @param {'actual'|'expected'} httpMessageOrigin
* @returns {[string, MediaType]}
*/
function getBodyType(body, contentType, httpMessageOrigin) {
Expand Down Expand Up @@ -116,14 +116,14 @@ ${error.message}\

/**
* Returns a body validator class based on the given
* real and expected body media types.
* @param {MediaType} realType
* actual and expected body media types.
* @param {MediaType} actualType
* @param {MediaType} expectedType
* @returns {Validator}
*/
function getBodyValidator(realType, expectedType) {
const both = (predicate) => (real, expected) => {
return [real, expected].every(predicate);
function getBodyValidator(actualType, expectedType) {
const both = (predicate) => (actual, expected) => {
return [actual, expected].every(predicate);
};

const validators = [
Expand All @@ -132,21 +132,21 @@ function getBodyValidator(realType, expectedType) {
// would resolve on "application/schema+json" media type too.
[
JsonSchema,
(real, expected) => {
return isJson(real) && isJsonSchema(expected);
(actual, expected) => {
return isJson(actual) && isJsonSchema(expected);
},
'json'
],
[JsonExample, both(isJson), 'json']
];

const validator = validators.find(([_name, predicate]) => {
return predicate(realType, expectedType);
return predicate(actualType, expectedType);
});

if (!validator) {
const error = `Can't validate actual media type '${mediaTyper.format(
realType
actualType
)}' against the expected media type '${mediaTyper.format(expectedType)}'.`;
return [error, null, null];
}
Expand All @@ -157,7 +157,7 @@ function getBodyValidator(realType, expectedType) {
/**
* Validates given bodies of transaction elements.
* @param {Object<string, any>} expected
* @param {Object<string, any>} real
* @param {Object<string, any>} actual
*/
function validateBody(expected, actual) {
const values = {
Expand All @@ -171,20 +171,20 @@ function validateBody(expected, actual) {
}

const errors = [];
const realBodyType = typeof actual.body;
const hasEmptyRealBody = actual.body === '';
const actualBodyType = typeof actual.body;
const hasEmptyActualBody = actual.body === '';

// Throw when user input for real body is not a string.
if (realBodyType !== 'string') {
// Throw when user input for actual body is not a string.
if (actualBodyType !== 'string') {
throw new Error(
`Expected HTTP body to be a string, but got: ${realBodyType}`
`Expected HTTP body to be a string, but got: ${actualBodyType}`
);
}

const [realTypeError, realType] = getBodyType(
const [actualTypeError, actualType] = getBodyType(
actual.body,
actual.headers && actual.headers['content-type'],
'real'
'actual'
);

const [expectedTypeError, expectedType] = expected.bodySchema
Expand All @@ -195,9 +195,9 @@ function validateBody(expected, actual) {
'expected'
);

if (realTypeError) {
if (actualTypeError) {
errors.push({
message: realTypeError
message: actualTypeError
});
}

Expand All @@ -210,18 +210,18 @@ function validateBody(expected, actual) {
const hasErrors = errors.length > 0;

// Skipping body validation in case errors during
// real/expected body type definition.
// actual/expected body type definition.
const [validatorError, ValidatorClass, kind] = hasErrors
? [null, null, null]
: getBodyValidator(realType, expectedType);
: getBodyValidator(actualType, expectedType);

if (validatorError) {
// In case determined media types mismtach, check if the real is not missing.
// In case determined media types mismtach, check if the actual is not missing.
// Keep in mind the following scenarios:
// 1. Expected '', and got '' (TextDiff/TextDiff, valid)
// 2. Expected {...}, but got '' (Json/TextDiff, invalid, produces "missing real body" error)
// 2. Expected {...}, but got '' (Json/TextDiff, invalid, produces "missing actual body" error)
// 3. Expected {...}, but got "foo" (Json/TextDiff, invalid, produces types mismatch error).
if (expected.body !== '' && hasEmptyRealBody) {
if (expected.body !== '' && hasEmptyActualBody) {
errors.push({
message: `Expected "body" of "${mediaTyper.format(
expectedType
Expand Down Expand Up @@ -253,6 +253,8 @@ function validateBody(expected, actual) {
return {
valid: isValidField({ errors }),
kind,
actualType,
expectedType,
values,
errors
};
Expand Down
4 changes: 2 additions & 2 deletions test/unit/units/validateBody.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ describe('validateBody', () => {
expect(result)
.to.have.errorAtIndex(0)
.withMessage(
/^Can't validate: real body 'Content-Type' header is 'application\/json' but body is not a parseable JSON:/
/^Can't validate: actual body 'Content-Type' header is 'application\/json' but body is not a parseable JSON:/
);
});
});
Expand Down Expand Up @@ -172,7 +172,7 @@ describe('validateBody', () => {
expect(result)
.to.have.errorAtIndex(0)
.withMessage(
/^Can't validate: real body 'Content-Type' header is 'application\/hal\+json' but body is not a parseable JSON:/
/^Can't validate: actual body 'Content-Type' header is 'application\/hal\+json' but body is not a parseable JSON:/
);
});
});
Expand Down

0 comments on commit 0536780

Please sign in to comment.