request.test.js
This file
Path
test/request.test.js
356 lines · 10.5 KiB · JavaScript
Last changed by
Fixed `req.notify()`, only escape argsTj Holowaychukbefore adoptionedb1dc0
History of this fileTake it
The walk stopped after 200 commits: lines older than 723774a point at it.
added quick start to guideTj Holowaychukbefore adoption723774a
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 },23added quick start to guideTj Holowaychukbefore adoption723774a
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(){added quick start to guideTj Holowaychukbefore adoption723774a
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 = {added quick start to guideTj Holowaychukbefore adoption723774a
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({});added quick start to guideTj Holowaychukbefore adoption723774a
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
line 169why, one step back169 req.notify('info', 'Email _sent_.');Fixed `req.notify()`, only escape argsTj Holowaychukbefore adoptionedb1dc0
lines 170–171why, one step back170 req.notify('info', '<em>%s</em>', 'html');171 req.notify('info').should.eql(['Email <em>sent</em>.', '<em>html</em>']);added quick start to guideTj Holowaychukbefore adoption723774a
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']);Fixed `req.notify()`, only escape argsTj Holowaychukbefore adoptionedb1dc0
lines 175–178why, one step back175176 req.notify('info', 'Test %s', '<script>');177 req.notify('info').should.eql(['Test <script>']);178Removed `req.flash()` referencesTj Holowaychukbefore adoption3ab3702
lines 179–180why, one step back179 req.notify('info', 'TJ has %d pets', 5.12323);180 req.notify('info').should.eql(['TJ has 5 pets']);make `req.notify()` arity basedTj Holowaychukbefore adoptionfd5d107
line 181why, one step back181 Removed `req.flash()` referencesTj Holowaychukbefore adoption3ab3702
lines 182–183why, one step back182 req.notify('error', 'Foo %u', 'bar');183 req.notify('error').should.eql(['Foo BAR']);added quick start to guideTj Holowaychukbefore adoption723774a
lines 184–323why, one step back184185 res.send('ok');186 });187 188 assert.response(app,189 { url: '/' },190 { body: 'ok' });191 },192 193 'test #is()': function(){194 var app = express.createServer();195196 app.is('an image', function(req){197 return 0 == req.headers['content-type'].indexOf('image');198 });199 200 app.is('text', function(req){201 return 0 == req.headers['content-type'].indexOf('text');202 });203204 app.get('/', function(req, res, next){205 if (req.is('html')) return res.send('html');206 res.send('not sure');207 });208 209 app.post('/', function(req, res, next){210 if (req.is('html')) return res.send('/ html');211 if (req.is('json')) return res.send('/ json');212 if (req.is('png')) return res.send('/ png');213 res.send('/ not sure');214 });215 216 app.post('/long', function(req, res, next){217 if (req.is('text/html')) return res.send('/long html');218 if (req.is('application/json')) return res.send('/long json');219 if (req.is('image/png')) return res.send('/long png');220 res.send('/long not sure');221 });222 223 app.put('/custom', function(req, res, next){224 if (req.is('an image')) return res.send('/custom an image');225 if (req.is('text')) return res.send('/custom text');226 });227228 assert.response(app,229 { url: '/' },230 { body: 'not sure' });231232 assert.response(app,233 { url: '/', method: 'POST', headers: { 'Content-Type': 'text/html' }},234 { body: '/ html' });235 assert.response(app,236 { url: '/', method: 'POST', headers: { 'Content-Type': 'application/json; charset=utf-8' }},237 { body: '/ json' });238 assert.response(app,239 { url: '/', method: 'POST', headers: { 'Content-Type': 'image/png' }},240 { body: '/ png' });241 assert.response(app,242 { url: '/', method: 'POST' },243 { body: '/ not sure' });244245 assert.response(app,246 { url: '/long', method: 'POST', headers: { 'Content-Type': 'text/html' }},247 { body: '/long html' });248 assert.response(app,249 { url: '/long', method: 'POST', headers: { 'Content-Type': 'application/json; charset=utf-8' }},250 { body: '/long json' });251 assert.response(app,252 { url: '/long', method: 'POST', headers: { 'Content-Type': 'image/png' }},253 { body: '/long png' });254 assert.response(app,255 { url: '/long', method: 'POST' },256 { body: '/long not sure' });257 258 assert.response(app,259 { url: '/custom', method: 'PUT', headers: { 'Content-Type': 'text/plain' }},260 { body: '/custom text' });261 assert.response(app,262 { url: '/custom', method: 'PUT', headers: { 'Content-Type': 'text/html' }},263 { body: '/custom text' });264 assert.response(app,265 { url: '/custom', method: 'PUT', headers: { 'Content-Type': 'image/jpeg' }},266 { body: '/custom an image' });267 assert.response(app,268 { url: '/custom', method: 'PUT', headers: { 'Content-Type': 'image/png' }},269 { body: '/custom an image' });270 },271 272 'test #is() wildcard': function(){273 var app = express.createServer();274275 app.post('/', function(req, res, next){276 if (req.is('image/*')) return res.send('image');277 if (req.is('*/json')) return res.send('json');278 res.send('not sure');279 });280281 assert.response(app,282 { url: '/', method: 'POST', headers: { 'Content-Type': 'image/png' }},283 { body: 'image' });284 assert.response(app,285 { url: '/', method: 'POST', headers: { 'Content-Type': 'image/jpeg' }},286 { body: 'image' });287 assert.response(app,288 { url: '/', method: 'POST', headers: { 'Content-Type': 'text/plain' }},289 { body: 'not sure' });290 assert.response(app,291 { url: '/', method: 'POST', headers: { 'Content-Type': 'text/json' }},292 { body: 'json' });293 assert.response(app,294 { url: '/', method: 'POST', headers: { 'Content-Type': 'application/json' }},295 { body: 'json' });296 },297 298 'test #header() referrer': function(){299 var app = express.createServer();300301 app.get('/correct', function(req, res){302 res.send(req.header('Referrer'));303 });304 305 app.get('/incorrect', function(req, res){306 res.send(req.header('Referer'));307 });308309 assert.response(app,310 { url: '/correct', headers: { Referer: 'expressjs.com' }},311 { body: 'expressjs.com' });312313 assert.response(app,314 { url: '/correct', headers: { Referrer: 'expressjs.com' }},315 { body: 'expressjs.com' });316317 assert.response(app,318 { url: '/incorrect', headers: { Referrer: 'expressjs.com' }},319 { body: 'expressjs.com' });320321 assert.response(app,322 { url: '/incorrect', headers: { Referer: 'expressjs.com' }},323 { body: 'expressjs.com' });Merge branch 'param'Tj Holowaychukbefore adoption1820ea6
lines 324–354why, one step back324 },325 326 'test #get(field, param)': function(){327 var app = express.createServer();328329 app.get('/', function(req, res, next){330 req.get('content-disposition', 'filename')331 .should.equal('foo bar.jpg');332333 req.get('Content-Disposition', 'filename')334 .should.equal('foo bar.jpg');335336 req.get('x-content-foo', 'foo').should.equal('bar');337 req.get('x-content-foo', 'bar').should.equal('foo bar baz');338 req.get('x-content-foo', 'woot').should.equal('tobi loki jane');339 req.get('cache-control', 'max-age').should.equal('500');340341 req.get('foo').should.equal('');342 req.get('foo', 'bar').should.equal('');343 res.end();344 });345346 var fields = {347 'Content-Disposition': 'attachment; filename="foo bar.jpg"'348 , 'X-Content-Foo': 'foo=bar; bar=foo bar baz; woot=tobi loki jane;'349 , 'Cache-Control': 'max-age = 500'350 };351352 assert.response(app,353 { url: '/', headers: fields },354 { body: '' });added quick start to guideTj Holowaychukbefore adoption723774a
lines 355–356why, one step back355 }356};