Skip to content
This repository has been archived by the owner on Feb 19, 2022. It is now read-only.

Commit

Permalink
Change publically exported API
Browse files Browse the repository at this point in the history
default export no longer creates or starts a server.
- .connect creates a connect app
- .server creates a connect app and http server, starts it listening
  • Loading branch information
grncdr committed Apr 10, 2012
1 parent 4331a61 commit cb58900
Show file tree
Hide file tree
Showing 9 changed files with 132 additions and 169 deletions.
36 changes: 8 additions & 28 deletions lazorse.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,18 @@

METHODS = ['DELETE', 'GET', 'HEAD', 'PATCH', 'POST', 'PUT', 'OPTIONS']

connect = require 'connect'
parser = require 'uri-template'
require './lib/uri-template-matchpatch'
errors = require './lib/errors'
parser = require 'uri-template'
connect = require 'connect'
# Used for loading example request JSON files
{readFileSync} = require 'fs'

module.exports = exports = (builder) ->
###
The main export is a function that constructs a ``LazyApp`` instance and
starts it listening on the port defined by the apps ``port`` property (default
is 3000)
###
wrappedBuilder = ->
for mw in ['logger', 'favicon', 'bodyParser']
@before @findResource, mw
builder.call @
app = new LazyApp wrappedBuilder
connect().use(app).listen app.port
module.exports = exports = (builder) -> new LazyApp builder

exports.connect = (builder) -> connect().use(exports(builder))

exports.app = (builder) ->
### Construct an app without starting a server ###
new LazyApp builder
exports.server = (address, builder) ->
app = exports.connect(builder)
require('http').createServer(app).listen address.port or 0, address.host

class LazyApp
###
Expand All @@ -50,18 +39,9 @@ class LazyApp
###

constructor: (builder) ->
###
The constructor takes a `builder` function as it's sole argument. This
function will be called in the context of the app object `before` the
default index, examples, and parameters resources are created. The builder
can change the location of these resources by setting ``@indexPath``,
``@examplePath``, and ``@parameterPath`` respectively, or disable any of
them entirely by setting the path to ``false``.
###
app = @ # Needed by some of the callbacks defined here

# Defaults
@port = 3000
@renderers = {}
@renderers[type] = func for type, func of require './lib/render'

Expand Down
48 changes: 22 additions & 26 deletions test/basics.test.coffee
Original file line number Diff line number Diff line change
@@ -1,43 +1,39 @@
lazorse = require '../'
client = require('./client')
client = require './client'
assert = require 'assert'

# Test server
server = lazorse ->
@_stack.shift() # drop logger
@port = 0
for method in client.METHODS
resource = {}
uri = "/#{method}me"
resource[uri] = {}
resource[uri][method] = if method is 'HEAD'
-> @res.end()
else
-> @ok "#{method}"
describe "A basic app", ->
server = lazorse.server port: 0, host: '127.0.0.1', ->
for method in client.METHODS
resource = {}
uri = "/#{method}me"
resource[uri] = {}
resource[uri][method] = if method is 'HEAD'
-> @res.end()
else
-> @ok "#{method}"

@resource resource
@resource resource

@resource '/indexed':
shortName: 'discoverableResource'
GET: -> @ok 'found it'
@resource '/indexed':
shortName: 'discoverableResource'
GET: -> @ok 'found it'

@resource '/404':
GET: -> @error 'NotFound', 'string error name', 'works'
@resource '/404':
GET: -> @error 'NotFound', 'string error name', 'works'

@resource '/500':
GET: -> @next new Error "I'm an unknown error type"
@resource '/500':
GET: -> @next new Error "I'm an unknown error type"

@resource '/422':
GET: -> @error 'InvalidParameter', 'bad param'
@resource '/422':
GET: -> @error 'InvalidParameter', 'bad param'

# Tests
describe "A basic app", ->
before -> client.usePort server.address().port
after -> server.close()

it "has an index with three resources", (done) ->
client.GET '/', (res, resources) ->
assert.equal res.statusCode, 200
assert.equal res.statusCode, 200, res.headers
assert.equal resources.length, 3
assert 'discoverableResource' in (r.shortName for r in resources)
done()
Expand Down
7 changes: 3 additions & 4 deletions test/before.test.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@ Tests that the @before builder function inserts new middleware into the correct
position in the internal middleware stack
###

client = require './client'
lazorse = require '../'
client = require('./client')
assert = require 'assert'

describe "An app with @before middleware", ->
stack = ['findResource', 'coerceParams', 'dispatchHandler', 'renderResponse']
server = lazorse ->
@_stack.shift() # drop logger
@port = 0

server = lazorse.server port: 0, host: '127.0.0.1', ->
response_data = word: 'up'

# Assert various attributes of the req/res state before each middleware
Expand Down
32 changes: 16 additions & 16 deletions test/client.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@ port = 0
exports.usePort = (newport) -> port = newport

exports.METHODS = ['GET', 'POST', 'DELETE', 'PUT', 'HEAD']
for method in exports.METHODS
do (method) ->
exports[method] = (path, opts={}, cb) ->
if not cb then cb = opts
accept='application/json'
req = {method, host: 'localhost', port, path, headers: {accept}}
req.headers[k] = v for k, v of opts.headers if opts.headers
rawBody = ""
req = http.request req, (res) ->
res.on 'data', (chnk) -> rawBody += chnk
res.on 'end', ->
cb res, JSON.parse(rawBody or null)
if body = opts.body
if 'string' != typeof body
body = JSON.stringify body
req.end body
for method in exports.METHODS then do (method) ->
exports[method] = (path, opts={}, cb) ->
if not cb then cb = opts
accept='application/json'
req = {method, host: 'localhost', port, path, headers: {accept}}
req.headers[k] = v for k, v of opts.headers if opts.headers
rawBody = ""
req = http.request req, (res) ->
res.on 'data', (chnk) -> rawBody += chnk
res.on 'end', ->
cb res, JSON.parse(rawBody or null)

if body = opts.body
if 'string' != typeof body
body = JSON.stringify body
req.end body
8 changes: 3 additions & 5 deletions test/coercions.test.coffee
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
lazorse = require('../')
client = require('./client')
client = require './client'
lazorse= require '../'
assert = require 'assert'

describe "Using coercions", ->
server = lazorse ->
@_stack.shift() # drop logger
@port = 0
server = lazorse.server port: 0, host: '127.0.0.1', ->

@resource '/hello/{name}': GET: -> @ok "Hello #{@name}"

Expand Down
26 changes: 12 additions & 14 deletions test/customErrors.test.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,22 @@ lazorse = require '../'
client = require('./client')
assert = require('assert')

TeapotError = ->
@code = 418
@message = "I'm a teapot"
Error.captureStackTrace @, TeapotError
describe "An app that uses custom errors", ->
TeapotError = ->
@code = 418
@message = "I'm a teapot"
Error.captureStackTrace @, TeapotError

server = lazorse ->
@_stack.shift() # drop logger
@port = 0
@resource '/byNameUnregistered':
GET: -> @error "TeapotError"
server = lazorse.server port: 0, host: '127.0.0.1', ->
@resource '/byNameUnregistered':
GET: -> @error "TeapotError"

@resource '/byNameRegistered':
GET: -> @error "TeapotError"
@resource '/byNameRegistered':
GET: -> @error "TeapotError"

@resource '/usingConstructor':
GET: -> @error TeapotError
@resource '/usingConstructor':
GET: -> @error TeapotError

describe "An app that uses custom errors", ->
before -> client.usePort server.address().port
after -> server.close()

Expand Down
39 changes: 17 additions & 22 deletions test/examples.test.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,25 @@ lazorse = require '../'
client = require('./client')
assert = require('assert')

errors = require '../lib/errors'

server = lazorse ->
@_stack.shift() # drop logger
@port = 0
@resource '/frob/{foozle}/{whatsit}':
GET: -> @error "teapot"
shortName: "frob"
examples: [
{
method: 'GET'
vars: {foozle: 'hem', whatsit: 'haw'}
}
{
method: 'GET'
vars: {foozle: 'ni', whatsit: 'cate'}
body: {
thing: 'is'
describe "An app with examples", ->
server = lazorse.server port: 0, host: '127.0.0.1', ->
@resource '/frob/{foozle}/{whatsit}':
GET: -> @error "teapot"
shortName: "frob"
examples: [
{
method: 'GET'
vars: {foozle: 'hem', whatsit: 'haw'}
}
}
]

{
method: 'GET'
vars: {foozle: 'ni', whatsit: 'cate'}
body: {
thing: 'is'
}
}
]

describe "An app with examples", ->
before -> client.usePort server.address().port
after -> server.close()

Expand Down
9 changes: 5 additions & 4 deletions test/named_middleware.test.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@ client = require('./client')
assert = require 'assert'

describe "Passing a string to @before", ->
server = lazorse ->
@_stack.shift() # drop logger
@port = 0
server = lazorse.server port: 0, host: '127.0.0.1', ->
@before @findResource, 'static', "#{__dirname}/static"

before -> client.usePort server.address().port
after -> server.close()

it 'uses connect middleware', (done) ->
client.GET '/data.json', (res) ->
client.GET '/data.json', (res, body) ->
assert.equal res.statusCode, 200
fs = require 'fs'
expected = JSON.parse fs.readFileSync "#{__dirname}/static/data.json"
assert.deepEqual expected, body
done()
Loading

0 comments on commit cb58900

Please sign in to comment.