Skip to content
Myr

response.test.js

This file

Path

test/response.test.js

174 lines · 5.7 KiB · JavaScript

Last changed by

By default a Buffer passed through res.send() is application/octet-streamTj Holowaychukbefore adoption76052b4

History of this file

Take it

Added response helpersTj Holowaychukbefore adoption874975d

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

Added buffer support to res.send()Tj Holowaychukbefore adoption14280df

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

Added response helpersTj Holowaychukbefore adoption874975d

lines 8–17why, one step back
8
9module.exports = {10 '#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){

req.send() headers take precedenceTj Holowaychukbefore adoption0d4b57d

lines 18–19why, one step back
18 res.header('X-Foo', 'bar');
19 res.send({ foo: 'bar' }, { 'X-Foo': 'baz' }, 201);

Added response helpersTj Holowaychukbefore adoption874975d

line 20why, one step back
20 });

res.send() only setting Content-Type when not already presentTj Holowaychukbefore adoptionf0a0966

lines 21–22why, one step back
21
22 app.get('/text', function(req, res){

Added res.header()Tj Holowaychukbefore adoption838c470

line 23why, one step back
23 res.header('X-Foo', 'bar');

res.send() only setting Content-Type when not already presentTj Holowaychukbefore adoptionf0a0966

lines 24–26why, one step back
24 res.contentType('.txt');
25 res.send('wahoo');26 });

Allowing status as first or second arg to res.send()Tj Holowaychukbefore adoption395d14a

lines 27–32why, one step back
27
28 app.get('/status', function(req, res){29 res.send(404);30 });31 32 app.get('/error', function(req, res){

Added res.send() Content-Type precedence testTj Holowaychukbefore adoptionc8baa8f

line 33why, one step back
33 res.send('Oh shit!', { 'Content-Type': 'text/plain' }, 500);

Allowing status as first or second arg to res.send()Tj Holowaychukbefore adoption395d14a

line 34why, one step back
34 });

Added buffer support to res.send()Tj Holowaychukbefore adoption14280df

lines 35–38why, one step back
35
36 app.get('/buffer', function(req, res){37 res.send(new Buffer('wahoo!'));38 });

Added response helpersTj Holowaychukbefore adoption874975d

lines 39–41why, one step back
39
40 assert.response(app,41 { url: '/html' },

res.send() only setting Content-Type when not already presentTj Holowaychukbefore adoptionf0a0966

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

Added response helpersTj Holowaychukbefore adoption874975d

lines 43–44why, one step back
43 assert.response(app,
44 { url: '/json' },

req.send() headers take precedenceTj Holowaychukbefore adoption0d4b57d

line 45why, one step back
45 { body: '{"foo":"bar"}', status: 201, headers: { 'Content-Type': 'application/json', 'X-Foo': 'baz' }});

res.send() only setting Content-Type when not already presentTj Holowaychukbefore adoptionf0a0966

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

Added res.header()Tj Holowaychukbefore adoption838c470

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

Allowing status as first or second arg to res.send()Tj Holowaychukbefore adoption395d14a

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

Added res.send() Content-Type precedence testTj Holowaychukbefore adoptionc8baa8f

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

Added buffer support to res.send()Tj Holowaychukbefore adoption14280df

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

By default a Buffer passed through res.send() is application/octet-streamTj Holowaychukbefore adoption76052b4

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

Added res.contentType() testsTj Holowaychukbefore adoptionac17a6a

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

Added res.attachment()Tj Holowaychukbefore adoption8fdf111

lines 72–92why, one step back
72 },
73 74 '#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 });8182 app.get('/*', function(req, res, params){83 res.attachment(params.splat[0]);84 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' }});

Merge branch 'redirect' into connectTj Holowaychukbefore adoption87cfca7

lines 93–100why, one step back
93 },
94 95 '#redirect()': function(assert){96 var app = express.createServer(),97 app2 = express.createServer();98 99 app2.set('home', '/blog');100

Added app.redirect(key, url)Tj Holowaychukbefore adoptionb75246e

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

Merge branch 'redirect' into connectTj Holowaychukbefore adoption87cfca7

line 102why, one step back
102

Added dynamic redirect mappingTj Holowaychukbefore adoption0bb4815

lines 103–108why, one step back
103 app2.redirect('blog', function(req, res, params){
104 return params.id105 ? '/user/' + params.id + '/blog'106 : null;107 });108

Merge branch 'redirect' into connectTj Holowaychukbefore adoption87cfca7

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

Added dynamic redirect mappingTj Holowaychukbefore adoption0bb4815

lines 136–139why, one step back
136
137 app2.get('/user/:id', function(req, res){138 res.redirect('blog');139 });

Merge branch 'redirect' into connectTj Holowaychukbefore adoption87cfca7

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

Added dynamic redirect mappingTj Holowaychukbefore adoption0bb4815

lines 169–171why, one step back
169 assert.response(app2,
170 { url: '/user/12' },171 { body: '', headers: { Location: '/user/12/blog' }});

Merge branch 'redirect' into connectTj Holowaychukbefore adoption87cfca7

line 172why, one step back
172

Added response helpersTj Holowaychukbefore adoption874975d

lines 173–174why, one step back
173 }
174};