Skip to content
Myr

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 file

Take it

The walk stopped after 200 commits: lines older than ce2a18f point at it.

master is 2.0.0-preTj Holowaychukbefore adoptionce2a18f

lines 1–5why, one step back
1
2/**3 * Module dependencies.4 */5

using should / refactored testsTj Holowaychukbefore adoption643879f

line 6why, one step back
6var express = require('express')

Fixed res.download() callback, passing stream. Closes #528Tj Holowaychukbefore adoption07eb66c

line 7why, one step back
7 , Stream = require('stream').Stream

using should / refactored testsTj Holowaychukbefore adoption643879f

lines 8–9why, one step back
8 , assert = require('assert')
9 , should = require('should');

master is 2.0.0-preTj Holowaychukbefore adoptionce2a18f

lines 10–11why, one step back
10
11module.exports = {

using should / refactored testsTj Holowaychukbefore adoption643879f

lines 12–13why, one step back
12 'test #send()': function(){
13 var app = express.createServer();

master is 2.0.0-preTj Holowaychukbefore adoptionce2a18f

line 14why, one step back
14

using should / refactored testsTj Holowaychukbefore adoption643879f

lines 15–51why, one step back
15 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 });

master is 2.0.0-preTj Holowaychukbefore adoptionce2a18f

line 52why, one step back
52

using should / refactored testsTj Holowaychukbefore adoption643879f

lines 53–54why, one step back
53 assert.response(app,
54 { url: '/html' },

Changed; charsets are no longer impliedTj Holowaychukbefore adoption9fc3499

line 55why, one step back
55 { body: '<p>test</p>', headers: { 'Content-Language': 'en', 'Content-Type': 'text/html' }});

using should / refactored testsTj Holowaychukbefore adoption643879f

lines 56–58why, one step back
56 assert.response(app,
57 { url: '/json' },58 { body: '{"foo":"bar"}', status: 201, headers: { 'Content-Type': 'application/json', 'X-Foo': 'baz' }});

master is 2.0.0-preTj Holowaychukbefore adoptionce2a18f

line 59why, one step back
59

using should / refactored testsTj Holowaychukbefore adoption643879f

lines 60–62why, one step back
60 assert.response(app,
61 { url: '/jsonp?callback=test' },62 { body: 'test({"foo":"bar"});', status: 201, headers: { 'Content-Type': 'text/javascript', 'X-Foo': 'baz' }});

master is 2.0.0-preTj Holowaychukbefore adoptionce2a18f

line 63why, one step back
63

using should / refactored testsTj Holowaychukbefore adoption643879f

lines 64–66why, one step back
64 assert.response(app,
65 { url: '/jsonp?callback=baz' },66 { body: 'baz({"foo":"bar"});', status: 201, headers: { 'Content-Type': 'text/javascript', 'X-Foo': 'baz' }});

master is 2.0.0-preTj Holowaychukbefore adoptionce2a18f

line 67why, one step back
67

using should / refactored testsTj Holowaychukbefore adoption643879f

line 68why, one step back
68 assert.response(app,

Strip unsafe chars from jsonp callbacksTj Holowaychukbefore adoption482fb5b

lines 69–72why, one step back
69 { url: '/jsonp?callback=invalid()[]' },
70 { body: 'invalid({"foo":"bar"});', status: 201, headers: { 'Content-Type': 'text/javascript', 'X-Foo': 'baz' }});7172 assert.response(app,

using should / refactored testsTj Holowaychukbefore adoption643879f

lines 73–74why, one step back
73 { url: '/json?callback=test' },
74 { body: '{"foo":"bar"}', status: 201, headers: { 'Content-Type': 'application/json', 'X-Foo': 'baz' }});

master is 2.0.0-preTj Holowaychukbefore adoptionce2a18f

line 75why, one step back
75

using should / refactored testsTj Holowaychukbefore adoption643879f

lines 76–77why, one step back
76 assert.response(app,
77 { url: '/text' },

Changed; charsets are no longer impliedTj Holowaychukbefore adoption9fc3499

line 78why, one step back
78 { body: 'wahoo', headers: { 'Content-Type': 'text/plain', 'X-Foo': 'bar' }});

using should / refactored testsTj Holowaychukbefore adoption643879f

lines 79–80why, one step back
79 assert.response(app,
80 { url: '/status' },

Changed; charsets are no longer impliedTj Holowaychukbefore adoption9fc3499

line 81why, one step back
81 { body: 'Not Found', status: 404, headers: { 'Content-Type': 'text/plain' }});

using should / refactored testsTj Holowaychukbefore adoption643879f

lines 82–83why, one step back
82 assert.response(app,
83 { url: '/error' },

more testsTj Holowaychukbefore adoption68fd658

line 84why, one step back
84 { body: 'Oh shit!', status: 500, headers: { 'Content-Type': 'text/plain', 'Content-Length': '8' }});

using should / refactored testsTj Holowaychukbefore adoption643879f

lines 85–86why, one step back
85 assert.response(app,
86 { url: '/buffer' },

more testsTj Holowaychukbefore adoption68fd658

line 87why, one step back
87 { body: 'wahoo!', headers: { 'Content-Type': 'application/octet-stream', 'Content-Length': '6' }});

using should / refactored testsTj Holowaychukbefore adoption643879f

lines 88–97why, one step back
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();

master is 2.0.0-preTj Holowaychukbefore adoptionce2a18f

line 98why, one step back
98

using should / refactored testsTj Holowaychukbefore adoption643879f

lines 99–103why, one step back
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 });

master is 2.0.0-preTj Holowaychukbefore adoptionce2a18f

line 104why, one step back
104

Added extname with no leading "." support to res.contentType)Tj Holowaychukbefore adoptiona76fba5

lines 105–109why, one step back
105 app.get('/json', function(req, res, next){
106 res.contentType('json');107 res.send('{"foo":"bar"}');108 });109

using should / refactored testsTj Holowaychukbefore adoption643879f

lines 110–111why, one step back
110 assert.response(app,
111 { url: '/html' },

Changed; charsets are no longer impliedTj Holowaychukbefore adoption9fc3499

line 112why, one step back
112 { body: '<p>yay</p>', headers: { 'Content-Type': 'text/html' }});

Added extname with no leading "." support to res.contentType)Tj Holowaychukbefore adoptiona76fba5

lines 113–115why, one step back
113 assert.response(app,
114 { url: '/json' },115 { headers: { 'Content-Type': 'application/json' }});

using should / refactored testsTj Holowaychukbefore adoption643879f

lines 116–124why, one step back
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 });

master is 2.0.0-preTj Holowaychukbefore adoptionce2a18f

line 125why, one step back
125

using should / refactored testsTj Holowaychukbefore adoption643879f

lines 126–129why, one step back
126 app.get('/*', function(req, res){
127 res.attachment(req.params[0]);128 res.send('whatever');129 });

master is 2.0.0-preTj Holowaychukbefore adoptionce2a18f

line 130why, one step back
130

using should / refactored testsTj Holowaychukbefore adoption643879f

lines 131–143why, one step back
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');

master is 2.0.0-preTj Holowaychukbefore adoptionce2a18f

line 144why, one step back
144

using should / refactored testsTj Holowaychukbefore adoption643879f

line 145why, one step back
145 app2.redirect('google', 'http://google.com');

master is 2.0.0-preTj Holowaychukbefore adoptionce2a18f

line 146why, one step back
146

using should / refactored testsTj Holowaychukbefore adoption643879f

lines 147–183why, one step back
147 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 });

master is 2.0.0-preTj Holowaychukbefore adoptionce2a18f

line 184why, one step back
184

using should / refactored testsTj Holowaychukbefore adoption643879f

lines 185–188why, one step back
185 app2.get('/user/:id', function(req, res){
186 res.header('X-Foo', 'bar');187 res.redirect('blog');188 });

master is 2.0.0-preTj Holowaychukbefore adoptionce2a18f

lines 189–190why, one step back
189
190

using should / refactored testsTj Holowaychukbefore adoption643879f

lines 191–208why, one step back
191 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: '/' }});

master is 2.0.0-preTj Holowaychukbefore adoptionce2a18f

line 209why, one step back
209

using should / refactored testsTj Holowaychukbefore adoption643879f

lines 210–237why, one step back
210 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 });

master is 2.0.0-preTj Holowaychukbefore adoptionce2a18f

line 238why, one step back
238

using should / refactored testsTj Holowaychukbefore adoption643879f

line 239why, one step back
239 app.use(express.errorHandler());

master is 2.0.0-preTj Holowaychukbefore adoptionce2a18f

line 240why, one step back
240

using should / refactored testsTj Holowaychukbefore adoption643879f

lines 241–282why, one step back
241 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 });

master is 2.0.0-preTj Holowaychukbefore adoptionce2a18f

line 283why, one step back
283

using should / refactored testsTj Holowaychukbefore adoption643879f

lines 284–290why, one step back
284 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 },

master is 2.0.0-preTj Holowaychukbefore adoptionce2a18f

line 291why, one step back
291

using should / refactored testsTj Holowaychukbefore adoption643879f

lines 292–293why, one step back
292 'test #sendfile() Range invalid syntax': function(){
293 var app = express.createServer();

master is 2.0.0-preTj Holowaychukbefore adoptionce2a18f

line 294why, one step back
294

using should / refactored testsTj Holowaychukbefore adoption643879f

lines 295–299why, one step back
295 app.get('/*', function(req, res, next){
296 var file = req.params[0]297 , path = __dirname + '/fixtures/' + file;298 res.sendfile(path);299 });

master is 2.0.0-preTj Holowaychukbefore adoptionce2a18f

line 300why, one step back
300

using should / refactored testsTj Holowaychukbefore adoption643879f

lines 301–302why, one step back
301 assert.response(app,
302 { url: '/large.json', headers: { 'Range': 'basdytes=asdf' }},

Added 416 support to res.sendfile()Tj Holowaychukbefore adoptione7df82f

line 303why, one step back
303 { body: 'Requested Range Not Satisfiable', status: 416 });

using should / refactored testsTj Holowaychukbefore adoption643879f

line 304why, one step back
304 },

master is 2.0.0-preTj Holowaychukbefore adoptionce2a18f

line 305why, one step back
305

using should / refactored testsTj Holowaychukbefore adoption643879f

lines 306–313why, one step back
306 '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 });

master is 2.0.0-preTj Holowaychukbefore adoptionce2a18f

line 314why, one step back
314

using should / refactored testsTj Holowaychukbefore adoption643879f

lines 315–316why, one step back
315 assert.response(app,
316 { url: '/large.json', headers: { 'Range': 'bytes=500-10' }},

Added 416 support to res.sendfile()Tj Holowaychukbefore adoptione7df82f

line 317why, one step back
317 { body: 'Requested Range Not Satisfiable', status: 416 });

using should / refactored testsTj Holowaychukbefore adoption643879f

line 318why, one step back
318 },

master is 2.0.0-preTj Holowaychukbefore adoptionce2a18f

line 319why, one step back
319

Fixed res.download() callback, passing stream. Closes #528Tj Holowaychukbefore adoption07eb66c

lines 320–322why, one step back
320 'test #download()': function(beforeExit){
321 var app = express.createServer()322 , calls = 0;

using should / refactored testsTj Holowaychukbefore adoption643879f

lines 323–325why, one step back
323
324 app.get('/json', function(req, res, next){325 var filePath = __dirname + '/fixtures/user.json';

Fixed res.download() callback, passing stream. Closes #528Tj Holowaychukbefore adoption07eb66c

lines 326–329why, one step back
326 res.download(filePath, 'account.json', function(err, path, stream){
327 ++calls;328 path.should.equal(filePath);329 stream.should.be.an.instanceof(Stream);

using should / refactored testsTj Holowaychukbefore adoption643879f

lines 330–331why, one step back
330 });
331 });

master is 2.0.0-preTj Holowaychukbefore adoptionce2a18f

line 332why, one step back
332

using should / refactored testsTj Holowaychukbefore adoption643879f

lines 333–335why, one step back
333 app.get('/*', function(req, res, next){
334 res.download(__dirname + '/fixtures/' + req.params[0]);335 });

master is 2.0.0-preTj Holowaychukbefore adoptionce2a18f

line 336why, one step back
336

using should / refactored testsTj Holowaychukbefore adoption643879f

lines 337–346why, one step back
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 }});

master is 2.0.0-preTj Holowaychukbefore adoptionce2a18f

line 347why, one step back
347

using should / refactored testsTj Holowaychukbefore adoption643879f

lines 348–354why, one step back
348 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 }});

master is 2.0.0-preTj Holowaychukbefore adoptionce2a18f

line 355why, one step back
355

using should / refactored testsTj Holowaychukbefore adoption643879f

lines 356–360why, one step back
356 assert.response(app,
357 { url: '/missing' },358 function(res){359 assert.equal(null, res.headers['content-disposition']);360 });

Fixed res.download() callback, passing stream. Closes #528Tj Holowaychukbefore adoption07eb66c

lines 361–364why, one step back
361
362 beforeExit(function(){363 calls.should.equal(1);364 });

using should / refactored testsTj Holowaychukbefore adoption643879f

lines 365–368why, one step back
365 },
366 367 'test #cookie()': function(){368 var app = express.createServer();

master is 2.0.0-preTj Holowaychukbefore adoptionce2a18f

line 369why, one step back
369

using should / refactored testsTj Holowaychukbefore adoption643879f

lines 370–371why, one step back
370 app.get('/', function(req, res){
371 res.cookie('rememberme', 'yes', { expires: new Date(1), httpOnly: true });

Added multiple Set-Cookie header testsTj Holowaychukbefore adoption80c3b98

line 372why, one step back
372 res.cookie('something', 'else');

using should / refactored testsTj Holowaychukbefore adoption643879f

lines 373–374why, one step back
373 res.redirect('/');
374 });

master is 2.0.0-preTj Holowaychukbefore adoptionce2a18f

line 375why, one step back
375

using should / refactored testsTj Holowaychukbefore adoption643879f

lines 376–379why, one step back
376 assert.response(app,
377 { url: '/' },378 function(res){379 res.headers['set-cookie']

Added multiple Set-Cookie header testsTj Holowaychukbefore adoption80c3b98

line 380why, one step back
380 .should.eql(['rememberme=yes; expires=Thu, 01 Jan 1970 00:00:00 GMT; httpOnly', 'something=else']);

using should / refactored testsTj Holowaychukbefore adoption643879f

lines 381–395why, one step back
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']

Fixed two testsTj Holowaychukbefore adoptiondf49769

line 396why, one step back
396 .should.eql(['rememberme=; expires=Thu, 01 Jan 1970 00:00:00 GMT']);

using should / refactored testsTj Holowaychukbefore adoption643879f

lines 397–410why, one step back
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 }

master is 2.0.0-preTj Holowaychukbefore adoptionce2a18f

line 411why, one step back
411};