Skip to content

Commit bf3e22e

Browse files
committed
additional httpie arguments + test
1 parent 16c33a4 commit bf3e22e

3 files changed

Lines changed: 89 additions & 7 deletions

File tree

‎src/targets/httpie.js‎

Lines changed: 58 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,65 @@ var util = require('util');
44

55
module.exports = function (options) {
66
var opts = util._extend({
7-
indent: ' '
7+
body: false,
8+
cert: false,
9+
headers: false,
10+
indent: ' ',
11+
pretty: false,
12+
print: false,
13+
short: false,
14+
style: false,
15+
timeout: false,
16+
verbose: false,
17+
verify: false
818
}, options);
919

1020
var code = [];
1121

22+
// start with body pipe
1223
if (this.source.postData) {
1324
code.push(util.format('echo %s | ', JSON.stringify(this.source.postData.text)));
1425
}
1526

16-
code.push(util.format('http %s %s%s', this.source.method, this.source.uriObj.hostname, this.source.uriObj.pathname));
27+
var flags = [];
28+
29+
if (opts.headers) {
30+
flags.push(opts.short ? '-h' : '--headers');
31+
}
32+
33+
if (opts.body) {
34+
flags.push(opts.short ? '-b' : '--body');
35+
}
36+
37+
if (opts.verbose) {
38+
flags.push(opts.short ? '-v' : '--verbose');
39+
}
40+
41+
if (opts.print) {
42+
flags.push(util.format('%s=%s', opts.short ? '-p' : '--print', opts.print));
43+
}
44+
45+
if (opts.verify) {
46+
flags.push(util.format('--verify=%s', opts.verify));
47+
}
48+
49+
if (opts.cert) {
50+
flags.push(util.format('--cert=%s', opts.cert));
51+
}
52+
53+
if (opts.pretty) {
54+
flags.push(util.format('--pretty=%s', opts.pretty));
55+
}
56+
57+
if (opts.style) {
58+
flags.push(util.format('--style=%s', opts.pretty));
59+
}
60+
61+
if (opts.timeout) {
62+
flags.push(util.format('--timeout=%s', opts.timeout));
63+
}
64+
65+
code.push(util.format('http %s%s %s', flags.length ? flags.join(' ') + ' ' : '', this.source.method, this.source.url));
1766

1867
// construct query params
1968
if (this.source.queryString && this.source.queryString.length) {
@@ -22,16 +71,20 @@ module.exports = function (options) {
2271
});
2372
}
2473

74+
// construct headers
2575
if (this.source.headers && this.source.headers.length) {
2676
this.source.headers.map(function (header) {
2777
code.push(util.format('%s:%s', header.name, header.value));
2878
});
2979
}
3080

31-
if (this.source.headers && this.source.headers.length) {
32-
this.source.headers.map(function (header) {
33-
code.push(util.format('%s:%s', header.name, header.value));
81+
// construct cookies argument
82+
if (this.source.cookies && this.source.cookies.length) {
83+
var cookies = this.source.cookies.map(function (cookie) {
84+
return encodeURIComponent(cookie.name) + '=' + encodeURIComponent(cookie.value);
3485
});
86+
87+
code.push(util.format('Cookie:%s', cookies.join('; ')));
3588
}
3689

3790
return code.join(opts.indent !== false ? ' \\\n' + opts.indent : ' ');

‎test/fixtures/short.json‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"method": "GET",
3+
"url": "http://httpconsole.com/echo"
4+
}

‎test/targets/httpie.js‎

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,32 @@ describe('httpie', function () {
1212
});
1313

1414
result.should.be.a.String;
15-
result.should.eql('echo "{\\"foo\\": \\"bar\\"}" | http POST httpconsole.com/debug Content-Type:application/json Content-Type:application/json');
15+
result.should.eql('echo "{\\"foo\\": \\"bar\\"}" | http POST http://httpconsole.com/debug Content-Type:application/json Cookie:bar=baz');
16+
17+
done();
18+
});
19+
20+
it('should ask for verbose output', function (done) {
21+
var result = new HTTPSnippet(fixtures.simple).httpie({
22+
indent: false,
23+
verbose: true
24+
});
25+
26+
result.should.be.a.String;
27+
result.should.eql('echo "{\\"foo\\": \\"bar\\"}" | http --verbose POST http://httpconsole.com/debug Content-Type:application/json Cookie:bar=baz');
28+
29+
done();
30+
});
31+
32+
it('should add flags', function (done) {
33+
var result = new HTTPSnippet(fixtures.short).httpie({
34+
indent: false,
35+
cert: 'foo',
36+
verbose: true
37+
});
38+
39+
result.should.be.a.String;
40+
result.should.eql('http --verbose --cert=foo GET http://httpconsole.com/echo');
1641

1742
done();
1843
});
@@ -23,7 +48,7 @@ describe('httpie', function () {
2348
});
2449

2550
result.should.be.a.String;
26-
result.replace(/\\\n/g, '').should.eql('echo "{\\"foo\\": \\"bar\\"}" | @http POST httpconsole.com/debug @Content-Type:application/json @Content-Type:application/json');
51+
result.replace(/\\\n/g, '').should.eql('echo "{\\"foo\\": \\"bar\\"}" | @http POST http://httpconsole.com/debug @Content-Type:application/json @Cookie:bar=baz');
2752

2853
done();
2954
});

0 commit comments

Comments
 (0)