Skip to content

Commit 7fa4348

Browse files
test: add test for manual incoming handler component
Signed-off-by: Victor Adossi <vadossi@cosmonic.com>
1 parent 66d0b48 commit 7fa4348

File tree

6 files changed

+122
-28
lines changed

6 files changed

+122
-28
lines changed

.github/workflows/main.yml

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,15 @@ jobs:
164164
key: starlingmonkey-${{matrix.build-type}}-${{ steps.starlingmonkey-commit.outputs.STARLINGMONKEY_HASH }}
165165
path: lib
166166

167+
- name: Restore Embedding Splicer from cache
168+
uses: actions/cache/restore@v4
169+
id: splicer-build
170+
with:
171+
key: splicer-${{ hashFiles('Cargo.lock', 'crates/spidermonkey-embedding-splicer/src/**/*.rs') }}
172+
path: |
173+
lib
174+
target
175+
167176
- uses: actions/setup-node@v4
168177
with:
169178
node-version: ${{matrix.node-version}}
@@ -192,6 +201,15 @@ jobs:
192201
key: starlingmonkey-release-${{ steps.starlingmonkey-commit.outputs.STARLINGMONKEY_HASH }}
193202
path: lib
194203

204+
- name: Restore Embedding Splicer from cache
205+
uses: actions/cache/restore@v4
206+
id: splicer-build
207+
with:
208+
key: splicer-${{ hashFiles('Cargo.lock', 'crates/spidermonkey-embedding-splicer/src/**/*.rs') }}
209+
path: |
210+
lib
211+
target
212+
195213
- uses: actions/setup-node@v4
196214
with:
197215
node-version: '23.10.0'
@@ -203,8 +221,8 @@ jobs:
203221
uses: actions/cache@v4
204222
with:
205223
path: 'examples/hello-world/host/target/release/wasmtime-test*'
206-
key: example-hello-world-cargo-${{ hashFiles('examples/hello-world/host/src/main.rs',
207-
'examples/hello-world/host/Cargo.lock',
224+
key: example-hello-world-cargo-${{ hashFiles('examples/hello-world/host/src/main.rs',
225+
'examples/hello-world/host/Cargo.lock',
208226
'examples/hello-world/guest/hello.wit') }}
209227

210228
- name: Test Example

test/cases/http-server/source.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import {
2+
IncomingRequest,
3+
ResponseOutparam,
4+
OutgoingBody,
5+
OutgoingResponse,
6+
Fields,
7+
} from 'wasi:http/types@0.2.3';
8+
9+
export const incomingHandler = {
10+
handle(incomingRequest, responseOutparam) {
11+
const outgoingResponse = new OutgoingResponse(new Fields());
12+
let outgoingBody = outgoingResponse.body();
13+
{
14+
let outputStream = outgoingBody.write();
15+
outputStream.blockingWriteAndFlush(
16+
new Uint8Array(new TextEncoder().encode('Hello world!')),
17+
);
18+
outputStream[Symbol.dispose]();
19+
}
20+
outgoingResponse.setStatusCode(200);
21+
OutgoingBody.finish(outgoingBody, undefined);
22+
ResponseOutparam.set(outgoingResponse, {
23+
tag: 'ok',
24+
val: outgoingResponse,
25+
});
26+
},
27+
};

test/cases/http-server/test.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { strictEqual } from 'node:assert';
2+
3+
import { HTTPServer } from '@bytecodealliance/preview2-shim/http';
4+
5+
import { getRandomPort } from '../../util.js';
6+
7+
export const enableFeatures = ['http'];
8+
export const worldName = 'test3';
9+
10+
export async function test(instance) {
11+
const server = new HTTPServer(instance.incomingHandler);
12+
let port = await getRandomPort();
13+
server.listen(port);
14+
15+
try {
16+
const resp = await fetch(`http://localhost:${port}`);
17+
const text = await resp.text();
18+
strictEqual(text, 'Hello world!');
19+
} finally {
20+
server.stop();
21+
}
22+
}

test/test.js

Lines changed: 33 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -138,41 +138,48 @@ suite('Bindings', () => {
138138
test(name, async () => {
139139
const source = await readFile(
140140
new URL(`./cases/${name}/source.js`, import.meta.url),
141-
'utf8'
141+
'utf8',
142142
);
143143

144+
const test = await import(`./cases/${name}/test.js`);
145+
146+
// Determine the relevant WIT world to use
144147
let witWorld,
145148
witPath,
146149
worldName,
147150
isWasiTarget = false;
148-
try {
149-
witWorld = await readFile(
150-
new URL(`./cases/${name}/world.wit`, import.meta.url),
151-
'utf8'
152-
);
153-
} catch (e) {
154-
if (e?.code == 'ENOENT') {
155-
try {
156-
isWasiTarget = true;
157-
witPath = fileURLToPath(
158-
new URL(`./cases/${name}/wit`, import.meta.url)
159-
);
160-
await readdir(witPath);
161-
} catch (e) {
162-
if (e?.code === 'ENOENT') {
163-
witPath = fileURLToPath(new URL('./wit', import.meta.url));
164-
worldName = 'test2';
165-
} else {
166-
throw e;
151+
if (test.worldName) {
152+
witPath = fileURLToPath(new URL('./wit', import.meta.url));
153+
worldName = test.worldName;
154+
isWasiTarget = true;
155+
} else {
156+
try {
157+
witWorld = await readFile(
158+
new URL(`./cases/${name}/world.wit`, import.meta.url),
159+
'utf8',
160+
);
161+
} catch (e) {
162+
if (e?.code == 'ENOENT') {
163+
try {
164+
isWasiTarget = true;
165+
witPath = fileURLToPath(
166+
new URL(`./cases/${name}/wit`, import.meta.url),
167+
);
168+
await readdir(witPath);
169+
} catch (e) {
170+
if (e?.code === 'ENOENT') {
171+
witPath = fileURLToPath(new URL('./wit', import.meta.url));
172+
worldName = 'test2';
173+
} else {
174+
throw e;
175+
}
167176
}
177+
} else {
178+
throw e;
168179
}
169-
} else {
170-
throw e;
171180
}
172181
}
173182

174-
const test = await import(`./cases/${name}/test.js`);
175-
176183
const enableFeatures = test.enableFeatures || ['http'];
177184
const disableFeatures =
178185
test.disableFeatures ||
@@ -229,14 +236,14 @@ suite('Bindings', () => {
229236

230237
await writeFile(
231238
new URL(`./output/${name}.component.wasm`, import.meta.url),
232-
component
239+
component,
233240
);
234241

235242
for (const file of Object.keys(files)) {
236243
let source = files[file];
237244
await writeFile(
238245
new URL(`./output/${name}/${file}`, import.meta.url),
239-
source
246+
source,
240247
);
241248
}
242249

test/util.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { createServer } from 'node:net';
2+
3+
// Utility function for getting a random port
4+
export async function getRandomPort() {
5+
return await new Promise((resolve) => {
6+
const server = createServer();
7+
server.listen(0, function () {
8+
const port = this.address().port;
9+
server.on('close', () => resolve(port));
10+
server.close();
11+
});
12+
});
13+
}

test/wit/test1.wit

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,10 @@ world test2 {
6464

6565
export get-result: func() -> string;
6666
}
67+
68+
world test3 {
69+
import wasi:cli/environment@0.2.3;
70+
import wasi:http/types@0.2.3;
71+
72+
export wasi:http/incoming-handler@0.2.3;
73+
}

0 commit comments

Comments
 (0)