response.test.js
This file
Path
test/response.test.js
221 lines · 7.4 KiB · JavaScript
Last changed by
Route callback signature changedTj Holowaychukbefore adoption75661a2
History of this fileTake it
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
/** * Module dependencies. */
var express = require('express'), Buffer = require('buffer').Buffer;
module.exports = { 'test #send()': function(assert){ var app = express.createServer();
app.get('/html', function(req, res){ res.send('<p>test</p>', { 'Content-Language': 'en' }); }); app.get('/json', function(req, res){ res.header('X-Foo', 'bar'); res.send({ foo: 'bar' }, { 'X-Foo': 'baz' }, 201); }); app.get('/text', function(req, res){ res.header('X-Foo', 'bar'); res.contentType('.txt'); res.send('wahoo'); }); app.get('/status', function(req, res){ res.send(404); }); app.get('/error', function(req, res){ res.send('Oh shit!', { 'Content-Type': 'text/plain' }, 500); }); app.get('/buffer', function(req, res){ res.send(new Buffer('wahoo!')); });
assert.response(app, { url: '/html' }, { body: '<p>test</p>', headers: { 'Content-Language': 'en', 'Content-Type': 'text/html; charset=utf-8' }}); assert.response(app, { url: '/json' }, { body: '{"foo":"bar"}', status: 201, headers: { 'Content-Type': 'application/json', 'X-Foo': 'baz' }}); assert.response(app, { url: '/text' }, { body: 'wahoo', headers: { 'Content-Type': 'text/plain; charset=utf-8', 'X-Foo': 'bar' }}); assert.response(app, { url: '/status' }, { body: 'Not Found', status: 404, headers: { 'Content-Type': 'text/plain; charset=utf-8' }}); assert.response(app, { url: '/error' }, { body: 'Oh shit!', status: 500, headers: { 'Content-Type': 'text/plain' }}); assert.response(app, { url: '/buffer' }, { body: 'wahoo!', headers: { 'Content-Type': 'application/octet-stream' }}); }, 'test #contentType': function(assert){ var app = express.createServer(); app.get('/html', function(req, res){ res.contentType('index.html'); res.writeHead(200, res.headers); res.end('<p>yay</p>'); }); assert.response(app, { url: '/html' }, { body: '<p>yay</p>', headers: { 'Content-Type': 'text/html; charset=utf-8' }}); }, 'test #attachment': function(assert){ var app = express.createServer(); app.get('/style.css', function(req, res){ res.attachment(); res.send('some stylezzz'); });
app.get('/*', function(req, res){ res.attachment(req.params[0]); res.send('whatever'); }); assert.response(app, { url: '/javascripts/jquery.js' }, { body: 'whatever', headers: { 'Content-Disposition': 'attachment; filename="jquery.js"' }}); assert.response(app, { url: '/style.css' }, { body: 'some stylezzz', headers: { 'Content-Disposition': 'attachment' }}); }, 'test #redirect()': function(assert){ var app = express.createServer(), app2 = express.createServer(); app2.set('home', '/blog');
app2.redirect('google', 'http://google.com');
app2.redirect('blog', function(req, res){ return req.params.id ? '/user/' + req.params.id + '/blog' : null; }); app.get('/', function(req, res){ res.redirect('http://google.com', 301); }); app.get('/back', function(req, res){ res.redirect('back'); }); app.get('/home', function(req, res){ res.redirect('home'); }); app2.get('/', function(req, res){ res.redirect('http://google.com', 301); }); app2.get('/back', function(req, res){ res.redirect('back'); }); app2.get('/home', function(req, res){ res.redirect('home'); }); app2.get('/google', function(req, res){ res.redirect('google'); });
app2.get('/user/:id', function(req, res){ res.redirect('blog'); }); assert.response(app, { url: '/' }, { body: '', status: 301, headers: { Location: 'http://google.com' }}); assert.response(app, { url: '/back' }, { body: '', status: 302, headers: { Location: '/' }}); assert.response(app, { url: '/back', headers: { Referer: '/foo' }}, { body: '', status: 302, headers: { Location: '/foo' }}); assert.response(app, { url: '/back', headers: { Referrer: '/foo' }}, { body: '', status: 302, headers: { Location: '/foo' }}); assert.response(app, { url: '/home' }, { body: '', status: 302, headers: { Location: '/' }});
assert.response(app2, { url: '/' }, { body: '', status: 301, headers: { Location: 'http://google.com' }}); assert.response(app2, { url: '/back' }, { body: '', status: 302, headers: { Location: '/blog' }}); assert.response(app2, { url: '/home' }, { body: '', status: 302, headers: { Location: '/blog' }}); assert.response(app2, { url: '/google' }, { body: '', headers: { Location: 'http://google.com' }}); assert.response(app2, { url: '/user/12' }, { body: '', headers: { Location: '/user/12/blog' }}); }, 'test #sendfile()': function(assert){ var app = express.createServer(); app.get('/*', function(req, res){ var file = req.params[0]; res.sendfile(__dirname + '/fixtures/' + file); }); assert.response(app, { url: '/user.json' }, { body: '{"name":"tj"}', status: 200, headers: { 'Content-Type': 'application/json' }}); assert.response(app, { url: '/hello.haml' }, { body: '%p Hello World', status: 200, headers: { 'Content-Type': 'application/octet-stream' }}); assert.response(app, { url: '/doesNotExist' }, { body: 'Not Found', status: 404 }); assert.response(app, { url: '/partials' }, { body: 'Internal Server Error', status: 500 }); }, 'test #download()': function(assert){ var app = express.createServer(); app.get('/json', function(req, res, next){ res.download(__dirname + '/fixtures/user.json', 'account.json'); });
app.get('/:file', function(req, res, next){ res.download(__dirname + '/fixtures/' + req.params.file); }); assert.response(app, { url: '/user.json' }, { body: '{"name":"tj"}', status: 200, headers: { 'Content-Type': 'application/json', 'Content-Disposition': 'attachment; filename="user.json"' }});
assert.response(app, { url: '/json' }, { body: '{"name":"tj"}', status: 200, headers: { 'Content-Type': 'application/json', 'Content-Disposition': 'attachment; filename="account.json"' }}); }};