Skip to content

Commit 84e35ed

Browse files
committed
Add more tests
1 parent f61a926 commit 84e35ed

File tree

7 files changed

+147
-30
lines changed

7 files changed

+147
-30
lines changed

test-programs/src/bin/server_form.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
use waki::{handler, ErrorCode, Request, Response};
2+
3+
#[handler]
4+
fn hello(req: Request) -> Result<Response, ErrorCode> {
5+
let form = req.form().unwrap();
6+
Response::builder()
7+
.body(format!(
8+
"{} {}",
9+
form.get("key1").unwrap(),
10+
form.get("key2").unwrap()
11+
))
12+
.build()
13+
}
14+
15+
// required since this file is built as a `bin`
16+
fn main() {}

test-programs/src/bin/server_hello.rs

Lines changed: 0 additions & 19 deletions
This file was deleted.

test-programs/src/bin/server_json.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
use std::collections::HashMap;
2+
use waki::{handler, ErrorCode, Request, Response};
3+
4+
#[handler]
5+
fn hello(req: Request) -> Result<Response, ErrorCode> {
6+
let json = req.json::<HashMap<String, String>>().unwrap();
7+
Response::builder()
8+
.body(json.get("data").unwrap().to_string())
9+
.build()
10+
}
11+
12+
// required since this file is built as a `bin`
13+
fn main() {}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
use waki::{handler, ErrorCode, Request, Response};
2+
3+
#[handler]
4+
fn hello(req: Request) -> Result<Response, ErrorCode> {
5+
let form = req.multipart().unwrap();
6+
let form_part = form.get("form").unwrap();
7+
let file_part = form.get("file").unwrap();
8+
Response::builder()
9+
.body([form_part.value.as_slice(), file_part.value.as_slice()].concat())
10+
.build()
11+
}
12+
13+
// required since this file is built as a `bin`
14+
fn main() {}

test-programs/src/bin/server_query.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
use waki::{handler, ErrorCode, Request, Response};
2+
3+
#[handler]
4+
fn hello(req: Request) -> Result<Response, ErrorCode> {
5+
let query = req.query();
6+
Response::builder()
7+
.body(format!(
8+
"Hello, {}!",
9+
query.get("name").unwrap_or(&"WASI".to_string())
10+
))
11+
.build()
12+
}
13+
14+
// required since this file is built as a `bin`
15+
fn main() {}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
use waki::{handler, ErrorCode, Request, Response};
2+
3+
#[handler]
4+
fn hello(_: Request) -> Result<Response, ErrorCode> {
5+
Response::builder().status_code(400).build()
6+
}
7+
8+
// required since this file is built as a `bin`
9+
fn main() {}

waki/tests/all/server.rs

Lines changed: 80 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,90 @@
11
use super::run_wasi_http;
22

33
use anyhow::Result;
4-
use wasmtime_wasi_http::body::HyperIncomingBody;
4+
5+
#[tokio::test(flavor = "multi_thread")]
6+
async fn form() -> Result<()> {
7+
let req = hyper::Request::builder()
8+
.uri("http://localhost")
9+
.body(body::full("key1=Hello&key2=World"))?;
10+
11+
let resp = run_wasi_http(test_programs_artifacts::SERVER_FORM_COMPONENT, req).await??;
12+
let body = resp.into_body().to_bytes();
13+
let body = std::str::from_utf8(&body)?;
14+
assert_eq!(body, "Hello World");
15+
16+
Ok(())
17+
}
18+
19+
#[tokio::test(flavor = "multi_thread")]
20+
async fn json() -> Result<()> {
21+
let req = hyper::Request::builder()
22+
.uri("http://localhost")
23+
.body(body::full("{\"data\": \"Hello World\"}"))?;
24+
25+
let resp = run_wasi_http(test_programs_artifacts::SERVER_JSON_COMPONENT, req).await??;
26+
let body = resp.into_body().to_bytes();
27+
let body = std::str::from_utf8(&body)?;
28+
assert_eq!(body, "Hello World");
29+
30+
Ok(())
31+
}
32+
33+
#[tokio::test(flavor = "multi_thread")]
34+
async fn multipart_form() -> Result<()> {
35+
let req = hyper::Request::builder()
36+
.uri("http://localhost")
37+
.header("Content-Type", "multipart/form-data; boundary=boundary")
38+
.body(body::full("--boundary\r\nContent-Disposition: form-data; name=form\r\n\r\nHello \r\n--boundary\r\nContent-Disposition: form-data; name=file; filename=file.txt\r\nContent-Type: text/plain\r\n\r\nWorld\r\n--boundary--"))?;
39+
40+
let resp = run_wasi_http(
41+
test_programs_artifacts::SERVER_MULTIPART_FORM_COMPONENT,
42+
req,
43+
)
44+
.await??;
45+
let body = resp.into_body().to_bytes();
46+
let body = std::str::from_utf8(&body)?;
47+
assert_eq!(body, "Hello World");
48+
49+
Ok(())
50+
}
551

652
#[tokio::test(flavor = "multi_thread")]
753
async fn query() -> Result<()> {
854
let req = hyper::Request::builder()
955
.uri("http://localhost?name=ia")
10-
.body(HyperIncomingBody::default())?;
11-
12-
match run_wasi_http(test_programs_artifacts::SERVER_HELLO_COMPONENT, req).await? {
13-
Ok(resp) => {
14-
let body = resp.into_body().to_bytes();
15-
let body = std::str::from_utf8(&body)?;
16-
assert_eq!(body, "Hello, ia!")
17-
}
18-
Err(e) => panic!("Error given in response: {e:?}"),
19-
};
56+
.body(body::empty())?;
57+
58+
let resp = run_wasi_http(test_programs_artifacts::SERVER_QUERY_COMPONENT, req).await??;
59+
let body = resp.into_body().to_bytes();
60+
let body = std::str::from_utf8(&body)?;
61+
assert_eq!(body, "Hello, ia!");
62+
2063
Ok(())
2164
}
65+
66+
#[tokio::test(flavor = "multi_thread")]
67+
async fn status_code() -> Result<()> {
68+
let req = hyper::Request::builder()
69+
.uri("http://localhost")
70+
.body(body::empty())?;
71+
72+
let resp = run_wasi_http(test_programs_artifacts::SERVER_STATUS_CODE_COMPONENT, req).await??;
73+
assert_eq!(resp.status(), 400);
74+
75+
Ok(())
76+
}
77+
78+
mod body {
79+
use http_body_util::{BodyExt, Full};
80+
use hyper::body::Bytes;
81+
use wasmtime_wasi_http::body::HyperIncomingBody;
82+
83+
pub fn full(bytes: &'static str) -> HyperIncomingBody {
84+
HyperIncomingBody::new(Full::new(Bytes::from(bytes)).map_err(|_| unreachable!()))
85+
}
86+
87+
pub fn empty() -> HyperIncomingBody {
88+
HyperIncomingBody::default()
89+
}
90+
}

0 commit comments

Comments
 (0)