Skip to content

Commit

Permalink
fixing inconsistent contract checking between main.coffee and admin p…
Browse files Browse the repository at this point in the history
…ortal
  • Loading branch information
mrak committed Aug 7, 2013
1 parent 46d0da5 commit 934310c
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 58 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 0.1.50

* bugfix: admin and programmatic APIs correctly parse incoming data

## 0.1.49

* updating styling of status page.
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "stubby",
"preferGlobal": true,
"version": "0.1.49",
"version": "0.1.50",
"author": {
"name": "Eric Mrak",
"email": "enmrak@gmail.com"
Expand Down
170 changes: 116 additions & 54 deletions spec/main.spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -7,66 +7,128 @@ sinon = require 'sinon'
waitsFor = require './helpers/waits-for'
assert = require 'assert'

xdescribe 'main', ->
sut = null
stopStubby = (finish) ->
if sut? then return sut.stop finish
finish()
describe 'main', ->
sut = null
stopStubby = (finish) ->
if sut? then return sut.stop finish
finish()

beforeEach (done) ->
finish = ->
sut = new (require('../src/main').Stubby)()
done()
beforeEach (done) ->
finish = ->
sut = new (require('../src/main').Stubby)()
done()

stopStubby finish
stopStubby finish

afterEach stopStubby
afterEach stopStubby

describe 'start', ->
beforeEach ->
options = {}
describe 'put', ->
it 'should return warning when the contract is violated', (done) ->
callback = sinon.spy()
sut.endpoints =
update: (_,__,cb) -> cb(null)

sut.put '42', {
request:
url: '/somewhere'
response:
status: 800
}, callback

waitsFor (-> callback.called), 'callback to have been called', 10, ->
assert callback.args[0][0] is "The supplied endpoint data couldn't be saved"
done()

describe 'callback', ->
it 'should treat the callback as optional', (done) ->
it 'should not return warning when the contract is upheld', (done) ->
callback = sinon.spy()
sut.start {}, callback
sut.endpoints =
update: (_,__,cb) -> cb(null)

sut.put '42', {
request:
url: '/somewhere'
response:
status: 200
}, callback

waitsFor (-> callback.called), 'callback to have been called', 10, ->
assert callback.args[0][0] is null
done()

describe 'post', ->
it 'should return warning when the contract is violated', (done) ->
callback = sinon.spy()

sut.post {
request:
url: '/somewhere'
response:
status: 800
}, callback

waitsFor (-> callback.called), 'callback to have been called', 1, done
waitsFor (-> callback.called), 'callback to have been called', 10, ->
assert callback.args[0][0] is "The supplied endpoint data couldn't be saved"
done()

it 'should take one parameter as a function', (done) ->
it 'should not return warning when the contract is upheld', (done) ->
callback = sinon.spy()
sut.start callback

waitsFor (-> callback.called), 'callback to have been called', 1, done

describe 'options', ->
it 'should default stub port to CLI port default', (done) ->
sut.start options, ->
assert options.stubs is defaults.stubs
done()

it 'should default admin port to CLI port default', (done) ->
sut.start options, ->
assert options.admin is defaults.admin
done()

it 'should default location to CLI default', (done) ->
sut.start options, ->
assert options.location is defaults.location
done()

it 'should default data to empty array', (done) ->
sut.start options, ->
assert options.data instanceof Array
assert options.data.length is 0
done()

it 'should default key to null', (done) ->
sut.start options, ->
assert options.key is defaults.key
done()

it 'should default cert to null', (done) ->
sut.start options, ->
assert options.cert is defaults.cert
done()

sut.post {
request:
url: '/somewhere'
response:
status: 200
}, callback

waitsFor (-> callback.called), 'callback to have been called', 10, ->
assert callback.args[0][0] is null
done()

describe 'start', ->
beforeEach ->
options = {}

describe 'callback', ->
it 'should treat the callback as optional', (done) ->
callback = sinon.spy()
sut.start {}, callback

waitsFor (-> callback.called), 'callback to have been called', 10, done

it 'should take one parameter as a function', (done) ->
callback = sinon.spy()
sut.start callback

waitsFor (-> callback.called), 'callback to have been called', 10, done

describe 'options', ->
it 'should default stub port to CLI port default', (done) ->
sut.start options, ->
assert options.stubs is defaults.stubs
done()

it 'should default admin port to CLI port default', (done) ->
sut.start options, ->
assert options.admin is defaults.admin
done()

it 'should default location to CLI default', (done) ->
sut.start options, ->
assert options.location is defaults.location
done()

it 'should default data to empty array', (done) ->
sut.start options, ->
assert options.data instanceof Array
assert options.data.length is 0
done()

it 'should default key to null', (done) ->
sut.start options, ->
assert options.key is defaults.key
done()

it 'should default cert to null', (done) ->
sut.start options, ->
assert options.cert is defaults.cert
done()
10 changes: 7 additions & 3 deletions src/main.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ http = require 'http'
https = require 'https'
contract = require './models/contract'

couldNotSave = "The supplied endpoint data couldn't be saved"

onListening = (portal, port, protocol = 'http', location) ->
out.status "#{portal} portal running at #{protocol}://#{location}:#{port}"
onError = (err, port, location) ->
Expand Down Expand Up @@ -40,6 +42,8 @@ setupStartOptions = (options, callback) ->

out.mute = options.mute

[options,callback]

createHttpsOptions = (options) ->
httpsOptions = {}
if options.key and options.cert
Expand All @@ -59,7 +63,7 @@ module.exports.Stubby = class Stubby
@adminPortal = null

start: (options = {}, callback = ->) => @stop =>
setupStartOptions options, callback
[options,callback] = setupStartOptions options, callback

if errors = contract options.data then return callback errors
if options.datadir? then @endpoints.datadir = options.datadir
Expand Down Expand Up @@ -95,7 +99,7 @@ module.exports.Stubby = class Stubby


post: (data, callback = ->) -> process.nextTick =>
if not contract data then return callback "The supplied endpoint data couldn't be saved"
if contract data then return callback couldNotSave
@endpoints.create data, callback

get: (id = (->), callback = id) -> process.nextTick =>
Expand All @@ -105,7 +109,7 @@ module.exports.Stubby = class Stubby
@endpoints.retrieve id, callback

put: (id, data, callback = ->) -> process.nextTick =>
if not contract data then return callback "The supplied endpoint data couldn't be saved"
if contract data then return callback couldNotSave
@endpoints.update id, data, callback

delete: (id = (->), callback = id) -> process.nextTick =>
Expand Down

0 comments on commit 934310c

Please sign in to comment.