diff --git a/README.md b/README.md index 98d32f0..c312ed3 100644 --- a/README.md +++ b/README.md @@ -330,8 +330,8 @@ 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 . +## Use strest file name as parameter in the tests +You can use the strest file name as a parameter in the tests . *note* that the strest suffix is removed @@ -350,6 +350,35 @@ requests: expect: 200 ``` +## Using dates and dates format +You can insert dates times plus format them using the custom [nunjucks date filter](https://www.npmjs.com/package/nunjucks-date) +under the hood its a wrapper for [momentjs](https://momentjs.com/) so all its [formatting](https://momentjs.com/docs/#/displaying/) is supported + +**Usage** +You can use the date filter inside a nunjuck brackets in the request and inside the validate parts. + +```yml +requests: + moment-in-request: + request: + url: https://postman-echo.com/get + method: GET + queryString: + - name: foo + value: <$ now | date('YYYY') $> + validate: + - jsonpath: content.args.foo + expect: "<$ '2019-10-10' | date('YYYY') $>" + moment-in-validate: + request: + url: https://postman-echo.com/time/format?timestamp=2019-10-10&format=YYYY + method: GET + validate: + - jsonpath: content.format + expect: "<$ '2019-10-10' | date('YYYY') $>" +``` + + ## Response Validation The immediate response is stored in [HAR Format](http://www.softwareishard.com/blog/har-12-spec/#response) diff --git a/package.json b/package.json index e85503b..d4ae094 100644 --- a/package.json +++ b/package.json @@ -31,6 +31,7 @@ "line-number": "0.1.0", "mkdirp": "^0.5.1", "nunjucks": "^3.1.3", + "nunjucks-date": "^1.2.0", "ora": "^3.0.0", "recursive-readdir": "^2.2.2", "request-to-curl": "^0.1.1", @@ -60,6 +61,7 @@ "@types/jsonpath": "^0.2.0", "@types/node": "^10.9.4", "@types/nunjucks": "^3.1.0", + "@types/nunjucks-date": "0.0.6", "@types/ora": "^1.3.4", "@types/recursive-readdir": "^2.2.0", "jest": "^23.6.0", diff --git a/src/test.ts b/src/test.ts index 81d4515..cd87619 100644 --- a/src/test.ts +++ b/src/test.ts @@ -12,12 +12,15 @@ import * as yaml from 'js-yaml'; import * as jsonfile from 'jsonfile' import * as path from 'path'; import * as Ajv from 'ajv'; +import * as nunjucksDate from 'nunjucks-date'; + var deepEql = require("deep-eql"); var lineNumber = require('line-number'); var getLineFromPos = require('get-line-from-pos'); require('request-to-curl'); +nunjucksDate.setDefaultFormat('MMMM Do YYYY, h:mm:ss a'); const nunjucksEnv = nunjucks.configure(".", { tags: { blockStart: '<%', @@ -29,6 +32,8 @@ const nunjucksEnv = nunjucks.configure(".", { }, throwOnUndefined: true }); + +nunjucksDate.install(nunjucksEnv); nunjucksEnv.addGlobal('Faker', function (faked: string) { return faker.fake(`{{${faked}}}`); }) diff --git a/tests/success/Value/moment.strest.yml b/tests/success/Value/moment.strest.yml new file mode 100644 index 0000000..389b49b --- /dev/null +++ b/tests/success/Value/moment.strest.yml @@ -0,0 +1,20 @@ +version: 2 + +requests: + moment-in-request: + request: + url: https://postman-echo.com/get + method: GET + queryString: + - name: foo + value: <$ now | date('YYYY') $> + validate: + - jsonpath: content.args.foo + expect: "<$ '2019-10-10' | date('YYYY') $>" + moment-in-validate: + request: + url: https://postman-echo.com/time/format?timestamp=2019-10-10&format=YYYY + method: GET + validate: + - jsonpath: content.format + expect: "<$ '2019-10-10' | date('YYYY') $>"