Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added baseUrl property #150

Merged
merged 1 commit into from
Oct 11, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ option | description | default value
`method`| request HTTP method | 'GET'
`url` | request URL | ''
`originalUrl` | request original URL | `url`
`baseUrl` | request base URL | `url`
`path` | request path | ''
`params` | object hash with params | {}
`session` | object hash with session values | `undefined`
Expand Down
1 change: 1 addition & 0 deletions lib/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ declare module 'node-mocks-http' {
method?: RequestMethod;
url?: string;
originalUrl?: string;
baseUrl?: string;
path?: string;
params?: Params;
session?: Session;
Expand Down
21 changes: 20 additions & 1 deletion lib/mockRequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
* method - The method value, see <mockRequest._setMethod>
* url - The url value, see <mockRequest._setURL>
* originalUrl - The originalUrl value, see <mockRequest._setOriginalUrl>
* baseUrl - The baseUrl value, see <mockRequest._setBaseUrl>
* params - The parameters, see <mockRequest._setParam>
* body - The body values, , see <mockRequest._setBody>
*/
Expand All @@ -35,7 +36,7 @@ var accepts = require('accepts');
var EventEmitter = require('events').EventEmitter;

var standardRequestOptions = [
'method', 'url', 'originalUrl', 'path', 'params', 'session', 'cookies', 'headers', 'body', 'query', 'files'
'method', 'url', 'originalUrl', 'baseUrl', 'path', 'params', 'session', 'cookies', 'headers', 'body', 'query', 'files'
];

function convertKeysToLowerCase(map) {
Expand Down Expand Up @@ -63,6 +64,7 @@ function createRequest(options) {
mockRequest.method = options.method ? options.method : 'GET';
mockRequest.url = options.url || options.path || '';
mockRequest.originalUrl = options.originalUrl || mockRequest.url;
mockRequest.baseUrl = options.baseUrl || mockRequest.url;
mockRequest.path = options.path ||
((options.url ? url.parse(options.url).pathname : ''));
mockRequest.params = options.params ? options.params : {};
Expand Down Expand Up @@ -320,6 +322,23 @@ function createRequest(options) {
mockRequest.url = value;
};

/**
* Function: _setBaseUrl
*
* Sets the URL value that the client gets when the called the 'baseUrl'
* property.
*
* Parameters:
*
* value - The request base path, e.g. /my-route
*
* Note: We don't validate the string. We just return it. Typically, these
* do not include hostname, port or that part of the URL.
*/
mockRequest._setBaseUrl = function(value) {
mockRequest.baseUrl = value;
};

/**
* Function: _setOriginalUrl
*
Expand Down
26 changes: 26 additions & 0 deletions test/lib/mockRequest.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ describe('mockRequest', function() {
expect(request.method).to.equal('GET');
expect(request.url).to.equal('');
expect(request.originalUrl).to.equal(request.url);
expect(request.baseUrl).to.equal(request.url);
expect(request.path).to.equal('');
expect(request.params).to.deep.equal({});
expect(request.session).to.not.exist;
Expand Down Expand Up @@ -83,6 +84,7 @@ describe('mockRequest', function() {
request = mockRequest.createRequest(options);
expect(request.url).to.equal(options.url);
expect(request.originalUrl).to.equal(options.url);
expect(request.baseUrl).to.equal(options.url);
});

it('should set .url automatically', function() {
Expand All @@ -96,6 +98,15 @@ describe('mockRequest', function() {
expect(request.url).to.equal(expectedUrl);
});

it('should set .baseUrl to options.baseUrl', function() {
var options = {
baseUrl: '/this'
};

request = mockRequest.createRequest(options);
expect(request.baseUrl).to.equal(options.baseUrl);
});

it('should set .originalUrl to options.originalUrl', function() {
var options = {
originalUrl: '/this/is/a/url'
Expand Down Expand Up @@ -616,6 +627,21 @@ describe('mockRequest', function() {

});

describe('._setBaseUrl()', function() {

it('should set baseUrl, when called with value', function() {
var value = '/path';
request._setBaseUrl(value);
expect(request.baseUrl).to.equal(value);
});

it('should unset baseUrl, when called with no arguments', function() {
request._setBaseUrl();
expect(request.baseUrl).to.not.exist;
});

});

describe('._setOriginalUrl()', function() {

it('should set originalUrl, when called with value', function() {
Expand Down