Skip to content

Commit 33a7258

Browse files
egor-romanovlaktek
authored andcommitted
feat: add hijack test
1 parent f71275a commit 33a7258

File tree

4 files changed

+75
-2
lines changed

4 files changed

+75
-2
lines changed

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,4 +109,7 @@ docs/
109109

110110
# Test reports
111111
allure-results
112-
allure-report
112+
allure-report
113+
114+
# IDE specific
115+
.vscode

test/functions/hijack/index.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { serve } from 'https://deno.land/[email protected]/http/server.ts'
2+
3+
serve((req) => {
4+
const p = Deno.upgradeHttp(req);
5+
6+
(
7+
// Run this async IIFE concurrently, first packet won't arrive
8+
// until we return HTTP101 response.
9+
async () => {
10+
const [conn, firstPacket] = await p
11+
const decoder = new TextDecoder()
12+
const text = decoder.decode(firstPacket)
13+
console.log(text)
14+
// Hello
15+
const uint8Array = new Uint8Array([72, 101, 108, 108, 111])
16+
conn.write(uint8Array)
17+
conn.close()
18+
}
19+
)()
20+
21+
// HTTP101 - Switching Protocols
22+
return new Response(null, { status: 101 })
23+
})

test/relay/container.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ export async function runRelay(
7979
'run',
8080
'--allow-all',
8181
'--watch',
82+
'--unstable',
8283
`/home/deno/${slug}/index.ts`,
8384
])
8485

@@ -95,7 +96,7 @@ export async function runRelay(
9596
},
9697
}
9798
)
98-
if (healthCheck.ok) {
99+
if (healthCheck.ok || healthCheck.status === 101) {
99100
log(`function started to serve: ${slug + '-' + id}`)
100101
return { container: startedRelay, id, execCache, execRun }
101102
}

test/spec/hijack.spec.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { Blob } from 'buffer'
2+
import querystring from 'querystring'
3+
4+
import 'jest'
5+
import { nanoid } from 'nanoid'
6+
import { sign } from 'jsonwebtoken'
7+
import { ContentType } from 'allure-js-commons'
8+
9+
import { FunctionsClient } from '../../src/index'
10+
11+
import { Relay, runRelay } from '../relay/container'
12+
import { attach, log } from '../utils/jest-custom-reporter'
13+
import { MirrorResponse } from '../models/mirrorResponse'
14+
15+
describe('hijack connection', () => {
16+
let relay: Relay
17+
const jwtSecret = nanoid(10)
18+
const apiKey = sign({ name: 'anon' }, jwtSecret)
19+
const func = 'hijack'
20+
21+
beforeAll(async () => {
22+
relay = await runRelay(func, jwtSecret)
23+
})
24+
25+
afterAll(async () => {
26+
relay && relay.container && (await relay.container.stop())
27+
})
28+
29+
test('invoke func', async () => {
30+
/**
31+
* @feature hijack
32+
*/
33+
log('create FunctionsClient')
34+
const fclient = new FunctionsClient(`http://localhost:${relay.container.getMappedPort(8081)}`, {
35+
Authorization: `Bearer ${apiKey}`,
36+
})
37+
38+
log('invoke func')
39+
const { data, error } = await fclient.invoke(func, {
40+
responseType: 'text',
41+
})
42+
43+
log('assert error to be "Connection Upgrade is not supported"')
44+
expect(error).not.toBeNull()
45+
})
46+
})

0 commit comments

Comments
 (0)