response.test.js
This file
Path
test/response.test.js
221 lines · 7.4 KiB · JavaScript
Last changed by
Route callback signature changedTj Holowaychukbefore adoption75661a2
History of this fileTake it
The walk stopped after 200 commits: lines older than 51611ec point at it.
Added test for multiple app.error() callsTj Holowaychukbefore adoption51611ec
lines 1–81why, 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 });3940 assert.response(app,41 { url: '/html' },42 { body: '<p>test</p>', headers: { 'Content-Language': 'en', 'Content-Type': 'text/html; charset=utf-8' }});43 assert.response(app,44 { url: '/json' },45 { body: '{"foo":"bar"}', status: 201, headers: { 'Content-Type': 'application/json', 'X-Foo': 'baz' }});46 assert.response(app,47 { url: '/text' },48 { body: 'wahoo', headers: { 'Content-Type': 'text/plain; charset=utf-8', 'X-Foo': 'bar' }});49 assert.response(app,50 { url: '/status' },51 { body: 'Not Found', status: 404, headers: { 'Content-Type': 'text/plain; charset=utf-8' }});52 assert.response(app,53 { url: '/error' },54 { body: 'Oh shit!', status: 500, headers: { 'Content-Type': 'text/plain' }});55 assert.response(app,56 { url: '/buffer' },57 { body: 'wahoo!', headers: { 'Content-Type': 'application/octet-stream' }});58 },59 60 'test #contentType': function(assert){61 var app = express.createServer();62 63 app.get('/html', function(req, res){64 res.contentType('index.html');65 res.writeHead(200, res.headers);66 res.end('<p>yay</p>');67 });68 69 assert.response(app,70 { url: '/html' },71 { body: '<p>yay</p>', headers: { 'Content-Type': 'text/html; charset=utf-8' }});72 },73 74 'test #attachment': function(assert){75 var app = express.createServer();76 77 app.get('/style.css', function(req, res){78 res.attachment();79 res.send('some stylezzz');80 });81Route callback signature changedTj Holowaychukbefore adoption75661a2
lines 82–83why, one step back82 app.get('/*', function(req, res){83 res.attachment(req.params[0]);Added test for multiple app.error() callsTj Holowaychukbefore adoption51611ec
lines 84–102why, one step back84 res.send('whatever');85 });86 87 assert.response(app,88 { url: '/javascripts/jquery.js' },89 { body: 'whatever', headers: { 'Content-Disposition': 'attachment; filename="jquery.js"' }});90 assert.response(app,91 { url: '/style.css' },92 { body: 'some stylezzz', headers: { 'Content-Disposition': 'attachment' }});93 },94 95 'test #redirect()': function(assert){96 var app = express.createServer(),97 app2 = express.createServer();98 99 app2.set('home', '/blog');100101 app2.redirect('google', 'http://google.com');102Route callback signature changedTj Holowaychukbefore adoption75661a2
lines 103–105why, one step back103 app2.redirect('blog', function(req, res){104 return req.params.id105 ? '/user/' + req.params.id + '/blog'Added test for multiple app.error() callsTj Holowaychukbefore adoption51611ec
lines 106–176why, one step back106 : null;107 });108 109 app.get('/', function(req, res){110 res.redirect('http://google.com', 301);111 });112 113 app.get('/back', function(req, res){114 res.redirect('back');115 });116 117 app.get('/home', function(req, res){118 res.redirect('home');119 });120 121 app2.get('/', function(req, res){122 res.redirect('http://google.com', 301);123 });124 125 app2.get('/back', function(req, res){126 res.redirect('back');127 });128 129 app2.get('/home', function(req, res){130 res.redirect('home');131 });132 133 app2.get('/google', function(req, res){134 res.redirect('google');135 });136137 app2.get('/user/:id', function(req, res){138 res.redirect('blog');139 });140 141 assert.response(app,142 { url: '/' },143 { body: '', status: 301, headers: { Location: 'http://google.com' }});144 assert.response(app,145 { url: '/back' },146 { body: '', status: 302, headers: { Location: '/' }});147 assert.response(app,148 { url: '/back', headers: { Referer: '/foo' }},149 { body: '', status: 302, headers: { Location: '/foo' }});150 assert.response(app,151 { url: '/back', headers: { Referrer: '/foo' }},152 { body: '', status: 302, headers: { Location: '/foo' }});153 assert.response(app,154 { url: '/home' },155 { body: '', status: 302, headers: { Location: '/' }});156157 assert.response(app2,158 { url: '/' },159 { body: '', status: 301, headers: { Location: 'http://google.com' }});160 assert.response(app2,161 { url: '/back' },162 { body: '', status: 302, headers: { Location: '/blog' }});163 assert.response(app2,164 { url: '/home' },165 { body: '', status: 302, headers: { Location: '/blog' }});166 assert.response(app2,167 { url: '/google' },168 { body: '', headers: { Location: 'http://google.com' }});169 assert.response(app2,170 { url: '/user/12' },171 { body: '', headers: { Location: '/user/12/blog' }});172 },173 174 'test #sendfile()': function(assert){175 var app = express.createServer();176 Route callback signature changedTj Holowaychukbefore adoption75661a2
lines 177–178why, one step back177 app.get('/*', function(req, res){178 var file = req.params[0];Added test for multiple app.error() callsTj Holowaychukbefore adoption51611ec
lines 179–198why, one step back179 res.sendfile(__dirname + '/fixtures/' + file);180 });181 182 assert.response(app,183 { url: '/user.json' },184 { body: '{"name":"tj"}', status: 200, headers: { 'Content-Type': 'application/json' }});185 assert.response(app,186 { url: '/hello.haml' },187 { body: '%p Hello World', status: 200, headers: { 'Content-Type': 'application/octet-stream' }});188 assert.response(app,189 { url: '/doesNotExist' },190 { body: 'Not Found', status: 404 });191 assert.response(app,192 { url: '/partials' },193 { body: 'Internal Server Error', status: 500 });194 },195 196 'test #download()': function(assert){197 var app = express.createServer();198 Route callback signature changedTj Holowaychukbefore adoption75661a2
line 199why, one step back199 app.get('/json', function(req, res, next){Added test for multiple app.error() callsTj Holowaychukbefore adoption51611ec
lines 200–202why, one step back200 res.download(__dirname + '/fixtures/user.json', 'account.json');201 });202Route callback signature changedTj Holowaychukbefore adoption75661a2
lines 203–204why, one step back203 app.get('/:file', function(req, res, next){204 res.download(__dirname + '/fixtures/' + req.params.file);Added test for multiple app.error() callsTj Holowaychukbefore adoption51611ec
lines 205–221why, one step back205 });206 207 assert.response(app,208 { url: '/user.json' },209 { body: '{"name":"tj"}', status: 200, headers: {210 'Content-Type': 'application/json',211 'Content-Disposition': 'attachment; filename="user.json"'212 }});213214 assert.response(app,215 { url: '/json' },216 { body: '{"name":"tj"}', status: 200, headers: {217 'Content-Type': 'application/json',218 'Content-Disposition': 'attachment; filename="account.json"'219 }});220 }221};