Skip to content

Commit

Permalink
Cleaned up some error handling code in REST frontend, and added a spa…
Browse files Browse the repository at this point in the history
…ce after most responses.
  • Loading branch information
josephg committed Feb 10, 2012
1 parent b8e3a4e commit cf06169
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
12 changes: 6 additions & 6 deletions src/server/rest.coffee
Expand Up @@ -7,7 +7,7 @@ url = require 'url'

connect = require 'connect'

send403 = (res, message = 'Forbidden') ->
send403 = (res, message = 'Forbidden\n') ->
res.writeHead 403, {'Content-Type': 'text/plain'}
res.end message

Expand All @@ -16,20 +16,20 @@ send404 = (res, message = '404: Your document could not be found.\n') ->
res.end message

sendError = (res, message) ->
if message == 'forbidden'
if message is 'forbidden'
send403 res
else if message == 'Document does not exist'
else if message is 'Document does not exist'
send404 res
else
console.warn "REST server does not know how to send error: '#{message}'"
#console.warn "REST server does not know how to send error: '#{message}'"
res.writeHead 500, {'Content-Type': 'text/plain'}
res.end "Error: #{message}"
res.end "#{message}\n"

send400 = (res, message) ->
res.writeHead 400, {'Content-Type': 'text/plain'}
res.end message

send200 = (res, message = 'OK') ->
send200 = (res, message = "OK\n") ->
res.writeHead 200, {'Content-Type': 'text/plain'}
res.end message

Expand Down
11 changes: 10 additions & 1 deletion test/rest.coffee
Expand Up @@ -23,6 +23,7 @@ fetch = (method, port, path, postData, extraHeaders, callback) ->
data = ''
response.on 'data', (chunk) -> data += chunk
response.on 'end', ->
data = data.trim()
if response.headers['content-type'] == 'application/json'
data = JSON.parse(data)

Expand Down Expand Up @@ -135,6 +136,13 @@ module.exports = testCase
test.strictEqual res.statusCode, 400
test.done()

"Can't POST an op to a nonexistant document": (test) ->
# This was found in the wild -
# https://github.com/josephg/ShareJS/issues/66
fetch 'POST', @port, "/doc/#{@name}?v=0", {foo:'bar'}, (res, data) ->
test.strictEqual res.statusCode, 404
test.done()

'DELETE deletes a document': (test) ->
@model.create @name, 'simple', =>
fetch 'DELETE', @port, "/doc/#{@name}", null, (res, data) =>
Expand Down Expand Up @@ -250,4 +258,5 @@ module.exports = testCase
@model.getSnapshot @name, (error, doc) ->
test.ok doc
test.done()



0 comments on commit cf06169

Please sign in to comment.