Skip to content
Myr

res.json.js

This file

Path

test/res.json.js

186 lines · 4.7 KiB · JavaScript

Last changed by

fix(refactor): prefix built-in node module importsSzymon Łągiewkabefore adoption4111359

History of this file

Take it

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

fix(deps): cookie@0.7.0Josh Bukerbefore adoption2027b87

lines 1–4why, one step back
1'use strict'
23var express = require('../')4 , request = require('supertest')

fix(refactor): prefix built-in node module importsSzymon Łągiewkabefore adoption4111359

line 5why, one step back
5 , assert = require('node:assert');

fix(deps): cookie@0.7.0Josh Bukerbefore adoption2027b87

lines 6–186why, one step back
6
7describe('res', function(){8 describe('.json(object)', function(){9 it('should not support jsonp callbacks', function(done){10 var app = express();1112 app.use(function(req, res){13 res.json({ foo: 'bar' });14 });1516 request(app)17 .get('/?callback=foo')18 .expect('{"foo":"bar"}', done);19 })2021 it('should not override previous Content-Types', function(done){22 var app = express();2324 app.get('/', function(req, res){25 res.type('application/vnd.example+json');26 res.json({ hello: 'world' });27 });2829 request(app)30 .get('/')31 .expect('Content-Type', 'application/vnd.example+json; charset=utf-8')32 .expect(200, '{"hello":"world"}', done);33 })3435 describe('when given primitives', function(){36 it('should respond with json for null', function(done){37 var app = express();3839 app.use(function(req, res){40 res.json(null);41 });4243 request(app)44 .get('/')45 .expect('Content-Type', 'application/json; charset=utf-8')46 .expect(200, 'null', done)47 })4849 it('should respond with json for Number', function(done){50 var app = express();5152 app.use(function(req, res){53 res.json(300);54 });5556 request(app)57 .get('/')58 .expect('Content-Type', 'application/json; charset=utf-8')59 .expect(200, '300', done)60 })6162 it('should respond with json for String', function(done){63 var app = express();6465 app.use(function(req, res){66 res.json('str');67 });6869 request(app)70 .get('/')71 .expect('Content-Type', 'application/json; charset=utf-8')72 .expect(200, '"str"', done)73 })74 })7576 describe('when given an array', function(){77 it('should respond with json', function(done){78 var app = express();7980 app.use(function(req, res){81 res.json(['foo', 'bar', 'baz']);82 });8384 request(app)85 .get('/')86 .expect('Content-Type', 'application/json; charset=utf-8')87 .expect(200, '["foo","bar","baz"]', done)88 })89 })9091 describe('when given an object', function(){92 it('should respond with json', function(done){93 var app = express();9495 app.use(function(req, res){96 res.json({ name: 'tobi' });97 });9899 request(app)100 .get('/')101 .expect('Content-Type', 'application/json; charset=utf-8')102 .expect(200, '{"name":"tobi"}', done)103 })104 })105106 describe('"json escape" setting', function () {107 it('should be undefined by default', function () {108 var app = express()109 assert.strictEqual(app.get('json escape'), undefined)110 })111112 it('should unicode escape HTML-sniffing characters', function (done) {113 var app = express()114115 app.enable('json escape')116117 app.use(function (req, res) {118 res.json({ '&': '<script>' })119 })120121 request(app)122 .get('/')123 .expect('Content-Type', 'application/json; charset=utf-8')124 .expect(200, '{"\\u0026":"\\u003cscript\\u003e"}', done)125 })126127 it('should not break undefined escape', function (done) {128 var app = express()129130 app.enable('json escape')131132 app.use(function (req, res) {133 res.json(undefined)134 })135136 request(app)137 .get('/')138 .expect('Content-Type', 'application/json; charset=utf-8')139 .expect(200, '', done)140 })141 })142143 describe('"json replacer" setting', function(){144 it('should be passed to JSON.stringify()', function(done){145 var app = express();146147 app.set('json replacer', function(key, val){148 return key[0] === '_'149 ? undefined150 : val;151 });152153 app.use(function(req, res){154 res.json({ name: 'tobi', _id: 12345 });155 });156157 request(app)158 .get('/')159 .expect('Content-Type', 'application/json; charset=utf-8')160 .expect(200, '{"name":"tobi"}', done)161 })162 })163164 describe('"json spaces" setting', function(){165 it('should be undefined by default', function(){166 var app = express();167 assert(undefined === app.get('json spaces'));168 })169170 it('should be passed to JSON.stringify()', function(done){171 var app = express();172173 app.set('json spaces', 2);174175 app.use(function(req, res){176 res.json({ name: 'tobi', age: 2 });177 });178179 request(app)180 .get('/')181 .expect('Content-Type', 'application/json; charset=utf-8')182 .expect(200, '{\n "name": "tobi",\n "age": 2\n}', done)183 })184 })185 })186})