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

res.format.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
import { opine, Router } from "../../mod.ts";import { expect, superdeno } from "../deps.ts";import { after, describe, it } from "../utils.ts";import type { NextFunction, Opine, OpineRequest, OpineResponse,} from "../../src/types.ts";
const app1 = opine();
app1.use(function (req, res, next) { res.format({ "text/plain": function () { res.send("hey"); },
"text/html": function () { res.send("<p>hey</p>"); },
"application/json": function (a: any, b: any, c: any) { expect(req).toEqual(a); expect(res).toEqual(b); expect(next).toEqual(c); res.send({ message: "hey" }); }, });});
app1.use( function ( err: any, req: OpineRequest, res: OpineResponse, next: NextFunction, ) { if (!err.types) { throw err; }
res.setStatus(err.status).send("Supports: " + err.types.join(", ")); },);
const app2 = opine();
app2.use(function (req, res, next) { res.format({ text: function () { res.send("hey"); }, html: function () { res.send("<p>hey</p>"); }, json: function () { res.send({ message: "hey" }); }, });});
app2.use( function ( err: any, req: OpineRequest, res: OpineResponse, next: NextFunction, ) { res.setStatus(err.status).send("Supports: " + err.types.join(", ")); },);
const app3 = opine();
app3.use(function (req, res, next) { res.format({ text: function () { res.send("hey"); }, default: function () { res.send("default"); }, });});
const app4 = opine();
app4.get("/", function (req, res, next) { res.format({ text: function () { res.send("hey"); }, html: function () { res.send("<p>hey</p>"); }, json: function () { res.send({ message: "hey" }); }, });});
app4.use( function ( err: any, req: OpineRequest, res: OpineResponse, next: NextFunction, ) { res.setStatus(err.status).send("Supports: " + err.types.join(", ")); },);
const app5 = opine();
app5.use(function (req, res, next) { res.format({ default: function () { res.send("hey"); }, });});
describe("res", function () { describe(".format(obj)", function () { describe("with canonicalized mime types", function () { test(app1); });
describe("with extnames", function () { test(app2); });
describe("with parameters", function () { const app = opine();
app.use(function (req, res, next) { res.format({ "text/plain; charset=utf-8": function () { res.send("hey"); }, "text/html; foo=bar; bar=baz": function () { res.send("<p>hey</p>"); }, "application/json; q=0.5": function () { res.send({ message: "hey" }); }, }); });
app.use( function ( err: any, req: OpineRequest, res: OpineResponse, next: NextFunction, ) { res.setStatus(err.status).send("Supports: " + err.types.join(", ")); }, );
test(app); });
describe("given .default", function () { it("should be invoked instead of auto-responding", function (done) { superdeno(app3) .get("/") .set("Accept", "text/html") .expect("default", done); });
it("should work when only .default is provided", function (done) { superdeno(app5) .get("/") .set("Accept", "*/*") .expect("hey", done); }); });
describe("in router", function () { test(app4); });
describe("in router", function () { const app = opine(); const router = Router();
router.get("/", function (req, res, next) { res.format({ text: function () { res.send("hey"); }, html: function () { res.send("<p>hey</p>"); }, json: function () { res.send({ message: "hey" }); }, }); });
router.use( function ( err: any, req: OpineRequest, res: OpineResponse, next: NextFunction, ) { res.setStatus(err.status).send("Supports: " + err.types.join(", ")); }, );
app.use(router);
test(app); }); });});
function test(app: Opine) { it("should utilize q values in negotiation", function (done) { superdeno(app) .get("/") .set("Accept", "text/html; q=.5, application/json, */*; q=.1") .expect({ "message": "hey" }, done); });
it("should allow wildcard type/subtypes", function (done) { superdeno(app) .get("/") .set("Accept", "text/html; q=.5, application/*, */*; q=.1") .expect({ "message": "hey" }, done); });
it("should default the Content-Type", function (done) { superdeno(app) .get("/") .set("Accept", "text/html; q=.5, text/plain") .expect("Content-Type", "text/plain; charset=utf-8") .expect("hey", done); });
it("should set the correct charset for the Content-Type", function (done) { const cb = after(3, done);
superdeno(app) .get("/") .set("Accept", "text/html") .expect("Content-Type", "text/html; charset=utf-8", cb);
superdeno(app) .get("/") .set("Accept", "text/plain") .expect("Content-Type", "text/plain; charset=utf-8", cb);
superdeno(app) .get("/") .set("Accept", "application/json") .expect("Content-Type", "application/json; charset=utf-8", cb); });
it("should vary: Accept", function (done) { superdeno(app) .get("/") .set("Accept", "text/html; q=.5, text/plain") .expect("vary", /Accept/, done); });
describe("when Accept is not present", function () { it("should invoke the first callback", function (done) { superdeno(app) .get("/") .expect("hey", done); }); });
describe("when no match is made", function () { it("should should respond with 406 not acceptable", function (done) { superdeno(app) .get("/") .set("Accept", "foo/bar") .expect("Supports: text/plain, text/html, application/json") .expect(406, done); }); });}
opine

Version Info

Tagged at
2 years ago