From 405da9274bad03d0d5a2ff96b6200b37fd69056d Mon Sep 17 00:00:00 2001 From: Emerson Jair Date: Mon, 20 Jan 2020 00:18:48 -0200 Subject: [PATCH 1/2] add support to read request data from json file fix bug in moment tests (update to 2020) --- README.md | 19 +++++++++++++++++++ package.json | 2 +- src/configSchema.ts | 2 ++ src/test.ts | 7 +++++++ tests/success/Value/moment.strest.yml | 2 +- tests/success/jsonfile/data.json | 3 +++ tests/success/jsonfile/jsonfile.strest.yml | 8 ++++++++ 7 files changed, 41 insertions(+), 2 deletions(-) create mode 100644 tests/success/jsonfile/data.json create mode 100644 tests/success/jsonfile/jsonfile.strest.yml diff --git a/README.md b/README.md index 98cc929..c7a59cf 100644 --- a/README.md +++ b/README.md @@ -378,6 +378,25 @@ requests: expect: "<$ '2019-10-10' | date('YYYY') $>" ``` +## Sending JSON requests from external files + +If you have a JSON file that represents the body of your request, you can use the `json` option. + +Strest will read the JSON file you have specified and add it to the body of the request, you won't even need to worry about the Content-Type header, Strest will take care of that for you. + +``` +yaml +version: 2 + +requests: + jsonfile: + request: + url: https://postman-echo.com/post + method: POST + json: tests/success/jsonfile/data.json # You have to put the whole path relative to the current directory that you run strest + log: true +``` + ## Sending files and form data Sending files and form data is easy, use params type in the postData prop. ```yaml diff --git a/package.json b/package.json index b58a56f..4c211cb 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@strest/cli", - "version": "2.4.1", + "version": "2.5.0", "description": "A new dimension of REST API testing", "main": "dist/main.js", "repository": "https://github.com/eykrehbein/strest", diff --git a/src/configSchema.ts b/src/configSchema.ts index e3d41ba..16e78d3 100644 --- a/src/configSchema.ts +++ b/src/configSchema.ts @@ -58,6 +58,7 @@ const requestSchema = Joi.object().keys({ headers: Joi.array().items(headerSchema).optional(), queryString: Joi.array().items(queryStringSchema).optional(), cookies: Joi.array().items(cookieSchema).optional(), + json: Joi.string().optional(), }) const validateSchema = Joi.object().keys({ @@ -121,6 +122,7 @@ interface requestObjectSchema { headers: Array, queryString: Array, cookies: cookiesObjectSchema, + json?: string, } interface queryStringObjectSchema { diff --git a/src/test.ts b/src/test.ts index 08107f6..4493542 100644 --- a/src/test.ts +++ b/src/test.ts @@ -438,6 +438,13 @@ const performRequest = async (requestObject: requestsObjectSchema, requestName: } } + if (typeof requestObject.request.json !== 'undefined') { + axiosObject.headers["Content-Type"] = 'application/json' + const file = requestObject.request.json; + const json = jsonfile.readFileSync(file); + axiosObject.data = json; + } + try { let axiosInstance = axios.create({ validateStatus: function (status) { diff --git a/tests/success/Value/moment.strest.yml b/tests/success/Value/moment.strest.yml index 389b49b..a07e07c 100644 --- a/tests/success/Value/moment.strest.yml +++ b/tests/success/Value/moment.strest.yml @@ -10,7 +10,7 @@ requests: value: <$ now | date('YYYY') $> validate: - jsonpath: content.args.foo - expect: "<$ '2019-10-10' | date('YYYY') $>" + expect: "<$ '2020-10-10' | date('YYYY') $>" moment-in-validate: request: url: https://postman-echo.com/time/format?timestamp=2019-10-10&format=YYYY diff --git a/tests/success/jsonfile/data.json b/tests/success/jsonfile/data.json new file mode 100644 index 0000000..86a346c --- /dev/null +++ b/tests/success/jsonfile/data.json @@ -0,0 +1,3 @@ +{ + "success": true +} \ No newline at end of file diff --git a/tests/success/jsonfile/jsonfile.strest.yml b/tests/success/jsonfile/jsonfile.strest.yml new file mode 100644 index 0000000..6f34dca --- /dev/null +++ b/tests/success/jsonfile/jsonfile.strest.yml @@ -0,0 +1,8 @@ +version: 2 + +requests: + jsonfile: + request: + url: https://postman-echo.com/post + method: POST + json: tests/success/jsonfile/data.json # You have to put the whole path relative to the current directory that you run strest From 072d843df9c2cc06727fc86988eb4a5be213a84f Mon Sep 17 00:00:00 2001 From: Emerson Jair Date: Fri, 13 Mar 2020 19:10:54 -0300 Subject: [PATCH 2/2] changes from code review --- README.md | 3 +-- src/test.ts | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index c7a59cf..ed5165d 100644 --- a/README.md +++ b/README.md @@ -384,8 +384,7 @@ If you have a JSON file that represents the body of your request, you can use th Strest will read the JSON file you have specified and add it to the body of the request, you won't even need to worry about the Content-Type header, Strest will take care of that for you. -``` -yaml +```yaml version: 2 requests: diff --git a/src/test.ts b/src/test.ts index 4493542..b811c08 100644 --- a/src/test.ts +++ b/src/test.ts @@ -438,7 +438,7 @@ const performRequest = async (requestObject: requestsObjectSchema, requestName: } } - if (typeof requestObject.request.json !== 'undefined') { + if (requestObject.request.json !== undefined) { axiosObject.headers["Content-Type"] = 'application/json' const file = requestObject.request.json; const json = jsonfile.readFileSync(file);