request.test.js
This file
Path
test/request.test.js
353 lines · 10.4 KiB · JavaScript
Last changed by
Added shorthand for the parsed request's pathnameTj Holowaychukbefore adoptiond10b7b4
History of this fileTake it
The walk stopped after 200 commits: lines older than 85df59a point at it.
comma firstTj Holowaychukbefore adoption85df59a
lines 1–11why, 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 = {Added shorthand for the parsed request's pathnameTj Holowaychukbefore adoptiond10b7b4
lines 12–23why, one step back12 'test #path': function(){13 var app = express.createServer();1415 app.get('/search', function(req, res){16 res.send(req.path);17 });1819 assert.response(app,20 { url: '/search?q=tobi' },21 { body: '/search' });22 },23comma firstTj Holowaychukbefore adoption85df59a
lines 24–142why, one step back24 'test #isXMLHttpRequest': function(){25 var app = express.createServer();2627 app.get('/isxhr', function(req, res){28 assert.equal(req.xhr, req.isXMLHttpRequest);29 res.send(req.isXMLHttpRequest30 ? 'yeaaa boy'31 : 'nope');32 });33 34 assert.response(app,35 { url: '/isxhr' },36 { body: 'nope' });37 38 assert.response(app,39 { url: '/isxhr', headers: { 'X-Requested-With': 'XMLHttpRequest' } },40 { body: 'yeaaa boy' });41 },42 43 'test #header()': function(){44 var app = express.createServer();45 46 app.get('/', function(req, res){47 req.header('Host').should.equal('foo.com');48 req.header('host').should.equal('foo.com');49 res.send('wahoo');50 });51 52 assert.response(app,53 { url: '/', headers: { Host: 'foo.com' }},54 { body: 'wahoo' });55 },56 57 'test #accepts()': function(){58 var app = express.createServer();59 60 app.get('/all', function(req, res){61 req.accepts('html').should.be.true;62 req.accepts('.html').should.be.true;63 req.accepts('json').should.be.true;64 req.accepts('.json').should.be.true;65 res.send('ok');66 });67 68 app.get('/', function(req, res){69 req.accepts('html').should.be.true;70 req.accepts('.html').should.be.true;71 req.accepts('text/html').should.be.true;72 req.accepts('text/*').should.be.true;73 req.accepts('json').should.be.true;74 req.accepts('application/json').should.be.true;75 76 req.accepts('xml').should.be.false;77 req.accepts('image/*').should.be.false;78 req.accepts('png').should.be.false;79 req.accepts().should.be.false;80 res.send('ok');81 });82 83 app.get('/type', function(req, res){84 req.accepts('html').should.be.true;85 req.accepts('text/html').should.be.true;86 req.accepts('json').should.be.true;87 req.accepts('application/json').should.be.true;88 89 req.accepts('png').should.be.false;90 req.accepts('image/png').should.be.false;91 res.send('ok'); 92 });93 94 assert.response(app,95 { url: '/all', headers: { Accept: '*/*' }},96 { body: 'ok' });97 assert.response(app,98 { url: '/type', headers: { Accept: 'text/*; application/*' }},99 { body: 'ok' });100 assert.response(app,101 { url: '/', headers: { Accept: 'text/html; application/json; text/*' }},102 { body: 'ok' });103 },104 105 'test #param()': function(){106 var app = express.createServer(107 connect.bodyParser()108 );109 110 app.get('/user/:id?', function(req, res){111 res.send('user ' + req.param('id', 'unknown'));112 });113 114 app.post('/user', function(req, res){115 res.send('user ' + req.param('id'));116 });117118 app.get('/:sort', function(req, res){119 res.send('sort ' + req.param('sort'));120 });121122 assert.response(app,123 { url: '/asc' },124 { body: 'sort asc' });125126 assert.response(app,127 { url: '/user/12' },128 { body: 'user 12' });129 130 assert.response(app,131 { url: '/user?id=5' },132 { body: 'user 5' });133 134 assert.response(app,135 { url: '/user' },136 { body: 'user unknown' });137 138 assert.response(app,139 { url: '/user', method: 'POST', data: 'id=1', headers: { 'Content-Type': 'application/x-www-form-urlencoded' }},140 { body: 'user 1' });141 },142 Removed `req.flash()` referencesTj Holowaychukbefore adoption3ab3702
line 143why, one step back143 'test #notify()': function(){comma firstTj Holowaychukbefore adoption85df59a
lines 144–148why, one step back144 var app = express.createServer(145 connect.cookieParser()146 , connect.session({ secret: 'something' })147 );148Renamed `app.flashFormatters` to `app.formatters`Tj Holowaychukbefore adoption96b72fe
line 149why, one step back149 app.formatters = {comma firstTj Holowaychukbefore adoption85df59a
lines 150–155why, one step back150 u: function(val){151 return String(val).toUpperCase();152 }153 };154155 app.get('/', function(req, res){Added `req.notify()` alias of `req.flash()`Tj Holowaychukbefore adoption9413d30
line 156why, one step back156 req.notify('info').should.be.empty;Removed `req.flash()` referencesTj Holowaychukbefore adoption3ab3702
lines 157–159why, one step back157 req.notify('error').should.be.empty;158 req.notify().should.eql({});159 req.session.notifications.should.eql({});comma firstTj Holowaychukbefore adoption85df59a
line 160why, one step back160Removed `req.flash()` referencesTj Holowaychukbefore adoption3ab3702
lines 161–164why, one step back161 req.notify('info', 'one').should.equal(1);162 req.notify('info', 'two').should.equal(2);163 req.notify('info').should.eql(['one', 'two']);164 req.notify('info').should.eql([]);make `req.notify()` arity basedTj Holowaychukbefore adoptionfd5d107
line 165why, one step back165 Removed `req.flash()` referencesTj Holowaychukbefore adoption3ab3702
lines 166–167why, one step back166 req.notify('info', 'one').should.equal(1);167 req.notify('info').should.eql(['one']);make `req.notify()` arity basedTj Holowaychukbefore adoptionfd5d107
line 168why, one step back168 Removed `req.flash()` referencesTj Holowaychukbefore adoption3ab3702
lines 169–171why, one step back169 req.notify('info', 'Email _sent_.');170 req.notify('info', '<script>');171 req.notify('info').should.eql(['Email <em>sent</em>.', '<script>']);comma firstTj Holowaychukbefore adoption85df59a
line 172why, one step back172 Removed `req.flash()` referencesTj Holowaychukbefore adoption3ab3702
lines 173–174why, one step back173 req.notify('info', 'Welcome _%s_ to %s', 'TJ', 'something');174 req.notify('info').should.eql(['Welcome <em>TJ</em> to something']);make `req.notify()` arity basedTj Holowaychukbefore adoptionfd5d107
line 175why, one step back175 Removed `req.flash()` referencesTj Holowaychukbefore adoption3ab3702
lines 176–177why, one step back176 req.notify('info', 'TJ has %d pets', 5.12323);177 req.notify('info').should.eql(['TJ has 5 pets']);make `req.notify()` arity basedTj Holowaychukbefore adoptionfd5d107
line 178why, one step back178 Removed `req.flash()` referencesTj Holowaychukbefore adoption3ab3702
lines 179–180why, one step back179 req.notify('error', 'Foo %u', 'bar');180 req.notify('error').should.eql(['Foo BAR']);comma firstTj Holowaychukbefore adoption85df59a
lines 181–320why, one step back181182 res.send('ok');183 });184 185 assert.response(app,186 { url: '/' },187 { body: 'ok' });188 },189 190 'test #is()': function(){191 var app = express.createServer();192193 app.is('an image', function(req){194 return 0 == req.headers['content-type'].indexOf('image');195 });196 197 app.is('text', function(req){198 return 0 == req.headers['content-type'].indexOf('text');199 });200201 app.get('/', function(req, res, next){202 if (req.is('html')) return res.send('html');203 res.send('not sure');204 });205 206 app.post('/', function(req, res, next){207 if (req.is('html')) return res.send('/ html');208 if (req.is('json')) return res.send('/ json');209 if (req.is('png')) return res.send('/ png');210 res.send('/ not sure');211 });212 213 app.post('/long', function(req, res, next){214 if (req.is('text/html')) return res.send('/long html');215 if (req.is('application/json')) return res.send('/long json');216 if (req.is('image/png')) return res.send('/long png');217 res.send('/long not sure');218 });219 220 app.put('/custom', function(req, res, next){221 if (req.is('an image')) return res.send('/custom an image');222 if (req.is('text')) return res.send('/custom text');223 });224225 assert.response(app,226 { url: '/' },227 { body: 'not sure' });228229 assert.response(app,230 { url: '/', method: 'POST', headers: { 'Content-Type': 'text/html' }},231 { body: '/ html' });232 assert.response(app,233 { url: '/', method: 'POST', headers: { 'Content-Type': 'application/json; charset=utf-8' }},234 { body: '/ json' });235 assert.response(app,236 { url: '/', method: 'POST', headers: { 'Content-Type': 'image/png' }},237 { body: '/ png' });238 assert.response(app,239 { url: '/', method: 'POST' },240 { body: '/ not sure' });241242 assert.response(app,243 { url: '/long', method: 'POST', headers: { 'Content-Type': 'text/html' }},244 { body: '/long html' });245 assert.response(app,246 { url: '/long', method: 'POST', headers: { 'Content-Type': 'application/json; charset=utf-8' }},247 { body: '/long json' });248 assert.response(app,249 { url: '/long', method: 'POST', headers: { 'Content-Type': 'image/png' }},250 { body: '/long png' });251 assert.response(app,252 { url: '/long', method: 'POST' },253 { body: '/long not sure' });254 255 assert.response(app,256 { url: '/custom', method: 'PUT', headers: { 'Content-Type': 'text/plain' }},257 { body: '/custom text' });258 assert.response(app,259 { url: '/custom', method: 'PUT', headers: { 'Content-Type': 'text/html' }},260 { body: '/custom text' });261 assert.response(app,262 { url: '/custom', method: 'PUT', headers: { 'Content-Type': 'image/jpeg' }},263 { body: '/custom an image' });264 assert.response(app,265 { url: '/custom', method: 'PUT', headers: { 'Content-Type': 'image/png' }},266 { body: '/custom an image' });267 },268 269 'test #is() wildcard': function(){270 var app = express.createServer();271272 app.post('/', function(req, res, next){273 if (req.is('image/*')) return res.send('image');274 if (req.is('*/json')) return res.send('json');275 res.send('not sure');276 });277278 assert.response(app,279 { url: '/', method: 'POST', headers: { 'Content-Type': 'image/png' }},280 { body: 'image' });281 assert.response(app,282 { url: '/', method: 'POST', headers: { 'Content-Type': 'image/jpeg' }},283 { body: 'image' });284 assert.response(app,285 { url: '/', method: 'POST', headers: { 'Content-Type': 'text/plain' }},286 { body: 'not sure' });287 assert.response(app,288 { url: '/', method: 'POST', headers: { 'Content-Type': 'text/json' }},289 { body: 'json' });290 assert.response(app,291 { url: '/', method: 'POST', headers: { 'Content-Type': 'application/json' }},292 { body: 'json' });293 },294 295 'test #header() referrer': function(){296 var app = express.createServer();297298 app.get('/correct', function(req, res){299 res.send(req.header('Referrer'));300 });301 302 app.get('/incorrect', function(req, res){303 res.send(req.header('Referer'));304 });305306 assert.response(app,307 { url: '/correct', headers: { Referer: 'expressjs.com' }},308 { body: 'expressjs.com' });309310 assert.response(app,311 { url: '/correct', headers: { Referrer: 'expressjs.com' }},312 { body: 'expressjs.com' });313314 assert.response(app,315 { url: '/incorrect', headers: { Referrer: 'expressjs.com' }},316 { body: 'expressjs.com' });317318 assert.response(app,319 { url: '/incorrect', headers: { Referer: 'expressjs.com' }},320 { body: 'expressjs.com' });Merge branch 'param'Tj Holowaychukbefore adoption1820ea6
lines 321–351why, one step back321 },322 323 'test #get(field, param)': function(){324 var app = express.createServer();325326 app.get('/', function(req, res, next){327 req.get('content-disposition', 'filename')328 .should.equal('foo bar.jpg');329330 req.get('Content-Disposition', 'filename')331 .should.equal('foo bar.jpg');332333 req.get('x-content-foo', 'foo').should.equal('bar');334 req.get('x-content-foo', 'bar').should.equal('foo bar baz');335 req.get('x-content-foo', 'woot').should.equal('tobi loki jane');336 req.get('cache-control', 'max-age').should.equal('500');337338 req.get('foo').should.equal('');339 req.get('foo', 'bar').should.equal('');340 res.end();341 });342343 var fields = {344 'Content-Disposition': 'attachment; filename="foo bar.jpg"'345 , 'X-Content-Foo': 'foo=bar; bar=foo bar baz; woot=tobi loki jane;'346 , 'Cache-Control': 'max-age = 500'347 };348349 assert.response(app,350 { url: '/', headers: fields },351 { body: '' });comma firstTj Holowaychukbefore adoption85df59a
lines 352–353why, one step back352 }353};