response.test.js
This file
Path
test/response.test.js
411 lines · 12.7 KiB · JavaScript
Last changed by
more testsTj Holowaychukbefore adoption68fd658
History of this fileTake it
12/**3 * Module dependencies.4 */56var express = require('express')7 , Stream = require('stream').Stream8 , assert = require('assert')9 , should = require('should');1011module.exports = {12 'test #send()': function(){13 var app = express.createServer();1415 app.get('/html', function(req, res){16 res.send('<p>test</p>', { 'Content-Language': 'en' });17 });18 19 app.get('/json', function(req, res){20 res.header('X-Foo', 'bar');21 res.send({ foo: 'bar' }, { 'X-Foo': 'baz' }, 201);22 });23 24 app.get('/jsonp', function(req, res){25 app.enable('jsonp callback');26 res.header('X-Foo', 'bar');27 res.send({ foo: 'bar' }, { 'X-Foo': 'baz' }, 201);28 app.disable('jsonp callback');29 });30 31 app.get('/text', function(req, res){32 res.header('X-Foo', 'bar');33 res.contentType('.txt');34 res.send('wahoo');35 });36 37 app.get('/status', function(req, res){38 res.send(404);39 });40 41 app.get('/error', function(req, res){42 res.send('Oh shit!', { 'Content-Type': 'text/plain' }, 500);43 });44 45 app.get('/buffer', function(req, res){46 res.send(new Buffer('wahoo!'));47 });48 49 app.get('/noargs', function(req, res, next){50 res.send();51 });5253 assert.response(app,54 { url: '/html' },55 { body: '<p>test</p>', headers: { 'Content-Language': 'en', 'Content-Type': 'text/html' }});56 assert.response(app,57 { url: '/json' },58 { body: '{"foo":"bar"}', status: 201, headers: { 'Content-Type': 'application/json', 'X-Foo': 'baz' }});5960 assert.response(app,61 { url: '/jsonp?callback=test' },62 { body: 'test({"foo":"bar"});', status: 201, headers: { 'Content-Type': 'text/javascript', 'X-Foo': 'baz' }});6364 assert.response(app,65 { url: '/jsonp?callback=baz' },66 { body: 'baz({"foo":"bar"});', status: 201, headers: { 'Content-Type': 'text/javascript', 'X-Foo': 'baz' }});6768 assert.response(app,69 { url: '/jsonp?callback=invalid()[]' },70 { body: 'invalid({"foo":"bar"});', status: 201, headers: { 'Content-Type': 'text/javascript', 'X-Foo': 'baz' }});7172 assert.response(app,73 { url: '/json?callback=test' },74 { body: '{"foo":"bar"}', status: 201, headers: { 'Content-Type': 'application/json', 'X-Foo': 'baz' }});7576 assert.response(app,77 { url: '/text' },78 { body: 'wahoo', headers: { 'Content-Type': 'text/plain', 'X-Foo': 'bar' }});79 assert.response(app,80 { url: '/status' },81 { body: 'Not Found', status: 404, headers: { 'Content-Type': 'text/plain' }});82 assert.response(app,83 { url: '/error' },84 { body: 'Oh shit!', status: 500, headers: { 'Content-Type': 'text/plain', 'Content-Length': '8' }});85 assert.response(app,86 { url: '/buffer' },87 { body: 'wahoo!', headers: { 'Content-Type': 'application/octet-stream', 'Content-Length': '6' }});88 assert.response(app,89 { url: '/noargs' },90 { status: 204 }, function(res){91 assert.equal(undefined, res.headers['content-type']);92 assert.equal(undefined, res.headers['content-length']);93 });94 },95 96 'test #contentType()': function(){97 var app = express.createServer();98 99 app.get('/html', function(req, res){100 res.contentType('index.html');101 res.writeHead(200, res.headers);102 res.end('<p>yay</p>');103 });104 105 app.get('/json', function(req, res, next){106 res.contentType('json');107 res.send('{"foo":"bar"}');108 });109 110 assert.response(app,111 { url: '/html' },112 { body: '<p>yay</p>', headers: { 'Content-Type': 'text/html' }});113 assert.response(app,114 { url: '/json' },115 { headers: { 'Content-Type': 'application/json' }});116 },117 118 'test #attachment()': function(){119 var app = express.createServer();120 121 app.get('/style.css', function(req, res){122 res.attachment();123 res.send('some stylezzz');124 });125126 app.get('/*', function(req, res){127 res.attachment(req.params[0]);128 res.send('whatever');129 });130 131 assert.response(app,132 { url: '/javascripts/jquery.js' },133 { body: 'whatever', headers: { 'Content-Disposition': 'attachment; filename="jquery.js"' }});134 assert.response(app,135 { url: '/style.css' },136 { body: 'some stylezzz', headers: { 'Content-Disposition': 'attachment' }});137 },138 139 'test #redirect()': function(){140 var app = express.createServer()141 , app2 = express.createServer();142 143 app2.set('home', '/blog');144145 app2.redirect('google', 'http://google.com');146147 app2.redirect('blog', function(req, res){148 return req.params.id149 ? '/user/' + req.params.id + '/blog'150 : null;151 });152 153 app.get('/', function(req, res){154 res.redirect('http://google.com', 301);155 });156 157 app.get('/back', function(req, res){158 res.redirect('back');159 });160 161 app.get('/home', function(req, res){162 res.redirect('home');163 });164 165 app.get('/html', function(req, res){166 res.redirect('http://google.com');167 });168 169 app2.get('/', function(req, res){170 res.redirect('http://google.com', 301);171 });172 173 app2.get('/back', function(req, res){174 res.redirect('back');175 });176 177 app2.get('/home', function(req, res){178 res.redirect('home');179 });180 181 app2.get('/google', function(req, res){182 res.redirect('google');183 });184185 app2.get('/user/:id', function(req, res){186 res.header('X-Foo', 'bar');187 res.redirect('blog');188 });189190191 assert.response(app,192 { url: '/html', headers: { Accept: 'text/html,text/plain' }},193 { body: '<p>Moved Temporarily. Redirecting to <a href="http://google.com">http://google.com</a></p>' });194 assert.response(app,195 { url: '/', headers: { Accept: 'text/plain' }},196 { body: 'Moved Permanently. Redirecting to http://google.com', status: 301, headers: { Location: 'http://google.com' }});197 assert.response(app,198 { url: '/back', headers: { Accept: 'text/plain' }},199 { body: 'Moved Temporarily. Redirecting to /', status: 302, headers: { Location: '/', 'Content-Type': 'text/plain' }});200 assert.response(app,201 { url: '/back', headers: { Referer: '/foo', Accept: 'text/plain' }},202 { body: 'Moved Temporarily. Redirecting to /foo', status: 302, headers: { Location: '/foo' }});203 assert.response(app,204 { url: '/back', headers: { Referrer: '/foo', Accept: 'text/plain' }},205 { body: 'Moved Temporarily. Redirecting to /foo', status: 302, headers: { Location: '/foo' }});206 assert.response(app,207 { url: '/home', headers: { Accept: 'text/plain' } },208 { body: 'Moved Temporarily. Redirecting to /', status: 302, headers: { Location: '/' }});209210 assert.response(app2,211 { url: '/', headers: { Accept: 'text/plain' }},212 { body: 'Moved Permanently. Redirecting to http://google.com', status: 301, headers: { Location: 'http://google.com' }});213 assert.response(app2,214 { url: '/back', headers: { Accept: 'text/plain' }},215 { body: 'Moved Temporarily. Redirecting to /blog', status: 302, headers: { Location: '/blog' }});216 assert.response(app2,217 { url: '/home', headers: { Accept: 'text/plain' }},218 { body: 'Moved Temporarily. Redirecting to /blog', status: 302, headers: { Location: '/blog' }});219 assert.response(app2,220 { url: '/google', headers: { Accept: 'text/plain' }},221 { body: 'Moved Temporarily. Redirecting to http://google.com', status: 302, headers: { Location: 'http://google.com' }});222 assert.response(app2,223 { url: '/user/12', headers: { Accept: 'text/plain' }},224 { body: 'Moved Temporarily. Redirecting to /user/12/blog', status: 302, headers: { Location: '/user/12/blog', 'X-Foo': 'bar' }});225 },226 227 'test #sendfile()': function(){228 var app = express.createServer();229230 app.get('/*', function(req, res, next){231 var file = req.params[0]232 , filePath = __dirname + '/fixtures/' + file;233 res.sendfile(filePath, function(err, path){234 assert.equal(path, filePath);235 if (err) next(err);236 });237 });238 239 app.use(express.errorHandler());240241 assert.response(app,242 { url: '/../express.test.js' },243 { body: 'Forbidden', status: 403 });244 assert.response(app,245 { url: '/user.json' },246 { body: '{"name":"tj"}', status: 200, headers: { 'Content-Type': 'application/json' }});247 assert.response(app,248 { url: '/hello.haml' },249 { body: '%p Hello World', status: 200, headers: { 'Content-Type': 'application/octet-stream' }});250 assert.response(app,251 { url: '/doesNotExist' },252 { body: 'Internal Server Error', status: 500 });253 assert.response(app,254 { url: '/partials' },255 { body: 'Internal Server Error', status: 500 });256 assert.response(app,257 { url: '/large.json' },258 { status: 200, headers: { 'Content-Type': 'application/json' }});259 },260 261 'test #sendfile() Accept-Ranges': function(){262 var app = express.createServer();263 264 app.get('/*', function(req, res, next){265 var file = req.params[0]266 , path = __dirname + '/fixtures/' + file;267 res.sendfile(path);268 });269 270 assert.response(app,271 { url: '/large.json' },272 { headers: { 'Accept-Ranges': 'bytes' }});273 },274 275 'test #sendfile() Range': function(){276 var app = express.createServer();277 278 app.get('/*', function(req, res, next){279 var file = req.params[0]280 , path = __dirname + '/fixtures/' + file;281 res.sendfile(path);282 });283284 assert.response(app,285 { url: '/large.json', headers: { 'Range': 'bytes=0-499' }},286 { headers: {287 'Content-Type': 'application/json',288 'Content-Range': 'bytes 0-499/2535'289 }, status: 206 });290 },291292 'test #sendfile() Range invalid syntax': function(){293 var app = express.createServer();294 295 app.get('/*', function(req, res, next){296 var file = req.params[0]297 , path = __dirname + '/fixtures/' + file;298 res.sendfile(path);299 });300301 assert.response(app,302 { url: '/large.json', headers: { 'Range': 'basdytes=asdf' }},303 { body: 'Requested Range Not Satisfiable', status: 416 });304 },305306 'test #sendfile() Range invalid range': function(){307 var app = express.createServer();308 309 app.get('/*', function(req, res, next){310 var file = req.params[0]311 , path = __dirname + '/fixtures/' + file;312 res.sendfile(path);313 });314315 assert.response(app,316 { url: '/large.json', headers: { 'Range': 'bytes=500-10' }},317 { body: 'Requested Range Not Satisfiable', status: 416 });318 },319 320 'test #download()': function(beforeExit){321 var app = express.createServer()322 , calls = 0;323 324 app.get('/json', function(req, res, next){325 var filePath = __dirname + '/fixtures/user.json';326 res.download(filePath, 'account.json', function(err, path, stream){327 ++calls;328 path.should.equal(filePath);329 stream.should.be.an.instanceof(Stream);330 });331 });332333 app.get('/*', function(req, res, next){334 res.download(__dirname + '/fixtures/' + req.params[0]);335 });336 337 app.error(function(err, req, res){338 res.send(404);339 })340 341 assert.response(app,342 { url: '/user.json' },343 { body: '{"name":"tj"}', status: 200, headers: {344 'Content-Type': 'application/json',345 'Content-Disposition': 'attachment; filename="user.json"'346 }});347348 assert.response(app,349 { url: '/json' },350 { body: '{"name":"tj"}', status: 200, headers: {351 'Content-Type': 'application/json',352 'Content-Length': '13',353 'Content-Disposition': 'attachment; filename="account.json"'354 }});355 356 assert.response(app,357 { url: '/missing' },358 function(res){359 assert.equal(null, res.headers['content-disposition']);360 });361362 beforeExit(function(){363 calls.should.equal(1);364 });365 },366 367 'test #cookie()': function(){368 var app = express.createServer();369 370 app.get('/', function(req, res){371 res.cookie('rememberme', 'yes', { expires: new Date(1), httpOnly: true });372 res.cookie('something', 'else');373 res.redirect('/');374 });375 376 assert.response(app,377 { url: '/' },378 function(res){379 res.headers['set-cookie']380 .should.eql(['rememberme=yes; expires=Thu, 01 Jan 1970 00:00:00 GMT; httpOnly', 'something=else']);381 });382 },383 384 'test #clearCookie()': function(){385 var app = express.createServer();386 387 app.get('/', function(req, res){388 res.clearCookie('rememberme');389 res.redirect('/');390 });391 392 assert.response(app,393 { url: '/' },394 function(res){395 res.headers['set-cookie']396 .should.eql(['rememberme=; expires=Thu, 01 Jan 1970 00:00:00 GMT']);397 });398 },399 400 'test HEAD': function(){401 var app = express.createServer();402 403 app.get('/', function(req, res, next){404 res.send('Hello World');405 });406 407 assert.response(app,408 { url: '/', method: 'HEAD' },409 { body: '', headers: { 'Content-Length': 11 }});410 }411};