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
The walk stopped after 200 commits: lines older than dcf2791 point at it.
Fixed tests due to params.splat removalTj Holowaychukbefore adoptiondcf2791
lines 1–38why, one step back12/**3 * Module dependencies.4 */56var express = require('express'),7 Buffer = require('buffer').Buffer;89module.exports = {10 'test #send()': function(assert){11 var app = express.createServer();1213 app.get('/html', function(req, res){14 res.send('<p>test</p>', { 'Content-Language': 'en' });15 });16 17 app.get('/json', function(req, res){18 res.header('X-Foo', 'bar');19 res.send({ foo: 'bar' }, { 'X-Foo': 'baz' }, 201);20 });21 22 app.get('/text', function(req, res){23 res.header('X-Foo', 'bar');24 res.contentType('.txt');25 res.send('wahoo');26 });27 28 app.get('/status', function(req, res){29 res.send(404);30 });31 32 app.get('/error', function(req, res){33 res.send('Oh shit!', { 'Content-Type': 'text/plain' }, 500);34 });35 36 app.get('/buffer', function(req, res){37 res.send(new Buffer('wahoo!'));38 });Merge branch 'master' of github.com:visionmedia/expressTj Holowaychukbefore adoption838a51d
lines 39–42why, one step back39 40 app.get('/noargs', function(req, res, next){41 res.send();42 });Fixed tests due to params.splat removalTj Holowaychukbefore adoptiondcf2791
lines 43–61why, one step back4344 assert.response(app,45 { url: '/html' },46 { body: '<p>test</p>', headers: { 'Content-Language': 'en', 'Content-Type': 'text/html; charset=utf-8' }});47 assert.response(app,48 { url: '/json' },49 { body: '{"foo":"bar"}', status: 201, headers: { 'Content-Type': 'application/json', 'X-Foo': 'baz' }});50 assert.response(app,51 { url: '/text' },52 { body: 'wahoo', headers: { 'Content-Type': 'text/plain; charset=utf-8', 'X-Foo': 'bar' }});53 assert.response(app,54 { url: '/status' },55 { body: 'Not Found', status: 404, headers: { 'Content-Type': 'text/plain; charset=utf-8' }});56 assert.response(app,57 { url: '/error' },58 { body: 'Oh shit!', status: 500, headers: { 'Content-Type': 'text/plain' }});59 assert.response(app,60 { url: '/buffer' },61 { body: 'wahoo!', headers: { 'Content-Type': 'application/octet-stream' }});Merge branch 'master' of github.com:visionmedia/expressTj Holowaychukbefore adoption838a51d
lines 62–67why, one step back62 assert.response(app,63 { url: '/noargs' },64 { status: 204 }, function(res){65 assert.equal(undefined, res.headers['content-type']);66 assert.equal(undefined, res.headers['content-length']);67 });Fixed tests due to params.splat removalTj Holowaychukbefore adoptiondcf2791
lines 68–69why, one step back68 },69 Fixed test labelsTj Holowaychukbefore adoption2844b1a
line 70why, one step back70 'test #contentType()': function(assert){Fixed tests due to params.splat removalTj Holowaychukbefore adoptiondcf2791
lines 71–83why, one step back71 var app = express.createServer();72 73 app.get('/html', function(req, res){74 res.contentType('index.html');75 res.writeHead(200, res.headers);76 res.end('<p>yay</p>');77 });78 79 assert.response(app,80 { url: '/html' },81 { body: '<p>yay</p>', headers: { 'Content-Type': 'text/html; charset=utf-8' }});82 },83 Fixed test labelsTj Holowaychukbefore adoption2844b1a
line 84why, one step back84 'test #attachment()': function(assert){Fixed tests due to params.splat removalTj Holowaychukbefore adoptiondcf2791
lines 85–91why, one step back85 var app = express.createServer();86 87 app.get('/style.css', function(req, res){88 res.attachment();89 res.send('some stylezzz');90 });91Route callback signature changedTj Holowaychukbefore adoption75661a2
lines 92–93why, one step back92 app.get('/*', function(req, res){93 res.attachment(req.params[0]);Fixed tests due to params.splat removalTj Holowaychukbefore adoptiondcf2791
lines 94–112why, one step back94 res.send('whatever');95 });96 97 assert.response(app,98 { url: '/javascripts/jquery.js' },99 { body: 'whatever', headers: { 'Content-Disposition': 'attachment; filename="jquery.js"' }});100 assert.response(app,101 { url: '/style.css' },102 { body: 'some stylezzz', headers: { 'Content-Disposition': 'attachment' }});103 },104 105 'test #redirect()': function(assert){106 var app = express.createServer(),107 app2 = express.createServer();108 109 app2.set('home', '/blog');110111 app2.redirect('google', 'http://google.com');112Route callback signature changedTj Holowaychukbefore adoption75661a2
lines 113–115why, one step back113 app2.redirect('blog', function(req, res){114 return req.params.id115 ? '/user/' + req.params.id + '/blog'Fixed tests due to params.splat removalTj Holowaychukbefore adoptiondcf2791
lines 116–147why, one step back116 : null;117 });118 119 app.get('/', function(req, res){120 res.redirect('http://google.com', 301);121 });122 123 app.get('/back', function(req, res){124 res.redirect('back');125 });126 127 app.get('/home', function(req, res){128 res.redirect('home');129 });130 131 app2.get('/', function(req, res){132 res.redirect('http://google.com', 301);133 });134 135 app2.get('/back', function(req, res){136 res.redirect('back');137 });138 139 app2.get('/home', function(req, res){140 res.redirect('home');141 });142 143 app2.get('/google', function(req, res){144 res.redirect('google');145 });146147 app2.get('/user/:id', function(req, res){Merge branch 'redirect-bodies'Tj Holowaychukbefore adoption640274e
line 148why, one step back148 res.header('X-Foo', 'bar');Fixed tests due to params.splat removalTj Holowaychukbefore adoptiondcf2791
lines 149–153why, one step back149 res.redirect('blog');150 });151 152 assert.response(app,153 { url: '/' },Merge branch 'redirect-bodies'Tj Holowaychukbefore adoption640274e
line 154why, one step back154 { body: 'Redirecting to http://google.com', status: 301, headers: { Location: 'http://google.com' }});Fixed tests due to params.splat removalTj Holowaychukbefore adoptiondcf2791
lines 155–156why, one step back155 assert.response(app,156 { url: '/back' },Merge branch 'redirect-bodies'Tj Holowaychukbefore adoption640274e
line 157why, one step back157 { body: 'Redirecting to /', status: 302, headers: { Location: '/', 'Content-Type': 'text/plain' }});Fixed tests due to params.splat removalTj Holowaychukbefore adoptiondcf2791
lines 158–159why, one step back158 assert.response(app,159 { url: '/back', headers: { Referer: '/foo' }},Merge branch 'redirect-bodies'Tj Holowaychukbefore adoption640274e
line 160why, one step back160 { body: 'Redirecting to /foo', status: 302, headers: { Location: '/foo' }});Fixed tests due to params.splat removalTj Holowaychukbefore adoptiondcf2791
lines 161–162why, one step back161 assert.response(app,162 { url: '/back', headers: { Referrer: '/foo' }},Merge branch 'redirect-bodies'Tj Holowaychukbefore adoption640274e
line 163why, one step back163 { body: 'Redirecting to /foo', status: 302, headers: { Location: '/foo' }});Fixed tests due to params.splat removalTj Holowaychukbefore adoptiondcf2791
lines 164–165why, one step back164 assert.response(app,165 { url: '/home' },Merge branch 'redirect-bodies'Tj Holowaychukbefore adoption640274e
line 166why, one step back166 { body: 'Redirecting to /', status: 302, headers: { Location: '/' }});Fixed tests due to params.splat removalTj Holowaychukbefore adoptiondcf2791
lines 167–169why, one step back167168 assert.response(app2,169 { url: '/' },Merge branch 'redirect-bodies'Tj Holowaychukbefore adoption640274e
line 170why, one step back170 { body: 'Redirecting to http://google.com', status: 301, headers: { Location: 'http://google.com' }});Fixed tests due to params.splat removalTj Holowaychukbefore adoptiondcf2791
lines 171–172why, one step back171 assert.response(app2,172 { url: '/back' },Merge branch 'redirect-bodies'Tj Holowaychukbefore adoption640274e
line 173why, one step back173 { body: 'Redirecting to /blog', status: 302, headers: { Location: '/blog' }});Fixed tests due to params.splat removalTj Holowaychukbefore adoptiondcf2791
lines 174–175why, one step back174 assert.response(app2,175 { url: '/home' },Merge branch 'redirect-bodies'Tj Holowaychukbefore adoption640274e
line 176why, one step back176 { body: 'Redirecting to /blog', status: 302, headers: { Location: '/blog' }});Fixed tests due to params.splat removalTj Holowaychukbefore adoptiondcf2791
lines 177–178why, one step back177 assert.response(app2,178 { url: '/google' },Merge branch 'redirect-bodies'Tj Holowaychukbefore adoption640274e
line 179why, one step back179 { body: 'Redirecting to http://google.com', headers: { Location: 'http://google.com' }});Fixed tests due to params.splat removalTj Holowaychukbefore adoptiondcf2791
lines 180–181why, one step back180 assert.response(app2,181 { url: '/user/12' },Merge branch 'redirect-bodies'Tj Holowaychukbefore adoption640274e
line 182why, one step back182 { body: 'Redirecting to /user/12/blog', headers: { Location: '/user/12/blog', 'X-Foo': 'bar' }});Fixed tests due to params.splat removalTj Holowaychukbefore adoptiondcf2791
lines 183–186why, one step back183 },184 185 'test #sendfile()': function(assert){186 var app = express.createServer();Fixed res.sendfile() error handling, defer via next()Tj Holowaychukbefore adoption8308e50
line 187why, one step back187Merge branch 'downloads'Tj Holowaychukbefore adoption949803d
lines 188–189why, one step back188 app.set('stream threshold', 2 * 1024)189Fixed res.sendfile() error handling, defer via next()Tj Holowaychukbefore adoption8308e50
line 190why, one step back190 app.get('/*', function(req, res, next){Added callback support for res.sendfile()Tj Holowaychukbefore adoption25909c0
lines 191–194why, one step back191 var file = req.params[0],192 filePath = __dirname + '/fixtures/' + file;193 res.sendfile(filePath, function(err, path){194 assert.equal(path, filePath);Fixed res.sendfile() error handling, defer via next()Tj Holowaychukbefore adoption8308e50
line 195why, one step back195 if (err) next(err);Added callback support for res.sendfile()Tj Holowaychukbefore adoption25909c0
line 196why, one step back196 });Fixed tests due to params.splat removalTj Holowaychukbefore adoptiondcf2791
lines 197–198why, one step back197 });198 Fixed res.sendfile() error handling, defer via next()Tj Holowaychukbefore adoption8308e50
line 199why, one step back199 app.use(express.errorHandler());res.sendfile() responding with 403 on malicious pathTj Holowaychukbefore adoption177a724
lines 200–203why, one step back200201 assert.response(app,202 { url: '/../express.test.js' },203 { body: 'Forbidden', status: 403 });Fixed tests due to params.splat removalTj Holowaychukbefore adoptiondcf2791
lines 204–211why, one step back204 assert.response(app,205 { url: '/user.json' },206 { body: '{"name":"tj"}', status: 200, headers: { 'Content-Type': 'application/json' }});207 assert.response(app,208 { url: '/hello.haml' },209 { body: '%p Hello World', status: 200, headers: { 'Content-Type': 'application/octet-stream' }});210 assert.response(app,211 { url: '/doesNotExist' },Fixed res.sendfile() error handling, defer via next()Tj Holowaychukbefore adoption8308e50
line 212why, one step back212 { body: 'Internal Server Error', status: 500 });Fixed tests due to params.splat removalTj Holowaychukbefore adoptiondcf2791
lines 213–215why, one step back213 assert.response(app,214 { url: '/partials' },215 { body: 'Internal Server Error', status: 500 });Merge branch 'downloads'Tj Holowaychukbefore adoption949803d
lines 216–218why, one step back216 assert.response(app,217 { url: '/large.json' },218 { status: 200, headers: { 'Content-Length': 2535, 'Content-Type': 'application/json' }});Fixed tests due to params.splat removalTj Holowaychukbefore adoptiondcf2791
lines 219–220why, one step back219 },220 Merge branch 'range'Tj Holowaychukbefore adoptionfab9af2
lines 221–294why, one step back221 'test #sendfile() Accept-Ranges': function(assert){222 var app = express.createServer();223 224 app.set('stream threshold', 1024);225 226 app.get('/*', function(req, res, next){227 var file = req.params[0],228 path = __dirname + '/fixtures/' + file;229 res.sendfile(path);230 });231 232 assert.response(app,233 { url: '/large.json' },234 { headers: { 'Accept-Ranges': 'bytes' }});235 },236 237 'test #sendfile() Range': function(assert){238 var app = express.createServer();239 240 app.set('stream threshold', 1024);241 242 app.get('/*', function(req, res, next){243 var file = req.params[0],244 path = __dirname + '/fixtures/' + file;245 res.sendfile(path);246 });247248 assert.response(app,249 { url: '/large.json', headers: { 'Range': 'bytes=0-499' }},250 { headers: {251 'Content-Type': 'application/json',252 'Content-Length': 500,253 'Content-Range': 'bytes 0-499/2535'254 }, status: 206 });255 },256257 'test #sendfile() Range invalid syntax': function(assert){258 var app = express.createServer();259 260 app.set('stream threshold', 1024);261 262 app.get('/*', function(req, res, next){263 var file = req.params[0],264 path = __dirname + '/fixtures/' + file;265 res.sendfile(path);266 });267268 assert.response(app,269 { url: '/large.json', headers: { 'Range': 'basdytes=asdf' }},270 { headers: {271 'Content-Type': 'application/json',272 'Content-Length': 2535273 }, status: 200 });274 },275 276 'test #sendfile() Range invalid range': function(assert){277 var app = express.createServer();278 279 app.set('stream threshold', 1024);280 281 app.get('/*', function(req, res, next){282 var file = req.params[0],283 path = __dirname + '/fixtures/' + file;284 res.sendfile(path);285 });286287 assert.response(app,288 { url: '/large.json', headers: { 'Range': 'bytes=500-10' }},289 { headers: {290 'Content-Type': 'application/json',291 'Content-Length': 2535292 }, status: 200 });293 },294 Fixed tests due to params.splat removalTj Holowaychukbefore adoptiondcf2791
lines 295–297why, one step back295 'test #download()': function(assert){296 var app = express.createServer();297 Route callback signature changedTj Holowaychukbefore adoption75661a2
line 298why, one step back298 app.get('/json', function(req, res, next){Added callback support to res.download()Tj Holowaychukbefore adoption6bfa759
lines 299–302why, one step back299 var filePath = __dirname + '/fixtures/user.json';300 res.download(filePath, 'account.json', function(err, path){301 assert.equal(filePath, path);302 });Fixed tests due to params.splat removalTj Holowaychukbefore adoptiondcf2791
lines 303–304why, one step back303 });304Fixed res.download() testTj Holowaychukbefore adoption7af7851
lines 305–306why, one step back305 app.get('/*', function(req, res, next){306 res.download(__dirname + '/fixtures/' + req.params[0]);Fixed tests due to params.splat removalTj Holowaychukbefore adoptiondcf2791
lines 307–308why, one step back307 });308 Merge branch 'download-example'Tj Holowaychukbefore adoptionf6ba793
lines 309–312why, one step back309 app.error(function(err, req, res){310 res.send(404);311 })312 Fixed tests due to params.splat removalTj Holowaychukbefore adoptiondcf2791
lines 313–323why, one step back313 assert.response(app,314 { url: '/user.json' },315 { body: '{"name":"tj"}', status: 200, headers: {316 'Content-Type': 'application/json',317 'Content-Disposition': 'attachment; filename="user.json"'318 }});319320 assert.response(app,321 { url: '/json' },322 { body: '{"name":"tj"}', status: 200, headers: {323 'Content-Type': 'application/json',Merge branch 'downloads'Tj Holowaychukbefore adoption949803d
line 324why, one step back324 'Content-Length': '13',Fixed tests due to params.splat removalTj Holowaychukbefore adoptiondcf2791
lines 325–326why, one step back325 'Content-Disposition': 'attachment; filename="account.json"'326 }});Merge branch 'download-example'Tj Holowaychukbefore adoptionf6ba793
lines 327–332why, one step back327 328 assert.response(app,329 { url: '/missing' },330 function(res){331 assert.equal(null, res.headers['content-disposition']);332 });Merge branch 'features/cookie'Tj Holowaychukbefore adoption6521d2b
lines 333–345why, one step back333 },334 335 'test #cookie()': function(assert){336 var app = express.createServer();337 338 app.get('/', function(req, res){339 res.cookie('rememberme', 'yes', { expires: new Date(1), httpOnly: true });340 res.redirect('/');341 });342 343 assert.response(app,344 { url: '/' },345 function(res){Fixed res.cookie() testTj Holowaychukbefore adoption6a68950
line 346why, one step back346 assert.equal(Merge branch 'features/cookie'Tj Holowaychukbefore adoption6521d2b
line 347why, one step back347 'rememberme=yes; expires=Thu, 01 Jan 1970 00:00:00 GMT; httpOnly',Fixed tests for node --version 0.2.1Tj Holowaychukbefore adoption87003c8
line 348why, one step back348 res.headers['set-cookie']);Merge branch 'features/cookie'Tj Holowaychukbefore adoption6521d2b
line 349why, one step back349 });Added test for res.clearCookie()Tj Holowaychukbefore adoptiona13df5b
lines 350–364why, one step back350 },351 352 'test #clearCookie()': function(assert){353 var app = express.createServer();354 355 app.get('/', function(req, res){356 res.clearCookie('rememberme');357 res.redirect('/');358 });359 360 assert.response(app,361 { url: '/' },362 function(res){363 assert.equal(364 'rememberme=; expires=Thu, 01 Jan 1970 00:00:00 GMT',Fixed tests for node --version 0.2.1Tj Holowaychukbefore adoption87003c8
line 365why, one step back365 res.headers['set-cookie']);Added test for res.clearCookie()Tj Holowaychukbefore adoptiona13df5b
line 366why, one step back366 });res.send() HEAD supportTj Holowaychukbefore adoptiona9819df
lines 367–378why, one step back367 },368 369 'test HEAD': function(assert){370 var app = express.createServer();371 372 app.get('/', function(req, res, next){373 res.send('Hello World');374 });375 376 assert.response(app,377 { url: '/', method: 'HEAD' },378 { body: '', headers: { 'Content-Length': 11 }});Fixed tests due to params.splat removalTj Holowaychukbefore adoptiondcf2791
lines 379–380why, one step back379 }380};