Skip to content
Myr

Fix handling of undefined when "json escape" is enabled

Before adoption: this commit came in with the history the project adopted, so no landing records it.

closes #4744

authored by drewm · committed by Douglas Christopher Wilson · parent 9dd0e7a · Browse files · 4 files+32 added−1 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

4 files+32 added−1 removed

  • tests:+30 added−0 removed
  • other:+2 added−1 removed

4 files+32 added−1 removed

modified:lib/response.js+1 added-1 removed
1125 unmodified lines
1126 var json = replacer || spaces1127 ? JSON.stringify(value, replacer, spaces)1128 : JSON.stringify(value);11291130 if (escape) {1130 if (escape && typeof json === 'string') {1131 json = json.replace(/[<>&]/g, function (c) {1132 switch (c.charCodeAt(0)) {1133 case 0x3c:1134 return '\\u003c'
13 unmodified lines
modified:test/res.json.js+15 added
120 unmodified lines
121 .get('/')122 .expect('Content-Type', 'application/json; charset=utf-8')123 .expect(200, '{"\\u0026":"\\u003cscript\\u003e"}', done)124 })125126 it('should not break undefined escape', function (done) {127 var app = express()128129 app.enable('json escape')130131 app.use(function (req, res) {132 res.json(undefined)133 })134135 request(app)136 .get('/')137 .expect('Content-Type', 'application/json; charset=utf-8')138 .expect(200, '', done)139 })140 })141142 describe('"json replacer" setting', function(){143 it('should be passed to JSON.stringify()', function(done){
85 unmodified lines
modified:test/res.jsonp.js+15 added
264 unmodified lines
265 .get('/?callback=foo')266 .expect('Content-Type', 'text/javascript; charset=utf-8')267 .expect(200, /foo\({"\\u0026":"\\u2028\\u003cscript\\u003e\\u2029"}\)/, done)268 })269270 it('should not break undefined escape', function (done) {271 var app = express()272273 app.enable('json escape')274275 app.use(function (req, res) {276 res.jsonp(undefined)277 })278279 request(app)280 .get('/?callback=cb')281 .expect('Content-Type', 'text/javascript; charset=utf-8')282 .expect(200, /cb\(\)/, done)283 })284 })285286 describe('"json replacer" setting', function(){287 it('should be passed to JSON.stringify()', function(done){
99 unmodified lines
modified:History.md+1 added
1unreleased2==========34 * Fix handling of `undefined` in `res.jsonp`5 * Fix handling of `undefined` when `"json escape"` is enabled6 * Fix incorrect middleware execution with unanchored `RegExp`s7 * Fix `res.jsonp(obj, status)` deprecation message8 * Fix typo in `res.is` JSDoc9 * deps: cookie@0.4.1
3481 unmodified lines