-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtunnels_test.ts
80 lines (72 loc) · 2.38 KB
/
tunnels_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
import { assertEquals } from "https://deno.land/[email protected]/assert/mod.ts";
import { StdioTunnel, ExecStatus, PortforwardTunnel } from "./tunnels.ts";
Deno.test('stdiotunnel output buffering', async () => {
const intendedStdout = 'hello world';
const tunnel = new StdioTunnel({
async getChannel(opts) {
return await {
readable: new ReadableStream({
start(ctlr) {
if (opts.streamIndex == 1) {
ctlr.enqueue(intendedStdout);
}
if (opts.streamIndex == 3) {
ctlr.enqueue(JSON.stringify({
status: 'Success',
} as ExecStatus)); // TODO: satisfies (since one of deno 1.29-1.32)
}
ctlr.close();
},
}).pipeThrough(new TextEncoderStream()) as any,
writable: new WritableStream() as any,
};
},
ready: () => Promise.resolve(),
stop: () => Promise.resolve(),
subProtocol: 'v4.tunnel.k8s.io',
transportProtocol: 'Opaque',
}, new URLSearchParams([
['stdout', '1'],
]));
await tunnel.ready;
const output = await tunnel.output();
console.log(new TextDecoder().decode(output.stdout));
assertEquals(new TextDecoder().decode(output.stdout), intendedStdout);
});
Deno.test('portforwardtunnel echo pipe', async () => {
const tunnel = new PortforwardTunnel({
async getChannel(opts) {
if (opts.streamIndex == 0) return new TransformStream({
start(ctlr) {
ctlr.enqueue(new Uint8Array([0,70])); // TODO: this should fail due to mismatch
},
}) as any;
return await {
readable: new ReadableStream({
start(ctlr) {
ctlr.close();
},
}).pipeThrough(new TextEncoderStream()) as any,
writable: new WritableStream() as any,
};
},
ready: () => Promise.resolve(),
stop: () => Promise.resolve(),
subProtocol: 'v4.tunnel.k8s.io',
transportProtocol: 'Opaque',
}, new URLSearchParams([
['ports', '80'],
]));
await tunnel.ready;
const intendedText = 'asdf pickel';
const socket = await tunnel.connectToPort(80);
const [output] = await Promise.all([
new Response(socket.readable).text(),
(async () => {
const writer = socket.writable.getWriter();
await writer.write(new TextEncoder().encode(intendedText));
writer.close();
})(),
]);
assertEquals(output, intendedText);
});