Skip to content
Myr

response.test.js

This file

Path

test/response.test.js

732 lines · 19.9 KiB · JavaScript

Last changed by

when cookie path === null dont default itTj Holowaychukbefore adoptionee4471b

History of this file

Take 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('/jsonp', function(req, res){76      app.enable('jsonp callback');77      res.header('X-Foo', 'bar');78      res.send({ foo: 'bar' }, { 'X-Foo': 'baz' }, 201);79      app.disable('jsonp callback');80    });81    82    app.get('/text', function(req, res){83      res.header('X-Foo', 'bar');84      res.contentType('txt');85      res.send('wahoo');86    });87    88    app.get('/status', function(req, res){89      res.send(404);90    });91    92    app.get('/status/text', function(req, res){93      res.send('Oh noes!', 404);94    });95    96    app.get('/error', function(req, res){97      res.send('Oh shit!', { 'Content-Type': 'text/plain' }, 500);98    });99    100    app.get('/buffer', function(req, res){101      res.send(new Buffer('wahoo!'));102    });103    104    app.get('/noargs', function(req, res, next){105      res.send();106    });107    108    app.get('/undefined', function(req, res, next){109      res.send(undefined);110    });111112    app.get('/bool', function(req, res, next){113      res.send(true);114    });115116    assert.response(app,117      { url: '/bool' },118      { body: 'true'119      , headers: { 'Content-Type': 'application/json; charset=utf-8' }});120121    assert.response(app,122      { url: '/html' },123      { body: '<p>test</p>'124      , headers: {125          'Content-Language': 'en'126        , 'Content-Type': 'text/html; charset=utf-8'127      }});128  129    assert.response(app,130      { url: '/json' },131      { body: '{"foo":"bar"}'132      , status: 201133      , headers: {134          'Content-Type': 'application/json; charset=utf-8'135        , 'X-Foo': 'baz'136      }});137  138    assert.response(app,139      { url: '/jsonp?callback=test' },140      { body: 'test({"foo":"bar"});'141      , status: 201142      , headers: {143          'Content-Type': 'text/javascript; charset=utf-8'144        , 'X-Foo': 'baz'145      }});146  147    assert.response(app,148      { url: '/jsonp?callback=baz' },149      { body: 'baz({"foo":"bar"});'150      , status: 201, headers: {151          'Content-Type': 'text/javascript; charset=utf-8'152        , 'X-Foo': 'baz'153      }});154  155    assert.response(app,156      { url: '/jsonp?callback=invalid()[]' },157      { body: 'invalid({"foo":"bar"});'158      , status: 201159      , headers: {160          'Content-Type': 'text/javascript; charset=utf-8'161        , 'X-Foo': 'baz'162      }});163  164    assert.response(app,165      { url: '/json?callback=test' },166      { body: '{"foo":"bar"}'167      , status: 201168      , headers: {169          'Content-Type': 'application/json; charset=utf-8'170        , 'X-Foo': 'baz'171      }});172  173    assert.response(app,174      { url: '/text' },175      { body: 'wahoo'176      , headers: {177          'Content-Type': 'text/plain'178        , 'X-Foo': 'bar'179      }});180181    assert.response(app,182      { url: '/status/text' },183      { body: 'Oh noes!', status: 404 });184185    assert.response(app,186      { url: '/status' },187      { body: 'Not Found'188      , status: 404189      , headers: { 'Content-Type': 'text/plain' }});190  191    assert.response(app,192      { url: '/error' },193      { body: 'Oh shit!'194      , status: 500195      , headers: {196          'Content-Type': 'text/plain; charset=utf-8'197        , 'Content-Length': '8'198      }});199  200    assert.response(app,201      { url: '/buffer' },202      { body: 'wahoo!'203      , headers: {204          'Content-Type': 'application/octet-stream'205        , 'Content-Length': '6'206      }});207  208    assert.response(app,209      { url: '/noargs' },210      { status: 204 }, function(res){211        assert.equal(undefined, res.headers['content-type']);212        assert.equal(undefined, res.headers['content-length']);213      });214215    assert.response(app,216      { url: '/undefined' },217      { status: 204 }, function(res){218        assert.equal(undefined, res.headers['content-type']);219        assert.equal(undefined, res.headers['content-length']);220      });221  },222  223  'test #contentType()': function(){224    var app = express.createServer();225    226    app.get('/html', function(req, res){227      res.contentType('index.html');228      res.writeHead(200, res.headers);229      res.end('<p>yay</p>');230    });231    232    app.get('/json', function(req, res, next){233      res.contentType('json');234      res.send('{"foo":"bar"}');235    });236237    app.get('/literal', function(req, res){238      res.contentType('application/json');239      res.send('{"foo":"bar"}')240    });241242    assert.response(app,243      { url: '/literal' },244      { headers: { 'Content-Type': 'application/json' }});    245    assert.response(app,246      { url: '/html' },247      { body: '<p>yay</p>', headers: { 'Content-Type': 'text/html' }});248    assert.response(app,249      { url: '/json' },250      { headers: { 'Content-Type': 'application/json' }});251  },252  253  'test #attachment()': function(){254    var app = express.createServer();255    256    app.get('/style.css', function(req, res){257      res.attachment();258      res.send('some stylezzz');259    });260261    app.get('/*', function(req, res){262      res.attachment(req.params[0]);263      res.send('whatever');264    });265    266    assert.response(app,267      { url: '/javascripts/jquery.js' },268      { body: 'whatever'269      , headers: { 'Content-Type': 'application/javascript'270                 , 'Content-Disposition': 'attachment; filename="jquery.js"' }});271272    assert.response(app,273      { url: '/style.css' },274      { body: 'some stylezzz'275      , headers: { 'Content-Type': 'text/html; charset=utf-8'276                 , 'Content-Disposition': 'attachment' }});277  },278  279  'test #redirect()': function(){280    var app = express.createServer()281      , app2 = express.createServer();282    283    app2.set('home', '/blog');284285    app2.redirect('google', 'http://google.com');286287    app2.redirect('blog', function(req, res){288      return req.params.id289        ? '/user/' + req.params.id + '/blog'290        : null;291    });292      293    app.get('/', function(req, res){294      res.redirect('http://google.com', 301);295    });296  297    app.get('/back', function(req, res){298      res.redirect('back');299    });300  301    app.get('/home', function(req, res){302      res.redirect('home');303    });304      305    app.get('/html', function(req, res){306      res.redirect('http://google.com');307    });308    309    app2.get('/', function(req, res){310      res.redirect('http://google.com', 301);311    });312    313    app2.get('/back', function(req, res){314      res.redirect('back');315    });316    317    app2.get('/home', function(req, res){318      res.redirect('home');319    });320    321    app2.get('/google', function(req, res){322      res.redirect('google');323    });324325    app2.get('/user/:id', function(req, res){326      res.header('X-Foo', 'bar');327      res.redirect('blog');328    });329330    assert.response(app,331      { url: '/html', headers: { Accept: 'text/html,text/plain', Host: 'foo.com' }},332      { body: '<p>Moved Temporarily. Redirecting to <a href="http://google.com">http://google.com</a></p>' });333334    assert.response(app,335      { url: '/', headers: { Accept: 'text/plain', Host: 'foo.com' }},336      { body: 'Moved Permanently. Redirecting to http://google.com'337      , status: 301, headers: { Location: 'http://google.com' }});338339    assert.response(app,340      { url: '/back', headers: { Accept: 'text/plain', Host: 'foo.com' }},341      { body: 'Moved Temporarily. Redirecting to http://foo.com/'342      , status: 302, headers: { Location: 'http://foo.com/', 'Content-Type': 'text/plain' }});343344    assert.response(app,345      { url: '/back', headers: { Referer: '/foo', Accept: 'text/plain', Host: 'foo.com' }},346      { body: 'Moved Temporarily. Redirecting to http://foo.com/foo'347      , status: 302, headers: { Location: 'http://foo.com/foo' }});348349    assert.response(app,350      { url: '/back', headers: { Referrer: '/foo', Accept: 'text/plain', Host: 'foo.com' }},351      { body: 'Moved Temporarily. Redirecting to http://foo.com/foo'352      , status: 302, headers: { Location: 'http://foo.com/foo' }});353354    assert.response(app,355      { url: '/home', headers: { Accept: 'text/plain', Host: 'foo.com' } },356      { body: 'Moved Temporarily. Redirecting to http://foo.com/'357      , status: 302, headers: { Location: 'http://foo.com/' }});358359    assert.response(app2,360      { url: '/', headers: { Accept: 'text/plain', Host: 'foo.com' }},361      { body: 'Moved Permanently. Redirecting to http://google.com'362      , status: 301, headers: { Location: 'http://google.com' }});363364    assert.response(app2,365      { url: '/back', headers: { Accept: 'text/plain', Host: 'foo.com' }},366      { body: 'Moved Temporarily. Redirecting to http://foo.com/blog'367      , status: 302, headers: { Location: 'http://foo.com/blog' }});368369    assert.response(app2,370      { url: '/home', headers: { Accept: 'text/plain', Host: 'foo.com' }},371      { body: 'Moved Temporarily. Redirecting to http://foo.com/blog'372      , status: 302, headers: { Location: 'http://foo.com/blog' }});373374    assert.response(app2,375      { url: '/google', headers: { Accept: 'text/plain', Host: 'foo.com' }},376      { body: 'Moved Temporarily. Redirecting to http://google.com'377      , status: 302, headers: { Location: 'http://google.com' }});378379    assert.response(app2,380      { url: '/user/12', headers: { Accept: 'text/plain', Host: 'foo.com' }},381      { body: 'Moved Temporarily. Redirecting to http://foo.com/user/12/blog'382      , status: 302, headers: { Location: 'http://foo.com/user/12/blog', 'X-Foo': 'bar' }});383  },384385  'test #redirect() when mounted': function(){386    var app = express.createServer()387      , blog = express.createServer();388389390    blog.get('/posts', function(req, res){391      res.redirect('/posts/all');392    });393394    blog.get('/posts/all', function(req, res){395      res.send('all blog posts');396    });397398    app.use('/blog', blog);399400    assert.response(app,401      { url: '/blog/posts', headers: { Host: 'foo.com' }},402      { status: 302403      , headers: { Location: 'http://foo.com/blog/posts/all' }});404  },405  406  'test #sendfile()': function(){407    var app = express.createServer();408409    app.get('/*', function(req, res, next){410      var file = req.params[0]411        , path = __dirname + '/fixtures/' + file;412      res.sendfile(path);413    });414    415    app.use(express.errorHandler());416417    assert.response(app,418      { url: '/../express.test.js' },419      { body: 'Forbidden', status: 403 });420421    assert.response(app,422      { url: '/user.json' },423      { body: '{"name":"tj"}'424      , status: 200, headers: { 'Content-Type': 'application/json' }});425426    assert.response(app,427      { url: '/hello.haml' },428      { body: '%p Hello World'429      , status: 200, headers: { 'Content-Type': 'application/octet-stream' }});430431    assert.response(app,432      { url: '/doesNotExist' },433      { body: 'Cannot GET /doesNotExist', status: 404 });434435    assert.response(app,436      { url: '/partials' },437      { body: 'Cannot GET /partials', status: 404 });438439    assert.response(app,440      { url: '/large.json' },441      { status: 200, headers: { 'Content-Type': 'application/json' }});442  },443  444  'test #sendfile(path, callback)': function(beforeExit){445    var app = express.createServer()446      , calls = 0;447    448    app.get('/*', function(req, res, next){449      var file = req.params[0]450        , path = __dirname + '/fixtures/' + file;451      res.sendfile(path, function(err){452        if (err) return res.send('got an error');453        ++calls;454      });455    });456457    assert.response(app,458      { url: '/forum' },459      { body: 'got an error' });460461    assert.response(app,462      { url: '/does-not-exist' },463      { body: 'got an error' });464465    assert.response(app,466      { url: '/large.json' },467      { headers: { 'Accept-Ranges': 'bytes' }});468469    beforeExit(function(){470      calls.should.equal(1);471    });472  },473  474  'test #sendfile(path, options, callback)': function(beforeExit){475    var app = express.createServer()476      , calls = 0;477    478    app.get('/*', function(req, res, next){479      var file = req.params[0]480        , path = __dirname + '/fixtures/' + file;481      res.sendfile(path, { maxAge: 3600000 }, function(err){482        if (err) return res.send('got an error');483        ++calls;484      });485    });486487    assert.response(app,488      { url: '/does-not-exist' },489      { body: 'got an error' });490491    assert.response(app,492      { url: '/large.json' },493      { headers: {494          'Accept-Ranges': 'bytes'495        , 'Cache-Control': 'public, max-age=3600'496      }});497498    beforeExit(function(){499      calls.should.equal(1);500    });501  },502 503   'test #sendfile() Accept-Ranges': function(){504     var app = express.createServer();505     506     app.get('/*', function(req, res, next){507       var file = req.params[0]508         , path = __dirname + '/fixtures/' + file;509       res.sendfile(path);510     });511     512     assert.response(app,513       { url: '/large.json' },514       { headers: { 'Accept-Ranges': 'bytes' }});515   },516   517   'test #sendfile() Range': function(){518     var app = express.createServer();519     520     app.get('/*', function(req, res, next){521       var file = req.params[0]522         , path = __dirname + '/fixtures/' + file;523       res.sendfile(path);524     });525   526     assert.response(app,527       { url: '/large.json', headers: { 'Range': 'bytes=0-499' }},528       { headers: {529         'Content-Type': 'application/json',530         'Content-Range': 'bytes 0-499/2535'531       }, status: 206 });532   },533  534  'test #sendfile() Range invalid syntax': function(){535    var app = express.createServer();536    537    app.get('/*', function(req, res, next){538      var file = req.params[0]539        , path = __dirname + '/fixtures/' + file;540      res.sendfile(path);541    });542  543    assert.response(app,544      { url: '/large.json', headers: { 'Range': 'basdytes=asdf' }},545      { body: 'Requested Range Not Satisfiable', status: 416 });546  },547  548  'test #sendfile() Range invalid range': function(){549    var app = express.createServer();550    551    app.get('/*', function(req, res, next){552      var file = req.params[0]553        , path = __dirname + '/fixtures/' + file;554      res.sendfile(path);555    });556  557    assert.response(app,558      { url: '/large.json', headers: { 'Range': 'bytes=500-10' }},559      { body: 'Requested Range Not Satisfiable', status: 416 });560  },561  562  'test #download()': function(beforeExit){563    var app = express.createServer()564      , calls = 0;565    566    app.get('/json', function(req, res, next){567      var path = __dirname + '/fixtures/user.json';568      res.download(path, 'account.json');569    });570571    app.get('/callback', function(req, res, next){572      res.download(__dirname + '/fixtures/pets.json', function(err){573        ++calls;574      });575    });576577    app.get('/*', function(req, res, next){578      res.download(__dirname + '/fixtures/' + req.params[0]);579    });580581    assert.response(app,582      { url: '/callback' },583      { body: '["tobi", "loki", "jane"]' });584585    assert.response(app,586      { url: '/user.json' },587      { body: '{"name":"tj"}', status: 200, headers: {588        'Content-Type': 'application/json',589        'Content-Disposition': 'attachment; filename="user.json"'590      }});591  592    assert.response(app,593      { url: '/json' },594      { body: '{"name":"tj"}', status: 200, headers: {595        'Content-Type': 'application/json',596        'Content-Length': '13',597        'Content-Disposition': 'attachment; filename="account.json"'598      }});599    600    assert.response(app,601      { url: '/missing' },602      function(res){603        assert.equal(null, res.headers['content-disposition']);604      });605606    assert.response(app,607      { url: '/some%20random%20text%20file.txt' },608      { body: 'hello' });609610    beforeExit(function(){611      calls.should.equal(1);612    });613  },614  615  'test #cookie() path default': function(){616    var app = express.createServer();617618    app.set('home', '/foo');619620    app.get('/', function(req, res){621      res.cookie('rememberme', 'yes', { expires: new Date(1), httpOnly: true });622      res.cookie('something', 'else');623      res.redirect('/');624    });625626    assert.response(app,627      { url: '/', headers: { Host: 'foo.com' }},628      function(res){629        res.headers['set-cookie']630          .should.eql(['rememberme=yes; path=/foo; expires=Thu, 01 Jan 1970 00:00:00 GMT; httpOnly', 'something=else; path=/foo']);631      });632  },633634  'test #cookie() explicit path': function(){635    var app = express.createServer();636637    app.set('/home', '/foo');638639    app.get('/', function(req, res){640      res.cookie('rememberme', 'yes', { path: '/', expires: new Date(1), httpOnly: true });641      res.cookie('something', 'else');642      res.redirect('/');643    });644645    assert.response(app,646      { url: '/', headers: { Host: 'foo.com' }},647      function(res){648        res.headers['set-cookie']649          .should.eql(['rememberme=yes; path=/; expires=Thu, 01 Jan 1970 00:00:00 GMT; httpOnly', 'something=else; path=/']);650      });651  },652653  'test #cookie() null path': function(){654    var app = express.createServer();655656    app.set('/home', '/foo');657658    app.get('/', function(req, res){659      res.cookie('rememberme', 'yes', { path: null, expires: new Date(1), httpOnly: true });660      res.cookie('something', 'else', { path: null });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; expires=Thu, 01 Jan 1970 00:00:00 GMT; httpOnly', 'something=else']);669      });670  },671672  'test #clearCookie() default path': function(){673    var app = express.createServer();674675    app.set('home', '/foo');676677    app.get('/', function(req, res){678      res.clearCookie('rememberme');679      res.redirect('/');680    });681    682    assert.response(app,683      { url: '/' },684      function(res){685        res.headers['set-cookie']686          .should.eql(['rememberme=; path=/foo; expires=Thu, 01 Jan 1970 00:00:00 GMT']);687      });688  },689690  'test #clearCookie() explicit path': function(){691    var app = express.createServer();692693    app.set('home', '/bar');694695    app.get('/', function(req, res){696      res.clearCookie('rememberme', { path: '/foo' });697      res.redirect('/');698    });699    700    assert.response(app,701      { url: '/' },702      function(res){703        res.headers['set-cookie']704          .should.eql(['rememberme=; path=/foo; expires=Thu, 01 Jan 1970 00:00:00 GMT']);705      });706  },707708  'test HEAD': function(){709    var app = express.createServer();710    711    app.get('/', function(req, res, next){712      res.send('Hello World');713    });714    715    assert.response(app,716      { url: '/', method: 'HEAD' },717      { body: '', headers: { 'Content-Length': 11 }});718  },719  720  'test .charset with res.send()': function(){721    var app = express.createServer();722723    app.get('/', function(req, res){724      res.charset = 'ISO-8859-1';725      res.send('<p>hey</p>');726    });727728    assert.response(app,729      { url: '/' },730      { headers: { 'Content-Type': 'text/html; charset=ISO-8859-1' }});731  }732};