Skip to content

Commit 0b4518d

Browse files
committed
http test runs get from postman-echo
1 parent b43c699 commit 0b4518d

File tree

5 files changed

+58
-8
lines changed

5 files changed

+58
-8
lines changed

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ wasi.workspace = true
2222

2323
[dev-dependencies]
2424
anyhow.workspace = true
25+
serde_json.workspace = true
2526
test-program-suite.workspace = true
2627
wasmtime.workspace = true
2728
wasmtime-wasi.workspace = true
@@ -42,6 +43,7 @@ license = "MIT OR Apache-2.0 OR Apache-2.0 WITH LLVM-exception"
4243
anyhow = "1"
4344
cargo_metadata = "0.18.1"
4445
heck = "0.5"
46+
serde_json = "1"
4547
slab = "0.4.9"
4648
test-programs = { path = "test-programs" }
4749
test-program-suite = { path = "test-programs/suite" }

examples/http_get.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
use std::error::Error;
2+
use wstd::http::{Client, Method, Request, Url};
3+
use wstd::io::AsyncRead;
4+
use wstd::runtime::block_on;
5+
6+
fn main() -> Result<(), Box<dyn Error>> {
7+
block_on(async move {
8+
let request = Request::new(Method::Get, Url::parse("https://postman-echo.com/get")?);
9+
let mut response = Client::new().send(request).await?;
10+
11+
let content_type = response
12+
.headers()
13+
.get(&"content-type".into())
14+
.ok_or_else(|| "response expected to have content-type header")?;
15+
assert_eq!(content_type.len(), 1, "one header value for content-type");
16+
assert_eq!(content_type[0], b"application/json; charset=utf-8");
17+
18+
// Would much prefer read_to_end here:
19+
let mut body_buf = vec![0; 4096];
20+
let body_len = response.body().read(&mut body_buf).await?;
21+
body_buf.truncate(body_len);
22+
23+
let val: serde_json::Value = serde_json::from_slice(&body_buf)?;
24+
let body_url = val
25+
.get("url")
26+
.ok_or_else(|| "body json has url")?
27+
.as_str()
28+
.ok_or_else(|| "body json url is str")?;
29+
assert!(
30+
body_url.contains("postman-echo.com/get"),
31+
"expected body url to contain the authority and path, got: {body_url}"
32+
);
33+
34+
Ok(())
35+
})
36+
}

test-programs/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ edition = "2021"
55
publish = false
66

77
[dependencies]
8+
serde_json.workspace = true
89
wstd.workspace = true

test-programs/src/bin/http_get.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
include!("../../../examples/http_get.rs");

tests/test-programs.rs

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use wasmtime::{
33
component::{Component, Linker, ResourceTable},
44
Config, Engine, Store,
55
};
6-
use wasmtime_wasi::{StdoutStream, WasiCtx, WasiView};
6+
use wasmtime_wasi::{pipe::MemoryOutputPipe, WasiCtx, WasiView};
77
use wasmtime_wasi_http::{WasiHttpCtx, WasiHttpView};
88

99
struct Ctx {
@@ -30,7 +30,7 @@ impl WasiHttpView for Ctx {
3030
}
3131
}
3232

33-
fn run_in_wasmtime(wasm: &[u8], stdout: impl StdoutStream + 'static) -> Result<()> {
33+
fn run_in_wasmtime(wasm: &[u8], stdout: Option<MemoryOutputPipe>) -> Result<()> {
3434
let config = Config::default();
3535
let engine = Engine::new(&config).context("creating engine")?;
3636
let component = Component::new(&engine, wasm).context("loading component")?;
@@ -40,15 +40,17 @@ fn run_in_wasmtime(wasm: &[u8], stdout: impl StdoutStream + 'static) -> Result<(
4040
wasmtime_wasi_http::add_only_http_to_linker_sync(&mut linker)
4141
.context("add wasi-http to linker")?;
4242

43+
let mut builder = WasiCtx::builder();
44+
builder.inherit_stderr().inherit_network();
45+
let wasi = match stdout {
46+
Some(stdout) => builder.stdout(stdout).build(),
47+
None => builder.inherit_stdout().build(),
48+
};
4349
let mut store = Store::new(
4450
&engine,
4551
Ctx {
4652
table: ResourceTable::new(),
47-
wasi: WasiCtx::builder()
48-
.stdout(stdout)
49-
.inherit_stderr()
50-
.inherit_network()
51-
.build(),
53+
wasi,
5254
http: WasiHttpCtx::new(),
5355
},
5456
);
@@ -84,7 +86,7 @@ fn tcp_echo_server() -> Result<()> {
8486

8587
let pipe = wasmtime_wasi::pipe::MemoryOutputPipe::new(1024 * 1024);
8688
let write_end = pipe.clone();
87-
let wasmtime_thread = std::thread::spawn(move || run_in_wasmtime(&wasm, write_end));
89+
let wasmtime_thread = std::thread::spawn(move || run_in_wasmtime(&wasm, Some(write_end)));
8890

8991
'wait: loop {
9092
sleep(Duration::from_millis(100));
@@ -117,3 +119,11 @@ fn tcp_echo_server() -> Result<()> {
117119
}
118120
Ok(())
119121
}
122+
123+
#[test]
124+
fn http_get() -> Result<()> {
125+
println!("testing {}", test_program_suite::HTTP_GET);
126+
let wasm = std::fs::read(test_program_suite::HTTP_GET).context("read wasm")?;
127+
128+
run_in_wasmtime(&wasm, None)
129+
}

0 commit comments

Comments
 (0)