-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from joanvila/response_schema_validation
Response schema validation
- Loading branch information
Showing
10 changed files
with
268 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
'use strict'; | ||
|
||
const _ = require('lodash/core'); | ||
|
||
const ResponseSchemaChecker = function() { | ||
|
||
const noResponseValidation = {valid: true, validationErrorReason: 'no body'}; | ||
|
||
this.check = (expectedResponses, response) => { | ||
if (_.isEmpty(expectedResponses)) return noResponseValidation; | ||
|
||
const expectedBody = getBodyFromCode(expectedResponses, response.statusCode); | ||
if (!expectedBody) return noResponseValidation; | ||
|
||
// If a body is defined we need to check it's schema | ||
const responseBody = JSON.parse(response.body); | ||
return checkSchema(expectedBody[0], responseBody); | ||
}; | ||
|
||
function getBodyFromCode(expectedResponses, responseCode) { | ||
let expectedBody = null; | ||
|
||
for (var i in expectedResponses) { | ||
const expectedCodeValue = parseInt(expectedResponses[i].code().value(), 10); | ||
if(expectedCodeValue === responseCode) { | ||
expectedBody = expectedResponses[i].body(); | ||
if (_.isEmpty(expectedBody)) expectedBody = null; | ||
break; | ||
} | ||
} | ||
|
||
return expectedBody; | ||
} | ||
|
||
function checkSchema(expectedBody, responseBody) { | ||
let validationResult = {}; | ||
|
||
if (expectedBody.type()[0] === typeof(responseBody)) { | ||
validationResult.valid = true; | ||
validationResult.validationErrorReason = ''; | ||
} else { | ||
validationResult.valid = false; | ||
validationResult.validationErrorReason = expectedBody.type()[0] + ' !== ' + typeof(responseBody); | ||
} | ||
|
||
return validationResult; | ||
} | ||
|
||
}; | ||
|
||
module.exports = ResponseSchemaChecker; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.