Skip to content

Commit

Permalink
Change route naming convention in tests
Browse files Browse the repository at this point in the history
- Resolve Issue #53:
  - Use lowercase, hyphenated names for route paths, to conform to
    better practices. So `api/some-long-route-name` instead of
    `api/someLongRouteName`.
  • Loading branch information
kahmali committed Jul 2, 2015
1 parent 80e07a9 commit 8e114da
Showing 1 changed file with 36 additions and 36 deletions.
72 changes: 36 additions & 36 deletions test/api_tests.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,20 @@ Meteor.startup ->
test.equal Restivus.config.auth.token, 'services.resume.loginTokens.hashedToken'

it 'should allow you to add an unconfigured route', (test) ->
Restivus.addRoute 'test1', {authRequired: true, roleRequired: 'admin'},
Restivus.addRoute 'add-unconfigured-route', {authRequired: true, roleRequired: 'admin'},
get: ->
1
# TODO: Access routes in a less brittle way than this index that can change when new routes are added (more below)
route = Restivus.routes[2]
test.equal route.path, 'test1'
test.equal route.path, 'add-unconfigured-route'
test.equal route.endpoints.get(), 1
test.isTrue route.options.authRequired
test.equal route.options.roleRequired, 'admin'
test.isUndefined route.endpoints.get.authRequired
test.isUndefined route.endpoints.get.roleRequired

it 'should allow you to add an unconfigured collection route', (test) ->
Restivus.addCollection new Mongo.Collection('tests'),
Restivus.addCollection new Mongo.Collection('add-unconfigured-collection'),
routeOptions:
authRequired: true
roleRequired: 'admin'
Expand All @@ -32,7 +32,7 @@ Meteor.startup ->
2

route = Restivus.routes[3]
test.equal route.path, 'tests'
test.equal route.path, 'add-unconfigured-collection'
test.equal route.endpoints.get.action(), 2
test.isTrue route.options.authRequired
test.equal route.options.roleRequired, 'admin'
Expand Down Expand Up @@ -74,37 +74,37 @@ Meteor.startup ->

describe 'A collection route', ->
it 'should be able to exclude endpoints using just the excludedEndpoints option', (test, next) ->
Restivus.addCollection new Mongo.Collection('test-excluded-endpoints'),
Restivus.addCollection new Mongo.Collection('excluded-endpoints'),
excludedEndpoints: ['get', 'getAll']

HTTP.get 'http://localhost:3000/api/v1/test-excluded-endpoints/10', (error, result) ->
HTTP.get 'http://localhost:3000/api/v1/excluded-endpoints/10', (error, result) ->
response = JSON.parse result.content
test.isTrue error
test.equal result.statusCode, 405
test.equal response.status, 'error'
test.equal response.message, 'API endpoint does not exist'

HTTP.get 'http://localhost:3000/api/v1/test-excluded-endpoints/', (error, result) ->
HTTP.get 'http://localhost:3000/api/v1/excluded-endpoints/', (error, result) ->
response = JSON.parse result.content
test.isTrue error
test.equal result.statusCode, 405
test.equal response.status, 'error'
test.equal response.message, 'API endpoint does not exist'

# Make sure it doesn't exclude any endpoints it shouldn't
HTTP.post 'http://localhost:3000/api/v1/test-excluded-endpoints/', data: test: 'abc', (error, result) ->
HTTP.post 'http://localhost:3000/api/v1/excluded-endpoints/', data: test: 'abc', (error, result) ->
response = JSON.parse result.content
test.equal result.statusCode, 201
test.equal response.status, 'success'
test.equal response.data.test, 'abc'
next()

context 'with the default autogenerated endpoints', ->
Restivus.addCollection new Mongo.Collection('testAutogen')
Restivus.addCollection new Mongo.Collection('autogen')
testId = null

it 'should support a POST on api/collection', (test) ->
result = HTTP.post 'http://localhost:3000/api/v1/testAutogen',
result = HTTP.post 'http://localhost:3000/api/v1/autogen',
data:
name: 'test name'
description: 'test description'
Expand All @@ -119,7 +119,7 @@ Meteor.startup ->
testId = responseData._id

it 'should not support a DELETE on api/collection', (test, next) ->
HTTP.del "http://localhost:3000/api/v1/testAutogen", (error, result) ->
HTTP.del "http://localhost:3000/api/v1/autogen", (error, result) ->
response = JSON.parse result.content
test.isTrue error
test.equal result.statusCode, 405
Expand All @@ -130,7 +130,7 @@ Meteor.startup ->
next()

it 'should support a PUT on api/collection/:id', (test) ->
result = HTTP.put "http://localhost:3000/api/v1/testAutogen/#{testId}",
result = HTTP.put "http://localhost:3000/api/v1/autogen/#{testId}",
data:
name: 'update name'
description: 'update description'
Expand All @@ -141,7 +141,7 @@ Meteor.startup ->
test.equal responseData.name, 'update name'
test.equal responseData.description, 'update description'

result = HTTP.put "http://localhost:3000/api/v1/testAutogen/#{testId}",
result = HTTP.put "http://localhost:3000/api/v1/autogen/#{testId}",
data:
name: 'update name with no description'
response = JSON.parse result.content
Expand All @@ -155,11 +155,11 @@ Meteor.startup ->
describe 'An endpoint', ->

it 'should respond with the default headers when not overridden', (test) ->
Restivus.addRoute 'testDefaultHeaders',
Restivus.addRoute 'default-headers',
get: ->
true

result = HTTP.get 'http://localhost:3000/api/v1/testDefaultHeaders'
result = HTTP.get 'http://localhost:3000/api/v1/default-headers'

test.equal result.statusCode, 200
test.equal result.headers['content-type'], 'text/json'
Expand All @@ -168,15 +168,15 @@ Meteor.startup ->
test.isTrue result.content

it 'should allow default headers to be overridden', (test) ->
Restivus.addRoute 'testOverrideDefaultHeaders',
Restivus.addRoute 'override-default-headers',
get: ->
headers:
'Content-Type': 'application/json'
'Access-Control-Allow-Origin': 'https://mywebsite.com'
body:
true

result = HTTP.get 'http://localhost:3000/api/v1/testOverrideDefaultHeaders'
result = HTTP.get 'http://localhost:3000/api/v1/override-default-headers'

test.equal result.statusCode, 200
test.equal result.headers['content-type'], 'application/json'
Expand All @@ -197,18 +197,18 @@ Meteor.startup ->
next()

it 'should return a 405 error if that method is not implemented on the route', (test, next) ->
Restivus.addCollection new Mongo.Collection('test-method-not-implemented'),
Restivus.addCollection new Mongo.Collection('method-not-implemented'),
excludedEndpoints: ['get', 'getAll']

HTTP.get 'http://localhost:3000/api/v1/test-method-not-implemented/', (error, result) ->
HTTP.get 'http://localhost:3000/api/v1/method-not-implemented/', (error, result) ->
response = JSON.parse result.content
test.isTrue error
test.equal result.statusCode, 405
test.isTrue result.headers['allow'].indexOf('POST') != -1
test.equal response.status, 'error'
test.equal response.message, 'API endpoint does not exist'

HTTP.get 'http://localhost:3000/api/v1/test-method-not-implemented/10', (error, result) ->
HTTP.get 'http://localhost:3000/api/v1/method-not-implemented/10', (error, result) ->
response = JSON.parse result.content
test.isTrue error
test.equal result.statusCode, 405
Expand All @@ -219,46 +219,46 @@ Meteor.startup ->
next()

it 'should cause an error when it returns null', (test, next) ->
Restivus.addRoute 'testNullResponse',
Restivus.addRoute 'null-response',
get: ->
null

HTTP.get 'http://localhost:3000/api/v1/testNullResponse', (error, result) ->
HTTP.get 'http://localhost:3000/api/v1/null-response', (error, result) ->
test.isTrue error
test.equal result.statusCode, 500
next()

it 'should cause an error when it returns undefined', (test, next) ->
Restivus.addRoute 'testUndefinedResponse',
Restivus.addRoute 'undefined-response',
get: ->
undefined

HTTP.get 'http://localhost:3000/api/v1/testUndefinedResponse', (error, result) ->
HTTP.get 'http://localhost:3000/api/v1/undefined-response', (error, result) ->
test.isTrue error
test.equal result.statusCode, 500
next()

it 'should be able to handle it\'s response manually', (test, next) ->
Restivus.addRoute 'testManualResponse',
Restivus.addRoute 'manual-response',
get: ->
@response.write 'Testing manual response.'
@response.end()
@done()

HTTP.get 'http://localhost:3000/api/v1/testManualResponse', (error, result) ->
HTTP.get 'http://localhost:3000/api/v1/manual-response', (error, result) ->
response = result.content

test.equal result.statusCode, 200
test.equal response, 'Testing manual response.'
next()

it 'should not have to call this.response.end() when handling the response manually', (test, next) ->
Restivus.addRoute 'testManualResponseNoEnd',
Restivus.addRoute 'manual-response-no-end',
get: ->
@response.write 'Testing this.end()'
@done()

HTTP.get 'http://localhost:3000/api/v1/testManualResponseNoEnd', (error, result) ->
HTTP.get 'http://localhost:3000/api/v1/manual-response-no-end', (error, result) ->
response = result.content

test.isFalse error
Expand All @@ -267,44 +267,44 @@ Meteor.startup ->
next()

it 'should be able to send it\'s response in chunks', (test, next) ->
Restivus.addRoute 'testChunkedResponse',
Restivus.addRoute 'chunked-response',
get: ->
@response.write 'Testing '
@response.write 'chunked response.'
@done()

HTTP.get 'http://localhost:3000/api/v1/testChunkedResponse', (error, result) ->
HTTP.get 'http://localhost:3000/api/v1/chunked-response', (error, result) ->
response = result.content

test.equal result.statusCode, 200
test.equal response, 'Testing chunked response.'
next()

it 'should respond with an error if this.done() isn\'t called after response is handled manually', (test, next) ->
Restivus.addRoute 'testManualResponseWithoutDone',
Restivus.addRoute 'manual-response-without-done',
get: ->
@response.write 'Testing'

HTTP.get 'http://localhost:3000/api/v1/testManualResponseWithoutDone', (error, result) ->
HTTP.get 'http://localhost:3000/api/v1/manual-response-without-done', (error, result) ->
test.isTrue error
test.equal result.statusCode, 500
next()

it 'should not wrap text with quotes when response Content-Type is text/plain', (test, next) ->
Restivus.addRoute 'testPlainTextResponse',
Restivus.addRoute 'plain-text-response',
get: ->
headers:
'Content-Type': 'text/plain'
body: 'foo"bar'

HTTP.get 'http://localhost:3000/api/v1/testPlainTextResponse', (error, result) ->
HTTP.get 'http://localhost:3000/api/v1/plain-text-response', (error, result) ->
response = result.content
test.equal result.statusCode, 200
test.equal response, 'foo"bar'
next()

it 'should have its context set', (test) ->
Restivus.addRoute 'testContext/:test',
Restivus.addRoute 'context/:test',
post: ->
test.equal @urlParams.test, '100'
test.equal @queryParams.test, "query"
Expand All @@ -316,7 +316,7 @@ Meteor.startup ->
test.isFalse @roleRequired
true

result = HTTP.post 'http://localhost:3000/api/v1/testContext/100?test=query',
result = HTTP.post 'http://localhost:3000/api/v1/context/100?test=query',
data:
test: 'body'

Expand Down

0 comments on commit 8e114da

Please sign in to comment.