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

bodyParser.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
import { json } from "../../mod.ts";import { Buffer, expect } from "../deps.ts";import { describe, it } from "../utils.ts";
const encoder = new TextEncoder();const mockJson = { hello: "deno" };
const jsonHeaders = new Headers();jsonHeaders.set("Content-Type", "application/json");
const textHeaders = new Headers();textHeaders.set("Content-Type", "text/plain");textHeaders.set("Content-Length", "1");
describe("bodyParser: json", () => { it("should handle requests without bodies", (done) => { const req: any = { headers: jsonHeaders }; const parser = json();
parser(req, {} as any, (err?: any) => { if (err) throw err; expect(req.parsedBody).toEqual({}); done(); }); });
it("should handle requests with encoded JSON bodies", (done) => { const req: any = { body: new Buffer(encoder.encode(JSON.stringify(mockJson))), headers: jsonHeaders, }; req.headers.set("Content-Length", "1"); const parser = json();
parser(req, {} as any, (err?: any) => { if (err) throw err; expect(req.parsedBody).toEqual(mockJson); done(); }); });
it("should handle requests with encoded JSON array bodies", (done) => { const req: any = { body: new Buffer(encoder.encode(JSON.stringify([mockJson]))), headers: jsonHeaders, }; req.headers.set("Content-Length", "1"); const parser = json();
parser(req, {} as any, (err?: any) => { if (err) throw err; expect(req.parsedBody).toEqual([mockJson]); done(); }); });
it("should handle requests with encoded JSON bodies containing whitespace", (done) => { const req: any = { body: new Buffer( encoder.encode(` \r\n\t\n${JSON.stringify(mockJson)}\n\t\r\n `), ), headers: jsonHeaders, }; req.headers.set("Content-Length", "1"); const parser = json();
parser(req, {} as any, (err?: any) => { if (err) throw err; expect(req.parsedBody).toEqual(mockJson); done(); }); });
it("should not alter request bodies when the content type is not JSON", (done) => { const mockBody = Symbol("test-body"); const req: any = { body: mockBody, headers: textHeaders, }; req.headers.set("Content-Length", "1"); const parser = json();
parser(req, {} as any, (err?: unknown) => { if (err) throw err; expect(req.body).toEqual(mockBody); expect(req.parsedBody).toBeUndefined(); done(); }); });});
opine

Version Info

Tagged at
2 years ago