Skip to content
Myr

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 file

Take it

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

Settings docsTj Holowaychukbefore adoptione562adc

lines 1–5why, one step back
1
2/**3 * Module dependencies.4 */5

using should / refactored testsTj Holowaychukbefore adoption643879f

lines 6–9why, one step back
6var 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 back
10 , MemoryStore = express.session.MemoryStore;

Settings docsTj Holowaychukbefore adoptione562adc

lines 11–15why, one step back
11
12// Prevent reap timer13var memoryStore = new MemoryStore({ reapInterval: -1 });1415module.exports = {

using should / refactored testsTj Holowaychukbefore adoption643879f

lines 16–24why, one step back
16 '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 back
25

using should / refactored testsTj Holowaychukbefore adoption643879f

lines 26–28why, one step back
26 assert.response(app,
27 { url: '/isxhr' },28 { body: 'nope' });

Settings docsTj Holowaychukbefore adoptione562adc

line 29why, one step back
29

using should / refactored testsTj Holowaychukbefore adoption643879f

lines 30–36why, one step back
30 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 back
37

using should / refactored testsTj Holowaychukbefore adoption643879f

lines 38–42why, one step back
38 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 back
43

using should / refactored testsTj Holowaychukbefore adoption643879f

lines 44–53why, one step back
44 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 back
54 req.accepts('.html').should.be.true;

using should / refactored testsTj Holowaychukbefore adoption643879f

line 55why, one step back
55 req.accepts('json').should.be.true;

Added more req.accepts() testsTj Holowaychukbefore adoption6624f6b

line 56why, one step back
56 req.accepts('.json').should.be.true;

using should / refactored testsTj Holowaychukbefore adoption643879f

lines 57–61why, one step back
57 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 back
62 req.accepts('.html').should.be.true;

using should / refactored testsTj Holowaychukbefore adoption643879f

lines 63–102why, one step back
63 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 back
103 res.send('user ' + req.param('id', 'unknown'));

using should / refactored testsTj Holowaychukbefore adoption643879f

lines 104–116why, one step back
104 });
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 back
117
118 assert.response(app,119 { url: '/user' },120 { body: 'user unknown' });

using should / refactored testsTj Holowaychukbefore adoption643879f

lines 121–129why, one step back
121
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 back
130 connect.session({ secret: 'something', store: memoryStore })

using should / refactored testsTj Holowaychukbefore adoption643879f

line 131why, one step back
131 );

Settings docsTj Holowaychukbefore adoptione562adc

line 132why, one step back
132

using should / refactored testsTj Holowaychukbefore adoption643879f

lines 133–137why, one step back
133 app.flashFormatters = {
134 u: function(val){135 return String(val).toUpperCase();136 }137 };

Settings docsTj Holowaychukbefore adoptione562adc

line 138why, one step back
138

using should / refactored testsTj Holowaychukbefore adoption643879f

lines 139–143why, one step back
139 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 back
144

using should / refactored testsTj Holowaychukbefore adoption643879f

lines 145–148why, one step back
145 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 back
149

using should / refactored testsTj Holowaychukbefore adoption643879f

lines 150–151why, one step back
150 req.flash('info', 'one').should.equal(1);
151 req.flash('info').should.eql(['one']);

Settings docsTj Holowaychukbefore adoptione562adc

line 152why, one step back
152

using should / refactored testsTj Holowaychukbefore adoption643879f

lines 153–164why, one step back
153 req.flash('info', 'Email _sent_.');
154 req.flash('info', '<script>');155 req.flash('info').should.eql(['Email <em>sent</em>.', '&lt;script&gt;']);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 back
165

using should / refactored testsTj Holowaychukbefore adoption643879f

lines 166–172why, one step back
166 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 back
173

using should / refactored testsTj Holowaychukbefore adoption643879f

lines 174–180why, one step back
174 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 back
181

using should / refactored testsTj Holowaychukbefore adoption643879f

lines 182–204why, one step back
182 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 back
205

using should / refactored testsTj Holowaychukbefore adoption643879f

lines 206–208why, one step back
206 assert.response(app,
207 { url: '/' },208 { body: 'not sure' });

Settings docsTj Holowaychukbefore adoptione562adc

line 209why, one step back
209

using should / refactored testsTj Holowaychukbefore adoption643879f

lines 210–221why, one step back
210 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 back
222

using should / refactored testsTj Holowaychukbefore adoption643879f

lines 223–234why, one step back
223 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 back
235

using should / refactored testsTj Holowaychukbefore adoption643879f

lines 236–251why, one step back
236 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 back
252

using should / refactored testsTj Holowaychukbefore adoption643879f

lines 253–257why, one step back
253 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 back
258

using should / refactored testsTj Holowaychukbefore adoption643879f

lines 259–274why, one step back
259 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 back
275};