Skip to content
Myr

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 file

Take it

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

Settings docsTj Holowaychukbefore adoptione562adc

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

using should / refactored testsTj Holowaychukbefore adoption643879f

lines 6–9why, one step back
6var express = require('express')
7 , Buffer = require('buffer').Buffer8 , assert = require('assert')9 , should = require('should');

Settings docsTj Holowaychukbefore adoptione562adc

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();

Settings docsTj Holowaychukbefore adoptione562adc

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 });

Settings docsTj Holowaychukbefore adoptione562adc

line 52why, one step back
52

using should / refactored testsTj Holowaychukbefore adoption643879f

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

Settings docsTj Holowaychukbefore adoptione562adc

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' }});

Settings docsTj Holowaychukbefore adoptione562adc

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' }});

Settings docsTj Holowaychukbefore adoptione562adc

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' }});

Settings docsTj Holowaychukbefore adoptione562adc

line 75why, one step back
75

using should / refactored testsTj Holowaychukbefore adoption643879f

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

Settings docsTj Holowaychukbefore adoptione562adc

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 });

Settings docsTj Holowaychukbefore adoptione562adc

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–112why, one step back
110 assert.response(app,
111 { url: '/html' },112 { body: '<p>yay</p>', headers: { 'Content-Type': 'text/html; charset=utf-8' }});

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 });

Settings docsTj Holowaychukbefore adoptione562adc

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 });

Settings docsTj Holowaychukbefore adoptione562adc

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');

Settings docsTj Holowaychukbefore adoptione562adc

line 144why, one step back
144

using should / refactored testsTj Holowaychukbefore adoption643879f

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

Settings docsTj Holowaychukbefore adoptione562adc

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 });

Settings docsTj Holowaychukbefore adoptione562adc

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 });

Settings docsTj Holowaychukbefore adoptione562adc

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: '/' }});

Settings docsTj Holowaychukbefore adoptione562adc

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 });

Settings docsTj Holowaychukbefore adoptione562adc

line 238why, one step back
238

using should / refactored testsTj Holowaychukbefore adoption643879f

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

Settings docsTj Holowaychukbefore adoptione562adc

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 });

Settings docsTj Holowaychukbefore adoptione562adc

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 },

Settings docsTj Holowaychukbefore adoptione562adc

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();

Settings docsTj Holowaychukbefore adoptione562adc

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 });

Settings docsTj Holowaychukbefore adoptione562adc

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 },

Settings docsTj Holowaychukbefore adoptione562adc

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 });

Settings docsTj Holowaychukbefore adoptione562adc

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 },

Settings docsTj Holowaychukbefore adoptione562adc

line 319why, one step back
319

using should / refactored testsTj Holowaychukbefore adoption643879f

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

Settings docsTj Holowaychukbefore adoptione562adc

line 329why, one step back
329

using should / refactored testsTj Holowaychukbefore adoption643879f

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

Settings docsTj Holowaychukbefore adoptione562adc

line 333why, one step back
333

using should / refactored testsTj Holowaychukbefore adoption643879f

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

Settings docsTj Holowaychukbefore adoptione562adc

line 344why, one step back
344

using should / refactored testsTj Holowaychukbefore adoption643879f

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

Settings docsTj Holowaychukbefore adoptione562adc

line 352why, one step back
352

using should / refactored testsTj Holowaychukbefore adoption643879f

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

Settings docsTj Holowaychukbefore adoptione562adc

line 362why, one step back
362

using should / refactored testsTj Holowaychukbefore adoption643879f

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

Added multiple Set-Cookie header testsTj Holowaychukbefore adoption80c3b98

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

using should / refactored testsTj Holowaychukbefore adoption643879f

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

Settings docsTj Holowaychukbefore adoptione562adc

line 368why, one step back
368

using should / refactored testsTj Holowaychukbefore adoption643879f

lines 369–372why, one step back
369 assert.response(app,
370 { url: '/' },371 function(res){372 res.headers['set-cookie']

Added multiple Set-Cookie header testsTj Holowaychukbefore adoption80c3b98

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

using should / refactored testsTj Holowaychukbefore adoption643879f

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

Fixed two testsTj Holowaychukbefore adoptiondf49769

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

using should / refactored testsTj Holowaychukbefore adoption643879f

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

Settings docsTj Holowaychukbefore adoptione562adc

line 404why, one step back
404};