Skip to content

Commit 62a01ed

Browse files
committed
[Kong#11][feature] Finished test and multipart for request. should be g2g
1 parent 95cdfc6 commit 62a01ed

11 files changed

Lines changed: 175 additions & 107 deletions

File tree

‎src/targets/node/request.js‎

Lines changed: 69 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -3,80 +3,108 @@
33
var util = require('util');
44
var reducer = require('.,/../../reducer');
55

6-
module.exports = function (options) {
6+
module.exports = function(options) {
77
var opts = util._extend({
88
indent: ' '
99
}, options);
1010

1111
var code = [];
1212

1313
var reqOpts = {
14-
method: this.source.method,
15-
hostname: this.source.uriObj.hostname,
16-
port: this.source.uriObj.port,
17-
path: this.source.uriObj.path,
14+
url: this.source.fullUrl,
1815
headers: this.source.headersObj
1916
};
17+
var fsReplace = [];
18+
if (this.source.headersObj['Content-Type'] === 'application/x-www-form-urlencoded') {
19+
reqOpts.formData = this.source.postData.params.reduce(function(finalObj, thisParam) {
20+
finalObj[thisParam.name] = thisParam.value;
21+
return finalObj;
22+
}, {});
23+
}
24+
else if (this.source.headersObj['Content-Type'] === 'application/json') {
25+
if (this.source.postData.params) {
26+
reqOpts.body = JSON.stringify(
27+
this.source.postData.params.reduce(function(finalObj, thisParam) {
28+
finalObj[thisParam.name] = thisParam.value;
29+
return finalObj;
30+
}, {})
31+
);
32+
}
33+
reqOpts.json = true;
34+
}
35+
else if(this.source.headersObj["Content-Type"] === "multipart/form-data"){
36+
this.source.postData.params.forEach(function(param){
37+
reqOpts.formData = reqOpts.formData || {};
38+
reqOpts.formData[param.name] = {};
39+
if (!param.value.length){
40+
param.value = 'fs.createReadStream(\''+param.fileName+'\')';
41+
fsReplace.push(param.value);
42+
reqOpts.formData[param.name].value =param.value;
43+
if(param.fileName.indexOf('/')>-1){
44+
param.fileName = param.fileName.split('/');
45+
param.fileName = param.fileName[param.fileName.length-1];
46+
}
47+
else if(param.fileName.indexOf('\\')>-1){
48+
param.fileName = param.fileName.split('\\');
49+
param.fileName = param.fileName[param.fileName.length-1];
50+
}
51+
reqOpts.formData[param.name].options = {
52+
filename:param.fileName,
53+
"content-type":param.contentType
54+
}
55+
}
56+
else{
57+
reqOpts.formData[param.name] = param.value;
58+
}
59+
})
60+
}
61+
else {
62+
reqOpts.body = this.source.postData.params;
63+
}
64+
var simpleRequest = this.source.bodySize === 0 && this.source.headerSize === 0;
2065

2166
// construct cookies argument
22-
var cookies = this.source.cookies.map(function (cookie) {
67+
var cookies = this.source.cookies.map(function(cookie) {
2368
return encodeURIComponent(cookie.name) + '=' + encodeURIComponent(cookie.value);
2469
});
2570

2671
if (cookies.length) {
2772
reqOpts.headers.Cookie = cookies.join('; ');
2873
}
2974

30-
code.push('var request = require("request");');
31-
32-
if (!this.source.postData.text && this.source.postData.params) {
33-
code.push('var querystring = require("querystring");');
75+
code.push('var request = require(\'request\');');
76+
if(fsReplace.length>0){
77+
code.push('var fs = require(\'fs\');');
3478
}
3579

3680
code.push(null);
3781

38-
code.push(util.format('var options = %s;', JSON.stringify(reqOpts, null, opts.indent)));
39-
40-
code.push(null);
41-
42-
code.push('var req = http.request(options, function (res) {');
43-
44-
code.push(opts.indent + 'var chunks = [];');
82+
var options = !simpleRequest ? util.format('var options = %s;', JSON.stringify(reqOpts, null, opts.indent)) : null;
83+
fsReplace.forEach(function(fsToReplace){
84+
options = options.replace('"'+fsToReplace+'"',fsToReplace);
85+
})
86+
code.push(options);
4587

4688
code.push(null);
47-
48-
code.push(opts.indent + 'res.on("data", function (chunk) {');
49-
code.push(opts.indent + opts.indent + 'chunks.push(chunk);');
50-
code.push(opts.indent + '});');
89+
var requestLine = 'request.';
90+
requestLine += this.source.method === 'DELETE' ? 'del(' : this.source.method.toLowerCase() + '(';
91+
requestLine += simpleRequest ? '\'' + this.source.fullUrl + '\'' : 'options';
92+
requestLine += ', function(error, response, body){';
93+
code.push(requestLine);
94+
code.push(opts.indent + 'if(error) throw new Error(error);');
5195

5296
code.push(null);
5397

54-
code.push(opts.indent + 'res.on("end", function () {');
55-
code.push(opts.indent + opts.indent + 'var body = Buffer.concat(chunks);');
56-
code.push(opts.indent + '});');
5798
code.push('});');
5899

59100
code.push(null);
60101

61-
if (this.source.postData.text) {
62-
code.push(util.format('req.write(%s);', JSON.stringify(this.source.postData.text)));
63-
}
64-
65-
if (!this.source.postData.text && this.source.postData.params) {
66-
var postData = this.source.postData.params.reduce(reducer, {});
67-
68-
code.push(util.format('var postData = querystring.stringify(%s);', JSON.stringify(postData)));
69-
code.push(util.format('req.write(postData);'));
70-
}
71-
72-
code.push('req.end();');
73-
74102
return code.join('\n');
75103
};
76104

77105
module.exports.info = {
78-
key: 'native',
79-
title: 'HTTP',
80-
link: 'http://nodejs.org/api/http.html#http_http_request_options_callback',
81-
description: 'Node.js native HTTP interface'
106+
key: 'request',
107+
title: 'request',
108+
link: 'https://github.com/request/request',
109+
description: 'Simplified HTTP request client'
82110
};
Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
var request = require('request');
2-
var querystring = require("querystring");
32

43
var options = {
5-
url: 'http://mockbin.com/har?',
6-
body: querystring.stringify({"foo": "bar"}),
7-
headers: {
4+
"url": "http://mockbin.com/har",
5+
"headers": {
86
"Content-Type": "application/x-www-form-urlencoded"
7+
},
8+
"formData": {
9+
"foo": "bar"
910
}
10-
}
11-
request.post(options, function(error, response, body) {
12-
if (error){
13-
//throw error here
14-
return;
15-
}
16-
// work with response and body here;
11+
};
12+
13+
request.post(options, function(error, response, body){
14+
if(error) throw new Error(error);
15+
1716
});
17+
Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
var request = require('request');
22

33
var options = {
4-
url: 'http://mockbin.com/har?',
5-
body: JSON.stringify({"foo": "bar"}),
6-
json:true
7-
}
8-
request.post(options, function(error, response, body) {
9-
if (error){
10-
//throw error here
11-
return;
12-
}
13-
// work with response and body here;
4+
"url": "http://mockbin.com/har",
5+
"headers": {
6+
"Content-Type": "application/json"
7+
},
8+
"json": true
9+
};
10+
11+
request.post(options, function(error, response, body){
12+
if(error) throw new Error(error);
13+
1414
});
15+
Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
var request = require('request');
22

33
var options = {
4-
url: 'http://mockbin.com/har?',
5-
headers: {
4+
"url": "http://mockbin.com/har",
5+
"headers": {
66
"Cookie": "foo=bar; bar=baz"
77
}
8-
}
9-
request.post(options, function(error, response, body) {
10-
if (error){
11-
//throw error here
12-
return;
13-
}
14-
// work with response and body here;
8+
};
9+
10+
request.post(options, function(error, response, body){
11+
if(error) throw new Error(error);
12+
1513
});
14+
Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,19 @@
11
var request = require('request');
22

33
var options = {
4-
url: 'http://mockbin.com/har?baz=abc&foo=bar&foo=baz',
5-
formData: {
6-
"foo": "bar"
7-
},
8-
headers: {
4+
"url": "http://mockbin.com/har?baz=abc&foo=bar&foo=baz",
5+
"headers": {
96
"Accept": "application/json",
107
"Content-Type": "application/x-www-form-urlencoded",
118
"Cookie": "foo=bar; bar=baz"
9+
},
10+
"formData": {
11+
"foo": "bar"
1212
}
13-
}
14-
request.post(options, function(error, response, body) {
15-
if (error){
16-
//throw error here
17-
return;
18-
}
19-
// work with response and body here;
13+
};
14+
15+
request.post(options, function(error, response, body){
16+
if(error) throw new Error(error);
17+
2018
});
19+
Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
var request = require('request');
22

33
var options = {
4-
url: 'http://mockbin.com/har?',
5-
headers: {
4+
"url": "http://mockbin.com/har",
5+
"headers": {
66
"Accept": "application/json",
77
"X-Foo": "Bar"
88
}
9-
}
10-
request.get(options, function(error, response, body) {
11-
if (error){
12-
//throw error here
13-
return;
14-
}
15-
// work with response and body here;
9+
};
10+
11+
request.get(options, function(error, response, body){
12+
if(error) throw new Error(error);
13+
1614
});
15+
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
var request = require('request');
2+
3+
var options = {
4+
"url": "http://mockbin.com/har",
5+
"headers": {
6+
"Content-Type": "multipart/form-data"
7+
},
8+
"formData": {
9+
"foo": "hello world"
10+
}
11+
};
12+
13+
request.post(options, function(error, response, body){
14+
if(error) throw new Error(error);
15+
16+
});
17+
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
var request = require('request');
2+
3+
var options = {
4+
"url": "http://mockbin.com/har",
5+
"headers": {
6+
"Content-Type": "multipart/form-data"
7+
},
8+
"formData": {
9+
"foo": "bar"
10+
}
11+
};
12+
13+
request.post(options, function(error, response, body){
14+
if(error) throw new Error(error);
15+
16+
});
17+
Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
var request = require('request');
22

33
var options = {
4-
url: 'http://mockbin.com/har?key=value&baz=abc&foo=bar&foo=baz',
5-
headers: {}
6-
}
7-
request.get(options, function(error, response, body) {
8-
if (error){
9-
//throw error here
10-
return;
11-
}
12-
// work with response and body here;
4+
"url": "http://mockbin.com/har?key=value&baz=abc&foo=bar&foo=baz",
5+
"headers": {}
6+
};
7+
8+
request.get(options, function(error, response, body){
9+
if(error) throw new Error(error);
10+
1311
});
12+
Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
var request = require('request');
22

3-
request.get('http://mockbin.com/har?', function(error, response, body) {
4-
if (error){
5-
//throw error here
6-
return;
7-
}
8-
// work with response and body here;
3+
var options = {
4+
"url": "http://mockbin.com/har",
5+
"headers": {}
6+
};
7+
8+
request.get(options, function(error, response, body){
9+
if(error) throw new Error(error);
10+
911
});
12+

0 commit comments

Comments
 (0)