Skip to content

Commit f6a2f8d

Browse files
committed
[#12][feature] Added unirest (multipart is still failing)
1 parent 3976ee3 commit f6a2f8d

File tree

13 files changed

+227
-0
lines changed

13 files changed

+227
-0
lines changed

src/targets/node/unirest.js

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
'use strict';
2+
3+
var util = require('util');
4+
var path = require('path');
5+
6+
module.exports = function (options) {
7+
var opts = util._extend({
8+
indent: ' '
9+
}, options);
10+
11+
var code = ['var unirest = require("unirest");', null];
12+
if(this.source.cookies.length){
13+
code.push('var CookieJar = unirest.jar();');
14+
var cookies = {};
15+
var url = this.source.url;
16+
this.source.cookies.forEach(function(cookie){
17+
code.push(util.format('CookieJar.add("%s=%s","%s")',encodeURIComponent(cookie.name), encodeURIComponent(cookie.value), url));
18+
})
19+
code.push(null);
20+
}
21+
code.push(util.format('unirest.%s("%s")', this.source.method.toLowerCase(), this.source.url));
22+
if (Object.keys(this.source.queryObj).length) {
23+
code.push(opts.indent + util.format('.query(%s)',JSON.stringify(this.source.queryObj)))
24+
}
25+
if(this.source.cookies.length){
26+
code.push(opts.indent + '.jar(CookieJar)')
27+
}
28+
if (Object.keys(this.source.headersObj).length) {
29+
var self = this;
30+
var newHeadersObj = Object.keys(this.source.headersObj).reduce(function(finalHeader, headerName){
31+
if(headerName !== 'Content-Type'){
32+
finalHeader[headerName] = self.source.headersObj[headerName];
33+
}
34+
return finalHeader;
35+
}, {})
36+
if(Object.keys(newHeadersObj).length){
37+
code.push(opts.indent + util.format('.headers(%s)',JSON.stringify(newHeadersObj)));
38+
}
39+
}
40+
if(this.source.postData.mimeType){
41+
if(this.source.postData.mimeType !== 'application/octet-stream'){
42+
code.push(opts.indent + util.format('.type("%s")',this.source.postData.mimeType))
43+
}
44+
if(this.source.postData.mimeType === 'multipart/form-data'){
45+
// not sure how unirest handles multipart
46+
}
47+
}
48+
if(this.source.postData.paramsObj.length){
49+
code.push(opts.indent + util.format('.send(%s)', this.source.postData.paramsObj));
50+
}
51+
code.push(opts.indent + '.end(function(response){')
52+
code.push(opts.indent + opts.indent + 'if (response.error) throw new Error(response.error);');
53+
code.push(null);
54+
code.push(opts.indent+opts.indent+ 'console.log(reponse.body);');
55+
code.push(opts.indent+'});');
56+
code.push(null);
57+
58+
return code.join('\n');
59+
};
60+
61+
module.exports.info = {
62+
key: 'unirest',
63+
title: 'Unirest',
64+
link: 'https://github.com/request/request',
65+
description: 'Simplified HTTP request client.'
66+
};

test/fixtures/available-targets.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@
3737
"title": "Request",
3838
"link": "https://github.com/request/request",
3939
"description": "Simplified HTTP request client"
40+
},
41+
{
42+
"key": "unirest",
43+
"title": "Unirest",
44+
"link": "https://github.com/request/request",
45+
"description": "Simplified HTTP request client"
4046
}
4147
]
4248
},
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
var unirest = require("unirest");
2+
3+
unirest.post("http://mockbin.com/har")
4+
.type("application/x-www-form-urlencoded")
5+
.end(function(response){
6+
if (response.error) throw new Error(response.error);
7+
8+
console.log(reponse.body);
9+
});
10+
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
var unirest = require("unirest");
2+
3+
unirest.post("http://mockbin.com/har")
4+
.type("application/json")
5+
.end(function(response){
6+
if (response.error) throw new Error(response.error);
7+
8+
console.log(reponse.body);
9+
});
10+
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
var unirest = require("unirest");
2+
3+
var CookieJar = unirest.jar();
4+
CookieJar.add("foo=bar","http://mockbin.com/har")
5+
CookieJar.add("bar=baz","http://mockbin.com/har")
6+
7+
unirest.post("http://mockbin.com/har")
8+
.jar(CookieJar)
9+
.end(function(response){
10+
if (response.error) throw new Error(response.error);
11+
12+
console.log(reponse.body);
13+
});
14+
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
var unirest = require("unirest");
2+
3+
var CookieJar = unirest.jar();
4+
CookieJar.add("foo=bar","http://mockbin.com/har")
5+
CookieJar.add("bar=baz","http://mockbin.com/har")
6+
7+
unirest.post("http://mockbin.com/har")
8+
.query({"baz":"abc","foo":["bar","baz"]})
9+
.jar(CookieJar)
10+
.headers({"Accept":"application/json"})
11+
.type("application/x-www-form-urlencoded")
12+
.end(function(response){
13+
if (response.error) throw new Error(response.error);
14+
15+
console.log(reponse.body);
16+
});
17+
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
var unirest = require("unirest");
2+
3+
unirest.get("http://mockbin.com/har")
4+
.headers({"Accept":"application/json","X-Foo":"Bar"})
5+
.end(function(response){
6+
if (response.error) throw new Error(response.error);
7+
8+
console.log(reponse.body);
9+
});
10+
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
var request = require("request");
2+
3+
request({
4+
"method": "POST",
5+
"url": "http://mockbin.com/har",
6+
"headers": {
7+
"Content-Type": "multipart/form-data"
8+
},
9+
"formData": {
10+
"foo": {
11+
"value": "Hello World",
12+
"options": {
13+
"filename": "hello.txt",
14+
"contentType": "text/plain"
15+
}
16+
}
17+
}
18+
}, function (error, response, body) {
19+
if (error) throw new Error(error);
20+
21+
console.log(body);
22+
});
23+
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
var request = require("request");
2+
3+
request({
4+
"method": "POST",
5+
"url": "http://mockbin.com/har",
6+
"headers": {
7+
"Content-Type": "multipart/form-data"
8+
},
9+
"formData": {
10+
"foo": {
11+
"value": "Hello World",
12+
"options": {
13+
"filename": "hello.txt",
14+
"contentType": "text/plain"
15+
}
16+
}
17+
}
18+
}, function (error, response, body) {
19+
if (error) throw new Error(error);
20+
21+
console.log(body);
22+
});
23+
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
var request = require("request");
2+
3+
request({
4+
"method": "POST",
5+
"url": "http://mockbin.com/har",
6+
"headers": {
7+
"Content-Type": "multipart/form-data"
8+
},
9+
"formData": {
10+
"foo": {
11+
"value": "Hello World",
12+
"options": {
13+
"filename": "hello.txt",
14+
"contentType": "text/plain"
15+
}
16+
}
17+
}
18+
}, function (error, response, body) {
19+
if (error) throw new Error(error);
20+
21+
console.log(body);
22+
});
23+

0 commit comments

Comments
 (0)