Skip to content

Commit 65d4eab

Browse files
committed
queryString parameters proper usage with test
1 parent 2245a9f commit 65d4eab

2 files changed

Lines changed: 35 additions & 8 deletions

File tree

‎src/targets/httpie.js‎

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

55
module.exports = function (options) {
66
var opts = util._extend({
7+
queryParams: false,
78
body: false,
89
cert: false,
910
headers: false,
@@ -62,13 +63,27 @@ module.exports = function (options) {
6263
flags.push(util.format('--timeout=%s', opts.timeout));
6364
}
6465

65-
code.push(util.format('http %s%s %s', flags.length ? flags.join(' ') + ' ' : '', this.source.method, this.source.url));
66+
code.push(util.format('http %s%s %s', flags.length ? flags.join(' ') + ' ' : '', this.source.method, opts.queryParams ? this.source.url : this.source.fullUrl));
67+
68+
var self = this;
6669

6770
// construct query params
68-
if (this.source.queryString && this.source.queryString.length) {
69-
this.source.queryString.map(function (query) {
70-
code.push(util.format('%s==%s', query.name, query.value));
71-
});
71+
if (opts.queryParams && this.source.queryString) {
72+
var queryStringKeys = Object.keys(this.source.queryString);
73+
74+
if (queryStringKeys.length) {
75+
queryStringKeys.map(function (name) {
76+
var value = self.source.queryString[name];
77+
78+
if (util.isArray(value)) {
79+
value.map(function (val) {
80+
code.push(util.format('%s==%s', name, val));
81+
});
82+
} else {
83+
code.push(util.format('%s==%s', name, value));
84+
}
85+
});
86+
}
7287
}
7388

7489
// construct headers

‎test/targets/httpie.js‎

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ describe('HTTPie', function () {
1111
});
1212

1313
result.should.be.a.String;
14-
result.should.eql('echo "{\\"foo\\": \\"bar\\"}" | http POST http://httpconsole.com/debug Content-Type:application/json Cookie:bar=baz');
14+
result.should.eql('echo "{\\"foo\\": \\"bar\\"}" | http POST http://httpconsole.com/debug?foo=bar Content-Type:application/json Cookie:bar=baz');
1515

1616
done();
1717
});
@@ -23,7 +23,7 @@ describe('HTTPie', function () {
2323
});
2424

2525
result.should.be.a.String;
26-
result.should.eql('echo "{\\"foo\\": \\"bar\\"}" | http --verbose POST http://httpconsole.com/debug Content-Type:application/json Cookie:bar=baz');
26+
result.should.eql('echo "{\\"foo\\": \\"bar\\"}" | http --verbose POST http://httpconsole.com/debug?foo=bar Content-Type:application/json Cookie:bar=baz');
2727

2828
done();
2929
});
@@ -47,7 +47,19 @@ describe('HTTPie', function () {
4747
});
4848

4949
result.should.be.a.String;
50-
result.replace(/\\\n/g, '').should.eql('echo "{\\"foo\\": \\"bar\\"}" | @http POST http://httpconsole.com/debug @Content-Type:application/json @Cookie:bar=baz');
50+
result.replace(/\\\n/g, '').should.eql('echo "{\\"foo\\": \\"bar\\"}" | @http POST http://httpconsole.com/debug?foo=bar @Content-Type:application/json @Cookie:bar=baz');
51+
52+
done();
53+
});
54+
55+
it('should use queryString parameters', function (done) {
56+
var result = new HTTPSnippet(fixtures.query).httpie({
57+
indent: false,
58+
queryParams: true
59+
});
60+
61+
result.should.be.a.String;
62+
result.replace(/\\\n/g, '').should.eql('http POST http://httpconsole.com/debug key==value baz==abc foo==bar foo==baz');
5163

5264
done();
5365
});

0 commit comments

Comments
 (0)