Skip to content

Commit cd5cc0c

Browse files
committed
unit testing gone wild! url.query & url.search are problamatic
1 parent a6f45b1 commit cd5cc0c

2 files changed

Lines changed: 26 additions & 14 deletions

File tree

src/index.js

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

33
var debug = require('debug')('httpsnippet');
44
var mapper = require('./mapper');
5+
var qs = require('querystring');
56
var targets = require('./targets');
67
var url = require('url');
78
var util = require('util');
@@ -35,24 +36,24 @@ var HTTPSnippet = function (req, lang) {
3536
// deconstruct the uri
3637
this.source.uriObj = url.parse(this.source.url, true, true);
3738

38-
// search property is evil
39-
// prevents re-construction with new query values
40-
this.source.uriObj.search = null;
41-
4239
// merge all possible queryString values
4340
this.source.queryString = util._extend(this.source.uriObj.query, this.source.queryObj);
4441

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
42+
// reset uriObj values for a clean url
5243
this.source.uriObj.query = null;
44+
this.source.uriObj.search = null;
45+
this.source.uriObj.path = this.source.uriObj.pathname;
5346

5447
// keep the base url clean of queryString
5548
this.source.url = url.format(this.source.uriObj);
49+
50+
// update the uri object
51+
this.source.uriObj.query = this.source.queryString;
52+
this.source.uriObj.search = qs.stringify(this.source.queryString);
53+
this.source.uriObj.path = this.source.uriObj.pathname + '?' + this.source.uriObj.search;
54+
55+
// construct a full url
56+
this.source.fullUrl = url.format(this.source.uriObj);
5657
};
5758

5859
HTTPSnippet.prototype.getSource = function () {

test/index.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,14 @@ describe('HTTPSnippet', function () {
2424
host: 'httpconsole.com',
2525
hostname: 'httpconsole.com',
2626
href: 'http://httpconsole.com/debug',
27-
path: '/debug',
27+
path: '/debug?foo=bar',
2828
pathname: '/debug',
2929
port: null,
3030
protocol: 'http:',
31-
query: null,
32-
search: null,
31+
query: {
32+
foo: 'bar'
33+
},
34+
search: 'foo=bar',
3335
slashes: true
3436
});
3537

@@ -76,6 +78,15 @@ describe('HTTPSnippet', function () {
7678
done();
7779
});
7880

81+
it('should fix "path" property of "uriObj" to match queryString', function (done) {
82+
var req = new HTTPSnippet(fixtures.query).getSource();
83+
84+
req.uriObj.path.should.be.a.String;
85+
req.uriObj.path.should.eql('/debug?key=value&baz=abc&foo=bar&foo=baz');
86+
87+
done();
88+
});
89+
7990
it('should parse and queryString in the url into querString object', function (done) {
8091
var req = new HTTPSnippet(fixtures.query).getSource();
8192

0 commit comments

Comments
 (0)