diff --git a/README.md b/README.md index 67c3ab9..e13e11e 100644 --- a/README.md +++ b/README.md @@ -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` diff --git a/lib/index.d.ts b/lib/index.d.ts index 2e93224..2a70e03 100644 --- a/lib/index.d.ts +++ b/lib/index.d.ts @@ -37,6 +37,7 @@ declare module 'node-mocks-http' { method?: RequestMethod; url?: string; originalUrl?: string; + baseUrl?: string; path?: string; params?: Params; session?: Session; diff --git a/lib/mockRequest.js b/lib/mockRequest.js index b2412d3..9c5fc58 100644 --- a/lib/mockRequest.js +++ b/lib/mockRequest.js @@ -25,6 +25,7 @@ * method - The method value, see * url - The url value, see * originalUrl - The originalUrl value, see + * baseUrl - The baseUrl value, see * params - The parameters, see * body - The body values, , see */ @@ -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) { @@ -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 : {}; @@ -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 * diff --git a/test/lib/mockRequest.spec.js b/test/lib/mockRequest.spec.js index a419a3d..f65e7b8 100644 --- a/test/lib/mockRequest.spec.js +++ b/test/lib/mockRequest.spec.js @@ -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; @@ -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() { @@ -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' @@ -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() {