request.test.js
This file
Path
test/request.test.js
341 lines · 10.2 KiB · JavaScript
Last changed by
make `req.notify()` arity basedTj Holowaychukbefore adoptionfd5d107
History of this fileTake it
The walk stopped after 200 commits: lines older than 5f0a854 point at it.
added test for route specific middleware regressionTj Holowaychukbefore adoption5f0a854
lines 1–130why, one step back12/**3 * Module dependencies.4 */56var express = require('express')7 , connect = require('connect')8 , assert = require('assert')9 , should = require('should');1011module.exports = {12 'test #isXMLHttpRequest': function(){13 var app = express.createServer();1415 app.get('/isxhr', function(req, res){16 assert.equal(req.xhr, req.isXMLHttpRequest);17 res.send(req.isXMLHttpRequest18 ? 'yeaaa boy'19 : 'nope');20 });21 22 assert.response(app,23 { url: '/isxhr' },24 { body: 'nope' });25 26 assert.response(app,27 { url: '/isxhr', headers: { 'X-Requested-With': 'XMLHttpRequest' } },28 { body: 'yeaaa boy' });29 },30 31 'test #header()': function(){32 var app = express.createServer();33 34 app.get('/', function(req, res){35 req.header('Host').should.equal('foo.com');36 req.header('host').should.equal('foo.com');37 res.send('wahoo');38 });39 40 assert.response(app,41 { url: '/', headers: { Host: 'foo.com' }},42 { body: 'wahoo' });43 },44 45 'test #accepts()': function(){46 var app = express.createServer();47 48 app.get('/all', function(req, res){49 req.accepts('html').should.be.true;50 req.accepts('.html').should.be.true;51 req.accepts('json').should.be.true;52 req.accepts('.json').should.be.true;53 res.send('ok');54 });55 56 app.get('/', function(req, res){57 req.accepts('html').should.be.true;58 req.accepts('.html').should.be.true;59 req.accepts('text/html').should.be.true;60 req.accepts('text/*').should.be.true;61 req.accepts('json').should.be.true;62 req.accepts('application/json').should.be.true;63 64 req.accepts('xml').should.be.false;65 req.accepts('image/*').should.be.false;66 req.accepts('png').should.be.false;67 req.accepts().should.be.false;68 res.send('ok');69 });70 71 app.get('/type', function(req, res){72 req.accepts('html').should.be.true;73 req.accepts('text/html').should.be.true;74 req.accepts('json').should.be.true;75 req.accepts('application/json').should.be.true;76 77 req.accepts('png').should.be.false;78 req.accepts('image/png').should.be.false;79 res.send('ok'); 80 });81 82 assert.response(app,83 { url: '/all', headers: { Accept: '*/*' }},84 { body: 'ok' });85 assert.response(app,86 { url: '/type', headers: { Accept: 'text/*; application/*' }},87 { body: 'ok' });88 assert.response(app,89 { url: '/', headers: { Accept: 'text/html; application/json; text/*' }},90 { body: 'ok' });91 },92 93 'test #param()': function(){94 var app = express.createServer(95 connect.bodyParser()96 );97 98 app.get('/user/:id?', function(req, res){99 res.send('user ' + req.param('id', 'unknown'));100 });101 102 app.post('/user', function(req, res){103 res.send('user ' + req.param('id'));104 });105106 app.get('/:sort', function(req, res){107 res.send('sort ' + req.param('sort'));108 });109110 assert.response(app,111 { url: '/asc' },112 { body: 'sort asc' });113114 assert.response(app,115 { url: '/user/12' },116 { body: 'user 12' });117 118 assert.response(app,119 { url: '/user?id=5' },120 { body: 'user 5' });121 122 assert.response(app,123 { url: '/user' },124 { body: 'user unknown' });125 126 assert.response(app,127 { url: '/user', method: 'POST', data: 'id=1', headers: { 'Content-Type': 'application/x-www-form-urlencoded' }},128 { body: 'user 1' });129 },130 Removed `req.flash()` referencesTj Holowaychukbefore adoption3ab3702
line 131why, one step back131 'test #notify()': function(){added test for route specific middleware regressionTj Holowaychukbefore adoption5f0a854
lines 132–136why, one step back132 var app = express.createServer(133 connect.cookieParser()134 , connect.session({ secret: 'something' })135 );136Renamed `app.flashFormatters` to `app.formatters`Tj Holowaychukbefore adoption96b72fe
line 137why, one step back137 app.formatters = {added test for route specific middleware regressionTj Holowaychukbefore adoption5f0a854
lines 138–143why, one step back138 u: function(val){139 return String(val).toUpperCase();140 }141 };142143 app.get('/', function(req, res){Added `req.notify()` alias of `req.flash()`Tj Holowaychukbefore adoption9413d30
line 144why, one step back144 req.notify('info').should.be.empty;Removed `req.flash()` referencesTj Holowaychukbefore adoption3ab3702
lines 145–147why, one step back145 req.notify('error').should.be.empty;146 req.notify().should.eql({});147 req.session.notifications.should.eql({});added test for route specific middleware regressionTj Holowaychukbefore adoption5f0a854
line 148why, one step back148Removed `req.flash()` referencesTj Holowaychukbefore adoption3ab3702
lines 149–152why, one step back149 req.notify('info', 'one').should.equal(1);150 req.notify('info', 'two').should.equal(2);151 req.notify('info').should.eql(['one', 'two']);152 req.notify('info').should.eql([]);make `req.notify()` arity basedTj Holowaychukbefore adoptionfd5d107
line 153why, one step back153 Removed `req.flash()` referencesTj Holowaychukbefore adoption3ab3702
lines 154–155why, one step back154 req.notify('info', 'one').should.equal(1);155 req.notify('info').should.eql(['one']);make `req.notify()` arity basedTj Holowaychukbefore adoptionfd5d107
line 156why, one step back156 Removed `req.flash()` referencesTj Holowaychukbefore adoption3ab3702
lines 157–159why, one step back157 req.notify('info', 'Email _sent_.');158 req.notify('info', '<script>');159 req.notify('info').should.eql(['Email <em>sent</em>.', '<script>']);added test for route specific middleware regressionTj Holowaychukbefore adoption5f0a854
line 160why, one step back160 Removed `req.flash()` referencesTj Holowaychukbefore adoption3ab3702
lines 161–162why, one step back161 req.notify('info', 'Welcome _%s_ to %s', 'TJ', 'something');162 req.notify('info').should.eql(['Welcome <em>TJ</em> to something']);make `req.notify()` arity basedTj Holowaychukbefore adoptionfd5d107
line 163why, one step back163 Removed `req.flash()` referencesTj Holowaychukbefore adoption3ab3702
lines 164–165why, one step back164 req.notify('info', 'TJ has %d pets', 5.12323);165 req.notify('info').should.eql(['TJ has 5 pets']);make `req.notify()` arity basedTj Holowaychukbefore adoptionfd5d107
line 166why, one step back166 Removed `req.flash()` referencesTj Holowaychukbefore adoption3ab3702
lines 167–168why, one step back167 req.notify('error', 'Foo %u', 'bar');168 req.notify('error').should.eql(['Foo BAR']);added test for route specific middleware regressionTj Holowaychukbefore adoption5f0a854
lines 169–308why, one step back169170 res.send('ok');171 });172 173 assert.response(app,174 { url: '/' },175 { body: 'ok' });176 },177 178 'test #is()': function(){179 var app = express.createServer();180181 app.is('an image', function(req){182 return 0 == req.headers['content-type'].indexOf('image');183 });184 185 app.is('text', function(req){186 return 0 == req.headers['content-type'].indexOf('text');187 });188189 app.get('/', function(req, res, next){190 if (req.is('html')) return res.send('html');191 res.send('not sure');192 });193 194 app.post('/', function(req, res, next){195 if (req.is('html')) return res.send('/ html');196 if (req.is('json')) return res.send('/ json');197 if (req.is('png')) return res.send('/ png');198 res.send('/ not sure');199 });200 201 app.post('/long', function(req, res, next){202 if (req.is('text/html')) return res.send('/long html');203 if (req.is('application/json')) return res.send('/long json');204 if (req.is('image/png')) return res.send('/long png');205 res.send('/long not sure');206 });207 208 app.put('/custom', function(req, res, next){209 if (req.is('an image')) return res.send('/custom an image');210 if (req.is('text')) return res.send('/custom text');211 });212213 assert.response(app,214 { url: '/' },215 { body: 'not sure' });216217 assert.response(app,218 { url: '/', method: 'POST', headers: { 'Content-Type': 'text/html' }},219 { body: '/ html' });220 assert.response(app,221 { url: '/', method: 'POST', headers: { 'Content-Type': 'application/json; charset=utf-8' }},222 { body: '/ json' });223 assert.response(app,224 { url: '/', method: 'POST', headers: { 'Content-Type': 'image/png' }},225 { body: '/ png' });226 assert.response(app,227 { url: '/', method: 'POST' },228 { body: '/ not sure' });229230 assert.response(app,231 { url: '/long', method: 'POST', headers: { 'Content-Type': 'text/html' }},232 { body: '/long html' });233 assert.response(app,234 { url: '/long', method: 'POST', headers: { 'Content-Type': 'application/json; charset=utf-8' }},235 { body: '/long json' });236 assert.response(app,237 { url: '/long', method: 'POST', headers: { 'Content-Type': 'image/png' }},238 { body: '/long png' });239 assert.response(app,240 { url: '/long', method: 'POST' },241 { body: '/long not sure' });242 243 assert.response(app,244 { url: '/custom', method: 'PUT', headers: { 'Content-Type': 'text/plain' }},245 { body: '/custom text' });246 assert.response(app,247 { url: '/custom', method: 'PUT', headers: { 'Content-Type': 'text/html' }},248 { body: '/custom text' });249 assert.response(app,250 { url: '/custom', method: 'PUT', headers: { 'Content-Type': 'image/jpeg' }},251 { body: '/custom an image' });252 assert.response(app,253 { url: '/custom', method: 'PUT', headers: { 'Content-Type': 'image/png' }},254 { body: '/custom an image' });255 },256 257 'test #is() wildcard': function(){258 var app = express.createServer();259260 app.post('/', function(req, res, next){261 if (req.is('image/*')) return res.send('image');262 if (req.is('*/json')) return res.send('json');263 res.send('not sure');264 });265266 assert.response(app,267 { url: '/', method: 'POST', headers: { 'Content-Type': 'image/png' }},268 { body: 'image' });269 assert.response(app,270 { url: '/', method: 'POST', headers: { 'Content-Type': 'image/jpeg' }},271 { body: 'image' });272 assert.response(app,273 { url: '/', method: 'POST', headers: { 'Content-Type': 'text/plain' }},274 { body: 'not sure' });275 assert.response(app,276 { url: '/', method: 'POST', headers: { 'Content-Type': 'text/json' }},277 { body: 'json' });278 assert.response(app,279 { url: '/', method: 'POST', headers: { 'Content-Type': 'application/json' }},280 { body: 'json' });281 },282 283 'test #header() referrer': function(){284 var app = express.createServer();285286 app.get('/correct', function(req, res){287 res.send(req.header('Referrer'));288 });289 290 app.get('/incorrect', function(req, res){291 res.send(req.header('Referer'));292 });293294 assert.response(app,295 { url: '/correct', headers: { Referer: 'expressjs.com' }},296 { body: 'expressjs.com' });297298 assert.response(app,299 { url: '/correct', headers: { Referrer: 'expressjs.com' }},300 { body: 'expressjs.com' });301302 assert.response(app,303 { url: '/incorrect', headers: { Referrer: 'expressjs.com' }},304 { body: 'expressjs.com' });305306 assert.response(app,307 { url: '/incorrect', headers: { Referer: 'expressjs.com' }},308 { body: 'expressjs.com' });Merge branch 'param'Tj Holowaychukbefore adoption1820ea6
lines 309–339why, one step back309 },310 311 'test #get(field, param)': function(){312 var app = express.createServer();313314 app.get('/', function(req, res, next){315 req.get('content-disposition', 'filename')316 .should.equal('foo bar.jpg');317318 req.get('Content-Disposition', 'filename')319 .should.equal('foo bar.jpg');320321 req.get('x-content-foo', 'foo').should.equal('bar');322 req.get('x-content-foo', 'bar').should.equal('foo bar baz');323 req.get('x-content-foo', 'woot').should.equal('tobi loki jane');324 req.get('cache-control', 'max-age').should.equal('500');325326 req.get('foo').should.equal('');327 req.get('foo', 'bar').should.equal('');328 res.end();329 });330331 var fields = {332 'Content-Disposition': 'attachment; filename="foo bar.jpg"'333 , 'X-Content-Foo': 'foo=bar; bar=foo bar baz; woot=tobi loki jane;'334 , 'Cache-Control': 'max-age = 500'335 };336337 assert.response(app,338 { url: '/', headers: fields },339 { body: '' });added test for route specific middleware regressionTj Holowaychukbefore adoption5f0a854
lines 340–341why, one step back340 }341};