From 5d30dc6e6df189fc404eab0326b305facacc38b4 Mon Sep 17 00:00:00 2001 From: Jeff Groom Date: Mon, 17 Dec 2018 11:38:24 -0700 Subject: [PATCH] - log on nunjucks error - update README - allow windows line endings --- README.md | 4 +-- package.json | 6 ++--- src/test.ts | 25 +++++++++++-------- src/yaml-parse.ts | 2 +- tests/failure/failureMaxRetries2.strest.yml | 13 ++++++++++ tests/failure/failureNunjucks.strest.yaml | 7 ++---- tests/failure/failureNunjucks2.strest.yaml | 11 ++++++++ tests/success/Methods/Fake/name.strest.yml | 2 +- .../other/windows_line_endings.strest.yml | 7 ++++++ tests/success/postman.strest.yml | 2 +- tests/success/validate/object.strest.yml | 4 +-- .../success/validate/object_array.strest.yml | 4 +-- tests/success/variables.strest.yml | 2 +- 13 files changed, 60 insertions(+), 29 deletions(-) create mode 100644 tests/failure/failureMaxRetries2.strest.yml create mode 100644 tests/failure/failureNunjucks2.strest.yaml create mode 100644 tests/success/other/windows_line_endings.strest.yml diff --git a/README.md b/README.md index ae8b140..b98aa0f 100644 --- a/README.md +++ b/README.md @@ -30,10 +30,10 @@ npm i -g @strest/cli ```bash # Via Docker # The image contains everything in the tests directory -docker run -it eykrehbein/strest:latest tests/success/chaining/ +docker run -it eykrehbein/strest:latest strest tests/success/chaining/ # Bring your own test and environment -docker run -it --env STREST_URL=https://jsonplaceholder.typicode.com -v ${PWD}:/app/data eykrehbein/strest:latest /data/tests/success/Env/ +docker run -it --env STREST_URL=https://jsonplaceholder.typicode.com -v ${PWD}:/app/data eykrehbein/strest:latest strest /data/tests/success/Env/ ``` We'll be using the [postman-echo](https://docs.postman-echo.com) test API in this tutorial. diff --git a/package.json b/package.json index 1532398..6116210 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@strest/cli", - "version": "2.1.1", + "version": "2.1.2", "description": "A new dimension of REST API testing", "main": "dist/main.js", "repository": "https://github.com/eykhagen/strest", @@ -51,13 +51,13 @@ ] }, "devDependencies": { - "@types/faker": "^4.1.3", - "@types/jsonpath": "^0.2.0", "@types/commander": "^2.12.2", + "@types/faker": "^4.1.3", "@types/jest": "^23.3.2", "@types/joi": "^13.4.4", "@types/js-yaml": "^3.11.2", "@types/jsonfile": "^4.0.1", + "@types/jsonpath": "^0.2.0", "@types/node": "^10.9.4", "@types/nunjucks": "^3.1.0", "@types/ora": "^1.3.4", diff --git a/src/test.ts b/src/test.ts index 8d88a3a..340b2e1 100644 --- a/src/test.ts +++ b/src/test.ts @@ -26,7 +26,8 @@ const nunjucksEnv = nunjucks.configure(".", { variableEnd: '$>', commentStart: '<#', commentEnd: '#>' - } + }, + throwOnUndefined: true }); nunjucksEnv.addGlobal('Faker', function (faked: string) { return faker.fake(`{{${faked}}}`); @@ -138,18 +139,19 @@ export const performTests = async (testObjects: object[], cmd: any) => { let nextIndex = keys.indexOf(requestName) +1; let nextRequest = keys[nextIndex]; let computed = computeRequestObject(requestReponsesObj, testObject.raw, requestName, nextRequest); - if (error !== null) { - // pass - } else { - if (typeof computed.if !== 'undefined') { - if (computed.if.operand == computed.if.equals) { - error = await performRequest(computed, requestName, printAll); + if (computed.error) { + error = { isError: true, message: computed.message, har: null, code: 0 } + } + if (error == null) { + if (typeof computed.parsed.if !== 'undefined') { + if (computed.parsed.if.operand == computed.parsed.if.equals) { + error = await performRequest(computed.parsed, requestName, printAll); } else { result = "skipped" error = { isError: false, message: null, har: null, code: 0 } } } else { - error = await performRequest(computed, requestName, printAll); + error = await performRequest(computed.parsed, requestName, printAll); } } @@ -263,9 +265,10 @@ export const computeRequestObject = (r: any, raw: string, requestName: string, n var newRaw = lines.slice(start, end - 1).join("\n") let converted = nunjucksEnv.renderString(newRaw, merged) const parsed: any = yaml.load(converted) - return parsed + return {parsed: parsed, error: null} } catch (e) { - throw e; + let err = validationError(`Failed to process ${requestName} request line using nunjucks:\n ${e}`); + return { parsed: null, error: true, message: err} } } @@ -440,7 +443,7 @@ const performRequest = async (requestObject: requestsObjectSchema, requestName: var ajv = new Ajv(); let validated = ajv.validate(validate.jsonschema, jsonPathValue); if (!validated) { - let err = validationError(`The jsonschema ${chalk.bold(validate.jsonschema.toString())} did not validate against ${chalk.bold(jsonPathValue)}`); + let err = validationError(`The jsonschema ${chalk.bold(JSON.stringify(validate.jsonschema))} did not validate against ${chalk.bold(JSON.stringify(jsonPathValue))}`); return { isError: true, har: har, message: err, code: 1, curl: response.request.toCurl() } } else { message = message + "jsonpath " + validate.jsonpath + "(" + jsonPathValue + ")" + " jsonschema validated on " + validate.jsonschema + "\n" diff --git a/src/yaml-parse.ts b/src/yaml-parse.ts index 69fad6f..c55df6b 100644 --- a/src/yaml-parse.ts +++ b/src/yaml-parse.ts @@ -22,7 +22,7 @@ export const parseTestingFiles = (pathArray: string[], dir: string) => { if(dir === null) { dir = ''; } - parsed.raw = data + parsed.raw = data.replace(/(\rn|\n|\r)/g, '\n') parsed.relativePath = removeFilename.replace(path.join(process.cwd(), dir), '.' + path.sep); responseData.push(parsed); } diff --git a/tests/failure/failureMaxRetries2.strest.yml b/tests/failure/failureMaxRetries2.strest.yml new file mode 100644 index 0000000..52219a9 --- /dev/null +++ b/tests/failure/failureMaxRetries2.strest.yml @@ -0,0 +1,13 @@ +version: 2 +requests: + maxRetries: + request: + url: https://postman-echo.com/time/now + method: GET + delay: 900 + maxRetries: 3 + validate: + - jsonpath: status + expect: 200 + - jsonpath: content + expect: <$ failed.path $> diff --git a/tests/failure/failureNunjucks.strest.yaml b/tests/failure/failureNunjucks.strest.yaml index 767e98f..93a08b7 100644 --- a/tests/failure/failureNunjucks.strest.yaml +++ b/tests/failure/failureNunjucks.strest.yaml @@ -5,10 +5,7 @@ variables: to_log: true requests: - invalidValidation: + nunjucksFailure: request: - url: <$ failUrl $>/todos/1 + url: <$ failUrl.0.foo $>/todos/1 method: GET - validate: - - jsonpath: content.id - expect: 100 diff --git a/tests/failure/failureNunjucks2.strest.yaml b/tests/failure/failureNunjucks2.strest.yaml new file mode 100644 index 0000000..f264841 --- /dev/null +++ b/tests/failure/failureNunjucks2.strest.yaml @@ -0,0 +1,11 @@ +version: 2 + +variables: + testUrl: https://jsonplaceholder.typicode.com/todos/1 + to_log: true + +requests: + nunjucksFailure2: + request: + url: <$ failUrl.foo $>/todos/1 + method: GET diff --git a/tests/success/Methods/Fake/name.strest.yml b/tests/success/Methods/Fake/name.strest.yml index ec5688f..fde6a95 100644 --- a/tests/success/Methods/Fake/name.strest.yml +++ b/tests/success/Methods/Fake/name.strest.yml @@ -10,4 +10,4 @@ requests: value: <$ Faker("name.firstName") $> - name: first_last value: <$ Faker("name.firstName") $> <$ Faker("name.lastName") $> - log: true + # log: true diff --git a/tests/success/other/windows_line_endings.strest.yml b/tests/success/other/windows_line_endings.strest.yml new file mode 100644 index 0000000..044dc69 --- /dev/null +++ b/tests/success/other/windows_line_endings.strest.yml @@ -0,0 +1,7 @@ +version: 2 + +requests: + arr: + request: + url: https://jsonplaceholder.typicode.com/todos + method: GET diff --git a/tests/success/postman.strest.yml b/tests/success/postman.strest.yml index 7539ebf..6a9879f 100644 --- a/tests/success/postman.strest.yml +++ b/tests/success/postman.strest.yml @@ -5,7 +5,7 @@ requests: # all test requests will be listed here request: url: https://postman-echo.com/get # required method: GET # required - log: true + # log: true validate: - jsonpath: status expect: 200 \ No newline at end of file diff --git a/tests/success/validate/object.strest.yml b/tests/success/validate/object.strest.yml index 76e6841..389cef2 100644 --- a/tests/success/validate/object.strest.yml +++ b/tests/success/validate/object.strest.yml @@ -9,7 +9,7 @@ requests: text: foo: bar baz: 1 - log: true + # log: true objectReset: request: url: https://postman-echo.com/post @@ -21,4 +21,4 @@ requests: validate: - jsonpath: content.data expect: {"new":{"foo":"bar","baz":1}} - log: true + # log: true diff --git a/tests/success/validate/object_array.strest.yml b/tests/success/validate/object_array.strest.yml index ca9eee5..23be25e 100644 --- a/tests/success/validate/object_array.strest.yml +++ b/tests/success/validate/object_array.strest.yml @@ -12,7 +12,7 @@ requests: baz: 1 - foo1: bar bax: 2 - log: true + # log: true object_arrayReset: request: url: https://postman-echo.com/post @@ -24,4 +24,4 @@ requests: validate: - jsonpath: content.data.new expect: {"foo": "bar","baz": 1} - log: true + # log: true diff --git a/tests/success/variables.strest.yml b/tests/success/variables.strest.yml index 3c55436..0d48710 100644 --- a/tests/success/variables.strest.yml +++ b/tests/success/variables.strest.yml @@ -9,4 +9,4 @@ requests: request: url: <$ testUrl $> method: GET - log: <$ to_log $> + # log: <$ to_log $>