request.test.js
This file
Path
test/request.test.js
140 lines · 4.5 KiB · JavaScript
Last changed by
Added tests for req.flash()Tj Holowaychukbefore adoption3c06c82
History of this fileTake it
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
/** * Module dependencies. */
var express = require('express'), connect = require('connect'), MemoryStore = require('connect/middleware/session/memory');
// Prevent reap timervar memoryStore = new MemoryStore({ reapInterval: -1 });
module.exports = { '#isXMLHttpRequest': function(assert){ var app = express.createServer(); app.get('/isxhr', function(req, res, params){ assert.equal(req.xhr, req.isXMLHttpRequest); res.send(req.isXMLHttpRequest ? 'yeaaa boy' : 'nope'); }); assert.response(app, { url: '/isxhr' }, { body: 'nope' }); assert.response(app, { url: '/isxhr', headers: { 'X-Requested-With': 'XMLHttpRequest' } }, { body: 'yeaaa boy' }); }, '#header()': function(assert){ var app = express.createServer(); app.get('/', function(req, res){ assert.equal('foo.com', req.header('Host')); assert.equal('foo.com', req.header('host')); res.send('wahoo'); }); assert.response(app, { url: '/', headers: { Host: 'foo.com' }}, { body: 'wahoo' }); }, '#accepts()': function(assert){ var app = express.createServer(); app.get('/all', function(req, res){ assert.strictEqual(true, req.accepts('html')); assert.strictEqual(true, req.accepts('json')); res.send('ok'); }); app.get('/', function(req, res){ assert.strictEqual(true, req.accepts('html')); assert.strictEqual(true, req.accepts('text/html')); assert.strictEqual(true, req.accepts('text/*')); assert.strictEqual(true, req.accepts('json')); assert.strictEqual(true, req.accepts('application/json')); assert.strictEqual(false, req.accepts('xml')); assert.strictEqual(false, req.accepts()); res.send('ok'); }); app.get('/type', function(req, res){ assert.strictEqual(true, req.accepts('html')); assert.strictEqual(true, req.accepts('text/html')); assert.strictEqual(true, req.accepts('json')); assert.strictEqual(true, req.accepts('application/json')); assert.strictEqual(false, req.accepts('png')); assert.strictEqual(false, req.accepts('image/png')); res.send('ok'); }); assert.response(app, { url: '/all', headers: { Accept: '*/*' }}, { body: 'ok' }); assert.response(app, { url: '/type', headers: { Accept: 'text/*; application/*' }}, { body: 'ok' }); assert.response(app, { url: '/', headers: { Accept: 'text/html; application/json; text/*' }}, { body: 'ok' }); }, '#param()': function(assert){ var app = express.createServer( connect.bodyDecoder() ); app.get('/user/:id?', function(req, res){ res.send('user ' + req.param('id')); }); app.post('/user', function(req, res){ res.send('user ' + req.param('id')); }); assert.response(app, { url: '/user/12' }, { body: 'user 12' }); assert.response(app, { url: '/user?id=5' }, { body: 'user 5' }); assert.response(app, { url: '/user', method: 'POST', data: 'id=1', headers: { 'Content-Type': 'application/x-www-form-urlencoded' }}, { body: 'user 1' }); }, '#flash()': function(assert){ var app = express.createServer( connect.cookieDecoder(), connect.session({ store: memoryStore }) );
app.get('/', function(req, res){ assert.eql([], req.flash('info')); assert.eql([], req.flash('error')); assert.eql({}, req.flash()); assert.eql({}, req.session.flash); assert.equal(1, req.flash('info', 'one')); assert.equal(2, req.flash('info', 'two')); assert.eql(['one', 'two'], req.flash('info')); assert.eql([], req.flash('info'));
assert.equal(1, req.flash('info', 'one')); assert.eql({ info: ['one'] }, req.flash()); res.send('ok'); }); assert.response(app, { url: '/' }, { body: 'ok' }); }};