request.test.js
This file
Path
test/request.test.js
262 lines · 9.1 KiB · JavaScript
Last changed by
Merge branch 'is'Tj Holowaychukbefore adoption7a71380
History of this fileTake it
The walk stopped after 200 commits: lines older than 0bb7b7c point at it.
Updated connect submoduleTj Holowaychukbefore adoption0bb7b7c
lines 1–119why, one step back12/**3 * Module dependencies.4 */56var express = require('express'),7 connect = require('connect'),8 MemoryStore = require('connect/middleware/session/memory');910// Prevent reap timer11var memoryStore = new MemoryStore({ reapInterval: -1 });1213module.exports = {14 'test #isXMLHttpRequest': function(assert){15 var app = express.createServer();16 17 app.get('/isxhr', function(req, res){18 assert.equal(req.xhr, req.isXMLHttpRequest);19 res.send(req.isXMLHttpRequest20 ? 'yeaaa boy'21 : 'nope');22 });23 24 assert.response(app,25 { url: '/isxhr' },26 { body: 'nope' });27 28 assert.response(app,29 { url: '/isxhr', headers: { 'X-Requested-With': 'XMLHttpRequest' } },30 { body: 'yeaaa boy' });31 },32 33 'test #header()': function(assert){34 var app = express.createServer();35 36 app.get('/', function(req, res){37 assert.equal('foo.com', req.header('Host'));38 assert.equal('foo.com', req.header('host'));39 res.send('wahoo');40 });41 42 assert.response(app,43 { url: '/', headers: { Host: 'foo.com' }},44 { body: 'wahoo' });45 },46 47 'test #accepts()': function(assert){48 var app = express.createServer();49 50 app.get('/all', function(req, res){51 assert.strictEqual(true, req.accepts('html'));52 assert.strictEqual(true, req.accepts('json'));53 res.send('ok');54 });55 56 app.get('/', function(req, res){57 assert.strictEqual(true, req.accepts('html'));58 assert.strictEqual(true, req.accepts('text/html'));59 assert.strictEqual(true, req.accepts('text/*'));60 assert.strictEqual(true, req.accepts('json'));61 assert.strictEqual(true, req.accepts('application/json'));62 assert.strictEqual(false, req.accepts('xml'));63 assert.strictEqual(false, req.accepts());64 res.send('ok');65 });66 67 app.get('/type', function(req, res){68 assert.strictEqual(true, req.accepts('html'));69 assert.strictEqual(true, req.accepts('text/html'));70 assert.strictEqual(true, req.accepts('json'));71 assert.strictEqual(true, req.accepts('application/json'));72 assert.strictEqual(false, req.accepts('png'));73 assert.strictEqual(false, req.accepts('image/png'));74 res.send('ok'); 75 });76 77 assert.response(app,78 { url: '/all', headers: { Accept: '*/*' }},79 { body: 'ok' });80 assert.response(app,81 { url: '/type', headers: { Accept: 'text/*; application/*' }},82 { body: 'ok' });83 assert.response(app,84 { url: '/', headers: { Accept: 'text/html; application/json; text/*' }},85 { body: 'ok' });86 },87 88 'test #param()': function(assert){89 var app = express.createServer(90 connect.bodyDecoder()91 );92 93 app.get('/user/:id?', function(req, res){94 res.send('user ' + req.param('id'));95 });96 97 app.post('/user', function(req, res){98 res.send('user ' + req.param('id'));99 });100 101 assert.response(app,102 { url: '/user/12' },103 { body: 'user 12' });104 105 assert.response(app,106 { url: '/user?id=5' },107 { body: 'user 5' });108 109 assert.response(app,110 { url: '/user', method: 'POST', data: 'id=1', headers: { 'Content-Type': 'application/x-www-form-urlencoded' }},111 { body: 'user 1' });112 },113 114 'test #flash()': function(assert){115 var app = express.createServer(116 connect.cookieDecoder(),117 connect.session({ store: memoryStore })118 );119Merge branch 'flash-formatting'Tj Holowaychukbefore adoptionfa3adc7
lines 120–125why, one step back120 app.flashFormatters = {121 u: function(val){122 return String(val).toUpperCase();123 }124 };125Updated connect submoduleTj Holowaychukbefore adoption0bb7b7c
lines 126–142why, one step back126 app.get('/', function(req, res){127 assert.eql([], req.flash('info'));128 assert.eql([], req.flash('error'));129 assert.eql({}, req.flash());130 assert.eql({}, req.session.flash);131 132 assert.equal(1, req.flash('info', 'one'));133 assert.equal(2, req.flash('info', 'two'));134 assert.eql(['one', 'two'], req.flash('info'));135 assert.eql([], req.flash('info'));136137 assert.equal(1, req.flash('info', 'one'));138 assert.eql({ info: ['one'] }, req.flash());139 140 req.flash('info', 'Email _sent_.');141 req.flash('info', '<script>');142 assert.eql(['Email <em>sent</em>.', '<script>'], req.flash('info'));Merge branch 'flash-formatting'Tj Holowaychukbefore adoptionfa3adc7
lines 143–149why, one step back143 144 req.flash('info', 'Welcome _%s_ to %s', 'TJ', 'something');145 assert.eql(['Welcome <em>TJ</em> to something'], req.flash('info'));146147 req.flash('info', 'Foo %u', 'bar');148 assert.eql(['Foo BAR'], req.flash('info'));149Updated connect submoduleTj Holowaychukbefore adoption0bb7b7c
lines 150–155why, one step back150 res.send('ok');151 });152 153 assert.response(app,154 { url: '/' },155 { body: 'ok' });Merge branch 'is'Tj Holowaychukbefore adoption5b7d231
lines 156–234why, one step back156 },157 158 'test #is()': function(assert){159 var app = express.createServer();160161 app.is('an image', function(req){162 return 0 == req.headers['content-type'].indexOf('image');163 });164 165 app.is('text', function(req){166 return 0 == req.headers['content-type'].indexOf('text');167 });168169 app.get('/', function(req, res, next){170 if (req.is('html')) return res.send('html');171 res.send('not sure');172 });173 174 app.post('/', function(req, res, next){175 if (req.is('html')) return res.send('/ html');176 if (req.is('json')) return res.send('/ json');177 if (req.is('png')) return res.send('/ png');178 res.send('/ not sure');179 });180 181 app.post('/long', function(req, res, next){182 if (req.is('text/html')) return res.send('/long html');183 if (req.is('application/json')) return res.send('/long json');184 if (req.is('image/png')) return res.send('/long png');185 res.send('/long not sure');186 });187 188 app.put('/custom', function(req, res, next){189 if (req.is('an image')) return res.send('/custom an image');190 if (req.is('text')) return res.send('/custom text');191 });192193 assert.response(app,194 { url: '/' },195 { body: 'not sure' });196197 assert.response(app,198 { url: '/', method: 'POST', headers: { 'Content-Type': 'text/html' }},199 { body: '/ html' });200 assert.response(app,201 { url: '/', method: 'POST', headers: { 'Content-Type': 'application/json; charset=utf-8' }},202 { body: '/ json' });203 assert.response(app,204 { url: '/', method: 'POST', headers: { 'Content-Type': 'image/png' }},205 { body: '/ png' });206 assert.response(app,207 { url: '/', method: 'POST' },208 { body: '/ not sure' });209210 assert.response(app,211 { url: '/long', method: 'POST', headers: { 'Content-Type': 'text/html' }},212 { body: '/long html' });213 assert.response(app,214 { url: '/long', method: 'POST', headers: { 'Content-Type': 'application/json; charset=utf-8' }},215 { body: '/long json' });216 assert.response(app,217 { url: '/long', method: 'POST', headers: { 'Content-Type': 'image/png' }},218 { body: '/long png' });219 assert.response(app,220 { url: '/long', method: 'POST' },221 { body: '/long not sure' });222 223 assert.response(app,224 { url: '/custom', method: 'PUT', headers: { 'Content-Type': 'text/plain' }},225 { body: '/custom text' });226 assert.response(app,227 { url: '/custom', method: 'PUT', headers: { 'Content-Type': 'text/html' }},228 { body: '/custom text' });229 assert.response(app,230 { url: '/custom', method: 'PUT', headers: { 'Content-Type': 'image/jpeg' }},231 { body: '/custom an image' });232 assert.response(app,233 { url: '/custom', method: 'PUT', headers: { 'Content-Type': 'image/png' }},234 { body: '/custom an image' });Merge branch 'is'Tj Holowaychukbefore adoption7a71380
lines 235–260why, one step back235 },236 237 'test #is() wildcard': function(assert){238 var app = express.createServer();239240 app.post('/', function(req, res, next){241 if (req.is('image/*')) return res.send('image');242 if (req.is('*/json')) return res.send('json');243 res.send('not sure');244 });245246 assert.response(app,247 { url: '/', method: 'POST', headers: { 'Content-Type': 'image/png' }},248 { body: 'image' });249 assert.response(app,250 { url: '/', method: 'POST', headers: { 'Content-Type': 'image/jpeg' }},251 { body: 'image' });252 assert.response(app,253 { url: '/', method: 'POST', headers: { 'Content-Type': 'text/plain' }},254 { body: 'not sure' });255 assert.response(app,256 { url: '/', method: 'POST', headers: { 'Content-Type': 'text/json' }},257 { body: 'json' });258 assert.response(app,259 { url: '/', method: 'POST', headers: { 'Content-Type': 'application/json' }},260 { body: 'json' });Updated connect submoduleTj Holowaychukbefore adoption0bb7b7c
lines 261–262why, one step back261 }262};