Skip to content

Commit 39c55e1

Browse files
committed
Copying over native to request. WIP
1 parent 389d499 commit 39c55e1

9 files changed

Lines changed: 250 additions & 0 deletions

File tree

‎src/targets/node/request.js‎

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
'use strict';
2+
3+
var util = require('util');
4+
var reducer = require('.,/../../reducer');
5+
6+
module.exports = function (options) {
7+
var opts = util._extend({
8+
indent: ' '
9+
}, options);
10+
11+
var code = [];
12+
13+
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,
18+
headers: this.source.headersObj
19+
};
20+
21+
// construct cookies argument
22+
var cookies = this.source.cookies.map(function (cookie) {
23+
return encodeURIComponent(cookie.name) + '=' + encodeURIComponent(cookie.value);
24+
});
25+
26+
if (cookies.length) {
27+
reqOpts.headers.Cookie = cookies.join('; ');
28+
}
29+
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");');
34+
}
35+
36+
code.push(null);
37+
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 = [];');
45+
46+
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 + '});');
51+
52+
code.push(null);
53+
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 + '});');
57+
code.push('});');
58+
59+
code.push(null);
60+
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+
74+
return code.join('\n');
75+
};
76+
77+
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'
82+
};

‎test/fixtures/available-targets.json‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@
3131
"title": "HTTP",
3232
"link": "http://nodejs.org/api/http.html#http_http_request_options_callback",
3333
"description": "Node.js native HTTP interface"
34+
},
35+
{
36+
"key": "request",
37+
"title": "request",
38+
"link": "https://github.com/request/request",
39+
"description": "Simplified HTTP request client"
3440
}
3541
]
3642
},
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
var http = require("http");
2+
var querystring = require("querystring");
3+
4+
var options = {
5+
"method": "POST",
6+
"hostname": "mockbin.com",
7+
"port": null,
8+
"path": "/har?",
9+
"headers": {
10+
"Content-Type": "application/x-www-form-urlencoded"
11+
}
12+
};
13+
14+
var req = http.request(options, function (res) {
15+
var chunks = [];
16+
17+
res.on("data", function (chunk) {
18+
chunks.push(chunk);
19+
});
20+
21+
res.on("end", function () {
22+
var body = Buffer.concat(chunks);
23+
});
24+
});
25+
26+
var postData = querystring.stringify({"foo":"bar"});
27+
req.write(postData);
28+
req.end();
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
var http = require("http");
2+
3+
var options = {
4+
"method": "POST",
5+
"hostname": "mockbin.com",
6+
"port": null,
7+
"path": "/har?",
8+
"headers": {
9+
"Content-Type": "application/json"
10+
}
11+
};
12+
13+
var req = http.request(options, function (res) {
14+
var chunks = [];
15+
16+
res.on("data", function (chunk) {
17+
chunks.push(chunk);
18+
});
19+
20+
res.on("end", function () {
21+
var body = Buffer.concat(chunks);
22+
});
23+
});
24+
25+
req.write("{\"foo\": \"bar\"}");
26+
req.end();
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
var http = require("http");
2+
3+
var options = {
4+
"method": "POST",
5+
"hostname": "mockbin.com",
6+
"port": null,
7+
"path": "/har?",
8+
"headers": {
9+
"Cookie": "foo=bar; bar=baz"
10+
}
11+
};
12+
13+
var req = http.request(options, function (res) {
14+
var chunks = [];
15+
16+
res.on("data", function (chunk) {
17+
chunks.push(chunk);
18+
});
19+
20+
res.on("end", function () {
21+
var body = Buffer.concat(chunks);
22+
});
23+
});
24+
25+
req.end();
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
var request = require('request');
2+
3+
var options = {
4+
url: 'http://mockbin.com/har?baz=abc&foo=bar&foo=baz',
5+
formData: {
6+
"foo": "bar"
7+
},
8+
headers: {
9+
"Accept": "application/json",
10+
"Content-Type": "application/x-www-form-urlencoded",
11+
"Cookie": "foo=bar; bar=baz"
12+
}
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;
20+
});
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+
"Accept": "application/json",
7+
"X-Foo": "Bar"
8+
}
9+
}
10+
request.get(options, function(error, response, body) {
11+
if (error){
12+
//throw error here
13+
return;
14+
}
15+
console.log(body);
16+
// work with response and body here;
17+
});
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
var http = require("http");
2+
3+
var options = {
4+
"method": "GET",
5+
"hostname": "mockbin.com",
6+
"port": null,
7+
"path": "/har?key=value&baz=abc&foo=bar&foo=baz",
8+
"headers": {}
9+
};
10+
11+
var req = http.request(options, function (res) {
12+
var chunks = [];
13+
14+
res.on("data", function (chunk) {
15+
chunks.push(chunk);
16+
});
17+
18+
res.on("end", function () {
19+
var body = Buffer.concat(chunks);
20+
});
21+
});
22+
23+
req.end();
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
var http = require("http");
2+
3+
var options = {
4+
"method": "GET",
5+
"hostname": "mockbin.com",
6+
"port": null,
7+
"path": "/har?",
8+
"headers": {}
9+
};
10+
11+
var req = http.request(options, function (res) {
12+
var chunks = [];
13+
14+
res.on("data", function (chunk) {
15+
chunks.push(chunk);
16+
});
17+
18+
res.on("end", function () {
19+
var body = Buffer.concat(chunks);
20+
});
21+
});
22+
23+
req.end();

0 commit comments

Comments
 (0)