response.test.js
This file
Path
test/response.test.js
587 lines · 15.6 KiB · JavaScript
Last changed by
Added test for res.contentType() literalTj Holowaychukbefore adoption1fe0aea
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 #send()': function(){13 var app = express.createServer();14 15 app.get('/html', function(req, res){16 res.send('<p>test</p>', { 'Content-Language': 'en' });17 });18 19 app.get('/json', function(req, res){20 res.header('X-Foo', 'bar');21 res.send({ foo: 'bar' }, { 'X-Foo': 'baz' }, 201);22 });23 24 app.get('/jsonp', function(req, res){25 app.enable('jsonp callback');26 res.header('X-Foo', 'bar');27 res.send({ foo: 'bar' }, { 'X-Foo': 'baz' }, 201);28 app.disable('jsonp callback');29 });30 31 app.get('/text', function(req, res){32 res.header('X-Foo', 'bar');33 res.contentType('.txt');34 res.send('wahoo');35 });36 37 app.get('/status', function(req, res){38 res.send(404);39 });40 41 app.get('/error', function(req, res){42 res.send('Oh shit!', { 'Content-Type': 'text/plain' }, 500);43 });44 45 app.get('/buffer', function(req, res){46 res.send(new Buffer('wahoo!'));47 });48 49 app.get('/noargs', function(req, res, next){50 res.send();51 });52 53 assert.response(app,54 { url: '/html' },55 { body: '<p>test</p>'56 , headers: {57 'Content-Language': 'en'58 , 'Content-Type': 'text/html; charset=utf8'59 }});60 61 assert.response(app,62 { url: '/json' },63 { body: '{"foo":"bar"}'64 , status: 20165 , headers: {66 'Content-Type': 'application/json'67 , 'X-Foo': 'baz'68 }});69 70 assert.response(app,71 { url: '/jsonp?callback=test' },72 { body: 'test({"foo":"bar"});'73 , status: 20174 , headers: {75 'Content-Type': 'text/javascript'76 , 'X-Foo': 'baz'77 }});78 79 assert.response(app,80 { url: '/jsonp?callback=baz' },81 { body: 'baz({"foo":"bar"});'82 , status: 201, headers: {83 'Content-Type': 'text/javascript'84 , 'X-Foo': 'baz'85 }});86 87 assert.response(app,88 { url: '/jsonp?callback=invalid()[]' },89 { body: 'invalid({"foo":"bar"});'90 , status: 20191 , headers: {92 'Content-Type': 'text/javascript'93 , 'X-Foo': 'baz'94 }});95 96 assert.response(app,97 { url: '/json?callback=test' },98 { body: '{"foo":"bar"}'99 , status: 201100 , headers: {101 'Content-Type': 'application/json'102 , 'X-Foo': 'baz'103 }});104 105 assert.response(app,106 { url: '/text' },107 { body: 'wahoo'108 , headers: {109 'Content-Type': 'text/plain'110 , 'X-Foo': 'bar'111 }});112 113 assert.response(app,114 { url: '/status' },115 { body: 'Not Found'116 , status: 404117 , headers: { 'Content-Type': 'text/plain' }});118 119 assert.response(app,120 { url: '/error' },121 { body: 'Oh shit!'122 , status: 500123 , headers: {124 'Content-Type': 'text/plain; charset=utf8'125 , 'Content-Length': '8'126 }});127 128 assert.response(app,129 { url: '/buffer' },130 { body: 'wahoo!'131 , headers: {132 'Content-Type': 'application/octet-stream'133 , 'Content-Length': '6'134 }});135 136 assert.response(app,137 { url: '/noargs' },138 { status: 204 }, function(res){139 assert.equal(undefined, res.headers['content-type']);140 assert.equal(undefined, res.headers['content-length']);141 });142 },143 144 'test #contentType()': function(){145 var app = express.createServer();146 147 app.get('/html', function(req, res){148 res.contentType('index.html');149 res.writeHead(200, res.headers);150 res.end('<p>yay</p>');151 });152 153 app.get('/json', function(req, res, next){154 res.contentType('json');155 res.send('{"foo":"bar"}');156 });157158 app.get('/literal', function(req, res){159 res.contentType('application/json');160 res.send('{"foo":"bar"}')161 });162163 assert.response(app,164 { url: '/literal' },165 { headers: { 'Content-Type': 'application/json' }}); 166 assert.response(app,167 { url: '/html' },168 { body: '<p>yay</p>', headers: { 'Content-Type': 'text/html' }});169 assert.response(app,170 { url: '/json' },171 { headers: { 'Content-Type': 'application/json' }});172 },173 174 'test #attachment()': function(){175 var app = express.createServer();176 177 app.get('/style.css', function(req, res){178 res.attachment();179 res.send('some stylezzz');180 });181182 app.get('/*', function(req, res){183 res.attachment(req.params[0]);184 res.send('whatever');185 });186 187 assert.response(app,188 { url: '/javascripts/jquery.js' },189 { body: 'whatever'190 , headers: { 'Content-Disposition': 'attachment; filename="jquery.js"' }});191192 assert.response(app,193 { url: '/style.css' },194 { body: 'some stylezzz'195 , headers: { 'Content-Disposition': 'attachment' }});196 },197 198 'test #redirect()': function(){199 var app = express.createServer()200 , app2 = express.createServer();201 202 app2.set('home', '/blog');203204 app2.redirect('google', 'http://google.com');205206 app2.redirect('blog', function(req, res){207 return req.params.id208 ? '/user/' + req.params.id + '/blog'209 : null;210 });211 212 app.get('/', function(req, res){213 res.redirect('http://google.com', 301);214 });215 216 app.get('/back', function(req, res){217 res.redirect('back');218 });219 220 app.get('/home', function(req, res){221 res.redirect('home');222 });223 224 app.get('/html', function(req, res){225 res.redirect('http://google.com');226 });227 228 app2.get('/', function(req, res){229 res.redirect('http://google.com', 301);230 });231 232 app2.get('/back', function(req, res){233 res.redirect('back');234 });235 236 app2.get('/home', function(req, res){237 res.redirect('home');238 });239 240 app2.get('/google', function(req, res){241 res.redirect('google');242 });243244 app2.get('/user/:id', function(req, res){245 res.header('X-Foo', 'bar');246 res.redirect('blog');247 });248249 assert.response(app,250 { url: '/html', headers: { Accept: 'text/html,text/plain' }},251 { body: '<p>Moved Temporarily. Redirecting to <a href="http://google.com">http://google.com</a></p>' });252253 assert.response(app,254 { url: '/', headers: { Accept: 'text/plain' }},255 { body: 'Moved Permanently. Redirecting to http://google.com'256 , status: 301, headers: { Location: 'http://google.com' }});257258 assert.response(app,259 { url: '/back', headers: { Accept: 'text/plain' }},260 { body: 'Moved Temporarily. Redirecting to /'261 , status: 302, headers: { Location: '/', 'Content-Type': 'text/plain' }});262263 assert.response(app,264 { url: '/back', headers: { Referer: '/foo', Accept: 'text/plain' }},265 { body: 'Moved Temporarily. Redirecting to /foo'266 , status: 302, headers: { Location: '/foo' }});267268 assert.response(app,269 { url: '/back', headers: { Referrer: '/foo', Accept: 'text/plain' }},270 { body: 'Moved Temporarily. Redirecting to /foo'271 , status: 302, headers: { Location: '/foo' }});272273 assert.response(app,274 { url: '/home', headers: { Accept: 'text/plain' } },275 { body: 'Moved Temporarily. Redirecting to /'276 , status: 302, headers: { Location: '/' }});277278 assert.response(app2,279 { url: '/', headers: { Accept: 'text/plain' }},280 { body: 'Moved Permanently. Redirecting to http://google.com'281 , status: 301, headers: { Location: 'http://google.com' }});282283 assert.response(app2,284 { url: '/back', headers: { Accept: 'text/plain' }},285 { body: 'Moved Temporarily. Redirecting to /blog'286 , status: 302, headers: { Location: '/blog' }});287288 assert.response(app2,289 { url: '/home', headers: { Accept: 'text/plain' }},290 { body: 'Moved Temporarily. Redirecting to /blog'291 , status: 302, headers: { Location: '/blog' }});292293 assert.response(app2,294 { url: '/google', headers: { Accept: 'text/plain' }},295 { body: 'Moved Temporarily. Redirecting to http://google.com'296 , status: 302, headers: { Location: 'http://google.com' }});297298 assert.response(app2,299 { url: '/user/12', headers: { Accept: 'text/plain' }},300 { body: 'Moved Temporarily. Redirecting to /user/12/blog'301 , status: 302, headers: { Location: '/user/12/blog', 'X-Foo': 'bar' }});302 },303304 'test #redirect() when mounted': function(){305 var app = express.createServer()306 , blog = express.createServer();307308309 blog.get('/posts', function(req, res){310 res.redirect('/posts/all');311 });312313 blog.get('/posts/all', function(req, res){314 res.send('all blog posts');315 });316317 app.use('/blog', blog);318319 assert.response(app,320 { url: '/blog/posts' },321 { status: 302322 , headers: { Location: '/blog/posts/all' }});323 },324 325 'test #sendfile()': function(){326 var app = express.createServer();327328 app.get('/*', function(req, res, next){329 var file = req.params[0]330 , path = __dirname + '/fixtures/' + file;331 res.sendfile(path);332 });333 334 app.use(express.errorHandler());335336 assert.response(app,337 { url: '/../express.test.js' },338 { body: 'Forbidden', status: 403 });339340 assert.response(app,341 { url: '/user.json' },342 { body: '{"name":"tj"}'343 , status: 200, headers: { 'Content-Type': 'application/json' }});344345 assert.response(app,346 { url: '/hello.haml' },347 { body: '%p Hello World'348 , status: 200, headers: { 'Content-Type': 'application/octet-stream' }});349350 assert.response(app,351 { url: '/doesNotExist' },352 { body: 'Cannot GET /doesNotExist', status: 404 });353354 assert.response(app,355 { url: '/partials' },356 { body: 'Cannot GET /partials', status: 404 });357358 assert.response(app,359 { url: '/large.json' },360 { status: 200, headers: { 'Content-Type': 'application/json' }});361 },362 363 'test #sendfile(path, callback)': function(beforeExit){364 var app = express.createServer()365 , calls = 0;366 367 app.get('/*', function(req, res, next){368 var file = req.params[0]369 , path = __dirname + '/fixtures/' + file;370 res.sendfile(path, function(err){371 if (err) return res.send('got an error');372 ++calls;373 });374 });375376 assert.response(app,377 { url: '/forum' },378 { body: 'got an error' });379380 assert.response(app,381 { url: '/does-not-exist' },382 { body: 'got an error' });383384 assert.response(app,385 { url: '/large.json' },386 { headers: { 'Accept-Ranges': 'bytes' }});387388 beforeExit(function(){389 calls.should.equal(1);390 });391 },392 393 'test #sendfile(path, options, callback)': function(beforeExit){394 var app = express.createServer()395 , calls = 0;396 397 app.get('/*', function(req, res, next){398 var file = req.params[0]399 , path = __dirname + '/fixtures/' + file;400 res.sendfile(path, { maxAge: 3600000 }, function(err){401 if (err) return res.send('got an error');402 ++calls;403 });404 });405406 assert.response(app,407 { url: '/does-not-exist' },408 { body: 'got an error' });409410 assert.response(app,411 { url: '/large.json' },412 { headers: {413 'Accept-Ranges': 'bytes'414 , 'Cache-Control': 'public, max-age=3600'415 }});416417 beforeExit(function(){418 calls.should.equal(1);419 });420 },421 422 'test #sendfile() Accept-Ranges': function(){423 var app = express.createServer();424 425 app.get('/*', function(req, res, next){426 var file = req.params[0]427 , path = __dirname + '/fixtures/' + file;428 res.sendfile(path);429 });430 431 assert.response(app,432 { url: '/large.json' },433 { headers: { 'Accept-Ranges': 'bytes' }});434 },435 436 'test #sendfile() Range': function(){437 var app = express.createServer();438 439 app.get('/*', function(req, res, next){440 var file = req.params[0]441 , path = __dirname + '/fixtures/' + file;442 res.sendfile(path);443 });444 445 assert.response(app,446 { url: '/large.json', headers: { 'Range': 'bytes=0-499' }},447 { headers: {448 'Content-Type': 'application/json',449 'Content-Range': 'bytes 0-499/2535'450 }, status: 206 });451 },452 453 'test #sendfile() Range invalid syntax': function(){454 var app = express.createServer();455 456 app.get('/*', function(req, res, next){457 var file = req.params[0]458 , path = __dirname + '/fixtures/' + file;459 res.sendfile(path);460 });461 462 assert.response(app,463 { url: '/large.json', headers: { 'Range': 'basdytes=asdf' }},464 { body: 'Requested Range Not Satisfiable', status: 416 });465 },466 467 'test #sendfile() Range invalid range': function(){468 var app = express.createServer();469 470 app.get('/*', function(req, res, next){471 var file = req.params[0]472 , path = __dirname + '/fixtures/' + file;473 res.sendfile(path);474 });475 476 assert.response(app,477 { url: '/large.json', headers: { 'Range': 'bytes=500-10' }},478 { body: 'Requested Range Not Satisfiable', status: 416 });479 },480 481 'test #download()': function(beforeExit){482 var app = express.createServer()483 , calls = 0;484 485 app.get('/json', function(req, res, next){486 var path = __dirname + '/fixtures/user.json';487 res.download(path, 'account.json');488 });489490 app.get('/callback', function(req, res, next){491 res.download(__dirname + '/fixtures/pets.json', function(err){492 ++calls;493 });494 });495496 app.get('/*', function(req, res, next){497 res.download(__dirname + '/fixtures/' + req.params[0]);498 });499500 assert.response(app,501 { url: '/callback' },502 { body: '["tobi", "loki", "jane"]' });503504 assert.response(app,505 { url: '/user.json' },506 { body: '{"name":"tj"}', status: 200, headers: {507 'Content-Type': 'application/json',508 'Content-Disposition': 'attachment; filename="user.json"'509 }});510 511 assert.response(app,512 { url: '/json' },513 { body: '{"name":"tj"}', status: 200, headers: {514 'Content-Type': 'application/json',515 'Content-Length': '13',516 'Content-Disposition': 'attachment; filename="account.json"'517 }});518 519 assert.response(app,520 { url: '/missing' },521 function(res){522 assert.equal(null, res.headers['content-disposition']);523 });524525 beforeExit(function(){526 calls.should.equal(1);527 });528 },529 530 'test #cookie()': function(){531 var app = express.createServer();532 533 app.get('/', function(req, res){534 res.cookie('rememberme', 'yes', { expires: new Date(1), httpOnly: true });535 res.cookie('something', 'else');536 res.redirect('/');537 });538539 assert.response(app,540 { url: '/' },541 function(res){542 res.headers['set-cookie']543 .should.eql(['rememberme=yes; expires=Thu, 01 Jan 1970 00:00:00 GMT; httpOnly', 'something=else']);544 });545 },546 547 'test #clearCookie()': function(){548 var app = express.createServer();549 550 app.get('/', function(req, res){551 res.clearCookie('rememberme');552 res.redirect('/');553 });554 555 assert.response(app,556 { url: '/' },557 function(res){558 res.headers['set-cookie']559 .should.eql(['rememberme=; expires=Thu, 01 Jan 1970 00:00:00 GMT']);560 });561 },562 563 'test HEAD': function(){564 var app = express.createServer();565 566 app.get('/', function(req, res, next){567 res.send('Hello World');568 });569 570 assert.response(app,571 { url: '/', method: 'HEAD' },572 { body: '', headers: { 'Content-Length': 11 }});573 },574 575 'test .charset with res.send()': function(){576 var app = express.createServer();577578 app.get('/', function(req, res){579 res.charset = 'ISO-8859-1';580 res.send('<p>hey</p>');581 });582583 assert.response(app,584 { url: '/' },585 { headers: { 'Content-Type': 'text/html; charset=ISO-8859-1' }});586 }587};