Permalink
Browse files

Merge pull request #68 from dcolens/master

HEAD request + testing
  • Loading branch information...
2 parents cf06169 + 47e6414 commit eb95a595c88ab6fb79e6b6f277a3a3b57703f430 @josephg josephg committed Feb 11, 2012
Showing with 46 additions and 14 deletions.
  1. +30 −14 src/server/rest.coffee
  2. +16 −0 test/rest.coffee
@@ -15,15 +15,25 @@ send404 = (res, message = '404: Your document could not be found.\n') ->
res.writeHead 404, {'Content-Type': 'text/plain'}
res.end message
-sendError = (res, message) ->
- if message is 'forbidden'
- send403 res
- else if message is 'Document does not exist'
- send404 res
+sendError = (res, message, head = false) ->
+ if message == 'forbidden'
+ if head
+ send403 res, ""
+ else
+ send403 res
+ else if message == 'Document does not exist'
+ if head
+ send404 res, ""
+ else
+ send404 res
else
- #console.warn "REST server does not know how to send error: '#{message}'"
- res.writeHead 500, {'Content-Type': 'text/plain'}
- res.end "#{message}\n"
+ console.warn "REST server does not know how to send error: '#{message}'"
+ if head
+ res.writeHead 500, {'Content-Type': 'text/plain'}
+ res.end "Error: #{message}\n"
+ else
+ res.writeHead 500, {}
+ res.end ""
send400 = (res, message) ->
res.writeHead 400, {'Content-Type': 'text/plain'}
@@ -73,16 +83,22 @@ router = (app, createClient, options) ->
# I'm not sure what to do with document metadata - it is inaccessable for now.
app.get '/doc/:name', auth, (req, res) ->
req._client.getSnapshot req.params.name, (error, doc) ->
- if doc
+ if doc
res.setHeader 'X-OT-Type', doc.type.name
res.setHeader 'X-OT-Version', doc.v
- if typeof doc.snapshot == 'string'
- send200 res, doc.snapshot
+ if req.method == "HEAD"
+ send200 res, ""
else
- sendJSON res, doc.snapshot
+ if typeof doc.snapshot == 'string'
+ send200 res, doc.snapshot
+ else
+ sendJSON res, doc.snapshot
else
- sendError res, error
-
+ if req.method == "HEAD"
+ sendError res, error, true
+ else
+ sendError res, error
+
# Put is used to create a document. The contents are a JSON object with {type:TYPENAME, meta:{...}}
app.put '/doc/:name', auth, (req, res) ->
expectJSONObject req, res, (obj) ->
View
@@ -71,7 +71,23 @@ module.exports = testCase
fetch 'GET', @port, "/doc/#{@name}", null, (res, data) ->
test.strictEqual(res.statusCode, 404)
test.done()
+
+ 'return 404 and empty body when on HEAD on a nonexistant document': (test) ->
+ fetch 'HEAD', @port, "/doc/#{@name}", null, (res, data) ->
+ test.strictEqual res.statusCode, 404
+ test.strictEqual data, ''
+ test.done()
+ 'return 200, empty body, version and type when on HEAD on a document': (test) ->
+ @model.create @name, 'simple', =>
+ @model.applyOp @name, {v:0, op:{position: 0, text: 'Hi'}}, =>
+ fetch 'HEAD', @port, "/doc/#{@name}", null, (res, data, headers) ->
+ test.strictEqual res.statusCode, 200
+ test.strictEqual headers['x-ot-version'], '1'
+ test.strictEqual headers['x-ot-type'], 'simple'
+ test.strictEqual data, ''
+ test.done()
+
'GET a document returns the document snapshot': (test) ->
@model.create @name, 'simple', =>
@model.applyOp @name, {v:0, op:{position: 0, text: 'Hi'}}, =>

0 comments on commit eb95a59

Please sign in to comment.