deno.land / x / opine@2.3.4 / test / units / req.query.test.ts

req.query.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
// deno-lint-ignore-file no-explicit-anyimport { opine } from "../../mod.ts";import { expect, superdeno } from "../deps.ts";import { describe, it } from "../utils.ts";
function createApp(setting?: any) { const app = opine();
if (setting !== undefined) { app.set("query parser", setting); }
app.use(function (req, res) { res.send(req.query); });
return app;}
describe("req", function () { describe(".query", function () { it("should default to {}", function (done) { const app = createApp();
superdeno(app) .get("/") .expect(200, "{}", done); });
it("should default to parse complex keys", function (done) { const app = createApp();
superdeno(app) .get("/?user[name]=deno") .expect(200, '{"user":{"name":"deno"}}', done); });
describe('when "query parser" is extended', function () { it("should parse complex keys", function (done) { const app = createApp("extended");
superdeno(app) .get("/?foo[0][bar]=baz&foo[0][fizz]=buzz&foo[]=done!") .expect(200, '{"foo":[{"bar":"baz","fizz":"buzz"},"done!"]}', done); });
it("should parse parameters with dots", function (done) { const app = createApp("extended");
superdeno(app) .get("/?user.name=deno") .expect(200, '{"user.name":"deno"}', done); }); });
describe('when "query parser" is simple', function () { it("should not parse complex keys", function (done) { const app = createApp("simple");
superdeno(app) .get("/?user%5Bname%5D=deno") .expect(200, '{"user[name]":"deno"}', done); }); });
describe('when "query parser" is a function', function () { it("should parse using function", function (done) { const app = createApp(function (str: string) { return { "length": (str || "").length }; });
superdeno(app) .get("/?user%5Bname%5D=deno") .expect(200, '{"length":19}', done); }); });
describe('when "query parser" disabled', function () { it("should not parse query", function (done) { const app = createApp(false);
superdeno(app) .get("/?user%5Bname%5D=deno") .expect(200, "{}", done); }); });
describe('when "query parser" enabled', function () { it("should not parse complex keys", function (done) { const app = createApp(true);
superdeno(app) .get("/?user%5Bname%5D=deno") .expect(200, '{"user[name]":"deno"}', done); }); });
describe('when "query parser fn" is missing', function () { it('should act like "extended"', function (done) { const app = opine();
delete app.settings["query parser"]; delete app.settings["query parser fn"];
app.use(function (req, res) { res.send(req.query); });
superdeno(app) .get("/?user[name]=deno&user.name=deno") .expect(200, '{"user":{"name":"deno"},"user.name":"deno"}', done); }); });
describe('when "query parser" an unknown value', function () { it("should throw", function () { expect(createApp.bind(null, "bogus")).toThrow( /unknown value.*query parser/, ); }); }); });});
opine

Version Info

Tagged at
2 years ago