Skip to content
Myr

response.test.js

This file

Path

test/response.test.js

769 lines · 22.2 KiB · JavaScript

Last changed by

fixed some testsTj Holowaychukbefore adoption71aca3c

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