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 c972ec6..26da6bc 100644 --- a/package.json +++ b/package.json @@ -31,7 +31,8 @@ "line-number": "0.1.0", "mkdirp": "^0.5.1", "nunjucks": "^3.1.3", - "ora": "^3.0.0", + "nunjucks-date": "^1.2.0", + "ora": "^3.2.0", "recursive-readdir": "^2.2.2", "request-to-curl": "^0.1.1", "sort-paths": "^1.1.1" @@ -60,7 +61,7 @@ "@types/jsonpath": "^0.2.0", "@types/node": "^10.9.4", "@types/nunjucks": "^3.1.0", - "@types/ora": "^1.3.4", + "@types/nunjucks-date": "0.0.6", "@types/recursive-readdir": "^2.2.0", "jest": "^23.6.0", "ts-jest": "^23.1.4", diff --git a/src/test.ts b/src/test.ts index d9058ba..383300b 100644 --- a/src/test.ts +++ b/src/test.ts @@ -1,6 +1,6 @@ import chalk from 'chalk'; import * as Joi from 'joi'; -import * as ora from 'ora'; +import ora from 'ora'; import axios from 'axios'; import * as faker from 'faker'; import { colorizeMain, colorizeCustomRed } from './handler'; @@ -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: '<%', @@ -30,6 +33,8 @@ const nunjucksEnv = nunjucks.configure(".", { throwOnUndefined: true, autoescape: false }); + +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') $>"