-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmod_test.ts
76 lines (68 loc) · 2.06 KB
/
mod_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
import { makeFetch } from 'https://deno.land/x/superfetch@1.0.4/mod.ts'
import { bodyParser, json, ReqWithBody, urlencoded } from './mod.ts'
import { describe, it, run } from 'https://deno.land/x/tincan@1.0.1/mod.ts'
describe('bodyParser(req)', () => {
it('should decode request body', async () => {
const fetch = makeFetch(async (req) => {
return new Response(await bodyParser((x) => x)(req))
})
const res = await fetch('/', { method: 'POST', body: 'Hello World' })
res.expect('Hello World')
})
})
describe('json(req)', () => {
it('should parse JSON body', async () => {
const fetch = makeFetch(
async (req: Request & ReqWithBody<Record<string, string>>) => {
await json(req)
return new Response(req.parsedBody?.hello || 'no body', {
status: 200,
})
},
)
const res = await fetch('/', {
method: 'POST',
body: '{ "hello": "world" }',
headers: { 'Content-Type': 'application/json' },
})
res.expect('world')
})
it('should pass JSON parsing error to next', async () => {
const fetch = makeFetch(
async (req: Request & ReqWithBody<Record<string, string>>) => {
let status = 200
await json(req, undefined, () => {
status = 400
})
return new Response(undefined, { status })
},
)
const res = await fetch('/', {
method: 'POST',
body: '{ "hello": "wor',
headers: { 'Content-Type': 'application/json' },
})
res.expect(400)
})
})
describe('urlencoded', () => {
it('should parse URL encoded form', async () => {
const fetch = makeFetch(
async (req: Request & ReqWithBody<Record<string, string>>) => {
await urlencoded(req)
return new Response(req.parsedBody?.hello || 'no body', {
status: 200,
})
},
)
const res = await fetch('/', {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
body: new URLSearchParams({ hello: 'world' }),
})
res.expect('world')
})
})
run()