Skip to content

Commit 73621e9

Browse files
committed
testing multipart support
1 parent 3b6a603 commit 73621e9

28 files changed

Lines changed: 226 additions & 53 deletions

‎CONTRIBUTING.md‎

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,9 @@ postData: {
312312

313313
###### application/json
314314

315-
*when postData.mimeType is one of: `application/json`, `text/json`, `text/x-json`, `application/x-json`*
315+
- will match when `postData.mimeType` is one of: `application/json`, `text/json`, `text/x-json`, `application/x-json`
316+
- In case of failure to parse `postData.text` as a JSON object, `postData.mimeType` is set to `text/plain`. this is done so that the implementing target, would still attempt to post the raw body as is.
317+
- This also emphasizes not to rely on `postData.mimeType` for the `Content-Type` header!
316318

317319
```js
318320
postData: {
@@ -338,15 +340,12 @@ postData: {
338340
}
339341
```
340342

341-
Notes:
342-
343-
- In case of failure to parse `postData.text` as a JSON object, `postData.mimeType` is set to `text/plain`. this is done so that the implementing target, would still attempt to post the raw body as is.
344-
- This also emphasizes not to rely on `postData.mimeType` for the `Content-Type` header!
345-
346343
###### multipart/form-data
347344

345+
- will match when `postData.mimeType` is one of: `multipart/mixed` `multipart/related`, `multipart/form-data`, `multipart/alternative`
346+
- will force `postData.mimeType` to `multipart/form-data`
348347
- will create/overwrite the `Content-Type` header if it does not exist, with the appropriate boundary flag.
349-
- will create/overwrite the `Content-Length` header
348+
- when no `params[].value` is present, will default to empty content
350349

351350
```js
352351
postData: {
@@ -379,10 +378,6 @@ postData: {
379378

380379
###### multipart/form-data (File Uploads)
381380

382-
- will create/overwrite the `Content-Type` header if it does not exist, with the appropriate boundary flag.
383-
- will create/overwrite the `Content-Length` header
384-
- when no params[].value is present, will default to empty content
385-
386381
```js
387382
postData: {
388383
// added to pass har-validator

‎src/index.js‎

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,6 @@ var HTTPSnippet = function (req, lang) {
5454
}, {});
5555
}
5656

57-
// create allHeaders object
58-
// this.source.allHeaders = util._extend(this.source.allHeaders, this.source.headersObj);
59-
this.source.allHeaders = util._extend(this.source.allHeaders, this.source.headersObj);
60-
6157
// construct Cookie header
6258
var cookies = this.source.cookies.map(function (cookie) {
6359
return encodeURIComponent(cookie.name) + '=' + encodeURIComponent(cookie.value);
@@ -68,9 +64,13 @@ var HTTPSnippet = function (req, lang) {
6864
}
6965

7066
switch (this.source.postData.mimeType) {
67+
case 'multipart/mixed':
68+
case 'multipart/related':
7169
case 'multipart/form-data':
72-
// reset the text value
70+
case 'multipart/alternative':
71+
// reset values
7372
this.source.postData.text = '';
73+
this.source.postData.mimeType = 'multipart/form-data';
7474

7575
var form = new FormData();
7676

@@ -89,7 +89,6 @@ var HTTPSnippet = function (req, lang) {
8989
}.bind(this)));
9090

9191
this.source.headersObj['content-type'] = 'multipart/form-data; boundary=' + form.getBoundary();
92-
this.source.headersObj['content-length'] = form.getLengthSync();
9392
break;
9493

9594
case 'application/x-www-form-urlencoded':
@@ -107,20 +106,25 @@ var HTTPSnippet = function (req, lang) {
107106
case 'text/x-json':
108107
case 'application/json':
109108
case 'application/x-json':
109+
this.source.postData.mimeType = 'application/json';
110+
110111
if (this.source.postData.text) {
111112
try {
112113
this.source.postData.jsonObj = JSON.parse(this.source.postData.text);
113114
} catch (e) {
114115
debug(e);
115-
116+
116117
// force back to text/plain
117118
// if headers have proper content-type value, then this should also work
118-
this.source.postData.mimeType = 'text/plain'
119+
this.source.postData.mimeType = 'text/plain';
119120
}
120121
}
121122
break;
122123
}
123124

125+
// create allHeaders object
126+
this.source.allHeaders = util._extend(this.source.allHeaders, this.source.headersObj);
127+
124128
// deconstruct the uri
125129
this.source.uriObj = url.parse(this.source.url, true, true);
126130

‎src/targets/ocaml/cohttp.js‎

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -30,28 +30,17 @@ module.exports = function (source, options) {
3030
}
3131

3232
// Add body
33-
var bodyPresent = source.postData && source.postData.text;
34-
35-
if (bodyPresent) {
33+
if (source.postData.text) {
3634
// Just text
3735
code.push(util.format('let body = %s in', JSON.stringify(source.postData.text)));
38-
} else if (source.postData && !source.postData.text && source.postData.params && source.postData.params.length) {
39-
// Post params
40-
bodyPresent = true;
41-
42-
var body = source.postData.params.map(function (param) {
43-
return param.name + '=' + param.value;
44-
}).join('&');
45-
46-
code.push(util.format('let body = "%s" in', body));
4736
}
4837

4938
// Do the request
5039
code.push('');
5140

5241
code.push(util.format('Client.call %s%s(Code.method_of_string "%s") uri',
5342
headers.length ? '~headers ' : '',
54-
bodyPresent ? '~body ' : '',
43+
source.postData.text ? '~body ' : '',
5544
source.method
5645
));
5746

‎src/targets/wget.js‎

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,6 @@ module.exports = function (source, options) {
2727
code.push('--body-data ' + JSON.stringify(source.postData.text));
2828
}
2929

30-
if (source.postData.mimeType === 'multipart/form-data') {
31-
source.postData.params.forEach(function (param) {
32-
if (param.value) {
33-
code.push(util.format('--body-data %s', JSON.stringify(param.value)));
34-
} else if (param.fileName) {
35-
code.push(util.format('--body-file "%s"', param.fileName));
36-
}
37-
});
38-
}
39-
4030
code.push(opts.short ? '-O' : '--output-document');
4131

4232
code.push(util.format('- "%s"', source.fullUrl));

‎test/fixtures/available-targets.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
"clients": [
4949
{
5050
"key": "cohttp",
51-
"title": "OCaml",
51+
"title": "CoHTTP",
5252
"link": "https://github.com/mirage/ocaml-cohttp",
5353
"description": "Cohttp is a very lightweight HTTP server using Lwt or Async for OCaml"
5454
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
curl --request POST \
2+
--url "http://mockbin.com/har" \
3+
--header "content-type: multipart/form-data; boundary=---011000010111000001101001" \
4+
--data "-----011000010111000001101001\r\nContent-Disposition: form-data; name=\"foo\"; filename=\"hello.txt\"\r\nContent-Type: text/plain\r\n\r\nHello World\r\n-----011000010111000001101001--"
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
curl --request POST \
2+
--url "http://mockbin.com/har" \
3+
--header "content-type: multipart/form-data; boundary=---011000010111000001101001" \
4+
--data "-----011000010111000001101001\r\nContent-Disposition: form-data; name=\"foo\"; filename=\"hello.txt\"\r\nContent-Type: text/plain\r\n\r\n\r\n-----011000010111000001101001--"
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
curl --request POST \
2+
--url "http://mockbin.com/har" \
3+
--header "content-type: multipart/form-data; boundary=---011000010111000001101001" \
4+
--data "-----011000010111000001101001\r\nContent-Disposition: form-data; name=\"foo\"\r\n\r\nbar\r\n-----011000010111000001101001--"
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
echo "-----011000010111000001101001\r\nContent-Disposition: form-data; name=\"foo\"; filename=\"hello.txt\"\r\nContent-Type: text/plain\r\n\r\nHello World\r\n-----011000010111000001101001--" | \
2+
http POST http://mockbin.com/har \
3+
content-type:multipart/form-data; boundary=---011000010111000001101001
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
echo "-----011000010111000001101001\r\nContent-Disposition: form-data; name=\"foo\"; filename=\"hello.txt\"\r\nContent-Type: text/plain\r\n\r\n\r\n-----011000010111000001101001--" | \
2+
http POST http://mockbin.com/har \
3+
content-type:multipart/form-data; boundary=---011000010111000001101001

0 commit comments

Comments
 (0)