Skip to content

Commit

Permalink
Merge branch 'hotfix/1.2.7' into 1.x
Browse files Browse the repository at this point in the history
  • Loading branch information
Johnny Estilles committed Mar 24, 2015
2 parents 47a907b + 42a3d07 commit ee5bd61
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 11 deletions.
23 changes: 22 additions & 1 deletion lib/mockRequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,13 @@ function createRequest(options) {
mockRequest.url = (options.url) ? options.url : '';
mockRequest.path = (options.url) ? url.parse(options.url).pathname : '';
mockRequest.params = (options.params) ? options.params : {};
mockRequest.session = (options.session) ? options.session : {};
if (options.session) {
mockRequest.session = options.session;
}
mockRequest.cookies = (options.cookies) ? options.cookies : {};
if (options.signedCookies) {
mockRequest.signedCookies = options.signedCookies;
}
mockRequest.headers = (options.headers) ? options.headers : {};
mockRequest.body = (options.body) ? options.body : {};
mockRequest.query = (options.query) ? options.query : {};
Expand Down Expand Up @@ -138,6 +143,9 @@ function createRequest(options) {
* @param value The value associated with the variable
*/
mockRequest._setSessionVariable = function(variable, value) {
if (typeof mockRequest.session !== 'object') {
mockRequest.session = {};
}
mockRequest.session[variable] = value;
};

Expand All @@ -151,6 +159,19 @@ function createRequest(options) {
mockRequest.cookies[variable] = value;
};

/**
* Sets a variable that is stored in the signed cookies.
*
* @param variable The variable to store in the signed cookies
* @param value The value associated with the variable
*/
mockRequest._setSignedCookiesVariable = function(variable, value) {
if (typeof mockRequest.signedCookies !== 'object') {
mockRequest.signedCookies = {};
}
mockRequest.signedCookies[variable] = value;
};

/**
* Sets a variable that is stored in the headers.
*
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"author": "Howard Abrams <[email protected]> (http://www.github.com/howardabrams)",
"name": "node-mocks-http",
"description": "Mock 'http' objects for testing Express routing functions",
"version": "1.2.6",
"version": "1.2.7",
"homepage": "http://www.github.com/howardabrams/node-mocks-http",
"license" : "MIT",
"keywords": [
Expand Down
61 changes: 52 additions & 9 deletions test/test-mockRequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,16 +111,27 @@ exports['setBody - Simple verification'] = function(test) {
test.done();
};

exports['body - Unset value'] = function(test) {
exports['Object creation - No values set'] = function(test) {
var request = httpMocks.createRequest();
test.equal(undefined, request.body.user);

test.equal(request.method, 'GET');
test.equal(request.url, '');
test.equal(request.path, '');
test.deepEqual(request.params, {});
test.equal(typeof request.session, 'undefined');
test.deepEqual(request.cookies, {});
test.equal(typeof request.signedCookies, 'undefined');
test.deepEqual(request.headers, {});
test.deepEqual(request.body, {});
test.deepEqual(request.query, {});
test.deepEqual(request.files, {});
test.done();
};


exports['Object creation - All values set'] = function(test) {
exports['Object creation - Most values set'] = function(test) {

var methodValue = "PUT";
var methodValue = 'PUT';
var idValue = 34;
var urlValue = 'http://localhost:6522/blingbling';
var usernameValue = "mittens";
Expand All @@ -132,16 +143,30 @@ exports['Object creation - All values set'] = function(test) {
id: idValue,
sort: 'asc'
},
session: {},
cookies: {
name: 'value'
},
signedCookies: {
name: 'value'
},
headers: {
name: 'value'
},
body: {
username: usernameValue,
email: '[email protected]'
}
});

test.equal(methodValue, request.method);
test.equal(urlValue, request.url);
test.equal(idValue, request.params.id);
test.equal(usernameValue, request.body.username);
test.equal(request.method, methodValue);
test.equal(request.url, urlValue);
test.equal(request.params.id, idValue);
test.deepEqual(request.session, {});
test.equal(request.cookies.name, 'value');
test.equal(request.signedCookies.name, 'value');
test.equal(request.header('name'), 'value');
test.equal(request.body.username, usernameValue);
test.done();
};

Expand Down Expand Up @@ -249,4 +274,22 @@ exports['path(pathname) has to be parsed from url'] = function(test) {
test.equal(request.path, '/iamthepath');

test.done();
}
};

exports['session - setting session variable using _setSessionVariable()'] = function(test) {
var request = httpMocks.createRequest();
request._setSessionVariable('name', 'value');

test.equal(request.session.name, 'value');

test.done();
};

exports['signedCookies - setting signed cookies variable using _setSignedCookiesVariable()'] = function(test) {
var request = httpMocks.createRequest();
request._setSignedCookiesVariable('name', 'value');

test.equal(request.signedCookies.name, 'value');

test.done();
};

0 comments on commit ee5bd61

Please sign in to comment.