Skip to content
This repository has been archived by the owner on Jun 7, 2024. It is now read-only.

Commit

Permalink
Merge pull request #11 from wacker-dev/tests
Browse files Browse the repository at this point in the history
Refactor integration tests to make them more independent and simplified
  • Loading branch information
iawia002 authored May 22, 2024
2 parents 4bcc958 + d9c1e48 commit cf49a49
Show file tree
Hide file tree
Showing 47 changed files with 248 additions and 11,927 deletions.
5 changes: 2 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ jobs:
- name: Install Rust
uses: dtolnay/rust-toolchain@1.78.0
with:
targets: wasm32-wasi
components: clippy, rustfmt
- name: cargo fmt
run: cargo fmt --all -- --check
Expand All @@ -26,6 +27,4 @@ jobs:
github_token: ${{ secrets.GITHUB_TOKEN }}
version: "v20.0.0"
- name: cargo test
run: |
cargo install cargo-component@0.11.0
cargo test
run: cargo test
7 changes: 7 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,10 @@ serde_urlencoded = "0.7.1"

[features]
json = ["dep:serde_json"]

[dev-dependencies]
test-programs-artifacts = { path = "crates/test-programs/artifacts" }

[workspace]
resolver = "2"
members = ["crates/test-programs"]
9 changes: 9 additions & 0 deletions crates/test-programs/Cargo.toml
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"] }
11 changes: 11 additions & 0 deletions crates/test-programs/artifacts/Cargo.toml
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"
69 changes: 69 additions & 0 deletions crates/test-programs/artifacts/build.rs
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)
}
1 change: 1 addition & 0 deletions crates/test-programs/artifacts/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include!(concat!(env!("OUT_DIR"), "/gen.rs"));
Binary file not shown.
14 changes: 14 additions & 0 deletions crates/test-programs/src/bin/get_chunk.rs
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);
}
}
16 changes: 16 additions & 0 deletions crates/test-programs/src/bin/get_with_json_response.rs
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")
}
10 changes: 10 additions & 0 deletions crates/test-programs/src/bin/get_with_query.rs
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);
}
12 changes: 12 additions & 0 deletions crates/test-programs/src/bin/post_with_form_data.rs
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);
}
13 changes: 13 additions & 0 deletions crates/test-programs/src/bin/post_with_json_data.rs
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 crates/test-programs/src/bin/post_with_multipart_form_data.rs
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);
}
73 changes: 59 additions & 14 deletions tests/main.rs
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());
}
27 changes: 0 additions & 27 deletions tests/program/Cargo.toml

This file was deleted.

Loading

0 comments on commit cf49a49

Please sign in to comment.