response.test.js
This file
Path
test/response.test.js
404 lines · 12.6 KiB · JavaScript
Last changed by
Added multiple Set-Cookie header testsTj Holowaychukbefore adoption80c3b98
History of this fileTake it
12/**3 * Module dependencies.4 */56var express = require('express')7 , Buffer = require('buffer').Buffer8 , 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; charset=utf-8' }});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; charset=utf-8', 'X-Foo': 'bar' }});79 assert.response(app,80 { url: '/status' },81 { body: 'Not Found', status: 404, headers: { 'Content-Type': 'text/plain; charset=utf-8' }});82 assert.response(app,83 { url: '/error' },84 { body: 'Oh shit!', status: 500, headers: { 'Content-Type': 'text/plain' }});85 assert.response(app,86 { url: '/buffer' },87 { body: 'wahoo!', headers: { 'Content-Type': 'application/octet-stream' }});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; charset=utf-8' }});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(){321 var app = express.createServer();322 323 app.get('/json', function(req, res, next){324 var filePath = __dirname + '/fixtures/user.json';325 res.download(filePath, 'account.json', function(err, path){326 assert.equal(filePath, path);327 });328 });329330 app.get('/*', function(req, res, next){331 res.download(__dirname + '/fixtures/' + req.params[0]);332 });333 334 app.error(function(err, req, res){335 res.send(404);336 })337 338 assert.response(app,339 { url: '/user.json' },340 { body: '{"name":"tj"}', status: 200, headers: {341 'Content-Type': 'application/json',342 'Content-Disposition': 'attachment; filename="user.json"'343 }});344345 assert.response(app,346 { url: '/json' },347 { body: '{"name":"tj"}', status: 200, headers: {348 'Content-Type': 'application/json',349 'Content-Length': '13',350 'Content-Disposition': 'attachment; filename="account.json"'351 }});352 353 assert.response(app,354 { url: '/missing' },355 function(res){356 assert.equal(null, res.headers['content-disposition']);357 });358 },359 360 'test #cookie()': function(){361 var app = express.createServer();362 363 app.get('/', function(req, res){364 res.cookie('rememberme', 'yes', { expires: new Date(1), httpOnly: true });365 res.cookie('something', 'else');366 res.redirect('/');367 });368 369 assert.response(app,370 { url: '/' },371 function(res){372 res.headers['set-cookie']373 .should.eql(['rememberme=yes; expires=Thu, 01 Jan 1970 00:00:00 GMT; httpOnly', 'something=else']);374 });375 },376 377 'test #clearCookie()': function(){378 var app = express.createServer();379 380 app.get('/', function(req, res){381 res.clearCookie('rememberme');382 res.redirect('/');383 });384 385 assert.response(app,386 { url: '/' },387 function(res){388 res.headers['set-cookie']389 .should.eql(['rememberme=; expires=Thu, 01 Jan 1970 00:00:00 GMT']);390 });391 },392 393 'test HEAD': function(){394 var app = express.createServer();395 396 app.get('/', function(req, res, next){397 res.send('Hello World');398 });399 400 assert.response(app,401 { url: '/', method: 'HEAD' },402 { body: '', headers: { 'Content-Length': 11 }});403 }404};