spec.routing.js
This file
Path
spec/spec.routing.js
235 lines · 6.6 KiB · JavaScript
Last changed by
Added "helpful 404" support pagevisionmediabefore adoption897c590
History of this fileTake it
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
describe 'Express' before_each reset() end describe 'route' describe 'with callback function' it 'should respond with a body string' get('/user', function(){ return 'test' }) get('/user').body.should.eql 'test' end end describe 'with options and callback function' it 'should respond with a body string' get('/user', {}, function(){ return 'test with options' }) get('/user').body.should.eql 'test with options' end end describe 'with a trailing slash in request path' it 'should still match' get('/user', {}, function(){ return 'test with options' }) get('/user/').body.should.eql 'test with options' end end describe 'with a trailing slash in route path' it 'should still match' get('/user/', {}, function(){ return 'test with options' }) get('/user').body.should.eql 'test with options' end end describe 'with a trailing whitespace in request path' it 'should still match' get('/user', {}, function(){ return 'test with options' }) get('/user/ ').body.should.eql 'test with options' end end describe 'with a trailing whitespace in route path' it 'should still match' get('/user/ ', {}, function(){ return 'test with options' }) get('/user').body.should.eql 'test with options' end end describe 'with several similar routes' it 'should match them properly' get('/foo', function(){ return 'bar' }) get('/foos', function(){ return 'baz' }) get('/foo').body.should.eql 'bar' get('/foos').body.should.eql 'baz' end end describe 'with several identical routes' it 'should match the first route' get('/foo', function(){ return 'bar' }) get('/foo', function(){ return 'baz' }) get('/foo').body.should.eql 'bar' end end describe 'with no response body' it 'should throw a InvalidResponseBody' get('/user', function(){ Express.server.respond() }) -{ get('/user') }.should.throw_error(/InvalidResponseBody: get "\/user" did not respond with a body string/) end end describe 'with a placeholder' it 'should still match' get('/user/:id', function(){ return 'yay' }) get('/user/12').body.should.eql 'yay' end it 'should not match with an additional path segment' get('/user/:id', function(){ return 'yay' }) get('/user/12/edit').body.should.eql 'Not Found' end it 'should pass it to the route function' get('/user/:id', function(id){ return id }) get('/user/12').body.should.eql '12' end end describe 'with several placeholders' it 'should still match' get('/user/:id/:op', function(){ return 'yay' }) get('/user/12/edit').body.should.eql 'yay' end it 'should pass them to the route function' get('/user/:id/:op', function(id, op){ return op + 'ing user ' + id }) get('/user/12/edit').body.should.eql 'editing user 12' end end describe 'with an optional placeholder' it 'should match with a value' get('/user/:id?', function(){ return 'yay' }) get('/user/1').body.should.eql 'yay' end it 'should pass it to the route function' get('/user/:id?', function(id){ return id || 'You' }) get('/user/12').body.should.eql '12' get('/user').body.should.eql 'You' end it 'should match without a value' get('/user/:id?', function(){ return 'yay' }) get('/user').body.should.eql 'yay' end it 'should not match with an additional path segment' get('/user/:id?', function(){ return 'yay' }) get('/user/12/edit').body.should.eql 'Not Found' end end describe 'with partial placeholder' it 'should still match' get('/report.:format', function(){ return 'yay' }) get('/report.csv').body.should.eql 'yay' get('/report.pdf').body.should.eql 'yay' end it 'should not match without value' get('/report.:format', function(){ return 'yay' }) get('/report.').body.should.eql 'Not Found' end end describe 'with an unmatchable request path' describe 'with "helpful 404" enabled' it 'should show the help html' enable('helpful 404') get('/something').status.should.eql 404 get('/something').body.should.include('<h1>Express</h1>') get('/something').body.should.include('<h2><em>404</em> Not Found</h2>') end end describe 'with "helpful 404" disabled' it 'should respond with 404 Not Found' get('/something').status.should.eql 404 get('/something').body.should.eql 'Not Found' end end end describe 'with an exception thrown' describe 'with "show exceptions" enabled' it 'should show the exception html' enable('show exceptions') get('/user', function(){ throw new Error('Access Denied') }) get('/user').status.should.eql 500 get('/user').body.should.include('<h1>Express</h1>') get('/user').body.should.include('<h2><em>500</em> Error: Access Denied</h2>') end end describe 'with "show exceptions" disabled' disable('show exceptions') it 'should show the regular body' get('/user', function(){ throw new Error('Access Denied') }) get('/user').status.should.eql 500 get('/user').body.should.include('Internal Server Error') end end describe 'with "throw exceptions" enabled' it 'should propegate to the global scope' enable('throw exceptions') get('/user', function(){ throw new Error('Access Denied') }) -{ get('/user') }.should.throw_error end end describe 'with "throw exceptions" disabled' it 'should not propegate to the global scope' disable('throw exceptions') get('/user', function(){ throw new Error('Access Denied') }) -{ get('/user') }.should.not.throw_error end end end endend