response.test.js
This file
Path
test/response.test.js
380 lines · 12.7 KiB · JavaScript
Last changed by
Merge branch 'range'Tj Holowaychukbefore adoptionfab9af2
History of this fileTake it
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
/** * 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!')); }); app.get('/noargs', function(req, res, next){ res.send(); });
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' }}); assert.response(app, { url: '/noargs' }, { status: 204 }, function(res){ assert.equal(undefined, res.headers['content-type']); assert.equal(undefined, res.headers['content-length']); }); }, '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.header('X-Foo', 'bar'); res.redirect('blog'); }); assert.response(app, { url: '/' }, { body: 'Redirecting to http://google.com', status: 301, headers: { Location: 'http://google.com' }}); assert.response(app, { url: '/back' }, { body: 'Redirecting to /', status: 302, headers: { Location: '/', 'Content-Type': 'text/plain' }}); assert.response(app, { url: '/back', headers: { Referer: '/foo' }}, { body: 'Redirecting to /foo', status: 302, headers: { Location: '/foo' }}); assert.response(app, { url: '/back', headers: { Referrer: '/foo' }}, { body: 'Redirecting to /foo', status: 302, headers: { Location: '/foo' }}); assert.response(app, { url: '/home' }, { body: 'Redirecting to /', status: 302, headers: { Location: '/' }});
assert.response(app2, { url: '/' }, { body: 'Redirecting to http://google.com', status: 301, headers: { Location: 'http://google.com' }}); assert.response(app2, { url: '/back' }, { body: 'Redirecting to /blog', status: 302, headers: { Location: '/blog' }}); assert.response(app2, { url: '/home' }, { body: 'Redirecting to /blog', status: 302, headers: { Location: '/blog' }}); assert.response(app2, { url: '/google' }, { body: 'Redirecting to http://google.com', headers: { Location: 'http://google.com' }}); assert.response(app2, { url: '/user/12' }, { body: 'Redirecting to /user/12/blog', headers: { Location: '/user/12/blog', 'X-Foo': 'bar' }}); }, 'test #sendfile()': function(assert){ var app = express.createServer();
app.set('stream threshold', 2 * 1024)
app.get('/*', function(req, res, next){ var file = req.params[0], filePath = __dirname + '/fixtures/' + file; res.sendfile(filePath, function(err, path){ assert.equal(path, filePath); if (err) next(err); }); }); app.use(express.errorHandler());
assert.response(app, { url: '/../express.test.js' }, { body: 'Forbidden', status: 403 }); 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: 'Internal Server Error', status: 500 }); assert.response(app, { url: '/partials' }, { body: 'Internal Server Error', status: 500 }); assert.response(app, { url: '/large.json' }, { status: 200, headers: { 'Content-Length': 2535, 'Content-Type': 'application/json' }}); }, 'test #sendfile() Accept-Ranges': function(assert){ var app = express.createServer(); app.set('stream threshold', 1024); app.get('/*', function(req, res, next){ var file = req.params[0], path = __dirname + '/fixtures/' + file; res.sendfile(path); }); assert.response(app, { url: '/large.json' }, { headers: { 'Accept-Ranges': 'bytes' }}); }, 'test #sendfile() Range': function(assert){ var app = express.createServer(); app.set('stream threshold', 1024); app.get('/*', function(req, res, next){ var file = req.params[0], path = __dirname + '/fixtures/' + file; res.sendfile(path); });
assert.response(app, { url: '/large.json', headers: { 'Range': 'bytes=0-499' }}, { headers: { 'Content-Type': 'application/json', 'Content-Length': 500, 'Content-Range': 'bytes 0-499/2535' }, status: 206 }); },
'test #sendfile() Range invalid syntax': function(assert){ var app = express.createServer(); app.set('stream threshold', 1024); app.get('/*', function(req, res, next){ var file = req.params[0], path = __dirname + '/fixtures/' + file; res.sendfile(path); });
assert.response(app, { url: '/large.json', headers: { 'Range': 'basdytes=asdf' }}, { headers: { 'Content-Type': 'application/json', 'Content-Length': 2535 }, status: 200 }); }, 'test #sendfile() Range invalid range': function(assert){ var app = express.createServer(); app.set('stream threshold', 1024); app.get('/*', function(req, res, next){ var file = req.params[0], path = __dirname + '/fixtures/' + file; res.sendfile(path); });
assert.response(app, { url: '/large.json', headers: { 'Range': 'bytes=500-10' }}, { headers: { 'Content-Type': 'application/json', 'Content-Length': 2535 }, status: 200 }); }, 'test #download()': function(assert){ var app = express.createServer(); app.get('/json', function(req, res, next){ var filePath = __dirname + '/fixtures/user.json'; res.download(filePath, 'account.json', function(err, path){ assert.equal(filePath, path); }); });
app.get('/*', function(req, res, next){ res.download(__dirname + '/fixtures/' + req.params[0]); }); app.error(function(err, req, res){ res.send(404); }) 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-Length': '13', 'Content-Disposition': 'attachment; filename="account.json"' }}); assert.response(app, { url: '/missing' }, function(res){ assert.equal(null, res.headers['content-disposition']); }); }, 'test #cookie()': function(assert){ var app = express.createServer(); app.get('/', function(req, res){ res.cookie('rememberme', 'yes', { expires: new Date(1), httpOnly: true }); res.redirect('/'); }); assert.response(app, { url: '/' }, function(res){ assert.equal( 'rememberme=yes; expires=Thu, 01 Jan 1970 00:00:00 GMT; httpOnly', res.headers['set-cookie']); }); }, 'test #clearCookie()': function(assert){ var app = express.createServer(); app.get('/', function(req, res){ res.clearCookie('rememberme'); res.redirect('/'); }); assert.response(app, { url: '/' }, function(res){ assert.equal( 'rememberme=; expires=Thu, 01 Jan 1970 00:00:00 GMT', res.headers['set-cookie']); }); }, 'test HEAD': function(assert){ var app = express.createServer(); app.get('/', function(req, res, next){ res.send('Hello World'); }); assert.response(app, { url: '/', method: 'HEAD' }, { body: '', headers: { 'Content-Length': 11 }}); }};