deno.land / x / domain_functions@v1.2.0 / src / input-resolvers.test.ts

input-resolvers.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
import { describe, it } from 'https://deno.land/std@0.156.0/testing/bdd.ts'import { assertEquals } from 'https://deno.land/std@0.117.0/testing/asserts.ts'
import * as subject from './input-resolvers.ts'
const makePost: (entries: Array<[string, string]>, url?: string) => Request = ( entries, url = 'http://localhost/test',) => new Request(url, { method: 'POST', body: new URLSearchParams(entries).toString(), headers: { 'Content-Type': 'application/x-www-form-urlencoded', }, })
const makeGet: (entries: Array<[string, string]>, url?: string) => Request = ( entries, url = 'http://localhost/test',) => new Request(url + '?' + new URLSearchParams(entries).toString(), { method: 'GET', })
describe('inputFromForm', () => { it("extracts the input values from a Request's FormData as an Object", async () => { const request = makePost([ ['foo', 'bar'], ['fizz', 'buzz'], ]) assertEquals(await subject.inputFromForm(request), { foo: 'bar', fizz: 'buzz', }) })
it('accepts array-like values', async () => { const request = makePost([ ['foo', 'bar'], ['arr[]', '1'], ['arr[]', '2'], ]) assertEquals(await subject.inputFromForm(request), { foo: 'bar', arr: ['1', '2'], }) })
it('accepts structured values', async () => { const request = makePost([ ['person[0][name]', 'John'], ['person[0][email]', 'john@email.com'], ['person[1][name]', 'Bill'], ['person[1][email]', 'bill@email.com'], ]) assertEquals(await subject.inputFromForm(request), { person: [ { name: 'John', email: 'john@email.com' }, { name: 'Bill', email: 'bill@email.com' }, ], }) })})
describe('inputFromUrl', () => { it("extracts the input values from a Request's URL as an Object", () => { const request = makeGet([ ['foo', 'bar'], ['fizz', 'buzz'], ]) assertEquals(subject.inputFromUrl(request), { foo: 'bar', fizz: 'buzz' }) })
it('accepts array-like values', async () => { const request = makeGet([ ['foo', 'bar'], ['arr[]', '1'], ['arr[]', '2'], ]) assertEquals(await subject.inputFromUrl(request), { foo: 'bar', arr: ['1', '2'], }) })
it('accepts structured values', async () => { const request = makeGet([ ['person[0][name]', 'John'], ['person[0][email]', 'john@email.com'], ['person[1][name]', 'Bill'], ['person[1][email]', 'bill@email.com'], ]) assertEquals(await subject.inputFromUrl(request), { person: [ { email: 'john@email.com', name: 'John' }, { name: 'Bill', email: 'bill@email.com' }, ], }) })})
describe('inputFromFormData', () => { it('extracts a structured object from a FormData', async () => { const request = makePost([ ['person[0][name]', 'John'], ['person[0][email]', 'john@email.com'], ['person[1][name]', 'Bill'], ['person[1][email]', 'bill@email.com'], ]) assertEquals(subject.inputFromFormData(await request.formData()), { person: [ { name: 'John', email: 'john@email.com' }, { name: 'Bill', email: 'bill@email.com' }, ], }) })
it('accepts manually constructed FormData', () => { const formData = new FormData() formData.append('email', 'john@doe.com') formData.append('tasks[]', 'one') formData.append('tasks[]', 'two') assertEquals(subject.inputFromFormData(formData), { email: 'john@doe.com', tasks: ['one', 'two'], }) })})
describe('inputFromSearch', () => { it('extracts a structured object from a URLSearchParams', () => { const request = makeGet([ ['person[0][name]', 'John'], ['person[0][email]', 'john@email.com'], ['person[1][name]', 'Bill'], ['person[1][email]', 'bill@email.com'], ]) assertEquals(subject.inputFromSearch(new URL(request.url).searchParams), { person: [ { email: 'john@email.com', name: 'John' }, { name: 'Bill', email: 'bill@email.com' }, ], }) })
it('accepts manually constructed URLSearchParams', () => { const qs = new URLSearchParams() qs.append('colors[]', 'red') qs.append('colors[]', 'green') qs.append('colors[]', 'blue') assertEquals(subject.inputFromSearch(qs), { colors: ['red', 'green', 'blue'], }) })})
domain_functions

Version Info

Tagged at
a year ago