Skip to content
Myr

response.test.js

This file

Path

test/response.test.js

289 lines · 9.8 KiB · JavaScript

Last changed by

res.send() HEAD supportTj Holowaychukbefore adoptiona9819df

History of this file

Take it

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

Added partial magic var docs. Closes #342Tj Holowaychukbefore adoptionbe85701

lines 1–38why, one step back
1
2/**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 back
39
40 app.get('/noargs', function(req, res, next){41 res.send();42 });

Added partial magic var docs. Closes #342Tj Holowaychukbefore adoptionbe85701

lines 43–61why, one step back
43
44 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 back
62 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 });

Added partial magic var docs. Closes #342Tj Holowaychukbefore adoptionbe85701

lines 68–69why, one step back
68 },
69

Fixed test labelsTj Holowaychukbefore adoption2844b1a

line 70why, one step back
70 'test #contentType()': function(assert){

Added partial magic var docs. Closes #342Tj Holowaychukbefore adoptionbe85701

lines 71–83why, one step back
71 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 back
84 'test #attachment()': function(assert){

Added partial magic var docs. Closes #342Tj Holowaychukbefore adoptionbe85701

lines 85–91why, one step back
85 var app = express.createServer();
86 87 app.get('/style.css', function(req, res){88 res.attachment();89 res.send('some stylezzz');90 });91

Route callback signature changedTj Holowaychukbefore adoption75661a2

lines 92–93why, one step back
92 app.get('/*', function(req, res){
93 res.attachment(req.params[0]);

Added partial magic var docs. Closes #342Tj Holowaychukbefore adoptionbe85701

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

Route callback signature changedTj Holowaychukbefore adoption75661a2

lines 113–115why, one step back
113 app2.redirect('blog', function(req, res){
114 return req.params.id115 ? '/user/' + req.params.id + '/blog'

Added partial magic var docs. Closes #342Tj Holowaychukbefore adoptionbe85701

lines 116–147why, one step back
116 : 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 back
148 res.header('X-Foo', 'bar');

Added partial magic var docs. Closes #342Tj Holowaychukbefore adoptionbe85701

lines 149–153why, one step back
149 res.redirect('blog');
150 });151 152 assert.response(app,153 { url: '/' },

Merge branch 'redirect-bodies'Tj Holowaychukbefore adoption640274e

line 154why, one step back
154 { body: 'Redirecting to http://google.com', status: 301, headers: { Location: 'http://google.com' }});

Added partial magic var docs. Closes #342Tj Holowaychukbefore adoptionbe85701

lines 155–156why, one step back
155 assert.response(app,
156 { url: '/back' },

Merge branch 'redirect-bodies'Tj Holowaychukbefore adoption640274e

line 157why, one step back
157 { body: 'Redirecting to /', status: 302, headers: { Location: '/', 'Content-Type': 'text/plain' }});

Added partial magic var docs. Closes #342Tj Holowaychukbefore adoptionbe85701

lines 158–159why, one step back
158 assert.response(app,
159 { url: '/back', headers: { Referer: '/foo' }},

Merge branch 'redirect-bodies'Tj Holowaychukbefore adoption640274e

line 160why, one step back
160 { body: 'Redirecting to /foo', status: 302, headers: { Location: '/foo' }});

Added partial magic var docs. Closes #342Tj Holowaychukbefore adoptionbe85701

lines 161–162why, one step back
161 assert.response(app,
162 { url: '/back', headers: { Referrer: '/foo' }},

Merge branch 'redirect-bodies'Tj Holowaychukbefore adoption640274e

line 163why, one step back
163 { body: 'Redirecting to /foo', status: 302, headers: { Location: '/foo' }});

Added partial magic var docs. Closes #342Tj Holowaychukbefore adoptionbe85701

lines 164–165why, one step back
164 assert.response(app,
165 { url: '/home' },

Merge branch 'redirect-bodies'Tj Holowaychukbefore adoption640274e

line 166why, one step back
166 { body: 'Redirecting to /', status: 302, headers: { Location: '/' }});

Added partial magic var docs. Closes #342Tj Holowaychukbefore adoptionbe85701

lines 167–169why, one step back
167
168 assert.response(app2,169 { url: '/' },

Merge branch 'redirect-bodies'Tj Holowaychukbefore adoption640274e

line 170why, one step back
170 { body: 'Redirecting to http://google.com', status: 301, headers: { Location: 'http://google.com' }});

Added partial magic var docs. Closes #342Tj Holowaychukbefore adoptionbe85701

lines 171–172why, one step back
171 assert.response(app2,
172 { url: '/back' },

Merge branch 'redirect-bodies'Tj Holowaychukbefore adoption640274e

line 173why, one step back
173 { body: 'Redirecting to /blog', status: 302, headers: { Location: '/blog' }});

Added partial magic var docs. Closes #342Tj Holowaychukbefore adoptionbe85701

lines 174–175why, one step back
174 assert.response(app2,
175 { url: '/home' },

Merge branch 'redirect-bodies'Tj Holowaychukbefore adoption640274e

line 176why, one step back
176 { body: 'Redirecting to /blog', status: 302, headers: { Location: '/blog' }});

Added partial magic var docs. Closes #342Tj Holowaychukbefore adoptionbe85701

lines 177–178why, one step back
177 assert.response(app2,
178 { url: '/google' },

Merge branch 'redirect-bodies'Tj Holowaychukbefore adoption640274e

line 179why, one step back
179 { body: 'Redirecting to http://google.com', headers: { Location: 'http://google.com' }});

Added partial magic var docs. Closes #342Tj Holowaychukbefore adoptionbe85701

lines 180–181why, one step back
180 assert.response(app2,
181 { url: '/user/12' },

Merge branch 'redirect-bodies'Tj Holowaychukbefore adoption640274e

line 182why, one step back
182 { body: 'Redirecting to /user/12/blog', headers: { Location: '/user/12/blog', 'X-Foo': 'bar' }});

Added partial magic var docs. Closes #342Tj Holowaychukbefore adoptionbe85701

lines 183–186why, one step back
183 },
184 185 'test #sendfile()': function(assert){186 var app = express.createServer();

Fixed res.sendfile() error handling, defer via next()Tj Holowaychukbefore adoption8308e50

lines 187–188why, one step back
187
188 app.get('/*', function(req, res, next){

Added callback support for res.sendfile()Tj Holowaychukbefore adoption25909c0

lines 189–192why, one step back
189 var file = req.params[0],
190 filePath = __dirname + '/fixtures/' + file;191 res.sendfile(filePath, function(err, path){192 assert.equal(path, filePath);

Fixed res.sendfile() error handling, defer via next()Tj Holowaychukbefore adoption8308e50

line 193why, one step back
193 if (err) next(err);

Added callback support for res.sendfile()Tj Holowaychukbefore adoption25909c0

line 194why, one step back
194 });

Added partial magic var docs. Closes #342Tj Holowaychukbefore adoptionbe85701

lines 195–196why, one step back
195 });
196

Fixed res.sendfile() error handling, defer via next()Tj Holowaychukbefore adoption8308e50

lines 197–198why, one step back
197 app.use(express.errorHandler());
198

Added partial magic var docs. Closes #342Tj Holowaychukbefore adoptionbe85701

lines 199–206why, one step back
199 assert.response(app,
200 { url: '/user.json' },201 { body: '{"name":"tj"}', status: 200, headers: { 'Content-Type': 'application/json' }});202 assert.response(app,203 { url: '/hello.haml' },204 { body: '%p Hello World', status: 200, headers: { 'Content-Type': 'application/octet-stream' }});205 assert.response(app,206 { url: '/doesNotExist' },

Fixed res.sendfile() error handling, defer via next()Tj Holowaychukbefore adoption8308e50

line 207why, one step back
207 { body: 'Internal Server Error', status: 500 });

Added partial magic var docs. Closes #342Tj Holowaychukbefore adoptionbe85701

lines 208–215why, one step back
208 assert.response(app,
209 { url: '/partials' },210 { body: 'Internal Server Error', status: 500 });211 },212 213 'test #download()': function(assert){214 var app = express.createServer();215

Route callback signature changedTj Holowaychukbefore adoption75661a2

line 216why, one step back
216 app.get('/json', function(req, res, next){

Added callback support to res.download()Tj Holowaychukbefore adoption6bfa759

lines 217–220why, one step back
217 var filePath = __dirname + '/fixtures/user.json';
218 res.download(filePath, 'account.json', function(err, path){219 assert.equal(filePath, path);220 });

Added partial magic var docs. Closes #342Tj Holowaychukbefore adoptionbe85701

lines 221–222why, one step back
221 });
222

Fixed res.download() testTj Holowaychukbefore adoption7af7851

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

Added partial magic var docs. Closes #342Tj Holowaychukbefore adoptionbe85701

lines 225–239why, one step back
225 });
226 227 assert.response(app,228 { url: '/user.json' },229 { body: '{"name":"tj"}', status: 200, headers: {230 'Content-Type': 'application/json',231 'Content-Disposition': 'attachment; filename="user.json"'232 }});233234 assert.response(app,235 { url: '/json' },236 { body: '{"name":"tj"}', status: 200, headers: {237 'Content-Type': 'application/json',238 'Content-Disposition': 'attachment; filename="account.json"'239 }});

Merge branch 'features/cookie'Tj Holowaychukbefore adoption6521d2b

lines 240–252why, one step back
240 },
241 242 'test #cookie()': function(assert){243 var app = express.createServer();244 245 app.get('/', function(req, res){246 res.cookie('rememberme', 'yes', { expires: new Date(1), httpOnly: true });247 res.redirect('/');248 });249 250 assert.response(app,251 { url: '/' },252 function(res){

Uncommented cookie assertions previously failingTj Holowaychukbefore adoption27dba65

line 253why, one step back
253 assert.length(res.headers['set-cookie'], 1);

Fixed res.cookie() testTj Holowaychukbefore adoption6a68950

line 254why, one step back
254 assert.equal(

Merge branch 'features/cookie'Tj Holowaychukbefore adoption6521d2b

lines 255–257why, one step back
255 'rememberme=yes; expires=Thu, 01 Jan 1970 00:00:00 GMT; httpOnly',
256 res.headers['set-cookie'][0]);257 });

Added test for res.clearCookie()Tj Holowaychukbefore adoptiona13df5b

lines 258–270why, one step back
258 },
259 260 'test #clearCookie()': function(assert){261 var app = express.createServer();262 263 app.get('/', function(req, res){264 res.clearCookie('rememberme');265 res.redirect('/');266 });267 268 assert.response(app,269 { url: '/' },270 function(res){

Uncommented cookie assertions previously failingTj Holowaychukbefore adoption27dba65

line 271why, one step back
271 assert.length(res.headers['set-cookie'], 1);

Added test for res.clearCookie()Tj Holowaychukbefore adoptiona13df5b

lines 272–275why, one step back
272 assert.equal(
273 'rememberme=; expires=Thu, 01 Jan 1970 00:00:00 GMT',274 res.headers['set-cookie'][0]);275 });

res.send() HEAD supportTj Holowaychukbefore adoptiona9819df

lines 276–287why, one step back
276 },
277 278 'test HEAD': function(assert){279 var app = express.createServer();280 281 app.get('/', function(req, res, next){282 res.send('Hello World');283 });284 285 assert.response(app,286 { url: '/', method: 'HEAD' },287 { body: '', headers: { 'Content-Length': 11 }});

Added partial magic var docs. Closes #342Tj Holowaychukbefore adoptionbe85701

lines 288–289why, one step back
288 }
289};