request.test.js
This file
Path
test/request.test.js
275 lines · 8.3 KiB · JavaScript
Last changed by
fixed session example/tests due to connect changesTj Holowaychukbefore adoptionb1004b6
History of this fileTake it
The walk stopped after 200 commits: lines older than e562adc point at it.
Settings docsTj Holowaychukbefore adoptione562adc
lines 1–5why, one step back12/**3 * Module dependencies.4 */5using should / refactored testsTj Holowaychukbefore adoption643879f
lines 6–9why, one step back6var express = require('express')7 , connect = require('connect')8 , assert = require('assert')9 , should = require('should')fixed session example/tests due to connect changesTj Holowaychukbefore adoptionb1004b6
line 10why, one step back10 , MemoryStore = express.session.MemoryStore;Settings docsTj Holowaychukbefore adoptione562adc
lines 11–15why, one step back1112// Prevent reap timer13var memoryStore = new MemoryStore({ reapInterval: -1 });1415module.exports = {using should / refactored testsTj Holowaychukbefore adoption643879f
lines 16–24why, one step back16 'test #isXMLHttpRequest': function(){17 var app = express.createServer();1819 app.get('/isxhr', function(req, res){20 assert.equal(req.xhr, req.isXMLHttpRequest);21 res.send(req.isXMLHttpRequest22 ? 'yeaaa boy'23 : 'nope');24 });Settings docsTj Holowaychukbefore adoptione562adc
line 25why, one step back25 using should / refactored testsTj Holowaychukbefore adoption643879f
lines 26–28why, one step back26 assert.response(app,27 { url: '/isxhr' },28 { body: 'nope' });Settings docsTj Holowaychukbefore adoptione562adc
line 29why, one step back29 using should / refactored testsTj Holowaychukbefore adoption643879f
lines 30–36why, one step back30 assert.response(app,31 { url: '/isxhr', headers: { 'X-Requested-With': 'XMLHttpRequest' } },32 { body: 'yeaaa boy' });33 },34 35 'test #header()': function(){36 var app = express.createServer();Settings docsTj Holowaychukbefore adoptione562adc
line 37why, one step back37 using should / refactored testsTj Holowaychukbefore adoption643879f
lines 38–42why, one step back38 app.get('/', function(req, res){39 req.header('Host').should.equal('foo.com');40 req.header('host').should.equal('foo.com');41 res.send('wahoo');42 });Settings docsTj Holowaychukbefore adoptione562adc
line 43why, one step back43 using should / refactored testsTj Holowaychukbefore adoption643879f
lines 44–53why, one step back44 assert.response(app,45 { url: '/', headers: { Host: 'foo.com' }},46 { body: 'wahoo' });47 },48 49 'test #accepts()': function(){50 var app = express.createServer();51 52 app.get('/all', function(req, res){53 req.accepts('html').should.be.true;Added more req.accepts() testsTj Holowaychukbefore adoption6624f6b
line 54why, one step back54 req.accepts('.html').should.be.true;using should / refactored testsTj Holowaychukbefore adoption643879f
line 55why, one step back55 req.accepts('json').should.be.true;Added more req.accepts() testsTj Holowaychukbefore adoption6624f6b
line 56why, one step back56 req.accepts('.json').should.be.true;using should / refactored testsTj Holowaychukbefore adoption643879f
lines 57–61why, one step back57 res.send('ok');58 });59 60 app.get('/', function(req, res){61 req.accepts('html').should.be.true;Added failing req.accepts() testTj Holowaychukbefore adoption05bbc65
line 62why, one step back62 req.accepts('.html').should.be.true;using should / refactored testsTj Holowaychukbefore adoption643879f
lines 63–102why, one step back63 req.accepts('text/html').should.be.true;64 req.accepts('text/*').should.be.true;65 req.accepts('json').should.be.true;66 req.accepts('application/json').should.be.true;67 68 req.accepts('xml').should.be.false;69 req.accepts('image/*').should.be.false;70 req.accepts('png').should.be.false;71 req.accepts().should.be.false;72 res.send('ok');73 });74 75 app.get('/type', function(req, res){76 req.accepts('html').should.be.true;77 req.accepts('text/html').should.be.true;78 req.accepts('json').should.be.true;79 req.accepts('application/json').should.be.true;80 81 req.accepts('png').should.be.false;82 req.accepts('image/png').should.be.false;83 res.send('ok'); 84 });85 86 assert.response(app,87 { url: '/all', headers: { Accept: '*/*' }},88 { body: 'ok' });89 assert.response(app,90 { url: '/type', headers: { Accept: 'text/*; application/*' }},91 { body: 'ok' });92 assert.response(app,93 { url: '/', headers: { Accept: 'text/html; application/json; text/*' }},94 { body: 'ok' });95 },96 97 'test #param()': function(){98 var app = express.createServer(99 connect.bodyDecoder()100 );101 102 app.get('/user/:id?', function(req, res){Added default param support to req.param(name, default)Tj Holowaychukbefore adoption0af9126
line 103why, one step back103 res.send('user ' + req.param('id', 'unknown'));using should / refactored testsTj Holowaychukbefore adoption643879f
lines 104–116why, one step back104 });105 106 app.post('/user', function(req, res){107 res.send('user ' + req.param('id'));108 });109 110 assert.response(app,111 { url: '/user/12' },112 { body: 'user 12' });113 114 assert.response(app,115 { url: '/user?id=5' },116 { body: 'user 5' });Added default param support to req.param(name, default)Tj Holowaychukbefore adoption0af9126
lines 117–120why, one step back117 118 assert.response(app,119 { url: '/user' },120 { body: 'user unknown' });using should / refactored testsTj Holowaychukbefore adoption643879f
lines 121–129why, one step back121 122 assert.response(app,123 { url: '/user', method: 'POST', data: 'id=1', headers: { 'Content-Type': 'application/x-www-form-urlencoded' }},124 { body: 'user 1' });125 },126 127 'test #flash()': function(){128 var app = express.createServer(129 connect.cookieDecoder(),fixed session example/tests due to connect changesTj Holowaychukbefore adoptionb1004b6
line 130why, one step back130 connect.session({ secret: 'something', store: memoryStore })using should / refactored testsTj Holowaychukbefore adoption643879f
line 131why, one step back131 );Settings docsTj Holowaychukbefore adoptione562adc
line 132why, one step back132using should / refactored testsTj Holowaychukbefore adoption643879f
lines 133–137why, one step back133 app.flashFormatters = {134 u: function(val){135 return String(val).toUpperCase();136 }137 };Settings docsTj Holowaychukbefore adoptione562adc
line 138why, one step back138using should / refactored testsTj Holowaychukbefore adoption643879f
lines 139–143why, one step back139 app.get('/', function(req, res){140 req.flash('info').should.be.empty;141 req.flash('error').should.be.empty;142 req.flash().should.eql({});143 req.session.flash.should.eql({});Settings docsTj Holowaychukbefore adoptione562adc
line 144why, one step back144using should / refactored testsTj Holowaychukbefore adoption643879f
lines 145–148why, one step back145 req.flash('info', 'one').should.equal(1);146 req.flash('info', 'two').should.equal(2);147 req.flash('info').should.eql(['one', 'two']);148 req.flash('info').should.eql([]);Settings docsTj Holowaychukbefore adoptione562adc
line 149why, one step back149using should / refactored testsTj Holowaychukbefore adoption643879f
lines 150–151why, one step back150 req.flash('info', 'one').should.equal(1);151 req.flash('info').should.eql(['one']);Settings docsTj Holowaychukbefore adoptione562adc
line 152why, one step back152using should / refactored testsTj Holowaychukbefore adoption643879f
lines 153–164why, one step back153 req.flash('info', 'Email _sent_.');154 req.flash('info', '<script>');155 req.flash('info').should.eql(['Email <em>sent</em>.', '<script>']);156 157 req.flash('info', 'Welcome _%s_ to %s', 'TJ', 'something');158 req.flash('info').should.eql(['Welcome <em>TJ</em> to something']);159160 req.flash('error', 'Foo %u', 'bar');161 req.flash('error').should.eql(['Foo BAR']);162163 res.send('ok');164 });Settings docsTj Holowaychukbefore adoptione562adc
line 165why, one step back165 using should / refactored testsTj Holowaychukbefore adoption643879f
lines 166–172why, one step back166 assert.response(app,167 { url: '/' },168 { body: 'ok' });169 },170 171 'test #is()': function(){172 var app = express.createServer();Settings docsTj Holowaychukbefore adoptione562adc
line 173why, one step back173using should / refactored testsTj Holowaychukbefore adoption643879f
lines 174–180why, one step back174 app.is('an image', function(req){175 return 0 == req.headers['content-type'].indexOf('image');176 });177 178 app.is('text', function(req){179 return 0 == req.headers['content-type'].indexOf('text');180 });Settings docsTj Holowaychukbefore adoptione562adc
line 181why, one step back181using should / refactored testsTj Holowaychukbefore adoption643879f
lines 182–204why, one step back182 app.get('/', function(req, res, next){183 if (req.is('html')) return res.send('html');184 res.send('not sure');185 });186 187 app.post('/', function(req, res, next){188 if (req.is('html')) return res.send('/ html');189 if (req.is('json')) return res.send('/ json');190 if (req.is('png')) return res.send('/ png');191 res.send('/ not sure');192 });193 194 app.post('/long', function(req, res, next){195 if (req.is('text/html')) return res.send('/long html');196 if (req.is('application/json')) return res.send('/long json');197 if (req.is('image/png')) return res.send('/long png');198 res.send('/long not sure');199 });200 201 app.put('/custom', function(req, res, next){202 if (req.is('an image')) return res.send('/custom an image');203 if (req.is('text')) return res.send('/custom text');204 });Settings docsTj Holowaychukbefore adoptione562adc
line 205why, one step back205using should / refactored testsTj Holowaychukbefore adoption643879f
lines 206–208why, one step back206 assert.response(app,207 { url: '/' },208 { body: 'not sure' });Settings docsTj Holowaychukbefore adoptione562adc
line 209why, one step back209using should / refactored testsTj Holowaychukbefore adoption643879f
lines 210–221why, one step back210 assert.response(app,211 { url: '/', method: 'POST', headers: { 'Content-Type': 'text/html' }},212 { body: '/ html' });213 assert.response(app,214 { url: '/', method: 'POST', headers: { 'Content-Type': 'application/json; charset=utf-8' }},215 { body: '/ json' });216 assert.response(app,217 { url: '/', method: 'POST', headers: { 'Content-Type': 'image/png' }},218 { body: '/ png' });219 assert.response(app,220 { url: '/', method: 'POST' },221 { body: '/ not sure' });Settings docsTj Holowaychukbefore adoptione562adc
line 222why, one step back222using should / refactored testsTj Holowaychukbefore adoption643879f
lines 223–234why, one step back223 assert.response(app,224 { url: '/long', method: 'POST', headers: { 'Content-Type': 'text/html' }},225 { body: '/long html' });226 assert.response(app,227 { url: '/long', method: 'POST', headers: { 'Content-Type': 'application/json; charset=utf-8' }},228 { body: '/long json' });229 assert.response(app,230 { url: '/long', method: 'POST', headers: { 'Content-Type': 'image/png' }},231 { body: '/long png' });232 assert.response(app,233 { url: '/long', method: 'POST' },234 { body: '/long not sure' });Settings docsTj Holowaychukbefore adoptione562adc
line 235why, one step back235 using should / refactored testsTj Holowaychukbefore adoption643879f
lines 236–251why, one step back236 assert.response(app,237 { url: '/custom', method: 'PUT', headers: { 'Content-Type': 'text/plain' }},238 { body: '/custom text' });239 assert.response(app,240 { url: '/custom', method: 'PUT', headers: { 'Content-Type': 'text/html' }},241 { body: '/custom text' });242 assert.response(app,243 { url: '/custom', method: 'PUT', headers: { 'Content-Type': 'image/jpeg' }},244 { body: '/custom an image' });245 assert.response(app,246 { url: '/custom', method: 'PUT', headers: { 'Content-Type': 'image/png' }},247 { body: '/custom an image' });248 },249 250 'test #is() wildcard': function(){251 var app = express.createServer();Settings docsTj Holowaychukbefore adoptione562adc
line 252why, one step back252using should / refactored testsTj Holowaychukbefore adoption643879f
lines 253–257why, one step back253 app.post('/', function(req, res, next){254 if (req.is('image/*')) return res.send('image');255 if (req.is('*/json')) return res.send('json');256 res.send('not sure');257 });Settings docsTj Holowaychukbefore adoptione562adc
line 258why, one step back258using should / refactored testsTj Holowaychukbefore adoption643879f
lines 259–274why, one step back259 assert.response(app,260 { url: '/', method: 'POST', headers: { 'Content-Type': 'image/png' }},261 { body: 'image' });262 assert.response(app,263 { url: '/', method: 'POST', headers: { 'Content-Type': 'image/jpeg' }},264 { body: 'image' });265 assert.response(app,266 { url: '/', method: 'POST', headers: { 'Content-Type': 'text/plain' }},267 { body: 'not sure' });268 assert.response(app,269 { url: '/', method: 'POST', headers: { 'Content-Type': 'text/json' }},270 { body: 'json' });271 assert.response(app,272 { url: '/', method: 'POST', headers: { 'Content-Type': 'application/json' }},273 { body: 'json' });274 }Settings docsTj Holowaychukbefore adoptione562adc
line 275why, one step back275};