-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from wacker-dev/tests
Add more tests
- Loading branch information
Showing
7 changed files
with
147 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() {} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
} |