Skip to content

Commit b96b482

Browse files
committed
Add new HTTP end-to-end test
Had to update wit-bindgen to git to fix bytecodealliance/wit-bindgen#1547.
1 parent 2aa833b commit b96b482

5 files changed

Lines changed: 93 additions & 10 deletions

File tree

tests/rust/wasm32-wasip3/Cargo.lock

Lines changed: 5 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/rust/wasm32-wasip3/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@ version = "0.1.0"
44
edition = "2024"
55

66
[dependencies]
7-
wit-bindgen = "0.53.1"
7+
wit-bindgen = { git = "https://github.com/bytecodealliance/wit-bindgen.git", rev = "85d10eb33a54191220d34215bbcc4e29cb959884", features = ["async-spawn"] }
88
futures = "0.3.31"
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"proposals": ["http", "http/service"],
3+
"operations": [
4+
{ "type": "run" },
5+
{ "type": "request",
6+
"method": "GET",
7+
"path": "/",
8+
"response": { "status": 200,
9+
"headers": { "content-type": "text/plain" },
10+
"body": "hey\n" } },
11+
{ "type": "request",
12+
"method": "GET",
13+
"path": "/",
14+
"response": { "status": 200,
15+
"headers": { "content-type": "text/plain" },
16+
"body": "hey\n" } },
17+
{ "type": "request",
18+
"method": "GET",
19+
"path": "/whatever",
20+
"response": { "status": 404 } },
21+
{ "type": "request",
22+
"method": "POST",
23+
"path": "/whatever",
24+
"response": { "status": 405 } },
25+
{ "type": "kill", "signal": "SIGINT" },
26+
{ "type": "wait" }
27+
]
28+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
use test_wasm32_wasip3::http::{export, exports::wasi::http::handler::Guest};
2+
use test_wasm32_wasip3::http::{
3+
wasi::http::types::{ErrorCode, Fields, Method, Request, Response, StatusCode},
4+
wit_future, wit_stream
5+
};
6+
7+
fn handle_root(headers: &Fields) -> (StatusCode, Option<Vec<u8>>) {
8+
headers.append("content-type", b"text/plain").unwrap();
9+
(200, Some(b"hey\n".to_vec()))
10+
}
11+
12+
fn handle_not_found(_headers: &Fields) -> (StatusCode, Option<Vec<u8>>) {
13+
(404, None)
14+
}
15+
16+
fn handle_method_not_allowed(_headers: &Fields) -> (StatusCode, Option<Vec<u8>>) {
17+
(405, None)
18+
}
19+
20+
struct Component;
21+
export!(Component);
22+
23+
impl Guest for Component {
24+
async fn handle(request: Request) -> Result<Response, ErrorCode> {
25+
let headers = Fields::new();
26+
let (status, payload) = match (request.get_method(), request.get_path_with_query()) {
27+
(Method::Get, Some(s)) if s == "/" => handle_root(&headers),
28+
(Method::Get, _) => handle_not_found(&headers),
29+
(_, _) => handle_method_not_allowed(&headers),
30+
};
31+
let (_, result_rx) = wit_future::new(|| Ok(()));
32+
let (body_rx, trailers) = Request::consume_body(request, result_rx);
33+
assert!(body_rx.collect().await.is_empty());
34+
assert!(trailers.await.unwrap().is_none());
35+
36+
let (trailers_tx, trailers_rx) = wit_future::new(|| Ok(None));
37+
drop(trailers_tx);
38+
let response_body = payload.map(|bytes| {
39+
headers
40+
.append("content-length", &bytes.len().to_string().into_bytes())
41+
.unwrap();
42+
let (mut body_tx, body_rx) = wit_stream::new();
43+
wit_bindgen::spawn(async move {
44+
let remaining = body_tx.write_all(bytes).await;
45+
assert!(remaining.is_empty());
46+
drop(body_tx);
47+
});
48+
body_rx
49+
});
50+
let (response, _sent) = Response::new(headers, response_body, trailers_rx);
51+
response.set_status_code(status).unwrap();
52+
Ok(response)
53+
}
54+
}
55+
56+
fn main() {
57+
unreachable!("main is a stub");
58+
}

tests/rust/wasm32-wasip3/src/http.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ wit_bindgen::generate!({
33
package wasi-testsuite:test;
44
55
world http-test {
6-
import wasi:http/types@0.3.0-rc-2026-02-09;
6+
include wasi:http/service@0.3.0-rc-2026-02-09;
77
}
88
",
99
additional_derives: [PartialEq, Eq, Hash, Clone],

0 commit comments

Comments
 (0)