diff --git a/tests/unit/mock/mockResponse.js b/tests/unit/mock/mockResponse.js new file mode 100644 index 0000000..076df69 --- /dev/null +++ b/tests/unit/mock/mockResponse.js @@ -0,0 +1,70 @@ +var _ = require('lodash'), + beforeEach = require('mocha').beforeEach, + describe = require('mocha').describe, + expect = require('chai').expect, + helpers = require('./helpers'), + it = require('mocha').it, + mock = require('../../../lib/mock'); + +require('../../fixtures/config'); + +describe('Mock middleware response setup', function () { + 'use strict'; + + var dummyRouter, mockResponse, request, response; + + beforeEach(function () { + dummyRouter = helpers.dummyRouter; + dummyRouter.param = function () { + }; + dummyRouter.get = function () { + }; + dummyRouter.set = function () { + }; + dummyRouter.enabled = function () { + }; + dummyRouter.disabled = function () { + }; + + var dummyDataStore = new helpers.DummyDataStore(); + var requestHandlerList = mock({}, dummyRouter, dummyDataStore); + mockResponse = requestHandlerList[0]; + + request = helpers.createRequest(); + request.swagger.operation.responses = {}; + + response = helpers.createResponse(); + response.statusCode = undefined; + }); + + it('should call next request handler without req.swagger', function (done) { + request.swagger = undefined; + response.on('end', helpers.expectNoResponse(response, done)); + mockResponse(request, response, done); + }); + + it('should call next request handler when mock is disabled', function (done) { + dummyRouter.get = function (key) { + if (key === 'mock') { + return false; + } + }; + response.on('end', helpers.expectNoResponse(response, done)); + mockResponse(request, response, done); + }); + + it('should throw TypeError when req.swagger.operation.responses is undefined', function (done) { + response.statusCode = 200; + request.swagger.operation.responses = undefined; + response.on('end', helpers.expectNoResponse(response, done)); + expect(function () { + mockResponse(request, response, helpers.createUnexpectedRequestHandler(done)); + }).to.throw(TypeError, /Cannot read property '200' of undefined/); + done(); + }); + + it('should foo', function (done) { + response.on('end', helpers.expectNoResponse(response, done)); + mockResponse(request, response, done); + }); +});