This repository has been archived by the owner on Jun 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
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
Refactor integration tests to make them more independent and simplified
- Loading branch information
Showing
47 changed files
with
248 additions
and
11,927 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
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
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 @@ | ||
[package] | ||
name = "test-programs" | ||
version = "0.0.0" | ||
edition = "2021" | ||
publish = false | ||
|
||
[dependencies] | ||
wasi-http-client = { path = "../../../wasi-http-client", features = ["json"] } | ||
serde = { version = "1.0.201", features = ["derive"] } |
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,11 @@ | ||
[package] | ||
name = "test-programs-artifacts" | ||
version = "0.0.0" | ||
edition = "2021" | ||
publish = false | ||
|
||
[build-dependencies] | ||
anyhow = "1.0.86" | ||
cargo_metadata = "0.18.1" | ||
wit-component = "0.208.1" | ||
heck = "0.5.0" |
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,69 @@ | ||
use anyhow::Result; | ||
use cargo_metadata::MetadataCommand; | ||
use heck::*; | ||
use std::path::{Path, PathBuf}; | ||
use std::process::Command; | ||
use std::{env, fs}; | ||
use wit_component::ComponentEncoder; | ||
|
||
fn main() -> Result<()> { | ||
let out_dir = PathBuf::from(env::var_os("OUT_DIR").unwrap()); | ||
|
||
println!("cargo::rerun-if-changed=../src"); | ||
|
||
let status = Command::new("cargo") | ||
.arg("build") | ||
.arg("--package=test-programs") | ||
.arg("--target=wasm32-wasi") | ||
.env("CARGO_TARGET_DIR", &out_dir) | ||
.env("CARGO_PROFILE_DEV_DEBUG", "1") | ||
.status()?; | ||
assert!(status.success()); | ||
|
||
let meta = MetadataCommand::new().exec()?; | ||
let targets = meta | ||
.packages | ||
.iter() | ||
.find(|p| p.name == "test-programs") | ||
.unwrap() | ||
.targets | ||
.iter() | ||
.filter(|t| t.kind == ["bin"]) | ||
.map(|t| &t.name) | ||
.collect::<Vec<_>>(); | ||
|
||
let mut generated_code = String::new(); | ||
|
||
for target in targets { | ||
let camel = target.to_shouty_snake_case(); | ||
let wasm = out_dir | ||
.join("wasm32-wasi") | ||
.join("debug") | ||
.join(format!("{target}.wasm")); | ||
|
||
let path = compile_component(&wasm)?; | ||
generated_code += &format!("pub const {camel}_COMPONENT: &str = {path:?};\n"); | ||
} | ||
|
||
fs::write(out_dir.join("gen.rs"), generated_code)?; | ||
|
||
Ok(()) | ||
} | ||
|
||
// Compile a component, return the path of the binary | ||
fn compile_component(wasm: &Path) -> Result<PathBuf> { | ||
let module = fs::read(wasm)?; | ||
let component = ComponentEncoder::default() | ||
.module(module.as_slice())? | ||
.validate(true) | ||
.adapter( | ||
"wasi_snapshot_preview1", | ||
include_bytes!("wasi_snapshot_preview1.command.wasm"), | ||
)? | ||
.encode()?; | ||
let out_dir = wasm.parent().unwrap(); | ||
let stem = wasm.file_stem().unwrap().to_str().unwrap(); | ||
let component_path = out_dir.join(format!("{stem}.component.wasm")); | ||
fs::write(&component_path, component)?; | ||
Ok(component_path) | ||
} |
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 @@ | ||
include!(concat!(env!("OUT_DIR"), "/gen.rs")); |
Binary file not shown.
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 wasi_http_client::Client; | ||
|
||
fn main() { | ||
let resp = Client::new() | ||
.get("https://httpbin.org/range/20") | ||
.query(&[("duration", "5"), ("chunk_size", "10")]) | ||
.send() | ||
.unwrap(); | ||
assert_eq!(resp.status(), 200); | ||
|
||
while let Some(chunk) = resp.chunk(1024).unwrap() { | ||
assert_eq!(String::from_utf8(chunk).unwrap().len(), 10); | ||
} | ||
} |
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 serde::Deserialize; | ||
use wasi_http_client::Client; | ||
|
||
#[derive(Deserialize)] | ||
struct Data { | ||
url: String, | ||
} | ||
|
||
fn main() { | ||
let resp = Client::new().get("https://httpbin.org/get").send().unwrap(); | ||
let status = resp.status(); | ||
assert_eq!(status, 200); | ||
|
||
let json_data = resp.json::<Data>().unwrap(); | ||
assert_eq!(json_data.url, "https://httpbin.org/get") | ||
} |
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,10 @@ | ||
use wasi_http_client::Client; | ||
|
||
fn main() { | ||
let resp = Client::new() | ||
.get("https://httpbin.org/get?a=b") | ||
.headers([("Content-Type", "application/json"), ("Accept", "*/*")]) | ||
.send() | ||
.unwrap(); | ||
assert_eq!(resp.status(), 200); | ||
} |
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,12 @@ | ||
use std::time::Duration; | ||
use wasi_http_client::Client; | ||
|
||
fn main() { | ||
let resp = Client::new() | ||
.post("https://httpbin.org/post") | ||
.form(&[("a", "b"), ("c", "")]) | ||
.connect_timeout(Duration::from_secs(5)) | ||
.send() | ||
.unwrap(); | ||
assert_eq!(resp.status(), 200); | ||
} |
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 std::time::Duration; | ||
use wasi_http_client::Client; | ||
|
||
fn main() { | ||
let resp = Client::new() | ||
.post("https://httpbin.org/post") | ||
.json(&HashMap::from([("data", "hello")])) | ||
.connect_timeout(Duration::from_secs(5)) | ||
.send() | ||
.unwrap(); | ||
assert_eq!(resp.status(), 200); | ||
} |
25 changes: 25 additions & 0 deletions
25
crates/test-programs/src/bin/post_with_multipart_form_data.rs
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,25 @@ | ||
use std::time::Duration; | ||
use wasi_http_client::Client; | ||
|
||
fn main() { | ||
let resp = Client::new() | ||
.post("https://httpbin.org/post") | ||
.header("Content-Type", "multipart/form-data; boundary=boundary") | ||
.body( | ||
"--boundary | ||
Content-Disposition: form-data; name=field1 | ||
value1 | ||
--boundary | ||
Content-Disposition: form-data; name=field2; filename=file.txt | ||
Content-Type: text/plain | ||
hello | ||
--boundary--" | ||
.as_bytes(), | ||
) | ||
.connect_timeout(Duration::from_secs(5)) | ||
.send() | ||
.unwrap(); | ||
assert_eq!(resp.status(), 200); | ||
} |
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,22 +1,67 @@ | ||
use anyhow::Result; | ||
use std::process::Command; | ||
|
||
fn wasmtime() -> Command { | ||
let mut wasmtime = Command::new("wasmtime"); | ||
wasmtime.arg("-S").arg("http"); | ||
wasmtime | ||
} | ||
|
||
#[test] | ||
fn get_chunk() { | ||
let mut cmd = wasmtime(); | ||
let status = cmd | ||
.arg(test_programs_artifacts::GET_CHUNK_COMPONENT) | ||
.status() | ||
.unwrap(); | ||
assert!(status.success()); | ||
} | ||
|
||
#[test] | ||
fn main() -> Result<()> { | ||
let status = Command::new("cargo") | ||
.current_dir("tests/program") | ||
.arg("component") | ||
.arg("build") | ||
.arg("--quiet") | ||
.status()?; | ||
fn get_with_json_response() { | ||
let mut cmd = wasmtime(); | ||
let status = cmd | ||
.arg(test_programs_artifacts::GET_WITH_JSON_RESPONSE_COMPONENT) | ||
.status() | ||
.unwrap(); | ||
assert!(status.success()); | ||
} | ||
|
||
let status = Command::new("wasmtime") | ||
.arg("-S") | ||
.arg("http") | ||
.arg("tests/program/target/wasm32-wasi/debug/wasi_http_client_test_program.wasm") | ||
.status()?; | ||
#[test] | ||
fn get_with_query() { | ||
let mut cmd = wasmtime(); | ||
let status = cmd | ||
.arg(test_programs_artifacts::GET_WITH_QUERY_COMPONENT) | ||
.status() | ||
.unwrap(); | ||
assert!(status.success()); | ||
} | ||
|
||
Ok(()) | ||
#[test] | ||
fn post_with_form_data() { | ||
let mut cmd = wasmtime(); | ||
let status = cmd | ||
.arg(test_programs_artifacts::POST_WITH_FORM_DATA_COMPONENT) | ||
.status() | ||
.unwrap(); | ||
assert!(status.success()); | ||
} | ||
|
||
#[test] | ||
fn post_with_json_data() { | ||
let mut cmd = wasmtime(); | ||
let status = cmd | ||
.arg(test_programs_artifacts::POST_WITH_JSON_DATA_COMPONENT) | ||
.status() | ||
.unwrap(); | ||
assert!(status.success()); | ||
} | ||
|
||
#[test] | ||
fn post_with_multipart_form_data() { | ||
let mut cmd = wasmtime(); | ||
let status = cmd | ||
.arg(test_programs_artifacts::POST_WITH_MULTIPART_FORM_DATA_COMPONENT) | ||
.status() | ||
.unwrap(); | ||
assert!(status.success()); | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.