Skip to content
Open
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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 <cordell.evan@gmail.com>",
"main": "lib/api-mock.js",
Expand Down
5 changes: 4 additions & 1 deletion src/api-mock.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion src/cors-support.coffee
Original file line number Diff line number Diff line change
@@ -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()
Expand Down
15 changes: 15 additions & 0 deletions test/unit/api-mock-test.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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 ()->
Expand Down