diff --git a/package.json b/package.json index 01d3b2d..67974a8 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "api-mock", - "version": "0.1.0", + "version": "0.1.1", "description": "A mock server generated from your API Blueprint.", "author": "Evan Cordell ", "main": "lib/api-mock.js", diff --git a/src/api-mock.coffee b/src/api-mock.coffee index 70369cf..b743f3c 100644 --- a/src/api-mock.coffee +++ b/src/api-mock.coffee @@ -28,7 +28,10 @@ class ApiMock ) if !@configuration.options['cors-disable'] - corsSupport = new CorsSupport @app + corsSupport = new CorsSupport( + @app, + headers: @configuration.options['custom-headers'] + ) run: () -> app = @app diff --git a/src/cors-support.coffee b/src/cors-support.coffee index 91a096e..7f0f339 100644 --- a/src/cors-support.coffee +++ b/src/cors-support.coffee @@ -1,13 +1,16 @@ winston = require 'winston' class CorsSupport - constructor: (app) -> + constructor: (app, configuration) -> options = origin: '*' methods: 'GET, PUT, POST, PATCH, DELETE, TRACE, OPTIONS' headers: 'Origin, X-Requested-With, Content-Type, Accept, Authorization, Referer, Prefer' + if configuration.headers + options.headers = options.headers + ', ' + configuration.headers + app.all '*', (req, res, next) -> unless req.get('Origin') next() diff --git a/test/unit/api-mock-test.coffee b/test/unit/api-mock-test.coffee index 20d0165..17e0542 100644 --- a/test/unit/api-mock-test.coffee +++ b/test/unit/api-mock-test.coffee @@ -73,6 +73,21 @@ describe 'ApiMock class', () -> assert.ok api_mock.configuration.options.port == 3005 assert.notOk CorsSupportStub.called + describe 'with custom Access-Control-Allow-Headers', () -> + + beforeEach ()-> + configuration = + blueprintPath: './test/fixtures/single-get.apib', + options: + port: 3005 + 'custom-headers': 'x-custom-header' + 'cors-disable': false + + it 'should add custom header to configuration', () -> + api_mock = new ApiMock(configuration) + assert.ok api_mock.configuration.options['custom-headers'] == 'x-custom-header' + assert.ok CorsSupportStub.called + describe 'with invalid configuration', () -> beforeEach ()->