From 5b80cac4fe78cc40be4c760ecdc5bc717c4698e0 Mon Sep 17 00:00:00 2001 From: Ivan Antolic-Soban Date: Fri, 11 Jan 2019 10:55:39 -0500 Subject: [PATCH] Allowing 0 and null in jsonpath expectations. --- src/configSchema.ts | 2 +- src/test.ts | 12 ++++++------ tests/success/validate/jsonpath.strest.yml | 9 +++++++++ 3 files changed, 16 insertions(+), 7 deletions(-) diff --git a/src/configSchema.ts b/src/configSchema.ts index 0c4272d..5400818 100644 --- a/src/configSchema.ts +++ b/src/configSchema.ts @@ -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() diff --git a/src/test.ts b/src/test.ts index 6b8ba75..5143a55 100644 --- a/src/test.ts +++ b/src/test.ts @@ -43,7 +43,7 @@ nunjucksEnv.addGlobal('Env', function (envi: string) { */ const requestReponses: Map = new Map() -// The manually defined variables +// The manually defined variables // Usable through <% variableName %> let definedVariables: any = { @@ -51,7 +51,7 @@ 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) => { @@ -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, dataToProof: any): boolean => { function isType(elem: string) { @@ -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 = {} @@ -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() } } diff --git a/tests/success/validate/jsonpath.strest.yml b/tests/success/validate/jsonpath.strest.yml index aa41eb6..c1392cf 100644 --- a/tests/success/validate/jsonpath.strest.yml +++ b/tests/success/validate/jsonpath.strest.yml @@ -11,6 +11,9 @@ requests: firstName: John lastName: doe age: 26 + active: false + count: 0 + link: null address: streetAddress: 'naist street' city: Nara @@ -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