deno.land / x / opine@2.3.4 / test / units / res.json.test.ts

res.json.test.ts
نووسراو ببینە
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import { opine } from "../../mod.ts";import { expect, superdeno } from "../deps.ts";import { describe, it } from "../utils.ts";
describe("res", function () { describe(".json(object)", function () { it("should not support jsonp callbacks", function (done) { const app = opine();
app.use(function (req, res) { res.json({ foo: "bar" }); });
superdeno(app) .get("/?callback=foo") .expect('{"foo":"bar"}', done); });
it("should not override previous Content-Types", function (done) { const app = opine();
app.get("/", function (req, res) { res.type("application/vnd.example+json"); res.json({ hello: "world" }); });
superdeno(app) .get("/") .expect("Content-Type", "application/vnd.example+json") .expect(200, '{"hello":"world"}', done); });
describe("when given primitives", function () { it("should respond with json for null", function (done) { const app = opine();
app.use(function (req, res) { res.json(null); });
superdeno(app) .get("/") .expect("Content-Type", "application/json; charset=utf-8") .expect(200, "null", done); });
it("should respond with json for Number", function (done) { const app = opine();
app.use(function (req, res) { res.json(300); });
superdeno(app) .get("/") .expect("Content-Type", "application/json; charset=utf-8") .expect(200, "300", done); });
it("should respond with json for String", function (done) { const app = opine();
app.use(function (req, res) { res.json("str"); });
superdeno(app) .get("/") .expect("Content-Type", "application/json; charset=utf-8") .expect(200, '"str"', done); }); });
describe("when given an array", function () { it("should respond with json", function (done) { const app = opine();
app.use(function (req, res) { res.json(["foo", "bar", "baz"]); });
superdeno(app) .get("/") .expect("Content-Type", "application/json; charset=utf-8") .expect(200, '["foo","bar","baz"]', done); }); });
describe("when given an object", function () { it("should respond with json", function (done) { const app = opine();
app.use(function (req, res) { res.json({ name: "deno" }); });
superdeno(app) .get("/") .expect("Content-Type", "application/json; charset=utf-8") .expect(200, '{"name":"deno"}', done); }); });
describe('"json escape" setting', function () { it("should be undefined by default", function () { const app = opine(); expect(app.get("json escape")).toEqual(undefined); });
it("should unicode escape HTML-sniffing characters", function (done) { const app = opine();
app.enable("json escape");
app.use(function (req, res) { res.json({ "&": "<script>" }); });
superdeno(app) .get("/") .expect("Content-Type", "application/json; charset=utf-8") .expect(200, '{"\\u0026":"\\u003cscript\\u003e"}', done); }); });
describe('"json replacer" setting', function () { it("should be passed to JSON.stringify()", function (done) { const app = opine();
app.set("json replacer", function (key: string, val: string) { return key[0] === "_" ? undefined : val; });
app.use(function (req, res) { res.json({ name: "deno", _id: 12345 }); });
superdeno(app) .get("/") .expect("Content-Type", "application/json; charset=utf-8") .expect(200, '{"name":"deno"}', done); }); });
describe('"json spaces" setting', function () { it("should be undefined by default", function () { const app = opine(); expect(app.get("json spaces")).toEqual(undefined); });
it("should be passed to JSON.stringify()", function (done) { const app = opine();
app.set("json spaces", 2);
app.use(function (req, res) { res.json({ name: "deno", age: 2 }); });
superdeno(app) .get("/") .expect("Content-Type", "application/json; charset=utf-8") .expect(200, '{\n "name": "deno",\n "age": 2\n}', done); }); }); });});
opine

Version Info

Tagged at
2 years ago