Skip to content

Commit 3c0b342

Browse files
committed
unit test all the things!!!
1 parent f9a91f0 commit 3c0b342

17 files changed

Lines changed: 338 additions & 73 deletions

File tree

.travis.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
language: node_js
2+
node_js:
3+
- 0.10
4+
- 0.11
5+
- 0.12
6+
7+
script: npm test

bin/httpsnippet

100644100755
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ commander
1414
.option('-l, --language <language>', 'target language')
1515
.parse(process.argv);
1616

17-
if (commander.args.length == 0 || !commander.language) {
17+
if (commander.args.length === 0 || !commander.language) {
1818
commander.help();
1919
}
2020

21-
var sources = commander.args.map(function (file) {
21+
commander.args.map(function (file) {
2222
fs.stat(file, function (err, stats) {
2323
if (err) {
2424
return debug(err);

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"main": "./src/index.js",
66
"bin": "./bin/httpsnippet",
77
"scripts": {
8-
"test": "mocha -R spec"
8+
"test": "mocha --recursive --reporter spec"
99
},
1010
"repository": {
1111
"type": "git",
@@ -17,8 +17,8 @@
1717
},
1818
"homepage": "https://github.com/ahmadnassri/httpsnippet",
1919
"devDependencies": {
20-
"mocha": "^2.0.1",
21-
"should": "^4.2.0"
20+
"mocha": "^2.1.0",
21+
"should": "^5.0.1"
2222
},
2323
"dependencies": {
2424
"commander": "^2.6.0",

src/index.js

Lines changed: 56 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,73 @@
11
'use strict';
22

3+
var debug = require('debug')('httpsnippet');
34
var mapper = require('./mapper');
5+
var targets = require('./targets');
46
var url = require('url');
7+
var util = require('util');
58

6-
var targets = require('./targets');
9+
// constructor
10+
var HTTPSnippet = function (req, lang) {
11+
this.source = util._extend({}, req);
712

8-
module.exports = function (req, lang, opts) {
913
// construct query string object
10-
req.queryObj = {};
11-
req.headersObj = {};
14+
this.source.queryObj = {};
15+
this.source.headersObj = {};
16+
17+
if (this.source.url === undefined) {
18+
throw new Error('a request url is required');
19+
}
1220

1321
// construct query objects
14-
if (req.queryString && req.queryString.length) {
15-
req.queryString.map(mapper(req.queryObj));
22+
if (this.source.queryString && this.source.queryString.length) {
23+
debug('queryString found, constructing queryString pair map');
24+
25+
this.source.queryString.map(mapper(this.source.queryObj));
1626
}
1727

1828
// construct headers objects
19-
if (req.headers && req.headers.length) {
20-
req.headers.map(mapper(req.headersObj));
29+
if (this.source.headers && this.source.headers.length) {
30+
debug('headers found, constructing header pair map');
31+
32+
this.source.headers.map(mapper(this.source.headersObj));
2133
}
2234

23-
// deconstruct and reset the uri
24-
req.uriObj = url.parse(req.url);
35+
// deconstruct the uri
36+
this.source.uriObj = url.parse(this.source.url, true, true);
37+
38+
// search property is evil
39+
// prevents re-construction with new query values
40+
this.source.uriObj.search = null;
41+
42+
// merge all possible queryString values
43+
this.source.queryString = util._extend(this.source.uriObj.query, this.source.queryObj);
44+
45+
// update the query object
46+
this.source.uriObj.query = this.source.queryString;
47+
48+
// construct a full url
49+
this.source.fullUrl = url.format(this.source.uriObj);
50+
51+
// reset queryString in url
52+
this.source.uriObj.query = null;
53+
54+
// keep the base url clean of queryString
55+
this.source.url = url.format(this.source.uriObj);
56+
};
57+
58+
HTTPSnippet.prototype.getSource = function () {
59+
return this.source;
60+
};
61+
62+
// add each target as a prototype
63+
for (var lang in targets) {
64+
HTTPSnippet.prototype[lang] = targets[lang];
65+
}
66+
67+
// exports
2568

26-
// reset the query string
27-
req.uriObj.query = req.queryObj;
28-
req.url = url.format(req.uriObj);
69+
module.exports = HTTPSnippet;
2970

30-
return targets[lang].call(lang, req, opts);
71+
module.exports._targets = function () {
72+
return Object.keys(targets);
3173
};

src/targets/curl.js

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,35 +2,41 @@
22

33
var util = require('util');
44

5-
module.exports = function (req, opts) {
5+
module.exports = function (options) {
6+
var opts = util._extend({
7+
short: false,
8+
lineBreaks: true,
9+
indent: ' '
10+
}, options);
11+
612
var code = [];
713

8-
code.push(util.format('curl --request %s', req.method));
14+
code.push(util.format('curl %s %s', opts.short ? '-X' : '--request', this.source.method));
915

10-
code.push(util.format('--url "%s"', req.url));
16+
code.push(util.format('%s"%s"', opts.short ? '' : '--url ', this.source.fullUrl));
1117

12-
if (req.httpVersion === 'HTTP/1.0') {
13-
code.push('--http1.0');
18+
if (this.source.httpVersion === 'HTTP/1.0') {
19+
code.push(opts.short ? '-0' : '--http1.0');
1420
}
1521

1622
// construct cookies argument
17-
if (req.cookies && req.cookies.length) {
18-
var cookies = req.cookies.map(function (cookie) {
23+
if (this.source.cookies && this.source.cookies.length) {
24+
var cookies = this.source.cookies.map(function (cookie) {
1925
return encodeURIComponent(cookie.name) + '=' + encodeURIComponent(cookie.value);
2026
});
2127

22-
code.push(util.format('--cookie "%s"', cookies.join('; ')));
28+
code.push(util.format('%s "%s"', opts.short ? '-b' : '--cookie', cookies.join('; ')));
2329
}
2430

25-
if (req.headers && req.headers.length) {
26-
req.headers.map(function (header) {
27-
code.push(util.format('--header "%s: %s"', header.name, header.value));
31+
if (this.source.headers && this.source.headers.length) {
32+
this.source.headers.map(function (header) {
33+
code.push(util.format('%s "%s: %s"', opts.short ? '-H' : '--header', header.name, header.value));
2834
});
2935
}
3036

31-
if (req.postData) {
32-
code.push('--data ' + JSON.stringify(req.postData.text));
37+
if (this.source.postData) {
38+
code.push(util.format('%s %s', opts.short ? '-d' : '--data', JSON.stringify(this.source.postData.text)));
3339
}
3440

35-
return code.join(' \\\n ');
41+
return code.join(opts.lineBreaks ? ' \\\n' + opts.indent : ' ');
3642
};

src/targets/httpie.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,30 @@
22

33
var util = require('util');
44

5-
module.exports = function (req, opts) {
5+
module.exports = function (opts) {
66
var code = [];
77

8-
if (req.postData) {
9-
code.push(util.format('echo %s | ', JSON.stringify(req.postData.text)));
8+
if (this.source.postData) {
9+
code.push(util.format('echo %s | ', JSON.stringify(this.source.postData.text)));
1010
}
1111

12-
code.push(util.format('http %s %s%s', req.method, req.uriObj.hostname, req.uriObj.pathname));
12+
code.push(util.format('http %s %s%s', this.source.method, this.source.uriObj.hostname, this.source.uriObj.pathname));
1313

1414
// construct query params
15-
if (req.queryString && req.queryString.length) {
16-
req.queryString.map(function (query) {
15+
if (this.source.queryString && this.source.queryString.length) {
16+
this.source.queryString.map(function (query) {
1717
code.push(util.format('%s==%s', query.name, query.value));
1818
});
1919
}
2020

21-
if (req.headers && req.headers.length) {
22-
req.headers.map(function (header) {
21+
if (this.source.headers && this.source.headers.length) {
22+
this.source.headers.map(function (header) {
2323
code.push(util.format('%s:%s', header.name, header.value));
2424
});
2525
}
2626

27-
if (req.headers && req.headers.length) {
28-
req.headers.map(function (header) {
27+
if (this.source.headers && this.source.headers.length) {
28+
this.source.headers.map(function (header) {
2929
code.push(util.format('%s:%s', header.name, header.value));
3030
});
3131
}

src/targets/node.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,20 @@
22

33
var util = require('util');
44

5-
module.exports = function (req, opts) {
5+
module.exports = function (opts) {
66
var code = [];
77

88
var options = {
9-
method: req.method,
10-
hostname: req.uriObj.hostname,
11-
port: req.uriObj.port,
12-
path: req.uriObj.path,
13-
headers: req.headersObj
9+
method: this.source.method,
10+
hostname: this.source.uriObj.hostname,
11+
port: this.source.uriObj.port,
12+
path: this.source.uriObj.path,
13+
headers: this.source.headersObj
1414
};
1515

1616
// construct cookies argument
17-
if (req.cookies && req.cookies.length) {
18-
var cookies = req.cookies.map(function (cookie) {
17+
if (this.source.cookies && this.source.cookies.length) {
18+
var cookies = this.source.cookies.map(function (cookie) {
1919
return encodeURIComponent(cookie.name) + '=' + encodeURIComponent(cookie.value);
2020
});
2121

@@ -34,11 +34,11 @@ module.exports = function (req, opts) {
3434
'});'
3535
].join('\n ') + '\n});');
3636

37-
if (req.postData) {
38-
code.push(util.format('req.write(%s)', JSON.stringify(req.postData.text)));
37+
if (this.source.postData) {
38+
code.push(util.format('this.source.write(%s)', JSON.stringify(this.source.postData.text)));
3939
}
4040

41-
code.push('req.end();');
41+
code.push('this.source.end();');
4242

4343
return code.join('\n');
4444
};

src/targets/php.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,35 +2,35 @@
22

33
var util = require('util');
44

5-
module.exports = function (req, opts) {
5+
module.exports = function (opts) {
66
var code = [];
77

88
code.push('$curl = curl_init();');
99

1010
var options = [{
1111
escape: true,
1212
name: 'CURLOPT_port',
13-
value: req.uriObj.port
13+
value: this.source.uriObj.port
1414
}, {
1515
escape: true,
1616
name: 'CURLOPT_URL',
17-
value: req.url
17+
value: this.source.url
1818
}, {
1919
escape: false,
2020
name: 'CURLOPT_RETURNTRANSFER',
2121
value: 'true'
2222
}, {
2323
escape: false,
2424
name: 'CURLOPT_HTTP_VERSION',
25-
value: req.httpVersion === 'HTTP/1.0' ? 'CURL_HTTP_VERSION_1_0' : 'CURL_HTTP_VERSION_1_1'
25+
value: this.source.httpVersion === 'HTTP/1.0' ? 'CURL_HTTP_VERSION_1_0' : 'CURL_HTTP_VERSION_1_1'
2626
}, {
2727
escape: true,
2828
name: 'CURLOPT_CUSTOMREQUEST',
29-
value: req.method
29+
value: this.source.method
3030
}, {
3131
escape: true,
3232
name: 'CURLOPT_POSTFIELDS',
33-
value: req.postData ? req.postData.text : undefined
33+
value: this.source.postData ? this.source.postData.text : undefined
3434
}];
3535

3636
code.push('curl_setopt_array($curl, array(');
@@ -44,17 +44,17 @@ module.exports = function (req, opts) {
4444
});
4545

4646
// construct cookies
47-
if (req.cookies && req.cookies.length) {
48-
var cookies = req.cookies.map(function (cookie) {
47+
if (this.source.cookies && this.source.cookies.length) {
48+
var cookies = this.source.cookies.map(function (cookie) {
4949
return encodeURIComponent(cookie.name) + '=' + encodeURIComponent(cookie.value);
5050
});
5151

5252
curlopts.push(util.format('CURLOPT_COOKIE => "%s",', cookies.join('; ')));
5353
}
5454

5555
// construct cookies
56-
if (req.headers && req.headers.length) {
57-
var headers = req.headers.map(function (header) {
56+
if (this.source.headers && this.source.headers.length) {
57+
var headers = this.source.headers.map(function (header) {
5858
return util.format('"%s: %s"', header.name, header.value);
5959
});
6060

src/targets/wget.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,35 +2,35 @@
22

33
var util = require('util');
44

5-
module.exports = function (req, opts) {
5+
module.exports = function (opts) {
66
var code = [];
77

88
code.push('wget --quiet');
99

10-
code.push(util.format('--method %s', req.method));
10+
code.push(util.format('--method %s', this.source.method));
1111

1212
// construct cookies argument
13-
if (req.cookies && req.cookies.length) {
14-
var cookies = req.cookies.map(function (cookie) {
13+
if (this.source.cookies && this.source.cookies.length) {
14+
var cookies = this.source.cookies.map(function (cookie) {
1515
return encodeURIComponent(cookie.name) + '=' + encodeURIComponent(cookie.value);
1616
});
1717

1818
code.push(util.format('--header "Cookie: %s"', cookies.join('; ')));
1919
}
2020

21-
if (req.headers && req.headers.length) {
22-
req.headers.map(function (header) {
21+
if (this.source.headers && this.source.headers.length) {
22+
this.source.headers.map(function (header) {
2323
code.push(util.format('--header "%s: %s"', header.name, header.value));
2424
});
2525
}
2626

27-
if (req.postData) {
28-
code.push('--body-data ' + JSON.stringify(req.postData.text));
27+
if (this.source.postData) {
28+
code.push('--body-data ' + JSON.stringify(this.source.postData.text));
2929
}
3030

3131
code.push('--output-document');
3232

33-
code.push(util.format('- "%s"', req.url));
33+
code.push(util.format('- "%s"', this.source.url));
3434

3535
return code.join(' \\\n ');
3636
};

0 commit comments

Comments
 (0)