Skip to content
This repository has been archived by the owner on Mar 26, 2021. It is now read-only.

Commit

Permalink
Merge pull request #146 from dungahk/read-from-json
Browse files Browse the repository at this point in the history
add support to read request data from json file
  • Loading branch information
eykrehbein authored Mar 14, 2020
2 parents 862639d + 072d843 commit 33731cd
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 2 deletions.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,24 @@ 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
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
2 changes: 2 additions & 0 deletions src/configSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down Expand Up @@ -121,6 +122,7 @@ interface requestObjectSchema {
headers: Array<headerObjectSchema>,
queryString: Array<queryStringObjectSchema>,
cookies: cookiesObjectSchema,
json?: string,
}

interface queryStringObjectSchema {
Expand Down
7 changes: 7 additions & 0 deletions src/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,13 @@ const performRequest = async (requestObject: requestsObjectSchema, requestName:
}
}

if (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) {
Expand Down
2 changes: 1 addition & 1 deletion tests/success/Value/moment.strest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions tests/success/jsonfile/data.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"success": true
}
8 changes: 8 additions & 0 deletions tests/success/jsonfile/jsonfile.strest.yml
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 33731cd

Please sign in to comment.