diff --git a/lib/controller/MockController.js b/lib/controller/MockController.js index bb44496..2a72c95 100644 --- a/lib/controller/MockController.js +++ b/lib/controller/MockController.js @@ -1,6 +1,7 @@ 'use strict'; +var nodePath = require('path'); var Utils = require('../Utils'); var extend = require('util')._extend; var ejs = require('ejs'); @@ -248,7 +249,7 @@ MockController.prototype = extend(MockController.prototype, { try { if (this._setMimeType(options.res, options.expectedResponse.type)) { // add statusCode defined by filename - var status = this._getHttpStatusFromFileName(options.responseFilePath); + var status = this._getHttpStatusFromFilePath(options.responseFilePath); if (status && options.res.statusCode === 200) { options.res.statusCode = status; } @@ -297,7 +298,7 @@ MockController.prototype = extend(MockController.prototype, { if (outStr) { // add statusCode defined by filename - var status = this._getHttpStatusFromFileName(options.responseFilePath); + var status = this._getHttpStatusFromFilePath(options.responseFilePath); if (status && options.res.statusCode === 200) { options.res.statusCode = status; } @@ -414,7 +415,7 @@ MockController.prototype = extend(MockController.prototype, { * @private */ _sendError: function (options) { - var status = this._getHttpStatusFromFileName(options.expectedResponse.name) || 500; + var status = this._getHttpStatusFromFilePath(options.expectedResponse.name) || 500; // add statusCode defined by filename if (options.res.statusCode === 200) { @@ -814,13 +815,13 @@ MockController.prototype = extend(MockController.prototype, { }, /** - * @method _getHttpStatusFromFileName - * @param {string} fileName + * @method _getHttpStatusFromFilePath + * @param {string} filePath - absolute path to file * @returns {number|void} HTTP status code * @private */ - _getHttpStatusFromFileName: function (fileName) { - var reg = (/([\w]+)(-)([0-9]{3})/).exec(fileName); + _getHttpStatusFromFilePath: function (filePath) { + var reg = (/([\w]+)(-)([0-9]{3})/).exec(nodePath.basename(filePath)); if (reg === null) { return; diff --git a/package.json b/package.json index 499501c..1c8c8ea 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "node-mock-server", - "version": "0.23.2", + "version": "0.23.3", "description": "File based Node REST API mock server", "email": "simon.mollweide@web.de", "author": "Simon Mollweide ", diff --git a/test/tests-mock-server.js b/test/tests-mock-server.js index d75e1d6..86d4974 100755 --- a/test/tests-mock-server.js +++ b/test/tests-mock-server.js @@ -22,6 +22,22 @@ module.exports = function(serverOptions, _getFile) { var pathExpected = './test/expected/mock-server', baseUrl = serverOptions.urlBase + serverOptions.urlPath; + describe('method _getHttpStatusFromFilePath', function () { + var getStatus = MockController.prototype._getHttpStatusFromFilePath; + + it('undefined', function () { + assert.deepEqual(getStatus('/Users/test/projects/node-mock-server/test/../demo/rest/_fallbacks/#/OPTIONS/mock/success.json'), undefined); + }); + + it('300', function () { + assert.deepEqual(getStatus('/Users/test/projects/node-mock-server/test/../demo/rest/_fallbacks/#/OPTIONS/mock/success-300.json'), 300); + }); + + it('PR-326', function () { + assert.deepEqual(getStatus('/Users/test/projects/node-mock-server/test/../demo/rest/_fallbacks/#/OPTIONS/mock/PR-326/success.json'), undefined); + }); + }); + describe('method _getExpectedResponse', function () { var func = MockController.prototype._getExpectedResponse;