Skip to content

Commit

Permalink
Updates to Sails request factory
Browse files Browse the repository at this point in the history
 -- Add req.originalUrl
 -- Compute req.path
 -- Compute req.query based on path
 -- Fix wantsJSON to allow `false` value, FWIW

refs #2732
  • Loading branch information
sgress454 committed Jan 20, 2016
1 parent 9830ca6 commit 121f3fe
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 26 deletions.
19 changes: 15 additions & 4 deletions lib/router/req.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
var _ = require('lodash');
var defaultsDeep = require('merge-defaults');
var MockReq = require('mock-req');

var parseurl = require('parseurl');

/**
* Factory which builds generic Sails request object (i.e. `req`).
Expand All @@ -20,10 +20,20 @@ var MockReq = require('mock-req');
*/

module.exports = function buildRequest (_req) {

// Make sure _req is not undefined
_req = _req||{};

// Start our request object, which will be built by inheriting/transforming
// properties of _req and adding some spice of our own
var req;

// Attempt to parse the URL in _req, so that we can get the querystring
// and path
var parsedUrl;
try {parsedUrl = parseurl(_req) || {};}
catch (e) {parsedUrl = {};}

// If `_req` appears to be a stream (duck-typing), then don't try
// and turn it into a mock stream again.
if (typeof _req === 'object' && _req.read) {
Expand Down Expand Up @@ -87,7 +97,7 @@ module.exports = function buildRequest (_req) {
// Provide defaults for other request state and methods
req = defaultsDeep(req, {
params: [],
query: (_req && _req.query) || {},
query: (_req && _req.query) || require('querystring').parse(parsedUrl.query) || {},
body: (_req && _req.body) || {},
param: function(paramName, defaultValue) {

Expand All @@ -114,9 +124,10 @@ module.exports = function buildRequest (_req) {
file: function (){
return res.send(500, 'Streaming file uploads via `req.file()` are only available over HTTP with Skipper.');
},
wantsJSON: (_req && _req.wantsJSON) || true,
wantsJSON: (_req && _req.wantsJSON === false) ? false : true,
method: 'GET',
path: _req.url
originalUrl: _req.originalUrl || _req.url,
path: _req.path || parsedUrl.pathname
}, _req||{});

return req;
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
"method-override": "2.3.5",
"mock-req": "0.2.0",
"mock-res": "0.3.0",
"parseurl": "^1.3.1",
"pluralize": "1.2.1",
"prompt": "0.2.14",
"rc": "1.0.1",
Expand Down
99 changes: 77 additions & 22 deletions test/unit/req.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,35 +15,90 @@ var buildReq = require('root-require')('lib/router/req');
*/
describe('Base Request (`req`)', function (){

var req;
describe('with empty request', function() {

var req;

// Mock the request object.
before(function (){
req = buildReq();
req.should.be.an.Object;
this.req = req;
});

// Mock the request object.
before(function (){
req = buildReq();
req.should.be.an.Object;
this.req = req;
});

it('.body', function () {
req.body.should.be.an.Object;
req.body.should.be.empty;
});

it('.params', function () {
req.params.should.be.an.Array;
req.params.should.be.empty;
});
it('.body', function () {
req.body.should.be.an.Object;
req.body.should.be.empty;
});

it('.params', function () {
req.params.should.be.an.Array;
req.params.should.be.empty;
});

it('.query', function (){
req.query.should.be.an.Object;
req.query.should.be.empty;
});

it('.query', function (){
req.query.should.be.an.Object;
req.query.should.be.empty;
it('.param()', function () {
should(req.param('foo'))
.not.be.ok;
});
});

it('.param()', function () {
should(req.param('foo'))
.not.be.ok;

describe('with url /hello?abc=123&foo=bar', function() {

var req;


// Mock the request object.
before(function (){
req = buildReq({url: '/hello?abc=123&foo=bar'});
req.should.be.an.Object;
this.req = req;
});


it('.body', function () {
req.body.should.be.an.Object;
req.body.should.be.empty;
});

it('.params', function () {
req.params.should.be.an.Array;
req.params.should.be.empty;
});

it('.query', function (){
req.query.should.be.an.Object;
req.query.should.have.property('abc', '123');
req.query.should.have.property('foo', 'bar');
});

it('.param()', function () {
should(req.param('abc')).equal('123');
should(req.param('foo')).equal('bar');
});

it('.path', function() {
req.path.should.be.an.String;
req.path.should.equal('/hello');
});

it('.url', function() {
req.url.should.be.an.String;
req.url.should.equal('/hello?abc=123&foo=bar');
});

it('.originalUrl', function() {
req.originalUrl.should.be.an.String;
req.originalUrl.should.equal('/hello?abc=123&foo=bar');
});

});
});

});

0 comments on commit 121f3fe

Please sign in to comment.