request.test.js
This file
Path
test/request.test.js
97 lines · 3.0 KiB · JavaScript
Last changed by
Added req.accepts()Tj Holowaychukbefore adoptioncf37966
History of this fileTake it
Added req.isXMLHttpRequestTj Holowaychukbefore adoption1fe7bb2
lines 1–5why, one step back12/**3 * Module dependencies.4 */5Merge branch 'params' into connectTj Holowaychukbefore adoption9449c4b
lines 6–7why, one step back6var express = require('express'),7 connect = require('connect');Added req.isXMLHttpRequestTj Holowaychukbefore adoption1fe7bb2
lines 8–13why, one step back89module.exports = {10 '#isXMLHttpRequest': function(assert){11 var app = express.createServer();12 13 app.get('/isxhr', function(req, res, params){Added req.xhr as alias of req.isXMLHttpRequestTj Holowaychukbefore adoption35af1ba
line 14why, one step back14 assert.equal(req.xhr, req.isXMLHttpRequest);Added req.isXMLHttpRequestTj Holowaychukbefore adoption1fe7bb2
lines 15–26why, one step back15 res.send(req.isXMLHttpRequest16 ? 'yeaaa boy'17 : 'nope');18 });19 20 assert.response(app,21 { url: '/isxhr' },22 { body: 'nope' });23 24 assert.response(app,25 { url: '/isxhr', headers: { 'X-Requested-With': 'XMLHttpRequest' } },26 { body: 'yeaaa boy' });Added req.header() testsTj Holowaychukbefore adoption7897460
lines 27–40why, one step back27 },28 29 '#header()': function(assert){30 var app = express.createServer();31 32 app.get('/', function(req, res){33 assert.equal('foo.com', req.header('Host'));34 assert.equal('foo.com', req.header('host'));35 res.send('wahoo');36 });37 38 assert.response(app,39 { url: '/', headers: { Host: 'foo.com' }},40 { body: 'wahoo' });Merge branch 'params' into connectTj Holowaychukbefore adoption9449c4b
lines 41–42why, one step back41 },42 Added req.accepts()Tj Holowaychukbefore adoptioncf37966
lines 43–71why, one step back43 '#accepts()': function(assert){44 var app = express.createServer();45 46 app.get('/all', function(req, res){47 assert.strictEqual(true, req.accepts('html'));48 assert.strictEqual(true, req.accepts('json'));49 res.send('ok');50 });51 52 app.get('/', function(req, res){53 assert.strictEqual(true, req.accepts('html'));54 assert.strictEqual(true, req.accepts('text/html'));55 assert.strictEqual(true, req.accepts('text/*'));56 assert.strictEqual(true, req.accepts('json'));57 assert.strictEqual(true, req.accepts('application/json'));58 assert.strictEqual(false, req.accepts('xml'));59 assert.strictEqual(false, req.accepts());60 assert.strictEqual(false, req.accepts(' '));61 res.send('ok');62 });63 64 assert.response(app,65 { url: '/all', headers: { Accept: '*/*' }},66 { body: 'ok' });67 assert.response(app,68 { url: '/', headers: { Accept: 'text/html; application/json; text/*' }},69 { body: 'ok' });70 },71 Merge branch 'params' into connectTj Holowaychukbefore adoption9449c4b
lines 72–95why, one step back72 '#param()': function(assert){73 var app = express.createServer(74 connect.bodyDecoder()75 );76 77 app.get('/user/:id?', function(req, res){78 res.send('user ' + req.param('id'));79 });80 81 app.post('/user', function(req, res){82 res.send('user ' + req.param('id'));83 });84 85 assert.response(app,86 { url: '/user/12' },87 { body: 'user 12' });88 89 assert.response(app,90 { url: '/user?id=5' },91 { body: 'user 5' });92 93 assert.response(app,94 { url: '/user', method: 'POST', data: 'id=1', headers: { 'Content-Type': 'application/x-www-form-urlencoded' }},95 { body: 'user 1' });Added req.isXMLHttpRequestTj Holowaychukbefore adoption1fe7bb2
lines 96–97why, one step back96 }97};