Skip to content

Commit aa1a00e

Browse files
committed
Add instructions for testing the endpoints.
This also removes echo-trailers endpoint for now as it is not trivially supported by StarlingMonkey so it will always return empty headers.
1 parent b1b0d61 commit aa1a00e

File tree

3 files changed

+27
-32
lines changed

3 files changed

+27
-32
lines changed

README.md

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,43 +14,58 @@ The following HTTP routes are available from the component:
1414
/sleep # Sleep for {ms} milliseconds
1515
/echo # Echo the HTTP body
1616
/echo-headers # Echo the HTTP headers
17-
/echo-trailers # Echo the HTTP trailers
18-
/upload # Echo uploaded blob
17+
/upload # Echo uploaded blob
18+
```
19+
20+
Testing routes:
21+
22+
```bash
23+
# Hello world
24+
$ curl localhost:8080
25+
# Sleep for {ms} milliseconds
26+
$ curl localhost:8080/sleep/2000
27+
# Echo the HTTP body
28+
$ curl -d "Test echo body" localhost:8080/echo
29+
# Echo the HTTP headers
30+
$ curl -H "X-Test-Header: 123" localhost:8080/echo-headers
31+
# Echo uploaded blob
32+
$ echo "Hello World!" > test_file.txt
33+
$ curl -H "Content-Type: text/plain" --data-binary @test_file.txt http://localhost:8080/upload
1934
```
2035

2136
## Quick Start
22-
The project uses [`Wasmtime`][wasmtime] as its runtime. However, if needed, it
23-
can easily be adjusted to use [`jco`][jco] instead. For `wasmtime` installation,
37+
The project uses [`Wasmtime`][wasmtime] as its runtime. However, if needed, it
38+
can easily be adjusted to use [`jco`][jco] instead. For `wasmtime` installation,
2439
simply run:
2540

2641
```bash
2742
$ curl https://wasmtime.dev/install.sh -sSf | bash
2843
```
2944

30-
The quickest way to start is by using [`just`][just].
45+
The quickest way to start is by using [`just`][just].
3146
```bash
3247
$ just serve # to build and serve the wasm component on `localhost:8080`
33-
$ curl 127.0.0.1:8080 # to send requests to component.
48+
$ curl localhost:8080 # to send requests to component.
3449
```
3550

3651
Alternatively, run:
3752

3853
```bash
3954
$ npm install
40-
$ npm build
55+
$ npm run build
4156
$ wasmtime serve -S common dist/server.component.wasm
4257
```
4358

4459
## See Also
4560

46-
- [sample-wasi-http-rust](https://github.com/bytecodealliance/sample-wasi-http-rust) An example `wasi:http` server component written in Rust.
61+
- [sample-wasi-http-rust][rust-example] An example `wasi:http` server component written in Rust.
4762

4863
## License
4964

5065
Apache-2.0 with LLVM Exception
5166

5267
[jco]: https://github.com/bytecodealliance/jco
5368
[just]: https://github.com/casey/just
54-
[rust-sample]: https://github.com/bytecodealliance/sample-wasi-http-rust
69+
[rust-sample]: https://github.com/bytecodealliance/sample-wasi-http-rust
5570
[wasi-http]: https://github.com/WebAssembly/wasi-http
56-
[wasmtime]: https://wasmtime.dev/
71+
[wasmtime]: https://wasmtime.dev/

src/endpoints/echo.js

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export { echo, echoHeaders, echoTrailers };
1+
export { echo, echoHeaders };
22

33
// Echo the body of the request in the response
44
const echo = async (req) => {
@@ -22,22 +22,3 @@ const echoHeaders = (req) => {
2222
},
2323
});
2424
};
25-
26-
// Echo all trailers from the request in JSON format
27-
const echoTrailers = async (req) => {
28-
let trailers;
29-
try {
30-
trailers = req.trailers ? await req.trailers : new Headers();
31-
} catch (e) {
32-
console.warning("Error fetching trailers:", e);
33-
trailers = new Headers();
34-
}
35-
36-
const trailersObj = Object.fromEntries(trailers.entries());
37-
return new Response(JSON.stringify(trailersObj, null, 2), {
38-
status: 200,
39-
headers: {
40-
"Content-Type": "application/json",
41-
},
42-
});
43-
};

src/server.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,13 @@ import { AutoRouter } from "itty-router";
33

44
import { sleep } from "./endpoints/sleep.js";
55
import { upload } from "./endpoints/upload.js";
6-
import { echo, echoHeaders, echoTrailers } from "./endpoints/echo.js";
6+
import { echo, echoHeaders } from "./endpoints/echo.js";
77

88
let router = AutoRouter();
99

1010
router
1111
.get("/", () => new Response("Hello, World!"))
1212
.get("/echo-headers", (req) => echoHeaders(req))
13-
.get("/echo-trailers", (req) => echoTrailers(req))
1413
.get("/sleep/:ms", async ({ ms }) => await sleep(ms))
1514
.post("/echo", (req) => echo(req))
1615
.post("/upload", async (req) => await upload(req));

0 commit comments

Comments
 (0)