res.render.js
This file
Path
test/res.render.js
367 lines · 8.9 KiB · JavaScript
Last changed by
Merge tag '4.17.3'Douglas Christopher Wilsonbefore adoption318fd4b
History of this fileTake it
The walk stopped after 200 commits: lines older than 454c4b2 point at it.
Merge tag '4.17.3'Douglas Christopher Wilsonbefore adoption318fd4b
line 1why, one step back1'use strict'Merge tag '3.15.2'Douglas Christopher Wilsonbefore adoption454c4b2
line 2why, one step back2Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
line 3why, one step back3var express = require('..');Merge tag '4.15.2'Douglas Christopher Wilsonbefore adoptione5dbb0c
line 4why, one step back4var path = require('path')Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
lines 5–6why, one step back5var request = require('supertest');6var tmpl = require('./support/tmpl');Merge tag '3.15.2'Douglas Christopher Wilsonbefore adoption454c4b2
lines 7–10why, one step back78describe('res', function(){9 describe('.render(name)', function(){10 it('should support absolute paths', function(done){Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
line 11why, one step back11 var app = createApp();Merge tag '3.15.2'Douglas Christopher Wilsonbefore adoption454c4b2
lines 12–15why, one step back1213 app.locals.user = { name: 'tobi' };1415 app.use(function(req, res){Merge tag '4.15.2'Douglas Christopher Wilsonbefore adoptione5dbb0c
line 16why, one step back16 res.render(path.join(__dirname, 'fixtures', 'user.tmpl'))Merge tag '3.15.2'Douglas Christopher Wilsonbefore adoption454c4b2
lines 17–24why, one step back17 });1819 request(app)20 .get('/')21 .expect('<p>tobi</p>', done);22 })2324 it('should support absolute paths with "view engine"', function(done){Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
line 25why, one step back25 var app = createApp();Merge tag '3.15.2'Douglas Christopher Wilsonbefore adoption454c4b2
lines 26–27why, one step back2627 app.locals.user = { name: 'tobi' };Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
line 28why, one step back28 app.set('view engine', 'tmpl');Merge tag '3.15.2'Douglas Christopher Wilsonbefore adoption454c4b2
lines 29–30why, one step back2930 app.use(function(req, res){Merge tag '4.15.2'Douglas Christopher Wilsonbefore adoptione5dbb0c
line 31why, one step back31 res.render(path.join(__dirname, 'fixtures', 'user'))Merge tag '3.15.2'Douglas Christopher Wilsonbefore adoption454c4b2
lines 32–38why, one step back32 });3334 request(app)35 .get('/')36 .expect('<p>tobi</p>', done);37 })38Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe
lines 39–52why, one step back39 it('should error without "view engine" set and file extension to a non-engine module', function (done) {40 var app = createApp()4142 app.locals.user = { name: 'tobi' }4344 app.use(function (req, res) {45 res.render(path.join(__dirname, 'fixtures', 'broken.send'))46 })4748 request(app)49 .get('/')50 .expect(500, /does not provide a view engine/, done)51 })52Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 53–58why, one step back53 it('should error without "view engine" set and no file extension', function (done) {54 var app = createApp();5556 app.locals.user = { name: 'tobi' };5758 app.use(function(req, res){Merge tag '4.15.2'Douglas Christopher Wilsonbefore adoptione5dbb0c
line 59why, one step back59 res.render(path.join(__dirname, 'fixtures', 'user'))Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 60–66why, one step back60 });6162 request(app)63 .get('/')64 .expect(500, /No default engine was specified/, done);65 })66Merge tag '3.15.2'Douglas Christopher Wilsonbefore adoption454c4b2
line 67why, one step back67 it('should expose app.locals', function(done){Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
line 68why, one step back68 var app = createApp();Merge tag '3.15.2'Douglas Christopher Wilsonbefore adoption454c4b2
line 69why, one step back69Merge tag '4.15.2'Douglas Christopher Wilsonbefore adoptione5dbb0c
line 70why, one step back70 app.set('views', path.join(__dirname, 'fixtures'))Merge tag '3.15.2'Douglas Christopher Wilsonbefore adoption454c4b2
lines 71–73why, one step back71 app.locals.user = { name: 'tobi' };7273 app.use(function(req, res){Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
line 74why, one step back74 res.render('user.tmpl');Merge tag '3.15.2'Douglas Christopher Wilsonbefore adoption454c4b2
lines 75–82why, one step back75 });7677 request(app)78 .get('/')79 .expect('<p>tobi</p>', done);80 })8182 it('should expose app.locals with `name` property', function(done){Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
line 83why, one step back83 var app = createApp();Merge tag '3.15.2'Douglas Christopher Wilsonbefore adoption454c4b2
line 84why, one step back84Merge tag '4.15.2'Douglas Christopher Wilsonbefore adoptione5dbb0c
line 85why, one step back85 app.set('views', path.join(__dirname, 'fixtures'))Merge tag '3.15.2'Douglas Christopher Wilsonbefore adoption454c4b2
lines 86–88why, one step back86 app.locals.name = 'tobi';8788 app.use(function(req, res){Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
line 89why, one step back89 res.render('name.tmpl');Merge tag '3.15.2'Douglas Christopher Wilsonbefore adoption454c4b2
lines 90–97why, one step back90 });9192 request(app)93 .get('/')94 .expect('<p>tobi</p>', done);95 })9697 it('should support index.<engine>', function(done){Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
line 98why, one step back98 var app = createApp();Merge tag '3.15.2'Douglas Christopher Wilsonbefore adoption454c4b2
line 99why, one step back99Merge tag '4.15.2'Douglas Christopher Wilsonbefore adoptione5dbb0c
line 100why, one step back100 app.set('views', path.join(__dirname, 'fixtures'))Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
line 101why, one step back101 app.set('view engine', 'tmpl');Merge tag '3.15.2'Douglas Christopher Wilsonbefore adoption454c4b2
lines 102–113why, one step back102103 app.use(function(req, res){104 res.render('blog/post');105 });106107 request(app)108 .get('/')109 .expect('<h1>blog post</h1>', done);110 })111112 describe('when an error occurs', function(){113 it('should next(err)', function(done){Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
line 114why, one step back114 var app = createApp();Merge tag '3.15.2'Douglas Christopher Wilsonbefore adoption454c4b2
line 115why, one step back115Merge tag '4.15.2'Douglas Christopher Wilsonbefore adoptione5dbb0c
line 116why, one step back116 app.set('views', path.join(__dirname, 'fixtures'))Merge tag '3.15.2'Douglas Christopher Wilsonbefore adoption454c4b2
lines 117–118why, one step back117118 app.use(function(req, res){Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
line 119why, one step back119 res.render('user.tmpl');Merge tag '3.15.2'Douglas Christopher Wilsonbefore adoption454c4b2
lines 120–122why, one step back120 });121122 app.use(function(err, req, res, next){Merge tag '4.15.5'Douglas Christopher Wilsonbefore adoption19c8d64
line 123why, one step back123 res.status(500).send('got error: ' + err.name)Merge tag '3.15.2'Douglas Christopher Wilsonbefore adoption454c4b2
lines 124–127why, one step back124 });125126 request(app)127 .get('/')Merge tag '4.15.5'Douglas Christopher Wilsonbefore adoption19c8d64
line 128why, one step back128 .expect(500, 'got error: RenderError', done)Merge tag '3.15.2'Douglas Christopher Wilsonbefore adoption454c4b2
lines 129–133why, one step back129 })130 })131132 describe('when "view engine" is given', function(){133 it('should render the template', function(done){Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
line 134why, one step back134 var app = createApp();Merge tag '3.15.2'Douglas Christopher Wilsonbefore adoption454c4b2
line 135why, one step back135Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
line 136why, one step back136 app.set('view engine', 'tmpl');Merge tag '4.15.2'Douglas Christopher Wilsonbefore adoptione5dbb0c
line 137why, one step back137 app.set('views', path.join(__dirname, 'fixtures'))Merge tag '3.15.2'Douglas Christopher Wilsonbefore adoption454c4b2
lines 138–147why, one step back138139 app.use(function(req, res){140 res.render('email');141 });142143 request(app)144 .get('/')145 .expect('<p>This is an email</p>', done);146 })147 })Add support for app.set('views', array)Douglas Christopher Wilsonbefore adoption6614352
lines 148–150why, one step back148149 describe('when "views" is given', function(){150 it('should lookup the file in the path', function(done){Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
line 151why, one step back151 var app = createApp();Add support for app.set('views', array)Douglas Christopher Wilsonbefore adoption6614352
line 152why, one step back152Merge tag '4.15.2'Douglas Christopher Wilsonbefore adoptione5dbb0c
line 153why, one step back153 app.set('views', path.join(__dirname, 'fixtures', 'default_layout'))Add support for app.set('views', array)Douglas Christopher Wilsonbefore adoption6614352
lines 154–155why, one step back154155 app.use(function(req, res){Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
line 156why, one step back156 res.render('user.tmpl', { user: { name: 'tobi' } });Add support for app.set('views', array)Douglas Christopher Wilsonbefore adoption6614352
lines 157–165why, one step back157 });158159 request(app)160 .get('/')161 .expect('<p>tobi</p>', done);162 })163164 describe('when array of paths', function(){165 it('should lookup the file in the path', function(done){Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
line 166why, one step back166 var app = createApp();Merge tag '4.15.2'Douglas Christopher Wilsonbefore adoptione5dbb0c
lines 167–170why, one step back167 var views = [168 path.join(__dirname, 'fixtures', 'local_layout'),169 path.join(__dirname, 'fixtures', 'default_layout')170 ]Add support for app.set('views', array)Douglas Christopher Wilsonbefore adoption6614352
lines 171–174why, one step back171172 app.set('views', views);173174 app.use(function(req, res){Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
line 175why, one step back175 res.render('user.tmpl', { user: { name: 'tobi' } });Add support for app.set('views', array)Douglas Christopher Wilsonbefore adoption6614352
lines 176–183why, one step back176 });177178 request(app)179 .get('/')180 .expect('<span>tobi</span>', done);181 })182183 it('should lookup in later paths until found', function(done){Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
line 184why, one step back184 var app = createApp();Merge tag '4.15.2'Douglas Christopher Wilsonbefore adoptione5dbb0c
lines 185–188why, one step back185 var views = [186 path.join(__dirname, 'fixtures', 'local_layout'),187 path.join(__dirname, 'fixtures', 'default_layout')188 ]Add support for app.set('views', array)Douglas Christopher Wilsonbefore adoption6614352
lines 189–192why, one step back189190 app.set('views', views);191192 app.use(function(req, res){Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
line 193why, one step back193 res.render('name.tmpl', { name: 'tobi' });Add support for app.set('views', array)Douglas Christopher Wilsonbefore adoption6614352
lines 194–201why, one step back194 });195196 request(app)197 .get('/')198 .expect('<p>tobi</p>', done);199 })200 })201 })Merge tag '3.15.2'Douglas Christopher Wilsonbefore adoption454c4b2
lines 202–205why, one step back202 })203204 describe('.render(name, option)', function(){205 it('should render the template', function(done){Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
line 206why, one step back206 var app = createApp();Merge tag '3.15.2'Douglas Christopher Wilsonbefore adoption454c4b2
line 207why, one step back207Merge tag '4.15.2'Douglas Christopher Wilsonbefore adoptione5dbb0c
line 208why, one step back208 app.set('views', path.join(__dirname, 'fixtures'))Merge tag '3.15.2'Douglas Christopher Wilsonbefore adoption454c4b2
lines 209–212why, one step back209210 var user = { name: 'tobi' };211212 app.use(function(req, res){Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
line 213why, one step back213 res.render('user.tmpl', { user: user });Merge tag '3.15.2'Douglas Christopher Wilsonbefore adoption454c4b2
lines 214–221why, one step back214 });215216 request(app)217 .get('/')218 .expect('<p>tobi</p>', done);219 })220221 it('should expose app.locals', function(done){Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
line 222why, one step back222 var app = createApp();Merge tag '3.15.2'Douglas Christopher Wilsonbefore adoption454c4b2
line 223why, one step back223Merge tag '4.15.2'Douglas Christopher Wilsonbefore adoptione5dbb0c
line 224why, one step back224 app.set('views', path.join(__dirname, 'fixtures'))Merge tag '3.15.2'Douglas Christopher Wilsonbefore adoption454c4b2
lines 225–227why, one step back225 app.locals.user = { name: 'tobi' };226227 app.use(function(req, res){Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
line 228why, one step back228 res.render('user.tmpl');Merge tag '3.15.2'Douglas Christopher Wilsonbefore adoption454c4b2
lines 229–236why, one step back229 });230231 request(app)232 .get('/')233 .expect('<p>tobi</p>', done);234 })235236 it('should expose res.locals', function(done){Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
line 237why, one step back237 var app = createApp();Merge tag '3.15.2'Douglas Christopher Wilsonbefore adoption454c4b2
line 238why, one step back238Merge tag '4.15.2'Douglas Christopher Wilsonbefore adoptione5dbb0c
line 239why, one step back239 app.set('views', path.join(__dirname, 'fixtures'))Merge tag '3.15.2'Douglas Christopher Wilsonbefore adoption454c4b2
lines 240–242why, one step back240241 app.use(function(req, res){242 res.locals.user = { name: 'tobi' };Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
line 243why, one step back243 res.render('user.tmpl');Merge tag '3.15.2'Douglas Christopher Wilsonbefore adoption454c4b2
lines 244–251why, one step back244 });245246 request(app)247 .get('/')248 .expect('<p>tobi</p>', done);249 })250251 it('should give precedence to res.locals over app.locals', function(done){Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
line 252why, one step back252 var app = createApp();Merge tag '3.15.2'Douglas Christopher Wilsonbefore adoption454c4b2
line 253why, one step back253Merge tag '4.15.2'Douglas Christopher Wilsonbefore adoptione5dbb0c
line 254why, one step back254 app.set('views', path.join(__dirname, 'fixtures'))Merge tag '3.15.2'Douglas Christopher Wilsonbefore adoption454c4b2
lines 255–258why, one step back255 app.locals.user = { name: 'tobi' };256257 app.use(function(req, res){258 res.locals.user = { name: 'jane' };Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
line 259why, one step back259 res.render('user.tmpl', {});Merge tag '3.15.2'Douglas Christopher Wilsonbefore adoption454c4b2
lines 260–267why, one step back260 });261262 request(app)263 .get('/')264 .expect('<p>jane</p>', done);265 })266267 it('should give precedence to res.render() locals over res.locals', function(done){Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
line 268why, one step back268 var app = createApp();Merge tag '3.15.2'Douglas Christopher Wilsonbefore adoption454c4b2
line 269why, one step back269Merge tag '4.15.2'Douglas Christopher Wilsonbefore adoptione5dbb0c
line 270why, one step back270 app.set('views', path.join(__dirname, 'fixtures'))Merge tag '3.15.2'Douglas Christopher Wilsonbefore adoption454c4b2
lines 271–274why, one step back271 var jane = { name: 'jane' };272273 app.use(function(req, res){274 res.locals.user = { name: 'tobi' };Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
line 275why, one step back275 res.render('user.tmpl', { user: jane });Merge tag '3.15.2'Douglas Christopher Wilsonbefore adoption454c4b2
lines 276–283why, one step back276 });277278 request(app)279 .get('/')280 .expect('<p>jane</p>', done);281 })282283 it('should give precedence to res.render() locals over app.locals', function(done){Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
line 284why, one step back284 var app = createApp();Merge tag '3.15.2'Douglas Christopher Wilsonbefore adoption454c4b2
line 285why, one step back285Merge tag '4.15.2'Douglas Christopher Wilsonbefore adoptione5dbb0c
line 286why, one step back286 app.set('views', path.join(__dirname, 'fixtures'))Merge tag '3.15.2'Douglas Christopher Wilsonbefore adoption454c4b2
lines 287–290why, one step back287 app.locals.user = { name: 'tobi' };288 var jane = { name: 'jane' };289290 app.use(function(req, res){Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
line 291why, one step back291 res.render('user.tmpl', { user: jane });Merge tag '3.15.2'Douglas Christopher Wilsonbefore adoption454c4b2
lines 292–301why, one step back292 });293294 request(app)295 .get('/')296 .expect('<p>jane</p>', done);297 })298 })299300 describe('.render(name, options, fn)', function(){301 it('should pass the resulting string', function(done){Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
line 302why, one step back302 var app = createApp();Merge tag '3.15.2'Douglas Christopher Wilsonbefore adoption454c4b2
line 303why, one step back303Merge tag '4.15.2'Douglas Christopher Wilsonbefore adoptione5dbb0c
line 304why, one step back304 app.set('views', path.join(__dirname, 'fixtures'))Merge tag '3.15.2'Douglas Christopher Wilsonbefore adoption454c4b2
lines 305–307why, one step back305306 app.use(function(req, res){307 var tobi = { name: 'tobi' };Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
line 308why, one step back308 res.render('user.tmpl', { user: tobi }, function (err, html) {Merge tag '3.15.2'Douglas Christopher Wilsonbefore adoption454c4b2
lines 309–321why, one step back309 html = html.replace('tobi', 'loki');310 res.end(html);311 });312 });313314 request(app)315 .get('/')316 .expect('<p>loki</p>', done);317 })318 })319320 describe('.render(name, fn)', function(){321 it('should pass the resulting string', function(done){Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
line 322why, one step back322 var app = createApp();Merge tag '3.15.2'Douglas Christopher Wilsonbefore adoption454c4b2
line 323why, one step back323Merge tag '4.15.2'Douglas Christopher Wilsonbefore adoptione5dbb0c
line 324why, one step back324 app.set('views', path.join(__dirname, 'fixtures'))Merge tag '3.15.2'Douglas Christopher Wilsonbefore adoption454c4b2
lines 325–327why, one step back325326 app.use(function(req, res){327 res.locals.user = { name: 'tobi' };Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
line 328why, one step back328 res.render('user.tmpl', function (err, html) {Merge tag '3.15.2'Douglas Christopher Wilsonbefore adoption454c4b2
lines 329–340why, one step back329 html = html.replace('tobi', 'loki');330 res.end(html);331 });332 });333334 request(app)335 .get('/')336 .expect('<p>loki</p>', done);337 })338339 describe('when an error occurs', function(){340 it('should pass it to the callback', function(done){Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
line 341why, one step back341 var app = createApp();Merge tag '3.15.2'Douglas Christopher Wilsonbefore adoption454c4b2
line 342why, one step back342Merge tag '4.15.2'Douglas Christopher Wilsonbefore adoptione5dbb0c
line 343why, one step back343 app.set('views', path.join(__dirname, 'fixtures'))Merge tag '3.15.2'Douglas Christopher Wilsonbefore adoption454c4b2
lines 344–345why, one step back344345 app.use(function(req, res){Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
line 346why, one step back346 res.render('user.tmpl', function (err) {Merge tag '4.15.5'Douglas Christopher Wilsonbefore adoption19c8d64
lines 347–349why, one step back347 if (err) {348 res.status(500).send('got error: ' + err.name)349 }Merge tag '3.15.2'Douglas Christopher Wilsonbefore adoption454c4b2
lines 350–354why, one step back350 });351 });352353 request(app)354 .get('/')Merge tag '4.15.5'Douglas Christopher Wilsonbefore adoption19c8d64
line 355why, one step back355 .expect(500, 'got error: RenderError', done)Merge tag '3.15.2'Douglas Christopher Wilsonbefore adoption454c4b2
lines 356–359why, one step back356 })357 })358 })359})Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
lines 360–367why, one step back360361function createApp() {362 var app = express();363364 app.engine('.tmpl', tmpl);365366 return app;367}