response.test.js
This file
Path
test/response.test.js
770 lines · 20.7 KiB · JavaScript
Last changed by
Removed `res.send()` with no args support for 204Tj Holowaychukbefore adoptionb13ec85
History of this fileTake it
12/**3 * Module dependencies.4 */56var express = require('express')7 , Stream = require('stream').Stream8 , assert = require('assert')9 , should = require('should');1011module.exports = {12 'test #json()': function(){13 var app = express.createServer()14 , json = 'application/json; charset=utf-8';1516 app.get('/user', function(req, res, next){17 res.json({ name: 'tj' });18 });1920 app.get('/string', function(req, res, next){21 res.json('whoop!');22 });2324 app.get('/error', function(req, res, next){25 res.json('oh noes!', 500);26 });2728 app.get('/headers', function(req, res, next){29 res.json(undefined, { 'X-Foo': 'bar' }, 302);30 });3132 assert.response(app,33 { url: '/error' },34 { body: '"oh noes!"'35 , status: 50036 , headers: { 'Content-Type': json }});3738 assert.response(app,39 { url: '/string' },40 { body: '"whoop!"'41 , headers: {42 'Content-Type': json43 , 'Content-Length': 844 }});4546 assert.response(app,47 { url: '/user' },48 { body: '{"name":"tj"}', headers: { 'Content-Type': json }});49 },5051 'test #status()': function(){52 var app = express.createServer();5354 app.get('/error', function(req, res, next){55 res.status(500).send('OH NO');56 });5758 assert.response(app,59 { url: '/error' },60 { body: 'OH NO', status: 500 });61 },6263 'test #send()': function(){64 var app = express.createServer();65 66 app.get('/html', function(req, res){67 res.send('<p>test</p>', { 'Content-Language': 'en' });68 });6970 app.get('/json', function(req, res){71 res.header('X-Foo', 'bar');72 res.send({ foo: 'bar' }, { 'X-Foo': 'baz' }, 201);73 });74 75 app.get('/text', function(req, res){76 res.header('X-Foo', 'bar');77 res.contentType('txt');78 res.send('wahoo');79 });80 81 app.get('/status', function(req, res){82 res.send(404);83 });84 85 app.get('/status/text', function(req, res){86 res.send('Oh noes!', 404);87 });88 89 app.get('/error', function(req, res){90 res.send('Oh shit!', { 'Content-Type': 'text/plain' }, 500);91 });92 93 app.get('/buffer', function(req, res){94 res.send(new Buffer('wahoo!'));95 });96 97 app.get('/204', function(req, res, next){98 res.send(204);99 });100 101 app.get('/undefined', function(req, res, next){102 res.send(undefined);103 });104105 app.get('/bool', function(req, res, next){106 res.send(true);107 });108109 assert.response(app,110 { url: '/bool' },111 { body: 'true'112 , headers: { 'Content-Type': 'application/json; charset=utf-8' }});113114 assert.response(app,115 { url: '/html' },116 { body: '<p>test</p>'117 , headers: {118 'Content-Language': 'en'119 , 'Content-Type': 'text/html; charset=utf-8'120 }});121 122 assert.response(app,123 { url: '/json' },124 { body: '{"foo":"bar"}'125 , status: 201126 , headers: {127 'Content-Type': 'application/json; charset=utf-8'128 , 'X-Foo': 'baz'129 }});130 131 assert.response(app,132 { url: '/text' },133 { body: 'wahoo'134 , headers: {135 'Content-Type': 'text/plain'136 , 'X-Foo': 'bar'137 }});138139 assert.response(app,140 { url: '/status/text' },141 { body: 'Oh noes!', status: 404 });142143 assert.response(app,144 { url: '/status' },145 { body: 'Not Found'146 , status: 404147 , headers: { 'Content-Type': 'text/plain' }});148 149 assert.response(app,150 { url: '/error' },151 { body: 'Oh shit!'152 , status: 500153 , headers: {154 'Content-Type': 'text/plain; charset=utf-8'155 , 'Content-Length': '8'156 }});157 158 assert.response(app,159 { url: '/buffer' },160 { body: 'wahoo!'161 , headers: {162 'Content-Type': 'application/octet-stream'163 , 'Content-Length': '6'164 }});165 166 assert.response(app,167 { url: '/204' },168 { status: 204 }, function(res){169 assert.equal(undefined, res.headers['content-type']);170 assert.equal(undefined, res.headers['content-length']);171 });172173 assert.response(app,174 { url: '/undefined' },175 { status: 500 });176177 assert.response(app,178 { url: '/json?callback=test' },179 { body: '{"foo":"bar"}'180 , status: 201181 , headers: {182 'Content-Type': 'application/json; charset=utf-8'183 , 'X-Foo': 'baz'184 }});185 },186187 'test #send() JSONP': function(){188 var app = express.createServer();189190 app.enable('jsonp callback');191192 app.get('/jsonp', function(req, res){193 res.header('X-Foo', 'bar');194 res.send({ foo: 'bar' }, { 'X-Foo': 'baz' }, 201);195 });196197 assert.response(app,198 { url: '/jsonp?callback=test' },199 { body: 'test({"foo":"bar"});'200 , status: 201201 , headers: {202 'Content-Type': 'text/javascript; charset=utf-8'203 , 'X-Foo': 'baz'204 }});205 206 assert.response(app,207 { url: '/jsonp?callback=baz' },208 { body: 'baz({"foo":"bar"});'209 , status: 201, headers: {210 'Content-Type': 'text/javascript; charset=utf-8'211 , 'X-Foo': 'baz'212 }});213 214 assert.response(app,215 { url: '/jsonp?callback=invalid()[]' },216 { body: 'invalid({"foo":"bar"});'217 , status: 201218 , headers: {219 'Content-Type': 'text/javascript; charset=utf-8'220 , 'X-Foo': 'baz'221 }});222 },223224 'test #json() JSONP': function(){225 var app = express.createServer();226227 app.enable('jsonp callback');228229 app.get('/jsonp', function(req, res){230 res.header('X-Foo', 'bar');231 res.json({ foo: 'bar' }, { 'X-Foo': 'baz' }, 201);232 });233234 assert.response(app,235 { url: '/jsonp?callback=test' },236 { body: 'test({"foo":"bar"});'237 , status: 201238 , headers: {239 'Content-Type': 'text/javascript; charset=utf-8'240 , 'X-Foo': 'baz'241 }});242 243 assert.response(app,244 { url: '/jsonp?callback=baz' },245 { body: 'baz({"foo":"bar"});'246 , status: 201, headers: {247 'Content-Type': 'text/javascript; charset=utf-8'248 , 'X-Foo': 'baz'249 }});250 251 assert.response(app,252 { url: '/jsonp?callback=invalid()[]' },253 { body: 'invalid({"foo":"bar"});'254 , status: 201255 , headers: {256 'Content-Type': 'text/javascript; charset=utf-8'257 , 'X-Foo': 'baz'258 }});259 },260261 'test #contentType()': function(){262 var app = express.createServer();263 264 app.get('/html', function(req, res){265 res.contentType('index.html');266 res.writeHead(200, res.headers);267 res.end('<p>yay</p>');268 });269 270 app.get('/json', function(req, res, next){271 res.contentType('json');272 res.send('{"foo":"bar"}');273 });274275 app.get('/literal', function(req, res){276 res.contentType('application/json');277 res.send('{"foo":"bar"}')278 });279280 assert.response(app,281 { url: '/literal' },282 { headers: { 'Content-Type': 'application/json' }}); 283 assert.response(app,284 { url: '/html' },285 { body: '<p>yay</p>', headers: { 'Content-Type': 'text/html' }});286 assert.response(app,287 { url: '/json' },288 { headers: { 'Content-Type': 'application/json' }});289 },290 291 'test #attachment()': function(){292 var app = express.createServer();293 294 app.get('/style.css', function(req, res){295 res.attachment();296 res.send('some stylezzz');297 });298299 app.get('/*', function(req, res){300 res.attachment(req.params[0]);301 res.send('whatever');302 });303 304 assert.response(app,305 { url: '/javascripts/jquery.js' },306 { body: 'whatever'307 , headers: { 'Content-Type': 'application/javascript'308 , 'Content-Disposition': 'attachment; filename="jquery.js"' }});309310 assert.response(app,311 { url: '/style.css' },312 { body: 'some stylezzz'313 , headers: { 'Content-Type': 'text/html; charset=utf-8'314 , 'Content-Disposition': 'attachment' }});315 },316 317 'test #redirect()': function(){318 var app = express.createServer()319 , app2 = express.createServer();320 321 app2.set('home', '/blog');322323 app2.redirect('google', 'http://google.com');324325 app2.redirect('blog', function(req, res){326 return req.params.id327 ? '/user/' + req.params.id + '/blog'328 : null;329 });330 331 app.get('/', function(req, res){332 res.redirect('http://google.com', 301);333 });334 335 app.get('/back', function(req, res){336 res.redirect('back');337 });338 339 app.get('/home', function(req, res){340 res.redirect('home');341 });342 343 app.get('/html', function(req, res){344 res.redirect('http://google.com');345 });346 347 app2.get('/', function(req, res){348 res.redirect('http://google.com', 301);349 });350 351 app2.get('/back', function(req, res){352 res.redirect('back');353 });354 355 app2.get('/home', function(req, res){356 res.redirect('home');357 });358 359 app2.get('/google', function(req, res){360 res.redirect('google');361 });362363 app2.get('/user/:id', function(req, res){364 res.header('X-Foo', 'bar');365 res.redirect('blog');366 });367368 assert.response(app,369 { url: '/html', headers: { Accept: 'text/html,text/plain', Host: 'foo.com' }},370 { body: '<p>Moved Temporarily. Redirecting to <a href="http://google.com">http://google.com</a></p>' });371372 assert.response(app,373 { url: '/', headers: { Accept: 'text/plain', Host: 'foo.com' }},374 { body: 'Moved Permanently. Redirecting to http://google.com'375 , status: 301, headers: { Location: 'http://google.com' }});376377 assert.response(app,378 { url: '/back', headers: { Accept: 'text/plain', Host: 'foo.com' }},379 { body: 'Moved Temporarily. Redirecting to http://foo.com/'380 , status: 302, headers: { Location: 'http://foo.com/', 'Content-Type': 'text/plain' }});381382 assert.response(app,383 { url: '/back', headers: { Referer: '/foo', Accept: 'text/plain', Host: 'foo.com' }},384 { body: 'Moved Temporarily. Redirecting to http://foo.com/foo'385 , status: 302, headers: { Location: 'http://foo.com/foo' }});386387 assert.response(app,388 { url: '/back', headers: { Referrer: '/foo', Accept: 'text/plain', Host: 'foo.com' }},389 { body: 'Moved Temporarily. Redirecting to http://foo.com/foo'390 , status: 302, headers: { Location: 'http://foo.com/foo' }});391392 assert.response(app,393 { url: '/home', headers: { Accept: 'text/plain', Host: 'foo.com' } },394 { body: 'Moved Temporarily. Redirecting to http://foo.com/'395 , status: 302, headers: { Location: 'http://foo.com/' }});396397 assert.response(app2,398 { url: '/', headers: { Accept: 'text/plain', Host: 'foo.com' }},399 { body: 'Moved Permanently. Redirecting to http://google.com'400 , status: 301, headers: { Location: 'http://google.com' }});401402 assert.response(app2,403 { url: '/back', headers: { Accept: 'text/plain', Host: 'foo.com' }},404 { body: 'Moved Temporarily. Redirecting to http://foo.com/blog'405 , status: 302, headers: { Location: 'http://foo.com/blog' }});406407 assert.response(app2,408 { url: '/home', headers: { Accept: 'text/plain', Host: 'foo.com' }},409 { body: 'Moved Temporarily. Redirecting to http://foo.com/blog'410 , status: 302, headers: { Location: 'http://foo.com/blog' }});411412 assert.response(app2,413 { url: '/google', headers: { Accept: 'text/plain', Host: 'foo.com' }},414 { body: 'Moved Temporarily. Redirecting to http://google.com'415 , status: 302, headers: { Location: 'http://google.com' }});416417 assert.response(app2,418 { url: '/user/12', headers: { Accept: 'text/plain', Host: 'foo.com' }},419 { body: 'Moved Temporarily. Redirecting to http://foo.com/user/12/blog'420 , status: 302, headers: { Location: 'http://foo.com/user/12/blog', 'X-Foo': 'bar' }});421 },422423 'test #redirect() when mounted': function(){424 var app = express.createServer()425 , blog = express.createServer();426427428 blog.get('/posts', function(req, res){429 res.redirect('/posts/all');430 });431432 blog.get('/posts/all', function(req, res){433 res.send('all blog posts');434 });435436 app.use('/blog', blog);437438 assert.response(app,439 { url: '/blog/posts', headers: { Host: 'foo.com' }},440 { status: 302441 , headers: { Location: 'http://foo.com/blog/posts/all' }});442 },443 444 'test #sendfile()': function(){445 var app = express.createServer();446447 app.get('/*', function(req, res, next){448 var file = req.params[0]449 , path = __dirname + '/fixtures/' + file;450 res.sendfile(path);451 });452 453 app.use(express.errorHandler());454455 assert.response(app,456 { url: '/../express.test.js' },457 { body: 'Forbidden', status: 403 });458459 assert.response(app,460 { url: '/user.json' },461 { body: '{"name":"tj"}'462 , status: 200, headers: { 'Content-Type': 'application/json' }});463464 assert.response(app,465 { url: '/hello.haml' },466 { body: '%p Hello World'467 , status: 200, headers: { 'Content-Type': 'application/octet-stream' }});468469 assert.response(app,470 { url: '/doesNotExist' },471 { body: 'Cannot GET /doesNotExist', status: 404 });472473 assert.response(app,474 { url: '/partials' },475 { body: 'Cannot GET /partials', status: 404 });476477 assert.response(app,478 { url: '/large.json' },479 { status: 200, headers: { 'Content-Type': 'application/json' }});480 },481 482 'test #sendfile(path, callback)': function(beforeExit){483 var app = express.createServer()484 , calls = 0;485 486 app.get('/*', function(req, res, next){487 var file = req.params[0]488 , path = __dirname + '/fixtures/' + file;489 res.sendfile(path, function(err){490 if (err) return res.send('got an error');491 ++calls;492 });493 });494495 assert.response(app,496 { url: '/forum' },497 { body: 'got an error' });498499 assert.response(app,500 { url: '/does-not-exist' },501 { body: 'got an error' });502503 assert.response(app,504 { url: '/large.json' },505 { headers: { 'Accept-Ranges': 'bytes' }});506507 beforeExit(function(){508 calls.should.equal(1);509 });510 },511 512 'test #sendfile(path, options, callback)': function(beforeExit){513 var app = express.createServer()514 , calls = 0;515 516 app.get('/*', function(req, res, next){517 var file = req.params[0]518 , path = __dirname + '/fixtures/' + file;519 res.sendfile(path, { maxAge: 3600000 }, function(err){520 if (err) return res.send('got an error');521 ++calls;522 });523 });524525 assert.response(app,526 { url: '/does-not-exist' },527 { body: 'got an error' });528529 assert.response(app,530 { url: '/large.json' },531 { headers: {532 'Accept-Ranges': 'bytes'533 , 'Cache-Control': 'public, max-age=3600'534 }});535536 beforeExit(function(){537 calls.should.equal(1);538 });539 },540 541 'test #sendfile() Accept-Ranges': function(){542 var app = express.createServer();543 544 app.get('/*', function(req, res, next){545 var file = req.params[0]546 , path = __dirname + '/fixtures/' + file;547 res.sendfile(path);548 });549 550 assert.response(app,551 { url: '/large.json' },552 { headers: { 'Accept-Ranges': 'bytes' }});553 },554 555 'test #sendfile() Range': function(){556 var app = express.createServer();557 558 app.get('/*', function(req, res, next){559 var file = req.params[0]560 , path = __dirname + '/fixtures/' + file;561 res.sendfile(path);562 });563 564 assert.response(app,565 { url: '/large.json', headers: { 'Range': 'bytes=0-499' }},566 { headers: {567 'Content-Type': 'application/json',568 'Content-Range': 'bytes 0-499/2535'569 }, status: 206 });570 },571 572 'test #sendfile() Range invalid syntax': function(){573 var app = express.createServer();574 575 app.get('/*', function(req, res, next){576 var file = req.params[0]577 , path = __dirname + '/fixtures/' + file;578 res.sendfile(path);579 });580 581 assert.response(app,582 { url: '/large.json', headers: { 'Range': 'basdytes=asdf' }},583 { body: 'Requested Range Not Satisfiable', status: 416 });584 },585 586 'test #sendfile() Range invalid range': function(){587 var app = express.createServer();588 589 app.get('/*', function(req, res, next){590 var file = req.params[0]591 , path = __dirname + '/fixtures/' + file;592 res.sendfile(path);593 });594 595 assert.response(app,596 { url: '/large.json', headers: { 'Range': 'bytes=500-10' }},597 { body: 'Requested Range Not Satisfiable', status: 416 });598 },599 600 'test #download()': function(beforeExit){601 var app = express.createServer()602 , calls = 0;603 604 app.get('/json', function(req, res, next){605 var path = __dirname + '/fixtures/user.json';606 res.download(path, 'account.json');607 });608609 app.get('/callback', function(req, res, next){610 res.download(__dirname + '/fixtures/pets.json', function(err){611 ++calls;612 });613 });614615 app.get('/*', function(req, res, next){616 res.download(__dirname + '/fixtures/' + req.params[0]);617 });618619 assert.response(app,620 { url: '/callback' },621 { body: '["tobi", "loki", "jane"]' });622623 assert.response(app,624 { url: '/user.json' },625 { body: '{"name":"tj"}', status: 200, headers: {626 'Content-Type': 'application/json',627 'Content-Disposition': 'attachment; filename="user.json"'628 }});629 630 assert.response(app,631 { url: '/json' },632 { body: '{"name":"tj"}', status: 200, headers: {633 'Content-Type': 'application/json',634 'Content-Length': '13',635 'Content-Disposition': 'attachment; filename="account.json"'636 }});637 638 assert.response(app,639 { url: '/missing' },640 function(res){641 assert.equal(null, res.headers['content-disposition']);642 });643644 assert.response(app,645 { url: '/some%20random%20text%20file.txt' },646 { body: 'hello' });647648 beforeExit(function(){649 calls.should.equal(1);650 });651 },652 653 'test #cookie() path default': function(){654 var app = express.createServer();655656 app.set('home', '/foo');657658 app.get('/', function(req, res){659 res.cookie('rememberme', 'yes', { expires: new Date(1), httpOnly: true });660 res.cookie('something', 'else');661 res.redirect('/');662 });663664 assert.response(app,665 { url: '/', headers: { Host: 'foo.com' }},666 function(res){667 res.headers['set-cookie']668 .should.eql(['rememberme=yes; path=/foo; expires=Thu, 01 Jan 1970 00:00:00 GMT; httpOnly', 'something=else; path=/foo']);669 });670 },671672 'test #cookie() explicit path': function(){673 var app = express.createServer();674675 app.set('/home', '/foo');676677 app.get('/', function(req, res){678 res.cookie('rememberme', 'yes', { path: '/', expires: new Date(1), httpOnly: true });679 res.cookie('something', 'else');680 res.redirect('/');681 });682683 assert.response(app,684 { url: '/', headers: { Host: 'foo.com' }},685 function(res){686 res.headers['set-cookie']687 .should.eql(['rememberme=yes; path=/; expires=Thu, 01 Jan 1970 00:00:00 GMT; httpOnly', 'something=else; path=/']);688 });689 },690691 'test #cookie() null path': function(){692 var app = express.createServer();693694 app.set('/home', '/foo');695696 app.get('/', function(req, res){697 res.cookie('rememberme', 'yes', { path: null, expires: new Date(1), httpOnly: true });698 res.cookie('something', 'else', { path: null });699 res.redirect('/');700 });701702 assert.response(app,703 { url: '/', headers: { Host: 'foo.com' }},704 function(res){705 res.headers['set-cookie']706 .should.eql(['rememberme=yes; expires=Thu, 01 Jan 1970 00:00:00 GMT; httpOnly', 'something=else']);707 });708 },709710 'test #clearCookie() default path': function(){711 var app = express.createServer();712713 app.set('home', '/foo');714715 app.get('/', function(req, res){716 res.clearCookie('rememberme');717 res.redirect('/');718 });719 720 assert.response(app,721 { url: '/' },722 function(res){723 res.headers['set-cookie']724 .should.eql(['rememberme=; path=/foo; expires=Thu, 01 Jan 1970 00:00:00 GMT']);725 });726 },727728 'test #clearCookie() explicit path': function(){729 var app = express.createServer();730731 app.set('home', '/bar');732733 app.get('/', function(req, res){734 res.clearCookie('rememberme', { path: '/foo' });735 res.redirect('/');736 });737 738 assert.response(app,739 { url: '/' },740 function(res){741 res.headers['set-cookie']742 .should.eql(['rememberme=; path=/foo; expires=Thu, 01 Jan 1970 00:00:00 GMT']);743 });744 },745746 'test HEAD': function(){747 var app = express.createServer();748 749 app.get('/', function(req, res, next){750 res.send('Hello World');751 });752 753 assert.response(app,754 { url: '/', method: 'HEAD' },755 { body: '', headers: { 'Content-Length': 11 }});756 },757 758 'test .charset with res.send()': function(){759 var app = express.createServer();760761 app.get('/', function(req, res){762 res.charset = 'ISO-8859-1';763 res.send('<p>hey</p>');764 });765766 assert.response(app,767 { url: '/' },768 { headers: { 'Content-Type': 'text/html; charset=ISO-8859-1' }});769 }770};