Skip to content

Commit 8f72fdc

Browse files
egor-romanovlaktek
authored andcommitted
fix: default invoke type cange to json
1 parent b1f558d commit 8f72fdc

File tree

4 files changed

+54
-44
lines changed

4 files changed

+54
-44
lines changed
File renamed without changes.
File renamed without changes.

test/relay/container.ts

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
import * as fs from 'fs'
22
import { nanoid } from 'nanoid'
3+
import crossFetch from 'cross-fetch'
4+
import { sign } from 'jsonwebtoken'
35
import { GenericContainer, Network, StartedTestContainer, Wait } from 'testcontainers'
46
import { ExecResult } from 'testcontainers/dist/docker/types'
57
import { ContentType } from 'allure2-js-commons'
8+
69
import { attach, log } from '../utils/allure'
710

811
/**
@@ -43,7 +46,7 @@ export async function runRelay(
4346
): Promise<Relay> {
4447
// read function to deploy
4548
log('read function body')
46-
const functionBytes = fs.readFileSync('test/functions/' + slug + '.ts', 'utf8')
49+
const functionBytes = fs.readFileSync('test/functions/' + slug + '/index.ts', 'utf8')
4750
attach('function body', functionBytes, ContentType.TEXT)
4851

4952
// random id for parallel execution
@@ -55,18 +58,9 @@ export async function runRelay(
5558

5659
// create relay container
5760
log(`create relay ${slug + '-' + id}`)
58-
const relay = await new GenericContainer('supabase/deno-relay')
61+
const relay = await new GenericContainer('supabase/deno-relay:v1.1.0')
5962
.withName(slug + '-' + id)
60-
.withCmd([
61-
'sh',
62-
'-c',
63-
`cat <<'EOF' > /home/deno/${slug}.ts && deno run --allow-all index.ts
64-
` +
65-
functionBytes +
66-
`
67-
EOF
68-
`,
69-
])
63+
.withBindMount(`${process.cwd()}/test/functions/${slug}`, `/home/deno/${slug}`, 'ro')
7064
.withNetworkMode(network.getName())
7165
.withExposedPorts(8081)
7266
.withWaitStrategy(Wait.forLogMessage('Listening on http://0.0.0.0:8081'))
@@ -80,16 +74,34 @@ EOF
8074
// start relay and function
8175
log(`start relay ${slug + '-' + id}`)
8276
const startedRelay = await relay.start()
83-
const execCache = startedRelay.exec(['deno', 'cache', `/home/deno/${slug}.ts`])
84-
const execRun = startedRelay.exec(['deno', 'run', '--allow-all', `/home/deno/${slug}.ts`])
77+
const execCache = startedRelay.exec(['deno', 'cache', `/home/deno/${slug}/index.ts`])
78+
const execRun = startedRelay.exec([
79+
'deno',
80+
'run',
81+
'--allow-all',
82+
'--watch',
83+
`/home/deno/${slug}/index.ts`,
84+
])
8585

8686
// wait till function is running
8787
log(`check function is healthy: ${slug + '-' + id}`)
8888
for (let ctr = 0; ctr < 30; ctr++) {
89-
const healthCheck = await startedRelay.exec(['nc', '-z', 'localhost', '8000'])
90-
if (healthCheck.exitCode == 0) {
91-
log(`function started to serve: ${slug + '-' + id}`)
92-
return { container: startedRelay, id, execCache, execRun }
89+
try {
90+
const healthCheck = await crossFetch(
91+
`http://localhost:${startedRelay.getMappedPort(8081)}/${slug}`,
92+
{
93+
method: 'POST',
94+
headers: {
95+
Authorization: `Bearer ${sign({ name: 'check' }, jwtSecret)}`,
96+
},
97+
}
98+
)
99+
if (healthCheck.ok) {
100+
log(`function started to serve: ${slug + '-' + id}`)
101+
return { container: startedRelay, id, execCache, execRun }
102+
}
103+
} catch {
104+
/* we actually don't care about errors here */
93105
}
94106
await new Promise((resolve) => setTimeout(resolve, 500))
95107
}

test/spec/hello.spec.ts

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ describe('basic tests (hello function)', () => {
3030
})
3131

3232
log('invoke hello')
33-
const { data, error } = await fclient.invoke('hello', { responseType: 'text' })
33+
const { data, error } = await fclient.invoke<string>('hello', { responseType: 'text' })
3434

3535
log('assert no error')
3636
assert.isNull(error)
@@ -45,7 +45,7 @@ describe('basic tests (hello function)', () => {
4545
fclient.setAuth(apiKey)
4646

4747
log('invoke hello')
48-
const { data, error } = await fclient.invoke('hello', { responseType: 'text' })
48+
const { data, error } = await fclient.invoke<string>('hello', { responseType: 'text' })
4949

5050
log('assert no error')
5151
assert.isNull(error)
@@ -61,13 +61,12 @@ describe('basic tests (hello function)', () => {
6161
fclient.setAuth(wrongKey)
6262

6363
log('invoke hello')
64-
const { data, error } = await fclient.invoke('hello', { responseType: 'text' })
64+
const { data, error } = await fclient.invoke<string>('hello', { responseType: 'text' })
6565

6666
log('check error')
6767
assert.isNotNull(error)
68-
// todo check error
69-
log(`assert ${data} is equal to 'Invalid JWT'`)
70-
assert.equal(data, 'Invalid JWT')
68+
assert.equal(error?.message, 'Invalid JWT')
69+
assert.isNull(data)
7170
})
7271

7372
it('invoke hello: auth override by setAuth wrong key', async () => {
@@ -80,13 +79,12 @@ describe('basic tests (hello function)', () => {
8079
fclient.setAuth(wrongKey)
8180

8281
log('invoke hello')
83-
const { data, error } = await fclient.invoke('hello', { responseType: 'text' })
82+
const { data, error } = await fclient.invoke<string>('hello', { responseType: 'text' })
8483

8584
log('check error')
8685
assert.isNotNull(error)
87-
// todo check error
88-
log(`assert ${data} is equal to 'Invalid JWT'`)
89-
assert.equal(data, 'Invalid JWT')
86+
assert.equal(error?.message, 'Invalid JWT')
87+
assert.isNull(data)
9088
})
9189

9290
it('invoke hello: auth override by setAuth right key', async () => {
@@ -101,7 +99,7 @@ describe('basic tests (hello function)', () => {
10199
fclient.setAuth(apiKey)
102100

103101
log('invoke hello')
104-
const { data, error } = await fclient.invoke('hello', { responseType: 'text' })
102+
const { data, error } = await fclient.invoke<string>('hello', { responseType: 'text' })
105103

106104
log('assert no error')
107105
assert.isNull(error)
@@ -114,7 +112,7 @@ describe('basic tests (hello function)', () => {
114112
const fclient = new FunctionsClient(`http://localhost:${relay.container.getMappedPort(8081)}`)
115113

116114
log('invoke hello with Authorization header')
117-
const { data, error } = await fclient.invoke('hello', {
115+
const { data, error } = await fclient.invoke<string>('hello', {
118116
responseType: 'text',
119117
headers: {
120118
Authorization: `Bearer ${apiKey}`,
@@ -136,7 +134,7 @@ describe('basic tests (hello function)', () => {
136134
fclient.setAuth(wrongKey)
137135

138136
log('invoke hello with Authorization header')
139-
const { data, error } = await fclient.invoke('hello', {
137+
const { data, error } = await fclient.invoke<string>('hello', {
140138
responseType: 'text',
141139
headers: {
142140
Authorization: `Bearer ${apiKey}`,
@@ -157,7 +155,7 @@ describe('basic tests (hello function)', () => {
157155

158156
const wrongKey = sign({ name: 'anon' }, 'wrong_jwt')
159157
log('invoke hello with wrong Authorization header')
160-
const { data, error } = await fclient.invoke('hello', {
158+
const { data, error } = await fclient.invoke<string>('hello', {
161159
responseType: 'text',
162160
headers: {
163161
Authorization: `Bearer ${wrongKey}`,
@@ -166,23 +164,23 @@ describe('basic tests (hello function)', () => {
166164

167165
log('check error')
168166
assert.isNotNull(error)
169-
// todo check error
170-
log(`assert ${data} is equal to 'Invalid JWT'`)
171-
assert.equal(data, 'Invalid JWT')
167+
assert.equal(error?.message, 'Invalid JWT')
168+
assert.isNull(data)
172169
})
173170

174-
it('invoke missing function', async () => {
171+
it.skip('invoke missing function', async () => {
175172
log('create FunctionsClient')
176173
const fclient = new FunctionsClient(`http://localhost:${relay.container.getMappedPort(8081)}`, {
177174
Authorization: `Bearer ${apiKey}`,
178175
})
179176

180177
log('invoke hello')
181-
const { data, error } = await fclient.invoke('missing', { responseType: 'text' })
178+
const { data, error } = await fclient.invoke<string>('missing', { responseType: 'text' })
182179

183180
log('check error')
184181
assert.isNotNull(error)
185-
// todo check error and data
182+
assert.equal(error?.message, 'Invalid JWT')
183+
assert.isNull(data)
186184
})
187185

188186
it('invoke with custom fetch', async () => {
@@ -199,7 +197,7 @@ describe('basic tests (hello function)', () => {
199197
)
200198

201199
log('invoke hello')
202-
const { data, error } = await fclient.invoke('', { responseType: 'text' })
200+
const { data, error } = await fclient.invoke<string>('', { responseType: 'text' })
203201

204202
log('assert no error')
205203
assert.isNull(error)
@@ -221,13 +219,13 @@ describe('basic tests (hello function)', () => {
221219
)
222220

223221
log('invoke hello')
224-
const { data, error } = await fclient.invoke('', { responseType: 'text' })
222+
const { data, error } = await fclient.invoke<string>('', { responseType: 'text' })
225223

226224
log('check error')
227225
assert.isNotNull(error)
228-
// todo check error
229-
log(`assert ${data} is equal to 'Only POST requests are supported'`)
230-
assert.equal(data, 'Only POST requests are supported')
226+
log(`assert ${error?.message} is equal to 'Only POST and OPTIONS requests are supported'`)
227+
assert.equal(error?.message, 'Only POST and OPTIONS requests are supported')
228+
assert.isNull(data)
231229
})
232230

233231
it('invoke hello with custom fetch override header', async () => {
@@ -247,7 +245,7 @@ describe('basic tests (hello function)', () => {
247245
)
248246

249247
log('invoke hello with Authorization header')
250-
const { data, error } = await fclient.invoke('hello', {
248+
const { data, error } = await fclient.invoke<string>('hello', {
251249
responseType: 'text',
252250
})
253251

0 commit comments

Comments
 (0)