Took a detour to write tests for BadMethod. Queue's tests not working yet.

This commit is contained in:
Ruben Vereecken 2014-06-09 18:01:43 +02:00
parent b66f4984c3
commit 925594ffe9
6 changed files with 100 additions and 5 deletions

View file

@ -4,7 +4,7 @@ errors = require '../commons/errors'
module.exports.setup = (app) -> module.exports.setup = (app) ->
app.all '/folder*', (req, res) -> app.all '/folder*', (req, res) ->
return folderGet(req, res) if req.route.method is 'get' return folderGet(req, res) if req.route.method is 'get'
return errors.badMethod(res) return errors.badMethod(res, ['GET'])
folderGet = (req, res) -> folderGet = (req, res) ->
folder = req.path[7..] folder = req.path[7..]
@ -15,4 +15,4 @@ folderGet = (req, res) ->
mongoose.connection.db.collection 'media.files', (errors, collection) -> mongoose.connection.db.collection 'media.files', (errors, collection) ->
collection.find({'metadata.path': folder}).toArray (err, results) -> collection.find({'metadata.path': folder}).toArray (err, results) ->
res.send(results) res.send(results)
res.end() res.end()

View file

@ -11,7 +11,7 @@ module.exports.setup = (app) ->
app.all '/languages', (req, res) -> app.all '/languages', (req, res) ->
# Now that these are in the client, not sure when we would use this, but hey # Now that these are in the client, not sure when we would use this, but hey
return errors.badMethod(res) if req.route.method isnt 'get' return errors.badMethod(res, ['GET']) if req.route.method isnt 'get'
res.send(languages) res.send(languages)
return res.end() return res.end()

View file

@ -28,7 +28,7 @@ module.exports.setup = (app) ->
app.all '/queue/*', (req, res) -> app.all '/queue/*', (req, res) ->
setResponseHeaderToJSONContentType res setResponseHeaderToJSONContentType res
queueName = getQueueNameFromPath req.path queueName = getQueueNameFromPath req.path
try try
handler = loadQueueHandler queueName handler = loadQueueHandler queueName
@ -64,7 +64,7 @@ isHTTPMethodPost = (req) -> return req.route.method is 'post'
isHTTPMethodPut = (req) -> return req.route.method is 'put' isHTTPMethodPut = (req) -> return req.route.method is 'put'
sendMethodNotSupportedError = (req, res) -> errors.badMethod(res,"Queues do not support the HTTP method used." ) sendMethodNotSupportedError = (req, res) -> errors.badMethod(res, ['GET', 'POST', 'PUT'], "Queues do not support the HTTP method used." )
sendQueueError = (req,res, error) -> errors.serverError(res, "Route #{req.path} had a problem: #{error}") sendQueueError = (req,res, error) -> errors.serverError(res, "Route #{req.path} had a problem: #{error}")

View file

@ -0,0 +1,35 @@
require '../common'
describe 'folder', ->
url = getURL('/folder')
allowHeader = 'GET'
it 'can\'t be requested with HTTP POST method', (done) ->
request.post {uri: url}, (err, res, body) ->
expect(res.statusCode).toBe(405)
expect(res.headers.allow).toBe(allowHeader)
done()
it 'can\'t be requested with HTTP PUT method', (done) ->
request.put {uri: url}, (err, res, body) ->
expect(res.statusCode).toBe(405)
expect(res.headers.allow).toBe(allowHeader)
done()
it 'can\'t be requested with HTTP PATCH method', (done) ->
request {method:'patch', uri: url}, (err, res, body) ->
expect(res.statusCode).toBe(405)
expect(res.headers.allow).toBe(allowHeader)
done()
it 'can\'t be requested with HTTP HEAD method', (done) ->
request.head {uri: url}, (err, res, body) ->
expect(res.statusCode).toBe(405)
expect(res.headers.allow).toBe(allowHeader)
done()
it 'can\'t be requested with HTTP DELETE method', (done) ->
request.del {uri: url}, (err, res, body) ->
expect(res.statusCode).toBe(405)
expect(res.headers.allow).toBe(allowHeader)
done()

View file

@ -0,0 +1,35 @@
require '../common'
describe 'languages', ->
url = getURL('/languages')
allowHeader = 'GET'
it 'can\'t be requested with HTTP POST method', (done) ->
request.post {uri: url}, (err, res, body) ->
expect(res.statusCode).toBe(405)
expect(res.headers.allow).toBe(allowHeader)
done()
it 'can\'t be requested with HTTP PUT method', (done) ->
request.put {uri: url}, (err, res, body) ->
expect(res.statusCode).toBe(405)
expect(res.headers.allow).toBe(allowHeader)
done()
it 'can\'t be requested with HTTP PATCH method', (done) ->
request {method:'patch', uri: url}, (err, res, body) ->
expect(res.statusCode).toBe(405)
expect(res.headers.allow).toBe(allowHeader)
done()
it 'can\'t be requested with HTTP HEAD method', (done) ->
request.head {uri: url}, (err, res, body) ->
expect(res.statusCode).toBe(405)
expect(res.headers.allow).toBe(allowHeader)
done()
it 'can\'t be requested with HTTP DELETE method', (done) ->
request.del {uri: url}, (err, res, body) ->
expect(res.statusCode).toBe(405)
expect(res.headers.allow).toBe(allowHeader)
done()

View file

@ -0,0 +1,25 @@
require '../common'
describe 'queue', ->
someURL = getURL('/queue/')
allowHeader = 'GET, POST, PUT'
xit 'can\'t be requested with HTTP PATCH method', (done) ->
request {method:'patch', uri: someURL}, (err, res, body) ->
expect(res.statusCode).toBe(405)
expect(res.headers.allow).toBe(allowHeader)
done()
xit 'can\'t be requested with HTTP HEAD method', (done) ->
request.head {uri: someURL}, (err, res, body) ->
expect(res.statusCode).toBe(405)
expect(res.headers.allow).toBe(allowHeader)
done()
xit 'can\'t be requested with HTTP DELETE method', (done) ->
request.del {uri: someURL}, (err, res, body) ->
expect(res.statusCode).toBe(405)
expect(res.headers.allow).toBe(allowHeader)
done()