Skip to content

Commit

Permalink
Merge pull request #11 from wacker-dev/tests
Browse files Browse the repository at this point in the history
Add more tests
  • Loading branch information
iawia002 authored Jun 17, 2024
2 parents f61a926 + 84e35ed commit 6a95ec8
Show file tree
Hide file tree
Showing 7 changed files with 147 additions and 30 deletions.
16 changes: 16 additions & 0 deletions test-programs/src/bin/server_form.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
use waki::{handler, ErrorCode, Request, Response};

#[handler]
fn hello(req: Request) -> Result<Response, ErrorCode> {
let form = req.form().unwrap();
Response::builder()
.body(format!(
"{} {}",
form.get("key1").unwrap(),
form.get("key2").unwrap()
))
.build()
}

// required since this file is built as a `bin`
fn main() {}
19 changes: 0 additions & 19 deletions test-programs/src/bin/server_hello.rs

This file was deleted.

13 changes: 13 additions & 0 deletions test-programs/src/bin/server_json.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
use std::collections::HashMap;
use waki::{handler, ErrorCode, Request, Response};

#[handler]
fn hello(req: Request) -> Result<Response, ErrorCode> {
let json = req.json::<HashMap<String, String>>().unwrap();
Response::builder()
.body(json.get("data").unwrap().to_string())
.build()
}

// required since this file is built as a `bin`
fn main() {}
14 changes: 14 additions & 0 deletions test-programs/src/bin/server_multipart_form.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
use waki::{handler, ErrorCode, Request, Response};

#[handler]
fn hello(req: Request) -> Result<Response, ErrorCode> {
let form = req.multipart().unwrap();
let form_part = form.get("form").unwrap();
let file_part = form.get("file").unwrap();
Response::builder()
.body([form_part.value.as_slice(), file_part.value.as_slice()].concat())
.build()
}

// required since this file is built as a `bin`
fn main() {}
15 changes: 15 additions & 0 deletions test-programs/src/bin/server_query.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
use waki::{handler, ErrorCode, Request, Response};

#[handler]
fn hello(req: Request) -> Result<Response, ErrorCode> {
let query = req.query();
Response::builder()
.body(format!(
"Hello, {}!",
query.get("name").unwrap_or(&"WASI".to_string())
))
.build()
}

// required since this file is built as a `bin`
fn main() {}
9 changes: 9 additions & 0 deletions test-programs/src/bin/server_status_code.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
use waki::{handler, ErrorCode, Request, Response};

#[handler]
fn hello(_: Request) -> Result<Response, ErrorCode> {
Response::builder().status_code(400).build()
}

// required since this file is built as a `bin`
fn main() {}
91 changes: 80 additions & 11 deletions waki/tests/all/server.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,90 @@
use super::run_wasi_http;

use anyhow::Result;
use wasmtime_wasi_http::body::HyperIncomingBody;

#[tokio::test(flavor = "multi_thread")]
async fn form() -> Result<()> {
let req = hyper::Request::builder()
.uri("http://localhost")
.body(body::full("key1=Hello&key2=World"))?;

let resp = run_wasi_http(test_programs_artifacts::SERVER_FORM_COMPONENT, req).await??;
let body = resp.into_body().to_bytes();
let body = std::str::from_utf8(&body)?;
assert_eq!(body, "Hello World");

Ok(())
}

#[tokio::test(flavor = "multi_thread")]
async fn json() -> Result<()> {
let req = hyper::Request::builder()
.uri("http://localhost")
.body(body::full("{\"data\": \"Hello World\"}"))?;

let resp = run_wasi_http(test_programs_artifacts::SERVER_JSON_COMPONENT, req).await??;
let body = resp.into_body().to_bytes();
let body = std::str::from_utf8(&body)?;
assert_eq!(body, "Hello World");

Ok(())
}

#[tokio::test(flavor = "multi_thread")]
async fn multipart_form() -> Result<()> {
let req = hyper::Request::builder()
.uri("http://localhost")
.header("Content-Type", "multipart/form-data; boundary=boundary")
.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--"))?;

let resp = run_wasi_http(
test_programs_artifacts::SERVER_MULTIPART_FORM_COMPONENT,
req,
)
.await??;
let body = resp.into_body().to_bytes();
let body = std::str::from_utf8(&body)?;
assert_eq!(body, "Hello World");

Ok(())
}

#[tokio::test(flavor = "multi_thread")]
async fn query() -> Result<()> {
let req = hyper::Request::builder()
.uri("http://localhost?name=ia")
.body(HyperIncomingBody::default())?;

match run_wasi_http(test_programs_artifacts::SERVER_HELLO_COMPONENT, req).await? {
Ok(resp) => {
let body = resp.into_body().to_bytes();
let body = std::str::from_utf8(&body)?;
assert_eq!(body, "Hello, ia!")
}
Err(e) => panic!("Error given in response: {e:?}"),
};
.body(body::empty())?;

let resp = run_wasi_http(test_programs_artifacts::SERVER_QUERY_COMPONENT, req).await??;
let body = resp.into_body().to_bytes();
let body = std::str::from_utf8(&body)?;
assert_eq!(body, "Hello, ia!");

Ok(())
}

#[tokio::test(flavor = "multi_thread")]
async fn status_code() -> Result<()> {
let req = hyper::Request::builder()
.uri("http://localhost")
.body(body::empty())?;

let resp = run_wasi_http(test_programs_artifacts::SERVER_STATUS_CODE_COMPONENT, req).await??;
assert_eq!(resp.status(), 400);

Ok(())
}

mod body {
use http_body_util::{BodyExt, Full};
use hyper::body::Bytes;
use wasmtime_wasi_http::body::HyperIncomingBody;

pub fn full(bytes: &'static str) -> HyperIncomingBody {
HyperIncomingBody::new(Full::new(Bytes::from(bytes)).map_err(|_| unreachable!()))
}

pub fn empty() -> HyperIncomingBody {
HyperIncomingBody::default()
}
}

0 comments on commit 6a95ec8

Please sign in to comment.