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 #141 from shrikster/feature/form-data
Browse files Browse the repository at this point in the history
Added support for sending form data
  • Loading branch information
jgroom33 authored Jun 4, 2019
2 parents cda14b5 + bc95e1a commit 112c00e
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 4 deletions.
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,24 @@ requests:
expect: "<$ '2019-10-10' | date('YYYY') $>"
```
## Sending files and form data
Sending files and form data is easy, use params type in the postData prop.
```yaml
version: 2
requests:
postwithfile:
request:
url: https://postman-echo.com/post
method: POST
postData:
mimeType: multipart/form-data
params:
- name: userId
value: "1"
- name: avatar
value: <$ file("tests/strest.png") $>

```
## Response Validation

The immediate response is stored in [HAR Format](http://www.softwareishard.com/blog/har-12-spec/#response)
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"commander": "^2.17.1",
"deep-eql": "4.0.0",
"faker": "^4.1.0",
"form-data": "^2.3.3",
"get-line-from-pos": "1.0.0",
"joi": "^13.6.0",
"js-yaml": "^3.12.0",
Expand Down Expand Up @@ -54,6 +55,7 @@
"devDependencies": {
"@types/commander": "^2.12.2",
"@types/faker": "^4.1.3",
"@types/form-data": "^2.2.1",
"@types/jest": "^23.3.2",
"@types/joi": "^13.4.4",
"@types/js-yaml": "^3.11.2",
Expand Down
1 change: 0 additions & 1 deletion src/configSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,6 @@ interface paramsObjectSchema {
name: string,
value: string,
}

interface postDataObjectSchema {
mimeType: string,
params: Array<paramsObjectSchema>,
Expand Down
31 changes: 28 additions & 3 deletions src/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import * as jsonfile from 'jsonfile'
import * as path from 'path';
import * as Ajv from 'ajv';
import * as nunjucksDate from 'nunjucks-date';
import * as fs from 'fs';
import * as FormData from 'form-data';

var deepEql = require("deep-eql");
var lineNumber = require('line-number');
Expand Down Expand Up @@ -44,6 +46,10 @@ nunjucksEnv.addGlobal('Env', function (envi: string) {
return environ;
})

nunjucksEnv.addGlobal('file', function (filePath: string) {
const key = `sendFile:${path.resolve(filePath)}`;
return key;
});
/**
* All Data that any request returns, will be stored here. After that it can be used in the following methods
*/
Expand Down Expand Up @@ -405,10 +411,29 @@ const performRequest = async (requestObject: requestsObjectSchema, requestName:
if (requestObject.request.postData.text) {
axiosObject.data = requestObject.request.postData.text;
}

if (requestObject.request.postData.params) {
const searchParams = new URLSearchParams()
requestObject.request.postData.params.forEach(item=>{searchParams.append(item.name,item.value)})
axiosObject.data = searchParams.toString();
if(requestObject.request.postData.mimeType && requestObject.request.postData.mimeType.toLowerCase()=='application/x-www-form-urlencoded')
{
const searchParams = new URLSearchParams()
requestObject.request.postData.params.forEach(item=>{searchParams.append(item.name,item.value)})
axiosObject.data = searchParams.toString();
}
else{
const form: FormData = new FormData();
requestObject.request.postData.params.forEach(item=>{
if(item.value.startsWith('sendFile:')){
const filePath = item.value.replace('sendFile:','');
const fileStream = fs.createReadStream(filePath);
form.append(item.name, fileStream, { filepath:filePath });
}
else{
form.append(item.name, item.value);
}
});
axiosObject.data = form;
axiosObject.headers = {...axiosObject.headers,...form.getHeaders()};
}
}
}

Expand Down
Binary file added tests/strest.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
27 changes: 27 additions & 0 deletions tests/success/formdata/file.strest.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
version: 2
requests:
postwithfile:
request:
# url: https://postman-echo.com/post
# url: http://localhost:3000/post
# url: http://jkorpela.fi/cgi-bin/echoraw.cgi
url: http://httpbin.org/post
method: POST
postData:
mimeType: multipart/form-data
params:
- name: title
value: foo
- name: body
value: bar
- name: userId
value: "1"
- name: avatar
value: <$ file("tests/strest.png") $>
- name: avatar1
value: <$ file("tests/strest.png") $>
validate:
- jsonpath: content.form.title
expect: foo
- jsonpath: content.files["avatar"]
type: [string]
19 changes: 19 additions & 0 deletions tests/success/formdata/simple.strest.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
version: 2

requests:
simpleformdata:
request:
url: https://postman-echo.com/post
method: POST
postData:
mimeType: application/x-www-form-urlencoded
params:
- name: title
value: foo
- name: body
value: bar
- name: userId
value: "1"
validate:
- jsonpath: content.form.title
expect: foo

0 comments on commit 112c00e

Please sign in to comment.