From 23bf277e83a4ac024450b4717857174f65e19ee0 Mon Sep 17 00:00:00 2001 From: Shahar Shelly Date: Sat, 19 Jan 2019 19:42:21 +0200 Subject: [PATCH 1/2] added support for filname as paramter --- README.md | 20 ++++++++++++++++++++ src/configSchema.ts | 1 + src/test.ts | 10 ++++++---- src/yaml-parse.ts | 2 ++ 4 files changed, 29 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 613cc79..2606f8f 100644 --- a/README.md +++ b/README.md @@ -332,6 +332,26 @@ requests: method: GET ``` +## Use strest file name as paramter in the tests +You can use the strest file name as a parmater in the tests . + +*note* that the strest suffix is removed + +**Usage** +The file name for this example is postman-echo.strest.yml + +```yml +version: 2 +requests: + test-file-name: + request: + url: https://<$ Filename() $>.com/get + method: GET + validate: + - jsonpath: status + expect: 200 +``` + ## Response Validation The immediate response is stored in [HAR Format](http://www.softwareishard.com/blog/har-12-spec/#response) diff --git a/src/configSchema.ts b/src/configSchema.ts index 5400818..a004c59 100644 --- a/src/configSchema.ts +++ b/src/configSchema.ts @@ -107,6 +107,7 @@ export const Schema = Joi.object({ // Created dynamically raw: Joi.string().required(), relativePath: Joi.string().required(), + fileName: Joi.string().required(), }); export const BulkSchema = Joi.array().items(Joi.string()); diff --git a/src/test.ts b/src/test.ts index 5143a55..3d8dfb2 100644 --- a/src/test.ts +++ b/src/test.ts @@ -137,7 +137,7 @@ export const performTests = async (testObjects: object[], cmd: any) => { let keys = Object.keys(requests); let nextIndex = keys.indexOf(requestName) +1; let nextRequest = keys[nextIndex]; - let computed = computeRequestObject(requestReponsesObj, testObject.raw, requestName, nextRequest); + let computed = computeRequestObject(requestReponsesObj, testObject.raw, requestName, nextRequest, testObject.fileName); if (computed.error) { error = { isError: true, message: computed.message, har: null, code: 0 } } @@ -244,12 +244,14 @@ export const performTests = async (testObjects: object[], cmd: any) => { * Use nunjucks to replace and update the object * @param obj working obj */ -export const computeRequestObject = (r: any, raw: string, requestName: string, nextRequest: string) => { - - let merged = { ...r, ...definedVariables }; +export const computeRequestObject = (r: any, raw: string, requestName: string, nextRequest: string, fileName: string) => { + let merged = { ...r, ...definedVariables}; nunjucksEnv.addGlobal('JsonPath', function (path: string) { return jp.value(merged, path) }) + nunjucksEnv.addGlobal('Filename', function () { + return fileName ? fileName.replace('.strest','') : ''; + }) // Parse obj using nunjucks try { const regexpStart = new RegExp("^\\s{1,6}" + requestName + ":","gm") diff --git a/src/yaml-parse.ts b/src/yaml-parse.ts index c55df6b..3ee1507 100644 --- a/src/yaml-parse.ts +++ b/src/yaml-parse.ts @@ -18,11 +18,13 @@ export const parseTestingFiles = (pathArray: string[], dir: string) => { if(typeof filePath === 'string') { const data = fs.readFileSync(filePath.toString(), 'utf8'); const parsed: any = yaml.safeLoad(data); + const { name:fileName } = path.parse(filePath); const removeFilename = path.dirname(filePath) + path.sep; if(dir === null) { dir = ''; } parsed.raw = data.replace(/(\rn|\n|\r)/g, '\n') + parsed.fileName = fileName; parsed.relativePath = removeFilename.replace(path.join(process.cwd(), dir), '.' + path.sep); responseData.push(parsed); } From 8f95e1773842093536907f1a1547d8ef7714a01b Mon Sep 17 00:00:00 2001 From: Shahar Shelly Date: Sat, 19 Jan 2019 19:42:52 +0200 Subject: [PATCH 2/2] added test files --- tests/success/file/postman-echo.com.strest.yml | 10 ++++++++++ tests/success/file/postman-echo.strest.yml | 10 ++++++++++ 2 files changed, 20 insertions(+) create mode 100644 tests/success/file/postman-echo.com.strest.yml create mode 100644 tests/success/file/postman-echo.strest.yml diff --git a/tests/success/file/postman-echo.com.strest.yml b/tests/success/file/postman-echo.com.strest.yml new file mode 100644 index 0000000..36bd22b --- /dev/null +++ b/tests/success/file/postman-echo.com.strest.yml @@ -0,0 +1,10 @@ +version: 2 # only version at the moment +requests: # all test requests will be listed here + test-file-name: # name the request however you want + request: + url: https://<$ Filename() $>/get # required + method: GET # required + # log: true + validate: + - jsonpath: status + expect: 200 \ No newline at end of file diff --git a/tests/success/file/postman-echo.strest.yml b/tests/success/file/postman-echo.strest.yml new file mode 100644 index 0000000..f3d78fc --- /dev/null +++ b/tests/success/file/postman-echo.strest.yml @@ -0,0 +1,10 @@ +version: 2 # only version at the moment +requests: # all test requests will be listed here + test-file-name-1: # name the request however you want + request: + url: https://<$ Filename() $>.com/get # required + method: GET # required + # log: true + validate: + - jsonpath: status + expect: 200 \ No newline at end of file