Skip to content

Commit 7fdb3f6

Browse files
egor-romanovlaktek
authored andcommitted
feat: add few headers and body tests
1 parent 7633387 commit 7fdb3f6

File tree

4 files changed

+277
-0
lines changed

4 files changed

+277
-0
lines changed

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,22 @@ JS Client library to interact with Supabase Functions.
44

55
## Docs
66
https://supabase.github.io/functions-js/index.html
7+
8+
## testing
9+
10+
You are going to need docker daemon running to execute tests.
11+
12+
To start test run use the following command:
13+
14+
```sh
15+
npm i
16+
npm run test
17+
```
18+
19+
Projects includes reporting with allure. Use the following command get more detailed test reports:
20+
21+
```sh
22+
test:report
23+
```
24+
25+
You may need to have Java8 installed to generate the report.

test/functions/mirror.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { serve } from 'https://deno.land/std/http/server.ts'
2+
3+
serve(async (request: Request) => {
4+
let body
5+
switch (request.headers.get('content-type')) {
6+
case 'application/json': {
7+
body = await request.json()
8+
break
9+
}
10+
case 'application/x-www-form-urlencoded': {
11+
const formBody = await request.formData()
12+
body = []
13+
for (const e of formBody.entries()) {
14+
body.push(e)
15+
}
16+
break
17+
}
18+
default: {
19+
body = await request.text()
20+
break
21+
}
22+
}
23+
const headers = []
24+
for (const h of request.headers.entries()) {
25+
headers.push(h)
26+
}
27+
const resp = {
28+
url: request.url ?? 'empty',
29+
method: request.method ?? 'empty',
30+
headers: headers ?? 'empty',
31+
body: body ?? 'empty',
32+
}
33+
34+
return new Response(JSON.stringify(resp), {
35+
status: 200,
36+
headers: {
37+
'content-type': 'application/json',
38+
},
39+
})
40+
})

test/models/mirrorResponse.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export interface MirrorResponse {
2+
url: string
3+
method: string
4+
headers: string | {}
5+
body: string | {}
6+
}

test/spec/params.spec.ts

Lines changed: 212 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,212 @@
1+
import 'mocha'
2+
import { assert } from 'chai'
3+
import { nanoid } from 'nanoid'
4+
import { sign } from 'jsonwebtoken'
5+
import { ContentType } from 'allure2-js-commons'
6+
7+
import { FunctionsClient } from '../../src/index'
8+
9+
import { Relay, runRelay } from '../relay/container'
10+
import { attach, log } from '../utils/allure'
11+
import { MirrorResponse } from '../models/mirrorResponse'
12+
13+
describe('params reached to function', () => {
14+
let relay: Relay
15+
const jwtSecret = nanoid(10)
16+
const apiKey = sign({ name: 'anon' }, jwtSecret)
17+
18+
before(async () => {
19+
relay = await runRelay('mirror', jwtSecret)
20+
})
21+
22+
after(async () => {
23+
relay && relay.container && (await relay.container.stop())
24+
})
25+
26+
it('invoke mirror', async () => {
27+
log('create FunctionsClient')
28+
const fclient = new FunctionsClient(`http://localhost:${relay.container.getMappedPort(8081)}`, {
29+
Authorization: `Bearer ${apiKey}`,
30+
})
31+
32+
log('invoke mirror')
33+
const { data, error } = await fclient.invoke<MirrorResponse>('mirror', { responseType: 'json' })
34+
35+
log('assert no error')
36+
assert.isNull(error)
37+
38+
const expected = {
39+
url: 'http://localhost:8000/mirror',
40+
method: 'POST',
41+
headers: data?.headers ?? [],
42+
body: '',
43+
}
44+
attach(
45+
'check data from function',
46+
`expected: ${JSON.stringify(expected)}\n actual: ${JSON.stringify(data)}`,
47+
ContentType.TEXT
48+
)
49+
assert.deepEqual(data, expected)
50+
})
51+
52+
it('invoke mirror with client header', async () => {
53+
log('create FunctionsClient')
54+
const fclient = new FunctionsClient(`http://localhost:${relay.container.getMappedPort(8081)}`, {
55+
Authorization: `Bearer ${apiKey}`,
56+
CustomHeader: 'check me',
57+
})
58+
59+
log('invoke mirror')
60+
const { data, error } = await fclient.invoke<MirrorResponse>('mirror', { responseType: 'json' })
61+
62+
log('assert no error')
63+
assert.isNull(error)
64+
65+
const expected = {
66+
url: 'http://localhost:8000/mirror',
67+
method: 'POST',
68+
headers: data?.headers ?? [],
69+
body: '',
70+
}
71+
attach(
72+
'check data from function',
73+
`expected: ${JSON.stringify(expected)}\n actual: ${JSON.stringify(data)}`,
74+
ContentType.TEXT
75+
)
76+
assert.deepEqual(data, expected)
77+
attach(
78+
'check headers from function',
79+
`expected to include: ${['customheader', 'check me']}\n actual: ${JSON.stringify(
80+
data?.headers
81+
)}`,
82+
ContentType.TEXT
83+
)
84+
assert.isTrue(
85+
(data?.headers as [Array<string>]).filter(
86+
([k, v]) => k === 'customheader' && v === 'check me'
87+
).length > 0
88+
)
89+
})
90+
91+
it('invoke mirror with invoke header', async () => {
92+
log('create FunctionsClient')
93+
const fclient = new FunctionsClient(`http://localhost:${relay.container.getMappedPort(8081)}`)
94+
95+
log('invoke mirror')
96+
const customHeader = nanoid()
97+
const { data, error } = await fclient.invoke<MirrorResponse>('mirror', {
98+
responseType: 'json',
99+
headers: {
100+
'custom-header': customHeader,
101+
Authorization: `Bearer ${apiKey}`,
102+
},
103+
})
104+
105+
log('assert no error')
106+
assert.isNull(error)
107+
108+
const expected = {
109+
url: 'http://localhost:8000/mirror',
110+
method: 'POST',
111+
headers: data?.headers ?? [],
112+
body: '',
113+
}
114+
attach(
115+
'check data from function',
116+
`expected: ${JSON.stringify(expected)}\n actual: ${JSON.stringify(data)}`,
117+
ContentType.TEXT
118+
)
119+
assert.deepEqual(data, expected)
120+
attach(
121+
'check headers from function',
122+
`expected to include: ${['custom-header', customHeader]}\n actual: ${JSON.stringify(
123+
data?.headers
124+
)}`,
125+
ContentType.TEXT
126+
)
127+
assert.isTrue(
128+
(data?.headers as [Array<string>]).filter(
129+
([k, v]) => k === 'custom-header' && v === customHeader
130+
).length > 0
131+
)
132+
})
133+
134+
it('invoke mirror with body formData', async () => {
135+
log('create FunctionsClient')
136+
const fclient = new FunctionsClient(`http://localhost:${relay.container.getMappedPort(8081)}`)
137+
attach('setAuth', apiKey, ContentType.TEXT)
138+
fclient.setAuth(apiKey)
139+
140+
log('invoke mirror')
141+
var form = new URLSearchParams()
142+
form.append(nanoid(5), nanoid(10))
143+
form.append(nanoid(7), nanoid(5))
144+
form.append(nanoid(15), nanoid())
145+
const { data, error } = await fclient.invoke<MirrorResponse>('mirror', {
146+
responseType: 'json',
147+
body: form,
148+
headers: {
149+
'content-type': 'application/x-www-form-urlencoded',
150+
},
151+
})
152+
153+
log('assert no error')
154+
assert.isNull(error)
155+
156+
const body = []
157+
for (const e of form.entries()) {
158+
body.push(e)
159+
}
160+
const expected = {
161+
url: 'http://localhost:8000/mirror',
162+
method: 'POST',
163+
headers: data?.headers ?? [],
164+
body: body,
165+
}
166+
attach(
167+
'check data from function',
168+
`expected: ${JSON.stringify(expected)}\n actual: ${JSON.stringify(data)}`,
169+
ContentType.TEXT
170+
)
171+
assert.deepEqual(data, expected)
172+
})
173+
174+
it('invoke mirror with body json', async () => {
175+
log('create FunctionsClient')
176+
const fclient = new FunctionsClient(`http://localhost:${relay.container.getMappedPort(8081)}`)
177+
attach('setAuth', apiKey, ContentType.TEXT)
178+
fclient.setAuth(apiKey)
179+
180+
log('invoke mirror')
181+
const body = {
182+
one: nanoid(10),
183+
two: nanoid(5),
184+
three: nanoid(),
185+
num: 11,
186+
flag: false,
187+
}
188+
const { data, error } = await fclient.invoke<MirrorResponse>('mirror', {
189+
responseType: 'json',
190+
body: JSON.stringify(body),
191+
headers: {
192+
'content-type': 'application/json',
193+
},
194+
})
195+
196+
log('assert no error')
197+
assert.isNull(error)
198+
199+
const expected = {
200+
url: 'http://localhost:8000/mirror',
201+
method: 'POST',
202+
headers: data?.headers ?? [],
203+
body: body,
204+
}
205+
attach(
206+
'check data from function',
207+
`expected: ${JSON.stringify(expected)}\n actual: ${JSON.stringify(data)}`,
208+
ContentType.TEXT
209+
)
210+
assert.deepEqual(data, expected)
211+
})
212+
})

0 commit comments

Comments
 (0)