Skip to content
Myr

request.test.js

This file

Path

test/request.test.js

268 lines · 8.0 KiB · JavaScript

Last changed by

using should / refactored testsTj Holowaychukbefore adoption643879f

History of this file

Take it

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

res.send() HEAD supportTj Holowaychukbefore adoptiona9819df

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

using should / refactored testsTj Holowaychukbefore adoption643879f

lines 6–10why, one step back
6var express = require('express')
7 , connect = require('connect')8 , assert = require('assert')9 , should = require('should')10 , MemoryStore = require('connect/middleware/session/memory');

res.send() HEAD supportTj Holowaychukbefore adoptiona9819df

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 });

res.send() HEAD supportTj Holowaychukbefore adoptiona9819df

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' });

res.send() HEAD supportTj Holowaychukbefore adoptiona9819df

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();

res.send() HEAD supportTj Holowaychukbefore adoptiona9819df

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 });

res.send() HEAD supportTj Holowaychukbefore adoptiona9819df

line 43why, one step back
43

using should / refactored testsTj Holowaychukbefore adoption643879f

lines 44–124why, 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;54 req.accepts('json').should.be.true;55 res.send('ok');56 });57 58 app.get('/', function(req, res){59 req.accepts('html').should.be.true;60 req.accepts('text/html').should.be.true;61 req.accepts('text/*').should.be.true;62 req.accepts('json').should.be.true;63 req.accepts('application/json').should.be.true;64 65 req.accepts('xml').should.be.false;66 req.accepts('image/*').should.be.false;67 req.accepts('png').should.be.false;68 req.accepts().should.be.false;69 res.send('ok');70 });71 72 app.get('/type', function(req, res){73 req.accepts('html').should.be.true;74 req.accepts('text/html').should.be.true;75 req.accepts('json').should.be.true;76 req.accepts('application/json').should.be.true;77 78 req.accepts('png').should.be.false;79 req.accepts('image/png').should.be.false;80 res.send('ok'); 81 });82 83 assert.response(app,84 { url: '/all', headers: { Accept: '*/*' }},85 { body: 'ok' });86 assert.response(app,87 { url: '/type', headers: { Accept: 'text/*; application/*' }},88 { body: 'ok' });89 assert.response(app,90 { url: '/', headers: { Accept: 'text/html; application/json; text/*' }},91 { body: 'ok' });92 },93 94 'test #param()': function(){95 var app = express.createServer(96 connect.bodyDecoder()97 );98 99 app.get('/user/:id?', function(req, res){100 res.send('user ' + req.param('id'));101 });102 103 app.post('/user', function(req, res){104 res.send('user ' + req.param('id'));105 });106 107 assert.response(app,108 { url: '/user/12' },109 { body: 'user 12' });110 111 assert.response(app,112 { url: '/user?id=5' },113 { body: 'user 5' });114 115 assert.response(app,116 { url: '/user', method: 'POST', data: 'id=1', headers: { 'Content-Type': 'application/x-www-form-urlencoded' }},117 { body: 'user 1' });118 },119 120 'test #flash()': function(){121 var app = express.createServer(122 connect.cookieDecoder(),123 connect.session({ store: memoryStore })124 );

res.send() HEAD supportTj Holowaychukbefore adoptiona9819df

line 125why, one step back
125

using should / refactored testsTj Holowaychukbefore adoption643879f

lines 126–130why, one step back
126 app.flashFormatters = {
127 u: function(val){128 return String(val).toUpperCase();129 }130 };

res.send() HEAD supportTj Holowaychukbefore adoptiona9819df

line 131why, one step back
131

using should / refactored testsTj Holowaychukbefore adoption643879f

lines 132–136why, one step back
132 app.get('/', function(req, res){
133 req.flash('info').should.be.empty;134 req.flash('error').should.be.empty;135 req.flash().should.eql({});136 req.session.flash.should.eql({});

res.send() HEAD supportTj Holowaychukbefore adoptiona9819df

line 137why, one step back
137

using should / refactored testsTj Holowaychukbefore adoption643879f

lines 138–141why, one step back
138 req.flash('info', 'one').should.equal(1);
139 req.flash('info', 'two').should.equal(2);140 req.flash('info').should.eql(['one', 'two']);141 req.flash('info').should.eql([]);

res.send() HEAD supportTj Holowaychukbefore adoptiona9819df

line 142why, one step back
142

using should / refactored testsTj Holowaychukbefore adoption643879f

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

res.send() HEAD supportTj Holowaychukbefore adoptiona9819df

line 145why, one step back
145

using should / refactored testsTj Holowaychukbefore adoption643879f

lines 146–157why, one step back
146 req.flash('info', 'Email _sent_.');
147 req.flash('info', '<script>');148 req.flash('info').should.eql(['Email <em>sent</em>.', '&lt;script&gt;']);149 150 req.flash('info', 'Welcome _%s_ to %s', 'TJ', 'something');151 req.flash('info').should.eql(['Welcome <em>TJ</em> to something']);152153 req.flash('error', 'Foo %u', 'bar');154 req.flash('error').should.eql(['Foo BAR']);155156 res.send('ok');157 });

Merge branch 'is'Tj Holowaychukbefore adoption5b7d231

line 158why, one step back
158

using should / refactored testsTj Holowaychukbefore adoption643879f

lines 159–165why, one step back
159 assert.response(app,
160 { url: '/' },161 { body: 'ok' });162 },163 164 'test #is()': function(){165 var app = express.createServer();

Merge branch 'is'Tj Holowaychukbefore adoption5b7d231

line 166why, one step back
166

using should / refactored testsTj Holowaychukbefore adoption643879f

lines 167–173why, one step back
167 app.is('an image', function(req){
168 return 0 == req.headers['content-type'].indexOf('image');169 });170 171 app.is('text', function(req){172 return 0 == req.headers['content-type'].indexOf('text');173 });

Merge branch 'is'Tj Holowaychukbefore adoption5b7d231

line 174why, one step back
174

using should / refactored testsTj Holowaychukbefore adoption643879f

lines 175–197why, one step back
175 app.get('/', function(req, res, next){
176 if (req.is('html')) return res.send('html');177 res.send('not sure');178 });179 180 app.post('/', function(req, res, next){181 if (req.is('html')) return res.send('/ html');182 if (req.is('json')) return res.send('/ json');183 if (req.is('png')) return res.send('/ png');184 res.send('/ not sure');185 });186 187 app.post('/long', function(req, res, next){188 if (req.is('text/html')) return res.send('/long html');189 if (req.is('application/json')) return res.send('/long json');190 if (req.is('image/png')) return res.send('/long png');191 res.send('/long not sure');192 });193 194 app.put('/custom', function(req, res, next){195 if (req.is('an image')) return res.send('/custom an image');196 if (req.is('text')) return res.send('/custom text');197 });

Merge branch 'is'Tj Holowaychukbefore adoption5b7d231

line 198why, one step back
198

using should / refactored testsTj Holowaychukbefore adoption643879f

lines 199–201why, one step back
199 assert.response(app,
200 { url: '/' },201 { body: 'not sure' });

Merge branch 'is'Tj Holowaychukbefore adoption5b7d231

line 202why, one step back
202

using should / refactored testsTj Holowaychukbefore adoption643879f

lines 203–214why, one step back
203 assert.response(app,
204 { url: '/', method: 'POST', headers: { 'Content-Type': 'text/html' }},205 { body: '/ html' });206 assert.response(app,207 { url: '/', method: 'POST', headers: { 'Content-Type': 'application/json; charset=utf-8' }},208 { body: '/ json' });209 assert.response(app,210 { url: '/', method: 'POST', headers: { 'Content-Type': 'image/png' }},211 { body: '/ png' });212 assert.response(app,213 { url: '/', method: 'POST' },214 { body: '/ not sure' });

Merge branch 'is'Tj Holowaychukbefore adoption5b7d231

line 215why, one step back
215

using should / refactored testsTj Holowaychukbefore adoption643879f

lines 216–227why, one step back
216 assert.response(app,
217 { url: '/long', method: 'POST', headers: { 'Content-Type': 'text/html' }},218 { body: '/long html' });219 assert.response(app,220 { url: '/long', method: 'POST', headers: { 'Content-Type': 'application/json; charset=utf-8' }},221 { body: '/long json' });222 assert.response(app,223 { url: '/long', method: 'POST', headers: { 'Content-Type': 'image/png' }},224 { body: '/long png' });225 assert.response(app,226 { url: '/long', method: 'POST' },227 { body: '/long not sure' });

Merge branch 'is'Tj Holowaychukbefore adoption7a71380

line 228why, one step back
228

using should / refactored testsTj Holowaychukbefore adoption643879f

lines 229–244why, one step back
229 assert.response(app,
230 { url: '/custom', method: 'PUT', headers: { 'Content-Type': 'text/plain' }},231 { body: '/custom text' });232 assert.response(app,233 { url: '/custom', method: 'PUT', headers: { 'Content-Type': 'text/html' }},234 { body: '/custom text' });235 assert.response(app,236 { url: '/custom', method: 'PUT', headers: { 'Content-Type': 'image/jpeg' }},237 { body: '/custom an image' });238 assert.response(app,239 { url: '/custom', method: 'PUT', headers: { 'Content-Type': 'image/png' }},240 { body: '/custom an image' });241 },242 243 'test #is() wildcard': function(){244 var app = express.createServer();

Merge branch 'is'Tj Holowaychukbefore adoption7a71380

line 245why, one step back
245

using should / refactored testsTj Holowaychukbefore adoption643879f

lines 246–250why, one step back
246 app.post('/', function(req, res, next){
247 if (req.is('image/*')) return res.send('image');248 if (req.is('*/json')) return res.send('json');249 res.send('not sure');250 });

Merge branch 'is'Tj Holowaychukbefore adoption7a71380

line 251why, one step back
251

using should / refactored testsTj Holowaychukbefore adoption643879f

lines 252–267why, one step back
252 assert.response(app,
253 { url: '/', method: 'POST', headers: { 'Content-Type': 'image/png' }},254 { body: 'image' });255 assert.response(app,256 { url: '/', method: 'POST', headers: { 'Content-Type': 'image/jpeg' }},257 { body: 'image' });258 assert.response(app,259 { url: '/', method: 'POST', headers: { 'Content-Type': 'text/plain' }},260 { body: 'not sure' });261 assert.response(app,262 { url: '/', method: 'POST', headers: { 'Content-Type': 'text/json' }},263 { body: 'json' });264 assert.response(app,265 { url: '/', method: 'POST', headers: { 'Content-Type': 'application/json' }},266 { body: 'json' });267 }

res.send() HEAD supportTj Holowaychukbefore adoptiona9819df

line 268why, one step back
268};