Skip to content
Myr

request.test.js

This file

Path

test/request.test.js

307 lines · 9.1 KiB · JavaScript

Last changed by

Fixed req.param() bug returning Array.prototype methods. Closes #552Tj Holowaychukbefore adoptioneb6ff62

History of this file

Take it

The walk stopped after 200 commits: lines older than e958988 point at it.

styling for stylus exampleTj Holowaychukbefore adoptione958988

lines 1–8why, one step back
1
2/**3 * Module dependencies.4 */56var express = require('express')7 , connect = require('connect')8 , assert = require('assert')

tests running againTj Holowaychukbefore adoptioncd519e9

line 9why, one step back
9 , should = require('should');

styling for stylus exampleTj Holowaychukbefore adoptione958988

lines 10–94why, one step back
10
11module.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(

Renaming usage of old connect middlewareTj Holowaychukbefore adoption2a7a5ae

line 95why, one step back
95 connect.bodyParser()

styling for stylus exampleTj Holowaychukbefore adoptione958988

lines 96–104why, one step back
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 });

Fixed req.param() bug returning Array.prototype methods. Closes #552Tj Holowaychukbefore adoptioneb6ff62

lines 105–113why, one step back
105
106 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' });113

styling for stylus exampleTj Holowaychukbefore adoptione958988

lines 114–132why, one step back
114 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 131 'test #flash()': function(){132 var app = express.createServer(

tests running againTj Holowaychukbefore adoptioncd519e9

lines 133–134why, one step back
133 connect.cookieParser()
134 , connect.session({ secret: 'something' })

styling for stylus exampleTj Holowaychukbefore adoptione958988

lines 135–277why, one step back
135 );
136137 app.flashFormatters = {138 u: function(val){139 return String(val).toUpperCase();140 }141 };142143 app.get('/', function(req, res){144 req.flash('info').should.be.empty;145 req.flash('error').should.be.empty;146 req.flash().should.eql({});147 req.session.flash.should.eql({});148149 req.flash('info', 'one').should.equal(1);150 req.flash('info', 'two').should.equal(2);151 req.flash('info').should.eql(['one', 'two']);152 req.flash('info').should.eql([]);153154 req.flash('info', 'one').should.equal(1);155 req.flash('info').should.eql(['one']);156157 req.flash('info', 'Email _sent_.');158 req.flash('info', '<script>');159 req.flash('info').should.eql(['Email <em>sent</em>.', '&lt;script&gt;']);160 161 req.flash('info', 'Welcome _%s_ to %s', 'TJ', 'something');162 req.flash('info').should.eql(['Welcome <em>TJ</em> to something']);163164 req.flash('error', 'Foo %u', 'bar');165 req.flash('error').should.eql(['Foo BAR']);166167 res.send('ok');168 });169 170 assert.response(app,171 { url: '/' },172 { body: 'ok' });173 },174 175 'test #is()': function(){176 var app = express.createServer();177178 app.is('an image', function(req){179 return 0 == req.headers['content-type'].indexOf('image');180 });181 182 app.is('text', function(req){183 return 0 == req.headers['content-type'].indexOf('text');184 });185186 app.get('/', function(req, res, next){187 if (req.is('html')) return res.send('html');188 res.send('not sure');189 });190 191 app.post('/', function(req, res, next){192 if (req.is('html')) return res.send('/ html');193 if (req.is('json')) return res.send('/ json');194 if (req.is('png')) return res.send('/ png');195 res.send('/ not sure');196 });197 198 app.post('/long', function(req, res, next){199 if (req.is('text/html')) return res.send('/long html');200 if (req.is('application/json')) return res.send('/long json');201 if (req.is('image/png')) return res.send('/long png');202 res.send('/long not sure');203 });204 205 app.put('/custom', function(req, res, next){206 if (req.is('an image')) return res.send('/custom an image');207 if (req.is('text')) return res.send('/custom text');208 });209210 assert.response(app,211 { url: '/' },212 { body: 'not sure' });213214 assert.response(app,215 { url: '/', method: 'POST', headers: { 'Content-Type': 'text/html' }},216 { body: '/ html' });217 assert.response(app,218 { url: '/', method: 'POST', headers: { 'Content-Type': 'application/json; charset=utf-8' }},219 { body: '/ json' });220 assert.response(app,221 { url: '/', method: 'POST', headers: { 'Content-Type': 'image/png' }},222 { body: '/ png' });223 assert.response(app,224 { url: '/', method: 'POST' },225 { body: '/ not sure' });226227 assert.response(app,228 { url: '/long', method: 'POST', headers: { 'Content-Type': 'text/html' }},229 { body: '/long html' });230 assert.response(app,231 { url: '/long', method: 'POST', headers: { 'Content-Type': 'application/json; charset=utf-8' }},232 { body: '/long json' });233 assert.response(app,234 { url: '/long', method: 'POST', headers: { 'Content-Type': 'image/png' }},235 { body: '/long png' });236 assert.response(app,237 { url: '/long', method: 'POST' },238 { body: '/long not sure' });239 240 assert.response(app,241 { url: '/custom', method: 'PUT', headers: { 'Content-Type': 'text/plain' }},242 { body: '/custom text' });243 assert.response(app,244 { url: '/custom', method: 'PUT', headers: { 'Content-Type': 'text/html' }},245 { body: '/custom text' });246 assert.response(app,247 { url: '/custom', method: 'PUT', headers: { 'Content-Type': 'image/jpeg' }},248 { body: '/custom an image' });249 assert.response(app,250 { url: '/custom', method: 'PUT', headers: { 'Content-Type': 'image/png' }},251 { body: '/custom an image' });252 },253 254 'test #is() wildcard': function(){255 var app = express.createServer();256257 app.post('/', function(req, res, next){258 if (req.is('image/*')) return res.send('image');259 if (req.is('*/json')) return res.send('json');260 res.send('not sure');261 });262263 assert.response(app,264 { url: '/', method: 'POST', headers: { 'Content-Type': 'image/png' }},265 { body: 'image' });266 assert.response(app,267 { url: '/', method: 'POST', headers: { 'Content-Type': 'image/jpeg' }},268 { body: 'image' });269 assert.response(app,270 { url: '/', method: 'POST', headers: { 'Content-Type': 'text/plain' }},271 { body: 'not sure' });272 assert.response(app,273 { url: '/', method: 'POST', headers: { 'Content-Type': 'text/json' }},274 { body: 'json' });275 assert.response(app,276 { url: '/', method: 'POST', headers: { 'Content-Type': 'application/json' }},277 { body: 'json' });

Added req.header() Referrer / Referer special-caseTj Holowaychukbefore adoptionb65b063

lines 278–305why, one step back
278 },
279 280 'test #header() referrer': function(){281 var app = express.createServer();282283 app.get('/correct', function(req, res){284 res.send(req.header('Referrer'));285 });286 287 app.get('/incorrect', function(req, res){288 res.send(req.header('Referer'));289 });290291 assert.response(app,292 { url: '/correct', headers: { Referer: 'expressjs.com' }},293 { body: 'expressjs.com' });294295 assert.response(app,296 { url: '/correct', headers: { Referrer: 'expressjs.com' }},297 { body: 'expressjs.com' });298299 assert.response(app,300 { url: '/incorrect', headers: { Referrer: 'expressjs.com' }},301 { body: 'expressjs.com' });302303 assert.response(app,304 { url: '/incorrect', headers: { Referer: 'expressjs.com' }},305 { body: 'expressjs.com' });

styling for stylus exampleTj Holowaychukbefore adoptione958988

lines 306–307why, one step back
306 }
307};