fixed a response test
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Tj Holowaychuk · committed by Tj Holowaychuk · parent 8f79856 · Browse files · 1 file·+670 added−670 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
1 file+670 added−670 removed
- tests:+670 added−670 removed
Provenance
- Commit
36e9c02- Tree
0f7e49e- Author
Tj Holowaychuk <tj@vision-media.ca>- Committer
Tj Holowaychuk <tj@vision-media.ca>- Parent
8f79856
1 file changed+670 added−670 removed
modified:test/response.test.js+670 added-670 removed
41 unmodified lines42 assert.response(app,43 { url: '/user' },44 { body: '{"name":"tj"}', headers: { 'Content-Type': json }});45 },46- 47- 'test #status()': function(){48- var app = express.createServer();49- 50- app.get('/error', function(req, res, next){51- res.status(500).send('OH NO');52- });53- 54- assert.response(app,55- { url: '/error' },56- { body: 'OH NO', status: 500 });57- },58- 59- 'test #send()': function(){60- var app = express.createServer();61- 62- app.get('/html', function(req, res){63- res.send('<p>test</p>');64- });65- 66- app.get('/json', function(req, res){67- res.header('X-Foo', 'bar');68- res.send({ foo: 'bar' }, 201);69- });46 71- app.get('/text', function(req, res){72- res.header('X-Foo', 'bar');73- res.contentType('txt');74- res.send('wahoo');75- });47+ 'test #status()': function(){48+ var app = express.createServer();49 77- app.get('/status', function(req, res){78- res.send(404);79- });50+ app.get('/error', function(req, res, next){51+ res.status(500).send('OH NO');52+ });53 81- app.get('/status/text', function(req, res){82- res.send('Oh noes!', 404);83- });54+ assert.response(app,55+ { url: '/error' },56+ { body: 'OH NO', status: 500 });57+ },58 85- app.get('/error', function(req, res){86- res.header('Content-Type', 'text/plain');87- res.send('Oh shit!', 500);88- });59+ 'test #send()': function(){60+ var app = express.createServer();61 90- app.get('/buffer', function(req, res){91- res.send(new Buffer('wahoo!'));92- });62+ app.get('/html', function(req, res){63+ res.send('<p>test</p>');64+ });65 94- app.get('/204', function(req, res, next){95- res.send(204);96- });97- 98- app.get('/undefined', function(req, res, next){99- res.send(undefined);100- });101- 102- app.get('/bool', function(req, res, next){103- res.send(true);104- });105- 106- assert.response(app,107- { url: '/bool' },108- { body: 'true'109- , headers: { 'Content-Type': 'application/json; charset=utf-8' }});110- 111- assert.response(app,112- { url: '/html' },113- { body: '<p>test</p>'114- , headers: {115- 'Content-Type': 'text/html; charset=utf-8'116- }});66+ app.get('/json', function(req, res){67+ res.header('X-Foo', 'bar');68+ res.send({ foo: 'bar' }, 201);69+ });70 118- assert.response(app,119- { url: '/json' },120- { body: '{"foo":"bar"}'121- , status: 201122- , headers: {123- 'Content-Type': 'application/json; charset=utf-8'124- , 'X-Foo': 'bar'125- }});71+ app.get('/text', function(req, res){72+ res.header('X-Foo', 'bar');73+ res.contentType('txt');74+ res.send('wahoo');75+ });76 127- assert.response(app,128- { url: '/text' },129- { body: 'wahoo'130- , headers: {131- 'Content-Type': 'text/plain'132- , 'X-Foo': 'bar'133- }});77+ app.get('/status', function(req, res){78+ res.send(404);79+ });80 135- assert.response(app,136- { url: '/status/text' },137- { body: 'Oh noes!', status: 404 });81+ app.get('/status/text', function(req, res){82+ res.send('Oh noes!', 404);83+ });84 139- assert.response(app,140- { url: '/status' },141- { body: 'Not Found'142- , status: 404143- , headers: { 'Content-Type': 'text/plain' }});85+ app.get('/error', function(req, res){86+ res.header('Content-Type', 'text/plain');87+ res.send('Oh shit!', 500);88+ });89 145- assert.response(app,146- { url: '/error' },147- { body: 'Oh shit!'148- , status: 500149- , headers: {150- 'Content-Type': 'text/plain'151- , 'Content-Length': '8'152- }});90+ app.get('/buffer', function(req, res){91+ res.send(new Buffer('wahoo!'));92+ });93 94+ app.get('/204', function(req, res, next){95+ res.send(204);96+ });97+98+ app.get('/undefined', function(req, res, next){99+ res.send(undefined);100+ });101+ 102+ app.get('/bool', function(req, res, next){103+ res.send(true);104+ });105+ 106+ assert.response(app,107+ { url: '/bool' },108+ { body: 'true'109+ , headers: { 'Content-Type': 'application/json; charset=utf-8' }});110+ 111+ assert.response(app,112+ { url: '/html' },113+ { body: '<p>test</p>'114+ , headers: {115+ 'Content-Type': 'text/html; charset=utf-8'116+ }});117+ 118+ assert.response(app,119+ { url: '/json' },120+ { body: '{"foo":"bar"}'121+ , status: 201122+ , headers: {123+ 'Content-Type': 'application/json; charset=utf-8'124+ , 'X-Foo': 'bar'125+ }});126+ 127+ assert.response(app,128+ { url: '/text' },129+ { body: 'wahoo'130+ , headers: {131+ 'Content-Type': 'text/plain'132+ , 'X-Foo': 'bar'133+ }});134+ 135+ assert.response(app,136+ { url: '/status/text' },137+ { body: 'Oh noes!', status: 404 });138+ 139+ assert.response(app,140+ { url: '/status' },141+ { body: 'Not Found'142+ , status: 404143+ , headers: { 'Content-Type': 'text/plain' }});144+ 145+ assert.response(app,146+ { url: '/error' },147+ { body: 'Oh shit!'148+ , status: 500149+ , headers: {150+ 'Content-Type': 'text/plain'151+ , 'Content-Length': '8'152+ }});153+ 154- assert.response(app,155- { url: '/buffer' },156- { body: 'wahoo!'157- , headers: {158- 'Content-Type': 'application/octet-stream'159- , 'Content-Length': '6'160- }});154+ assert.response(app,155+ { url: '/buffer' },156+ { body: 'wahoo!'157+ , headers: {158+ 'Content-Type': 'application/octet-stream'159+ , 'Content-Length': '6'160+ }});161+ 162+ assert.response(app,163+ { url: '/204' },164+ { status: 204 }, function(res){165+ assert.equal(undefined, res.headers['content-type']);166+ assert.equal(undefined, res.headers['content-length']);167+ });168+169+ assert.response(app,170+ { url: '/undefined' },171+ { body: '' });172+ 173+ assert.response(app,174+ { url: '/json?callback=test' },175+ { body: '{"foo":"bar"}'176+ , status: 201177+ , headers: {178+ 'Content-Type': 'application/json; charset=utf-8'179+ , 'X-Foo': 'bar'180+ }});181+ },182+ 183+ 'test #send() JSONP': function(){184+ var app = express.createServer();185+ 186+ app.enable('jsonp callback');187+ 188+ app.get('/jsonp', function(req, res){189+ res.header('X-Foo', 'bar');190+ res.send({ foo: 'bar' }, 201);191+ });192+ 193+ assert.response(app,194+ { url: '/jsonp?callback=test' },195+ { body: 'test({"foo":"bar"});'196+ , status: 201197+ , headers: {198+ 'Content-Type': 'text/javascript; charset=utf-8'199+ , 'X-Foo': 'bar'200+ }});201+ 202+ assert.response(app,203+ { url: '/jsonp?callback=baz' },204+ { body: 'baz({"foo":"bar"});'205+ , status: 201, headers: {206+ 'Content-Type': 'text/javascript; charset=utf-8'207+ , 'X-Foo': 'bar'208+ }});209+ 210+ assert.response(app,211+ { url: '/jsonp?callback=invalid()[]' },212+ { body: 'invalid({"foo":"bar"});'213+ , status: 201214+ , headers: {215+ 'Content-Type': 'text/javascript; charset=utf-8'216+ , 'X-Foo': 'bar'217+ }});218+ },219+ 220+ 'test #json() JSONP': function(){221+ var app = express.createServer();222+ 223+ app.enable('jsonp callback');224+ 225+ app.get('/jsonp', function(req, res){226+ res.header('X-Foo', 'bar');227+ res.json({ foo: 'bar' }, 201);228+ });229+ 230+ assert.response(app,231+ { url: '/jsonp?callback=test' },232+ { body: 'test({"foo":"bar"});'233+ , status: 201234+ , headers: {235+ 'Content-Type': 'text/javascript; charset=utf-8'236+ , 'X-Foo': 'bar'237+ }});238+ 239+ assert.response(app,240+ { url: '/jsonp?callback=baz' },241+ { body: 'baz({"foo":"bar"});'242+ , status: 201, headers: {243+ 'Content-Type': 'text/javascript; charset=utf-8'244+ , 'X-Foo': 'bar'245+ }});246+ 247+ assert.response(app,248+ { url: '/jsonp?callback=invalid()[]' },249+ { body: 'invalid({"foo":"bar"});'250+ , status: 201251+ , headers: {252+ 'Content-Type': 'text/javascript; charset=utf-8'253+ , 'X-Foo': 'bar'254+ }});255+ },256+ 257+ 'test #contentType()': function(){258+ var app = express.createServer();259 162- assert.response(app,163- { url: '/204' },164- { status: 204 }, function(res){165- assert.equal(undefined, res.headers['content-type']);166- assert.equal(undefined, res.headers['content-length']);260+ app.get('/html', function(req, res){261+ res.contentType('index.html');262+ res.writeHead(200, res.headers);263+ res.end('<p>yay</p>');264 });265 266+ app.get('/json', function(req, res, next){267+ res.contentType('json');268+ res.send('{"foo":"bar"}');269+ });270+ 271+ app.get('/literal', function(req, res){272+ res.contentType('application/json');273+ res.send('{"foo":"bar"}')274+ });275+ 169- assert.response(app,170- { url: '/undefined' },171- { status: 500 });276+ assert.response(app,277+ { url: '/literal' },278+ { headers: { 'Content-Type': 'application/json' }}); 279+ assert.response(app,280+ { url: '/html' },281+ { body: '<p>yay</p>', headers: { 'Content-Type': 'text/html' }});282+ assert.response(app,283+ { url: '/json' },284+ { headers: { 'Content-Type': 'application/json' }});285+ },286+ 287+ 'test #attachment()': function(){288+ var app = express.createServer();289 173- assert.response(app,174- { url: '/json?callback=test' },175- { body: '{"foo":"bar"}'176- , status: 201177- , headers: {178- 'Content-Type': 'application/json; charset=utf-8'179- , 'X-Foo': 'bar'180- }});181- },182- 183- 'test #send() JSONP': function(){184- var app = express.createServer();185- 186- app.enable('jsonp callback');187- 188- app.get('/jsonp', function(req, res){189- res.header('X-Foo', 'bar');190- res.send({ foo: 'bar' }, 201);191- });290+ app.get('/style.css', function(req, res){291+ res.attachment();292+ res.send('some stylezzz');293+ });192- 193- assert.response(app,194- { url: '/jsonp?callback=test' },195- { body: 'test({"foo":"bar"});'196- , status: 201197- , headers: {198- 'Content-Type': 'text/javascript; charset=utf-8'199- , 'X-Foo': 'bar'200- }});201- 202- assert.response(app,203- { url: '/jsonp?callback=baz' },204- { body: 'baz({"foo":"bar"});'205- , status: 201, headers: {206- 'Content-Type': 'text/javascript; charset=utf-8'207- , 'X-Foo': 'bar'208- }});209- 210- assert.response(app,211- { url: '/jsonp?callback=invalid()[]' },212- { body: 'invalid({"foo":"bar"});'213- , status: 201214- , headers: {215- 'Content-Type': 'text/javascript; charset=utf-8'216- , 'X-Foo': 'bar'217- }});218- },219- 220- 'test #json() JSONP': function(){221- var app = express.createServer();222- 223- app.enable('jsonp callback');224- 225- app.get('/jsonp', function(req, res){226- res.header('X-Foo', 'bar');227- res.json({ foo: 'bar' }, 201);228- });229- 230- assert.response(app,231- { url: '/jsonp?callback=test' },232- { body: 'test({"foo":"bar"});'233- , status: 201234- , headers: {235- 'Content-Type': 'text/javascript; charset=utf-8'236- , 'X-Foo': 'bar'237- }});238- 239- assert.response(app,240- { url: '/jsonp?callback=baz' },241- { body: 'baz({"foo":"bar"});'242- , status: 201, headers: {243- 'Content-Type': 'text/javascript; charset=utf-8'244- , 'X-Foo': 'bar'245- }});246- 247- assert.response(app,248- { url: '/jsonp?callback=invalid()[]' },249- { body: 'invalid({"foo":"bar"});'250- , status: 201251- , headers: {252- 'Content-Type': 'text/javascript; charset=utf-8'253- , 'X-Foo': 'bar'254- }});255- },256- 257- 'test #contentType()': function(){258- var app = express.createServer();294 260- app.get('/html', function(req, res){261- res.contentType('index.html');262- res.writeHead(200, res.headers);263- res.end('<p>yay</p>');264- });295+ app.get('/*', function(req, res){296+ res.attachment(req.params[0]);297+ res.send('whatever');298+ });299+ 300+ assert.response(app,301+ { url: '/javascripts/jquery.js' },302+ { body: 'whatever'303+ , headers: { 'Content-Type': 'application/javascript'304+ , 'Content-Disposition': 'attachment; filename="jquery.js"' }});305 266- app.get('/json', function(req, res, next){267- res.contentType('json');268- res.send('{"foo":"bar"}');269- });270- 271- app.get('/literal', function(req, res){272- res.contentType('application/json');273- res.send('{"foo":"bar"}')274- });275- 276- assert.response(app,277- { url: '/literal' },278- { headers: { 'Content-Type': 'application/json' }}); 279- assert.response(app,280- { url: '/html' },281- { body: '<p>yay</p>', headers: { 'Content-Type': 'text/html' }});282- assert.response(app,283- { url: '/json' },284- { headers: { 'Content-Type': 'application/json' }});285- },286- 287- 'test #attachment()': function(){288- var app = express.createServer();306+ assert.response(app,307+ { url: '/style.css' },308+ { body: 'some stylezzz'309+ , headers: { 'Content-Type': 'text/html; charset=utf-8'310+ , 'Content-Disposition': 'attachment' }});311+ },312 290- app.get('/style.css', function(req, res){291- res.attachment();292- res.send('some stylezzz');293- });294- 295- app.get('/*', function(req, res){296- res.attachment(req.params[0]);297- res.send('whatever');298- });313+ 'test #redirect()': function(){314+ var app = express.createServer()315+ , app2 = express.createServer();316+ 317+ app2.set('home', '/blog');318 300- assert.response(app,301- { url: '/javascripts/jquery.js' },302- { body: 'whatever'303- , headers: { 'Content-Type': 'application/javascript'304- , 'Content-Disposition': 'attachment; filename="jquery.js"' }});305- 306- assert.response(app,307- { url: '/style.css' },308- { body: 'some stylezzz'309- , headers: { 'Content-Type': 'text/html; charset=utf-8'310- , 'Content-Disposition': 'attachment' }});311- },312- 313- 'test #redirect()': function(){314- var app = express.createServer()315- , app2 = express.createServer();319+ app2.redirect('google', 'http://google.com');320 317- app2.set('home', '/blog');318- 319- app2.redirect('google', 'http://google.com');320- 321- app2.redirect('blog', function(req, res){322- return req.params.id323- ? '/user/' + req.params.id + '/blog'324- : null;325- });321+ app2.redirect('blog', function(req, res){322+ return req.params.id323+ ? '/user/' + req.params.id + '/blog'324+ : null;325+ });326+ 327+ app.get('/', function(req, res){328+ res.redirect('http://google.com', 301);329+ });330+ 331+ app.get('/back', function(req, res){332+ res.redirect('back');333+ });334+ 335+ app.get('/home', function(req, res){336+ res.redirect('home');337+ });338+ 339+ app.get('/html', function(req, res){340+ res.redirect('http://google.com');341+ });342 327- app.get('/', function(req, res){328- res.redirect('http://google.com', 301);329- });330- 331- app.get('/back', function(req, res){332- res.redirect('back');333- });334- 335- app.get('/home', function(req, res){336- res.redirect('home');337- });343+ app2.get('/', function(req, res){344+ res.redirect('http://google.com', 301);345+ });346 339- app.get('/html', function(req, res){340- res.redirect('http://google.com');341- });347+ app2.get('/back', function(req, res){348+ res.redirect('back');349+ });350+ 351+ app2.get('/home', function(req, res){352+ res.redirect('home');353+ });354+ 355+ app2.get('/google', function(req, res){356+ res.redirect('google');357+ });358 343- app2.get('/', function(req, res){344- res.redirect('http://google.com', 301);345- });359+ app2.get('/user/:id', function(req, res){360+ res.header('X-Foo', 'bar');361+ res.redirect('blog');362+ });363 347- app2.get('/back', function(req, res){348- res.redirect('back');349- });364+ assert.response(app,365+ { url: '/html', headers: { Accept: 'text/html,text/plain', Host: 'foo.com' }},366+ { body: '<p>Moved Temporarily. Redirecting to <a href="http://google.com">http://google.com</a></p>' });367 351- app2.get('/home', function(req, res){352- res.redirect('home');353- });368+ assert.response(app,369+ { url: '/', headers: { Accept: 'text/plain', Host: 'foo.com' }},370+ { body: 'Moved Permanently. Redirecting to http://google.com'371+ , status: 301, headers: { Location: 'http://google.com' }});372 355- app2.get('/google', function(req, res){356- res.redirect('google');357- });358- 359- app2.get('/user/:id', function(req, res){360- res.header('X-Foo', 'bar');361- res.redirect('blog');362- });363- 364- assert.response(app,365- { url: '/html', headers: { Accept: 'text/html,text/plain', Host: 'foo.com' }},366- { body: '<p>Moved Temporarily. Redirecting to <a href="http://google.com">http://google.com</a></p>' });367- 368- assert.response(app,369- { url: '/', headers: { Accept: 'text/plain', Host: 'foo.com' }},370- { body: 'Moved Permanently. Redirecting to http://google.com'371- , status: 301, headers: { Location: 'http://google.com' }});372- 373- assert.response(app,374- { url: '/back', headers: { Accept: 'text/plain', Host: 'foo.com' }},375- { body: 'Moved Temporarily. Redirecting to http://foo.com/'376- , status: 302, headers: { Location: 'http://foo.com/', 'Content-Type': 'text/plain' }});373+ assert.response(app,374+ { url: '/back', headers: { Accept: 'text/plain', Host: 'foo.com' }},375+ { body: 'Moved Temporarily. Redirecting to http://foo.com/'376+ , status: 302, headers: { Location: 'http://foo.com/', 'Content-Type': 'text/plain' }});377- 378- assert.response(app,379- { url: '/back', headers: { Referer: '/foo', Accept: 'text/plain', Host: 'foo.com' }},380- { body: 'Moved Temporarily. Redirecting to http://foo.com/foo'381- , status: 302, headers: { Location: 'http://foo.com/foo' }});382- 383- assert.response(app,384- { url: '/back', headers: { Referrer: '/foo', Accept: 'text/plain', Host: 'foo.com' }},385- { body: 'Moved Temporarily. Redirecting to http://foo.com/foo'386- , status: 302, headers: { Location: 'http://foo.com/foo' }});387- 388- assert.response(app,389- { url: '/home', headers: { Accept: 'text/plain', Host: 'foo.com' } },390- { body: 'Moved Temporarily. Redirecting to http://foo.com/'391- , status: 302, headers: { Location: 'http://foo.com/' }});392- 393- assert.response(app2,394- { url: '/', headers: { Accept: 'text/plain', Host: 'foo.com' }},395- { body: 'Moved Permanently. Redirecting to http://google.com'396- , status: 301, headers: { Location: 'http://google.com' }});397- 398- assert.response(app2,399- { url: '/back', headers: { Accept: 'text/plain', Host: 'foo.com' }},400- { body: 'Moved Temporarily. Redirecting to http://foo.com/blog'401- , status: 302, headers: { Location: 'http://foo.com/blog' }});402- 403- assert.response(app2,404- { url: '/home', headers: { Accept: 'text/plain', Host: 'foo.com' }},405- { body: 'Moved Temporarily. Redirecting to http://foo.com/blog'406- , status: 302, headers: { Location: 'http://foo.com/blog' }});407- 408- assert.response(app2,409- { url: '/google', headers: { Accept: 'text/plain', Host: 'foo.com' }},410- { body: 'Moved Temporarily. Redirecting to http://google.com'411- , status: 302, headers: { Location: 'http://google.com' }});412- 413- assert.response(app2,414- { url: '/user/12', headers: { Accept: 'text/plain', Host: 'foo.com' }},415- { body: 'Moved Temporarily. Redirecting to http://foo.com/user/12/blog'416- , status: 302, headers: { Location: 'http://foo.com/user/12/blog', 'X-Foo': 'bar' }});417- },418- 419- 'test #redirect() when mounted': function(){420- var app = express.createServer()421- , blog = express.createServer();422- 423- 424- blog.get('/posts', function(req, res){425- res.redirect('/posts/all');426- });427- 428- blog.get('/posts/all', function(req, res){429- res.send('all blog posts');430- });431- 432- app.use('/blog', blog);433- 434- assert.response(app,435- { url: '/blog/posts', headers: { Host: 'foo.com' }},436- { status: 302437- , headers: { Location: 'http://foo.com/blog/posts/all' }});438- },439- 440- 'test #sendfile()': function(){441- var app = express.createServer();442- 443- app.get('/*', function(req, res, next){444- var file = req.params[0]445- , path = __dirname + '/fixtures/' + file;446- res.sendfile(path);447- });377 449- app.use(express.errorHandler());450- 451- assert.response(app,452- { url: '/../express.test.js' },453- { body: 'Forbidden', status: 403 });454- 455- assert.response(app,456- { url: '/user.json' },457- { body: '{"name":"tj"}'458- , status: 200, headers: { 'Content-Type': 'application/json' }});459- 460- assert.response(app,461- { url: '/hello.haml' },462- { body: '%p Hello World'463- , status: 200, headers: { 'Content-Type': 'application/octet-stream' }});464- 465- assert.response(app,466- { url: '/doesNotExist' },467- { body: 'Cannot GET /doesNotExist', status: 404 });468- 469- assert.response(app,470- { url: '/partials' },471- { body: 'Cannot GET /partials', status: 404 });472- 473- assert.response(app,474- { url: '/large.json' },475- { status: 200, headers: { 'Content-Type': 'application/json' }});476- },477- 478- 'test #sendfile(path, callback)': function(beforeExit){479- var app = express.createServer()480- , calls = 0;378+ assert.response(app,379+ { url: '/back', headers: { Referer: '/foo', Accept: 'text/plain', Host: 'foo.com' }},380+ { body: 'Moved Temporarily. Redirecting to http://foo.com/foo'381+ , status: 302, headers: { Location: 'http://foo.com/foo' }});382 482- app.get('/*', function(req, res, next){483- var file = req.params[0]484- , path = __dirname + '/fixtures/' + file;485- res.sendfile(path, function(err){486- if (err) return res.send('got an error');487- ++calls;383+ assert.response(app,384+ { url: '/back', headers: { Referrer: '/foo', Accept: 'text/plain', Host: 'foo.com' }},385+ { body: 'Moved Temporarily. Redirecting to http://foo.com/foo'386+ , status: 302, headers: { Location: 'http://foo.com/foo' }});387+ 388+ assert.response(app,389+ { url: '/home', headers: { Accept: 'text/plain', Host: 'foo.com' } },390+ { body: 'Moved Temporarily. Redirecting to http://foo.com/'391+ , status: 302, headers: { Location: 'http://foo.com/' }});392+ 393+ assert.response(app2,394+ { url: '/', headers: { Accept: 'text/plain', Host: 'foo.com' }},395+ { body: 'Moved Permanently. Redirecting to http://google.com'396+ , status: 301, headers: { Location: 'http://google.com' }});397+ 398+ assert.response(app2,399+ { url: '/back', headers: { Accept: 'text/plain', Host: 'foo.com' }},400+ { body: 'Moved Temporarily. Redirecting to http://foo.com/blog'401+ , status: 302, headers: { Location: 'http://foo.com/blog' }});402+ 403+ assert.response(app2,404+ { url: '/home', headers: { Accept: 'text/plain', Host: 'foo.com' }},405+ { body: 'Moved Temporarily. Redirecting to http://foo.com/blog'406+ , status: 302, headers: { Location: 'http://foo.com/blog' }});407+ 408+ assert.response(app2,409+ { url: '/google', headers: { Accept: 'text/plain', Host: 'foo.com' }},410+ { body: 'Moved Temporarily. Redirecting to http://google.com'411+ , status: 302, headers: { Location: 'http://google.com' }});412+ 413+ assert.response(app2,414+ { url: '/user/12', headers: { Accept: 'text/plain', Host: 'foo.com' }},415+ { body: 'Moved Temporarily. Redirecting to http://foo.com/user/12/blog'416+ , status: 302, headers: { Location: 'http://foo.com/user/12/blog', 'X-Foo': 'bar' }});417+ },418+ 419+ 'test #redirect() when mounted': function(){420+ var app = express.createServer()421+ , blog = express.createServer();422+ 423+ 424+ blog.get('/posts', function(req, res){425+ res.redirect('/posts/all');426 });489- });490- 491- assert.response(app,492- { url: '/forum' },493- { body: 'got an error' });494- 495- assert.response(app,496- { url: '/does-not-exist' },497- { body: 'got an error' });498- 499- assert.response(app,500- { url: '/large.json' },501- { headers: { 'Accept-Ranges': 'bytes' }});502- 503- beforeExit(function(){504- calls.should.equal(1);505- });506- },507- 508- 'test #sendfile(path, options, callback)': function(beforeExit){509- var app = express.createServer()510- , calls = 0;427 512- app.get('/*', function(req, res, next){513- var file = req.params[0]514- , path = __dirname + '/fixtures/' + file;515- res.sendfile(path, { maxAge: 3600000 }, function(err){516- if (err) return res.send('got an error');517- ++calls;428+ blog.get('/posts/all', function(req, res){429+ res.send('all blog posts');430 });519- });520- 521- assert.response(app,522- { url: '/does-not-exist' },523- { body: 'got an error' });524- 525- assert.response(app,526- { url: '/large.json' },527- { headers: {528- 'Accept-Ranges': 'bytes'529- , 'Cache-Control': 'public, max-age=3600'530- }});531- 532- beforeExit(function(){533- calls.should.equal(1);534- });535- },536- 537- 'test #sendfile() Accept-Ranges': function(){538- var app = express.createServer();431+ 432+ app.use('/blog', blog);433+ 434+ assert.response(app,435+ { url: '/blog/posts', headers: { Host: 'foo.com' }},436+ { status: 302437+ , headers: { Location: 'http://foo.com/blog/posts/all' }});438+ },439+ 440+ 'test #sendfile()': function(){441+ var app = express.createServer();442+ 443+ app.get('/*', function(req, res, next){444+ var file = req.params[0]445+ , path = __dirname + '/fixtures/' + file;446+ res.sendfile(path);447+ });448+ 449+ app.use(express.errorHandler());450+ 451+ assert.response(app,452+ { url: '/../express.test.js' },453+ { body: 'Forbidden', status: 403 });454+ 455+ assert.response(app,456+ { url: '/user.json' },457+ { body: '{"name":"tj"}'458+ , status: 200, headers: { 'Content-Type': 'application/json' }});459+ 460+ assert.response(app,461+ { url: '/hello.haml' },462+ { body: '%p Hello World'463+ , status: 200, headers: { 'Content-Type': 'application/octet-stream' }});464+ 465+ assert.response(app,466+ { url: '/doesNotExist' },467+ { body: 'Cannot GET /doesNotExist', status: 404 });468+ 469+ assert.response(app,470+ { url: '/partials' },471+ { body: 'Cannot GET /partials', status: 404 });472+ 473+ assert.response(app,474+ { url: '/large.json' },475+ { status: 200, headers: { 'Content-Type': 'application/json' }});476+ },477+ 478+ 'test #sendfile(path, callback)': function(beforeExit){479+ var app = express.createServer()480+ , calls = 0;481+ 482+ app.get('/*', function(req, res, next){483+ var file = req.params[0]484+ , path = __dirname + '/fixtures/' + file;485+ res.sendfile(path, function(err){486+ if (err) return res.send('got an error');487+ ++calls;488+ });489+ });490+ 491+ assert.response(app,492+ { url: '/forum' },493+ { body: 'got an error' });494+ 495+ assert.response(app,496+ { url: '/does-not-exist' },497+ { body: 'got an error' });498+ 499+ assert.response(app,500+ { url: '/large.json' },501+ { headers: { 'Accept-Ranges': 'bytes' }});502+ 503+ beforeExit(function(){504+ calls.should.equal(1);505+ });506+ },507+ 508+ 'test #sendfile(path, options, callback)': function(beforeExit){509+ var app = express.createServer()510+ , calls = 0;511+ 512+ app.get('/*', function(req, res, next){513+ var file = req.params[0]514+ , path = __dirname + '/fixtures/' + file;515+ res.sendfile(path, { maxAge: 3600000 }, function(err){516+ if (err) return res.send('got an error');517+ ++calls;518+ });519+ });520+ 521+ assert.response(app,522+ { url: '/does-not-exist' },523+ { body: 'got an error' });524+ 525+ assert.response(app,526+ { url: '/large.json' },527+ { headers: {528+ 'Accept-Ranges': 'bytes'529+ , 'Cache-Control': 'public, max-age=3600'530+ }});531+ 532+ beforeExit(function(){533+ calls.should.equal(1);534+ });535+ },536 537+ 'test #sendfile() Accept-Ranges': function(){538+ var app = express.createServer();539+ 540- app.get('/*', function(req, res, next){541- var file = req.params[0]542- , path = __dirname + '/fixtures/' + file;543- res.sendfile(path);544- });540+ app.get('/*', function(req, res, next){541+ var file = req.params[0]542+ , path = __dirname + '/fixtures/' + file;543+ res.sendfile(path);544+ });545+ 546+ assert.response(app,547+ { url: '/large.json' },548+ { headers: { 'Accept-Ranges': 'bytes' }});549+ },550 546- assert.response(app,547- { url: '/large.json' },548- { headers: { 'Accept-Ranges': 'bytes' }});549- },550- 551- 'test #sendfile() Range': function(){552- var app = express.createServer();551+ 'test #sendfile() Range': function(){552+ var app = express.createServer();553+ 554+ app.get('/*', function(req, res, next){555+ var file = req.params[0]556+ , path = __dirname + '/fixtures/' + file;557+ res.sendfile(path);558+ });559 554- app.get('/*', function(req, res, next){555- var file = req.params[0]556- , path = __dirname + '/fixtures/' + file;557- res.sendfile(path);558- });559- 560- assert.response(app,561- { url: '/large.json', headers: { 'Range': 'bytes=0-499' }},562- { headers: {563- 'Content-Type': 'application/json',564- 'Content-Range': 'bytes 0-499/2535'565- }, status: 206 });566- },560+ assert.response(app,561+ { url: '/large.json', headers: { 'Range': 'bytes=0-499' }},562+ { headers: {563+ 'Content-Type': 'application/json',564+ 'Content-Range': 'bytes 0-499/2535'565+ }, status: 206 });566+ },567- 568- 'test #sendfile() Range invalid syntax': function(){569- var app = express.createServer();567 571- app.get('/*', function(req, res, next){572- var file = req.params[0]573- , path = __dirname + '/fixtures/' + file;574- res.sendfile(path);575- });576- 577- assert.response(app,578- { url: '/large.json', headers: { 'Range': 'basdytes=asdf' }},579- { body: 'Requested Range Not Satisfiable', status: 416 });580- },581- 582- 'test #sendfile() Range invalid range': function(){583- var app = express.createServer();568+ 'test #sendfile() Range invalid syntax': function(){569+ var app = express.createServer();570+ 571+ app.get('/*', function(req, res, next){572+ var file = req.params[0]573+ , path = __dirname + '/fixtures/' + file;574+ res.sendfile(path);575+ });576 585- app.get('/*', function(req, res, next){586- var file = req.params[0]587- , path = __dirname + '/fixtures/' + file;588- res.sendfile(path);589- });590- 591- assert.response(app,592- { url: '/large.json', headers: { 'Range': 'bytes=500-10' }},593- { body: 'Requested Range Not Satisfiable', status: 416 });594- },577+ assert.response(app,578+ { url: '/large.json', headers: { 'Range': 'basdytes=asdf' }},579+ { body: 'Requested Range Not Satisfiable', status: 416 });580+ },595- 596- 'test #download()': function(beforeExit){597- var app = express.createServer()598- , calls = 0;581 600- app.get('/json', function(req, res, next){601- var path = __dirname + '/fixtures/user.json';602- res.download(path, 'account.json');603- });604- 605- app.get('/callback', function(req, res, next){606- res.download(__dirname + '/fixtures/pets.json', function(err){607- ++calls;582+ 'test #sendfile() Range invalid range': function(){583+ var app = express.createServer();584+ 585+ app.get('/*', function(req, res, next){586+ var file = req.params[0]587+ , path = __dirname + '/fixtures/' + file;588+ res.sendfile(path);589 });609- });610- 611- app.get('/*', function(req, res, next){612- res.download(__dirname + '/fixtures/' + req.params[0]);613- });614- 615- assert.response(app,616- { url: '/callback' },617- { body: '["tobi", "loki", "jane"]' });618- 619- assert.response(app,620- { url: '/user.json' },621- { body: '{"name":"tj"}', status: 200, headers: {622- 'Content-Type': 'application/json',623- 'Content-Disposition': 'attachment; filename="user.json"'624- }});625- 626- assert.response(app,627- { url: '/json' },628- { body: '{"name":"tj"}', status: 200, headers: {629- 'Content-Type': 'application/json',630- 'Content-Length': '13',631- 'Content-Disposition': 'attachment; filename="account.json"'632- }});590 634- assert.response(app,635- { url: '/missing' },636- function(res){637- assert.equal(null, res.headers['content-disposition']);591+ assert.response(app,592+ { url: '/large.json', headers: { 'Range': 'bytes=500-10' }},593+ { body: 'Requested Range Not Satisfiable', status: 416 });594+ },595+ 596+ 'test #download()': function(beforeExit){597+ var app = express.createServer()598+ , calls = 0;599+ 600+ app.get('/json', function(req, res, next){601+ var path = __dirname + '/fixtures/user.json';602+ res.download(path, 'account.json');603 });639- 640- assert.response(app,641- { url: '/some%20random%20text%20file.txt' },642- { body: 'hello' });643- 644- beforeExit(function(){645- calls.should.equal(1);646- });647- },648- 649- 'test #cookie() path default': function(){650- var app = express.createServer();651- 652- app.set('home', '/foo');653- 654- app.get('/', function(req, res){655- res.cookie('rememberme', 'yes', { expires: new Date(1), httpOnly: true });656- res.cookie('something', 'else');657- res.redirect('/');658- });659- 660- assert.response(app,661- { url: '/', headers: { Host: 'foo.com' }},662- function(res){663- res.headers['set-cookie']664- .should.eql(['rememberme=yes; path=/foo; expires=Thu, 01 Jan 1970 00:00:00 GMT; httpOnly', 'something=else; path=/foo']);604+ 605+ app.get('/callback', function(req, res, next){606+ res.download(__dirname + '/fixtures/pets.json', function(err){607+ ++calls;608+ });609 });666- },667- 668- 'test #cookie() explicit path': function(){669- var app = express.createServer();670- 671- app.set('/home', '/foo');672- 673- app.get('/', function(req, res){674- res.cookie('rememberme', 'yes', { path: '/', expires: new Date(1), httpOnly: true });675- res.cookie('something', 'else');676- res.redirect('/');677- });678- 679- assert.response(app,680- { url: '/', headers: { Host: 'foo.com' }},681- function(res){682- res.headers['set-cookie']683- .should.eql(['rememberme=yes; path=/; expires=Thu, 01 Jan 1970 00:00:00 GMT; httpOnly', 'something=else; path=/']);610+ 611+ app.get('/*', function(req, res, next){612+ res.download(__dirname + '/fixtures/' + req.params[0]);613 });685- },686- 687- 'test #cookie() null path': function(){688- var app = express.createServer();689- 690- app.set('/home', '/foo');691- 692- app.get('/', function(req, res){693- res.cookie('rememberme', 'yes', { path: null, expires: new Date(1), httpOnly: true });694- res.cookie('something', 'else', { path: null });695- res.redirect('/');696- });697- 698- assert.response(app,699- { url: '/', headers: { Host: 'foo.com' }},700- function(res){701- res.headers['set-cookie']702- .should.eql(['rememberme=yes; expires=Thu, 01 Jan 1970 00:00:00 GMT; httpOnly', 'something=else']);614+ 615+ assert.response(app,616+ { url: '/callback' },617+ { body: '["tobi", "loki", "jane"]' });618+ 619+ assert.response(app,620+ { url: '/user.json' },621+ { body: '{"name":"tj"}', status: 200, headers: {622+ 'Content-Type': 'application/json',623+ 'Content-Disposition': 'attachment; filename="user.json"'624+ }});625+ 626+ assert.response(app,627+ { url: '/json' },628+ { body: '{"name":"tj"}', status: 200, headers: {629+ 'Content-Type': 'application/json',630+ 'Content-Length': '13',631+ 'Content-Disposition': 'attachment; filename="account.json"'632+ }});633+ 634+ assert.response(app,635+ { url: '/missing' },636+ function(res){637+ assert.equal(null, res.headers['content-disposition']);638+ });639+ 640+ assert.response(app,641+ { url: '/some%20random%20text%20file.txt' },642+ { body: 'hello' });643+ 644+ beforeExit(function(){645+ calls.should.equal(1);646 });704- },705- 706- 'test #clearCookie() default path': function(){707- var app = express.createServer();708- 709- app.set('home', '/foo');710- 711- app.get('/', function(req, res){712- res.clearCookie('rememberme');713- res.redirect('/');714- });647+ },648 716- assert.response(app,717- { url: '/' },718- function(res){719- res.headers['set-cookie']720- .should.eql(['rememberme=; path=/foo; expires=Thu, 01 Jan 1970 00:00:00 GMT']);649+ 'test #cookie() path default': function(){650+ var app = express.createServer();651+ 652+ app.set('home', '/foo');653+ 654+ app.get('/', function(req, res){655+ res.cookie('rememberme', 'yes', { expires: new Date(1), httpOnly: true });656+ res.cookie('something', 'else');657+ res.redirect('/');658 });722- },723- 724- 'test #clearCookie() explicit path': function(){725- var app = express.createServer();726- 727- app.set('home', '/bar');728- 729- app.get('/', function(req, res){730- res.clearCookie('rememberme', { path: '/foo' });731- res.redirect('/');732- });659 734- assert.response(app,735- { url: '/' },736- function(res){737- res.headers['set-cookie']738- .should.eql(['rememberme=; path=/foo; expires=Thu, 01 Jan 1970 00:00:00 GMT']);660+ assert.response(app,661+ { url: '/', headers: { Host: 'foo.com' }},662+ function(res){663+ res.headers['set-cookie']664+ .should.eql(['rememberme=yes; path=/foo; expires=Thu, 01 Jan 1970 00:00:00 GMT; httpOnly', 'something=else; path=/foo']);665+ });666+ },667+ 668+ 'test #cookie() explicit path': function(){669+ var app = express.createServer();670+ 671+ app.set('/home', '/foo');672+ 673+ app.get('/', function(req, res){674+ res.cookie('rememberme', 'yes', { path: '/', expires: new Date(1), httpOnly: true });675+ res.cookie('something', 'else');676+ res.redirect('/');677 });740- },741- 742- 'test HEAD': function(){743- var app = express.createServer();678 745- app.get('/', function(req, res, next){746- res.send('Hello World');747- });679+ assert.response(app,680+ { url: '/', headers: { Host: 'foo.com' }},681+ function(res){682+ res.headers['set-cookie']683+ .should.eql(['rememberme=yes; path=/; expires=Thu, 01 Jan 1970 00:00:00 GMT; httpOnly', 'something=else; path=/']);684+ });685+ },686 687+ 'test #cookie() null path': function(){688+ var app = express.createServer();689+ 690+ app.set('/home', '/foo');691+ 692+ app.get('/', function(req, res){693+ res.cookie('rememberme', 'yes', { path: null, expires: new Date(1), httpOnly: true });694+ res.cookie('something', 'else', { path: null });695+ res.redirect('/');696+ });697+ 698+ assert.response(app,699+ { url: '/', headers: { Host: 'foo.com' }},700+ function(res){701+ res.headers['set-cookie']702+ .should.eql(['rememberme=yes; expires=Thu, 01 Jan 1970 00:00:00 GMT; httpOnly', 'something=else']);703+ });704+ },705+ 706+ 'test #clearCookie() default path': function(){707+ var app = express.createServer();708+ 709+ app.set('home', '/foo');710+ 711+ app.get('/', function(req, res){712+ res.clearCookie('rememberme');713+ res.redirect('/');714+ });715+ 716+ assert.response(app,717+ { url: '/' },718+ function(res){719+ res.headers['set-cookie']720+ .should.eql(['rememberme=; path=/foo; expires=Thu, 01 Jan 1970 00:00:00 GMT']);721+ });722+ },723+ 724+ 'test #clearCookie() explicit path': function(){725+ var app = express.createServer();726+ 727+ app.set('home', '/bar');728+ 729+ app.get('/', function(req, res){730+ res.clearCookie('rememberme', { path: '/foo' });731+ res.redirect('/');732+ });733+ 734+ assert.response(app,735+ { url: '/' },736+ function(res){737+ res.headers['set-cookie']738+ .should.eql(['rememberme=; path=/foo; expires=Thu, 01 Jan 1970 00:00:00 GMT']);739+ });740+ },741+ 742+ 'test HEAD': function(){743+ var app = express.createServer();744+ 745+ app.get('/', function(req, res, next){746+ res.send('Hello World');747+ });748+ 749- assert.response(app,750- { url: '/', method: 'HEAD' },751- { body: '', headers: { 'Content-Length': 11 }});752- },753- 754- 'test .charset with res.send()': function(){755- var app = express.createServer();756- 757- app.get('/', function(req, res){758- res.charset = 'ISO-8859-1';759- res.send('<p>hey</p>');760- });761- 762- assert.response(app,763- { url: '/' },764- { headers: { 'Content-Type': 'text/html; charset=ISO-8859-1' }});765- }749+ assert.response(app,750+ { url: '/', method: 'HEAD' },751+ { body: '', headers: { 'Content-Length': 11 }});752+ },753+ 754+ 'test .charset with res.send()': function(){755+ var app = express.createServer();756+ 757+ app.get('/', function(req, res){758+ res.charset = 'ISO-8859-1';759+ res.send('<p>hey</p>');760+ });761+ 762+ assert.response(app,763+ { url: '/' },764+ { headers: { 'Content-Type': 'text/html; charset=ISO-8859-1' }});765+ }766 };