Skip to content
Myr

JSON and JSONP default to UTF-8 in the same way as HTML. Introduces app.set('charset') to set charset default at the application level. Closes #632.

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

authored by Daniel Shaw · committed by Tj Holowaychuk · parent ccc39e5 · Browse files · 2 files+9 added−7 removed
This commit

State

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

Trailers

  • Signed-off-by: Tj Holowaychuk <tj@vision-media.ca>

Diff stats

2 files+9 added−7 removed

  • tests:+6 added−6 removed
  • other:+3 added−1 removed

2 files+9 added−7 removed

modified:lib/response.js+3 added-1 removed
61 unmodified lines
62 body = http.STATUS_CODES[status = body];63 break;64 case 'string':65 if (!this.header('Content-Type')) {66 this.charset = this.charset || 'utf-8';66 this.charset = this.charset || this.app.set('charset') || 'utf-8';67 this.contentType('.html');68 }69 break;70 case 'boolean':
3 unmodified lines
74 this.contentType('.bin');75 }76 } else {77 if (!this.header('Content-Type')) {78 this.charset = this.charset || this.app.set('charset') || 'utf-8';79 this.contentType('.json');80 }81 body = JSON.stringify(body);82 if (this.req.query.callback && this.app.set('jsonp callback')) {83 this.charset = this.charset || this.app.set('charset') || 'utf-8';84 this.header('Content-Type', 'text/javascript');85 body = this.req.query.callback.replace(/[^\w$.]/g, '') + '(' + body + ');';86 }87 }
335 unmodified lines
modified:test/response.test.js+6 added-6 removed
59 unmodified lines
6061 assert.response(app,62 { url: '/bool' },63 { body: 'true'64 , headers: { 'Content-Type': 'application/json' }});64 , headers: { 'Content-Type': 'application/json; charset=utf-8' }});6566 assert.response(app,67 { url: '/html' },68 { body: '<p>test</p>'
6 unmodified lines
75 { url: '/json' },76 { body: '{"foo":"bar"}'77 , status: 20178 , headers: {79 'Content-Type': 'application/json'79 'Content-Type': 'application/json; charset=utf-8'80 , 'X-Foo': 'baz'81 }});82 83 assert.response(app,84 { url: '/jsonp?callback=test' },85 { body: 'test({"foo":"bar"});'86 , status: 20187 , headers: {88 'Content-Type': 'text/javascript'88 'Content-Type': 'text/javascript; charset=utf-8'89 , 'X-Foo': 'baz'90 }});91 92 assert.response(app,93 { url: '/jsonp?callback=baz' },94 { body: 'baz({"foo":"bar"});'95 , status: 201, headers: {96 'Content-Type': 'text/javascript'96 'Content-Type': 'text/javascript; charset=utf-8'97 , 'X-Foo': 'baz'98 }});99 100 assert.response(app,101 { url: '/jsonp?callback=invalid()[]' },102 { body: 'invalid({"foo":"bar"});'103 , status: 201104 , headers: {105 'Content-Type': 'text/javascript'105 'Content-Type': 'text/javascript; charset=utf-8'106 , 'X-Foo': 'baz'107 }});108 109 assert.response(app,110 { url: '/json?callback=test' },111 { body: '{"foo":"bar"}'112 , status: 201113 , headers: {114 'Content-Type': 'application/json'114 'Content-Type': 'application/json; charset=utf-8'115 , 'X-Foo': 'baz'116 }});117 118 assert.response(app,
491 unmodified lines