Skip to content

Commit

Permalink
Merge pull request #30 from alexjamesbrown/master
Browse files Browse the repository at this point in the history
Parse query object from supplied query string in url
  • Loading branch information
howardabrams committed Jan 16, 2015
2 parents 5ea3e0d + 9ab491b commit ffcc082
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
5 changes: 5 additions & 0 deletions lib/mockRequest.js
Expand Up @@ -48,6 +48,11 @@ function createRequest(options) {
mockRequest.query = (options.query) ? options.query : {};
mockRequest.files = (options.files) ? options.files : {};

//parse query string from url to object
if (Object.keys(mockRequest.query).length == 0) {
mockRequest.query = require('querystring').parse(mockRequest.url.split('?')[1])
}

/**
* Function: header
*
Expand Down
29 changes: 29 additions & 0 deletions test/test-mockRequest.js
Expand Up @@ -167,3 +167,32 @@ exports['.param - returns value in correct order (query)'] = function(test) {

test.done();
};

exports['query object is parsed from url query string'] = function(test) {
var request = httpMocks.createRequest({
url: 'http://www.whatever.com?a=1&b=2&c=3'
});

test.equal(request.query['a'], '1');
test.equal(request.query['b'], '2');
test.equal(request.query['c'], '3');

test.done();
};

exports['query object is parsed from supplied options if provided'] = function(test) {
var request = httpMocks.createRequest({
url: 'http://www.whatever.com?a=1&b=2&c=3',
query: {
'a': '7',
'b': '8',
'c': '9'
}
});

test.equal(request.query['a'], '7');
test.equal(request.query['b'], '8');
test.equal(request.query['c'], '9');

test.done();
};

0 comments on commit ffcc082

Please sign in to comment.