Skip to content
This repository has been archived by the owner on Mar 26, 2021. It is now read-only.

Commit

Permalink
Allowing 0 and null in jsonpath expectations.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ivan Antolic-Soban committed Jan 11, 2019
1 parent 0974426 commit 5b80cac
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/configSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ const requestSchema = Joi.object().keys({

const validateSchema = Joi.object().keys({
jsonpath: Joi.string().required(),
expect: Joi.alternatives().try(Joi.boolean(), Joi.number(), Joi.object(), Joi.string()).optional(),
expect: Joi.alternatives().try(Joi.boolean(), Joi.number(), Joi.object(), Joi.string(), null).optional(),
type: Joi.array().items(Joi.string()).optional(),
jsonschema: Joi.any().optional(),
regex: Joi.string().optional()
Expand Down
12 changes: 6 additions & 6 deletions src/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,15 @@ nunjucksEnv.addGlobal('Env', function (envi: string) {
*/
const requestReponses: Map<string, object> = new Map<string, object>()

// The manually defined variables
// The manually defined variables
// Usable through <% variableName %>
let definedVariables: any = {

}

/**
* Main handler that will perform the tests with each valid test object
* @param testObjects
* @param testObjects
* @param printAll If true, all response information will be logged in the console
*/
export const performTests = async (testObjects: object[], cmd: any) => {
Expand Down Expand Up @@ -285,8 +285,8 @@ const validationError = (message: string) => {

/**
* Checks whether a type matches the dataToProof
* @param type
* @param dataToProof
* @param type
* @param dataToProof
*/
export const validateType = (type: Array<string>, dataToProof: any): boolean => {
function isType(elem: string) {
Expand Down Expand Up @@ -355,7 +355,7 @@ const performRequest = async (requestObject: requestsObjectSchema, requestName:
}

let axiosObject: AxiosObject = {};
// optional keys
// optional keys
axiosObject.url = requestObject.request.url;
axiosObject.method = requestObject.request.method;
axiosObject.headers = {}
Expand Down Expand Up @@ -414,7 +414,7 @@ const performRequest = async (requestObject: requestsObjectSchema, requestName:
if ('validate' in requestObject) {
for (let validate of requestObject.validate) {
let jsonPathValue = jp.value(har, validate.jsonpath)
if (!jsonPathValue) {
if (typeof jsonPathValue === 'undefined') {
let err = validationError(`The jsonpath ${chalk.bold(validate.jsonpath)} resolved to ${chalk.bold(typeof jsonPathValue)}`);
return { isError: true, har: har, message: err, code: 1, curl: response.request.toCurl() }
}
Expand Down
9 changes: 9 additions & 0 deletions tests/success/validate/jsonpath.strest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ requests:
firstName: John
lastName: doe
age: 26
active: false
count: 0
link: null
address:
streetAddress: 'naist street'
city: Nara
Expand All @@ -21,6 +24,12 @@ requests:
validate:
- jsonpath: $.content.phoneNumbers[:1].type
expect: iPhone
- jsonpath: content.active
expect: false
- jsonpath: content.count
expect: 0
- jsonpath: content.link
expect: null
- jsonpath: content.age
expect: 26
- jsonpath: $.content.age
Expand Down

0 comments on commit 5b80cac

Please sign in to comment.