Skip to content

Commit

Permalink
Merge pull request #2274 from lann/moar-tests
Browse files Browse the repository at this point in the history
tests: Migrate wagi test to integration test suite
  • Loading branch information
lann authored Feb 13, 2024
2 parents 4bd8ffe + f1da0a8 commit 6a3cbe1
Show file tree
Hide file tree
Showing 10 changed files with 92 additions and 70 deletions.
1 change: 0 additions & 1 deletion build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ error: the `wasm32-wasi` target is not installed
"crates/trigger-http/tests/rust-http-test",
);
build_wasm_test_program("redis-rust.wasm", "crates/trigger-redis/tests/rust");
build_wasm_test_program("wagi-test.wasm", "crates/trigger-http/tests/wagi-test");

build_wasm_test_program(
"spin-http-benchmark.wasm",
Expand Down
40 changes: 0 additions & 40 deletions crates/trigger-http/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -541,10 +541,7 @@ impl OutboundWasiHttpHandler for HttpRuntimeData {

#[cfg(test)]
mod tests {
use std::collections::BTreeMap;

use anyhow::Result;
use serde::Deserialize;
use spin_testing::test_socket_addr;

use super::*;
Expand Down Expand Up @@ -696,43 +693,6 @@ mod tests {
Ok(())
}

#[tokio::test]
async fn test_wagi_http() -> Result<()> {
let trigger: HttpTrigger = spin_testing::HttpTestConfig::default()
.test_program("wagi-test.wasm")
.http_wagi_trigger("/test", Default::default())
.build_trigger()
.await;

let body = body::full(Bytes::from_static("Fermyon".as_bytes()));
let req = http::Request::builder()
.method("POST")
.uri("https://myservice.fermyon.dev/test?abc=def")
.header("x-custom-foo", "bar")
.header("x-custom-foo2", "bar2")
.body(body)
.unwrap();

let res = trigger
.handle(req, Scheme::HTTPS, test_socket_addr())
.await?;
assert_eq!(res.status(), StatusCode::OK);
let body_bytes = res.into_body().collect().await.unwrap().to_bytes();

#[derive(Deserialize)]
struct Env {
args: Vec<String>,
vars: BTreeMap<String, String>,
}
let env: Env =
serde_json::from_str(std::str::from_utf8(body_bytes.as_ref()).unwrap()).unwrap();

assert_eq!(env.args, ["/test", "abc=def"]);
assert_eq!(env.vars["HTTP_X_CUSTOM_FOO"], "bar".to_string());

Ok(())
}

#[test]
fn parse_listen_addr_prefers_ipv4() {
let addr = parse_listen_addr("localhost:12345").unwrap();
Expand Down
2 changes: 0 additions & 2 deletions crates/trigger-http/tests/wagi-test/.cargo/config.toml

This file was deleted.

9 changes: 0 additions & 9 deletions crates/trigger-http/tests/wagi-test/Cargo.toml

This file was deleted.

18 changes: 0 additions & 18 deletions crates/trigger-http/tests/wagi-test/src/main.rs

This file was deleted.

45 changes: 45 additions & 0 deletions tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1036,6 +1036,51 @@ route = "/..."
Ok(())
}

#[test]
fn test_wagi_http() -> anyhow::Result<()> {
run_test(
"wagi-http",
testing_framework::SpinMode::Http,
[],
testing_framework::ServicesConfig::none(),
move |env| {
let spin = env.runtime_mut();
assert_spin_request(
spin,
testing_framework::Request::new(reqwest::Method::GET, "/base/hello"),
testing_framework::Response::new_with_body(200, "I'm a teapot"),
)?;
assert_spin_request(
spin,
testing_framework::Request::full(
reqwest::Method::GET,
"/base/echo",
&[],
Some("Echo..."),
),
testing_framework::Response::new_with_body(200, "Echo..."),
)?;
assert_spin_request(
spin,
testing_framework::Request::new(reqwest::Method::GET, "/base/args?x=y"),
testing_framework::Response::new_with_body(200, r#"["/base/args", "x=y"]"#),
)?;
assert_spin_request(
spin,
testing_framework::Request::full(
reqwest::Method::GET,
"/base/env?HTTP_X_CUSTOM_FOO",
&[("X-Custom-Foo", "bar")],
Option::<Vec<u8>>::None,
),
testing_framework::Response::new_with_body(200, "bar"),
)
},
)?;

Ok(())
}

#[test]
fn test_outbound_post() -> anyhow::Result<()> {
run_test(
Expand Down
4 changes: 4 additions & 0 deletions tests/test-components/components/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions tests/test-components/components/integration-wagi/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[package]
name = "integration_wagi"
version = "0.1.0"
edition = "2021"
21 changes: 21 additions & 0 deletions tests/test-components/components/integration-wagi/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
use std::error::Error;

fn main() -> Result<(), Box<dyn Error>> {
let body = match std::env::var("PATH_INFO")?.as_str() {
"/hello" => "I'm a teapot".into(),
"/echo" => std::io::read_to_string(std::io::stdin())?,
"/args" => format!("{:?}", std::env::args().collect::<Vec<_>>()),
"/env" => {
let key = std::env::args().nth(1).unwrap_or_default();
std::env::var(key)?
}
other => {
println!("Content-Type: text/plain");
println!("Status: 404\n\n");
println!("Not Found (PATH_INFO={other:?})");
return Ok(());
}
};
print!("Content-Type: text/plain\n\n{body}");
Ok(())
}
18 changes: 18 additions & 0 deletions tests/testcases/wagi-http/spin.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
spin_manifest_version = 2

[application]
authors = ["Fermyon Engineering <[email protected]>"]
description = "Test using WAGI HTTP."
name = "wagi-http"
version = "1.0.0"

[application.trigger.http]
base = "/base"

[[trigger.http]]
route = "/..."
component = "wagi-http"
executor = { type = "wagi" }

[component.wagi-http]
source = "%{source=integration-wagi}"

0 comments on commit 6a3cbe1

Please sign in to comment.