Skip to content

Commit f1da0a8

Browse files
committed
tests: Migrate wagi test to integration test suite
Signed-off-by: Lann Martin <[email protected]>
1 parent 84511c0 commit f1da0a8

File tree

10 files changed

+92
-70
lines changed

10 files changed

+92
-70
lines changed

build.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@ error: the `wasm32-wasi` target is not installed
6969
"crates/trigger-http/tests/rust-http-test",
7070
);
7171
build_wasm_test_program("redis-rust.wasm", "crates/trigger-redis/tests/rust");
72-
build_wasm_test_program("wagi-test.wasm", "crates/trigger-http/tests/wagi-test");
7372

7473
build_wasm_test_program(
7574
"spin-http-benchmark.wasm",

crates/trigger-http/src/lib.rs

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -541,10 +541,7 @@ impl OutboundWasiHttpHandler for HttpRuntimeData {
541541

542542
#[cfg(test)]
543543
mod tests {
544-
use std::collections::BTreeMap;
545-
546544
use anyhow::Result;
547-
use serde::Deserialize;
548545
use spin_testing::test_socket_addr;
549546

550547
use super::*;
@@ -696,43 +693,6 @@ mod tests {
696693
Ok(())
697694
}
698695

699-
#[tokio::test]
700-
async fn test_wagi_http() -> Result<()> {
701-
let trigger: HttpTrigger = spin_testing::HttpTestConfig::default()
702-
.test_program("wagi-test.wasm")
703-
.http_wagi_trigger("/test", Default::default())
704-
.build_trigger()
705-
.await;
706-
707-
let body = body::full(Bytes::from_static("Fermyon".as_bytes()));
708-
let req = http::Request::builder()
709-
.method("POST")
710-
.uri("https://myservice.fermyon.dev/test?abc=def")
711-
.header("x-custom-foo", "bar")
712-
.header("x-custom-foo2", "bar2")
713-
.body(body)
714-
.unwrap();
715-
716-
let res = trigger
717-
.handle(req, Scheme::HTTPS, test_socket_addr())
718-
.await?;
719-
assert_eq!(res.status(), StatusCode::OK);
720-
let body_bytes = res.into_body().collect().await.unwrap().to_bytes();
721-
722-
#[derive(Deserialize)]
723-
struct Env {
724-
args: Vec<String>,
725-
vars: BTreeMap<String, String>,
726-
}
727-
let env: Env =
728-
serde_json::from_str(std::str::from_utf8(body_bytes.as_ref()).unwrap()).unwrap();
729-
730-
assert_eq!(env.args, ["/test", "abc=def"]);
731-
assert_eq!(env.vars["HTTP_X_CUSTOM_FOO"], "bar".to_string());
732-
733-
Ok(())
734-
}
735-
736696
#[test]
737697
fn parse_listen_addr_prefers_ipv4() {
738698
let addr = parse_listen_addr("localhost:12345").unwrap();

crates/trigger-http/tests/wagi-test/.cargo/config.toml

Lines changed: 0 additions & 2 deletions
This file was deleted.

crates/trigger-http/tests/wagi-test/Cargo.toml

Lines changed: 0 additions & 9 deletions
This file was deleted.

crates/trigger-http/tests/wagi-test/src/main.rs

Lines changed: 0 additions & 18 deletions
This file was deleted.

tests/integration.rs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1036,6 +1036,51 @@ route = "/..."
10361036
Ok(())
10371037
}
10381038

1039+
#[test]
1040+
fn test_wagi_http() -> anyhow::Result<()> {
1041+
run_test(
1042+
"wagi-http",
1043+
testing_framework::SpinMode::Http,
1044+
[],
1045+
testing_framework::ServicesConfig::none(),
1046+
move |env| {
1047+
let spin = env.runtime_mut();
1048+
assert_spin_request(
1049+
spin,
1050+
testing_framework::Request::new(reqwest::Method::GET, "/base/hello"),
1051+
testing_framework::Response::new_with_body(200, "I'm a teapot"),
1052+
)?;
1053+
assert_spin_request(
1054+
spin,
1055+
testing_framework::Request::full(
1056+
reqwest::Method::GET,
1057+
"/base/echo",
1058+
&[],
1059+
Some("Echo..."),
1060+
),
1061+
testing_framework::Response::new_with_body(200, "Echo..."),
1062+
)?;
1063+
assert_spin_request(
1064+
spin,
1065+
testing_framework::Request::new(reqwest::Method::GET, "/base/args?x=y"),
1066+
testing_framework::Response::new_with_body(200, r#"["/base/args", "x=y"]"#),
1067+
)?;
1068+
assert_spin_request(
1069+
spin,
1070+
testing_framework::Request::full(
1071+
reqwest::Method::GET,
1072+
"/base/env?HTTP_X_CUSTOM_FOO",
1073+
&[("X-Custom-Foo", "bar")],
1074+
Option::<Vec<u8>>::None,
1075+
),
1076+
testing_framework::Response::new_with_body(200, "bar"),
1077+
)
1078+
},
1079+
)?;
1080+
1081+
Ok(())
1082+
}
1083+
10391084
#[test]
10401085
fn test_outbound_post() -> anyhow::Result<()> {
10411086
run_test(

tests/test-components/components/Cargo.lock

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[package]
2+
name = "integration_wagi"
3+
version = "0.1.0"
4+
edition = "2021"
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
use std::error::Error;
2+
3+
fn main() -> Result<(), Box<dyn Error>> {
4+
let body = match std::env::var("PATH_INFO")?.as_str() {
5+
"/hello" => "I'm a teapot".into(),
6+
"/echo" => std::io::read_to_string(std::io::stdin())?,
7+
"/args" => format!("{:?}", std::env::args().collect::<Vec<_>>()),
8+
"/env" => {
9+
let key = std::env::args().nth(1).unwrap_or_default();
10+
std::env::var(key)?
11+
}
12+
other => {
13+
println!("Content-Type: text/plain");
14+
println!("Status: 404\n\n");
15+
println!("Not Found (PATH_INFO={other:?})");
16+
return Ok(());
17+
}
18+
};
19+
print!("Content-Type: text/plain\n\n{body}");
20+
Ok(())
21+
}

tests/testcases/wagi-http/spin.toml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
spin_manifest_version = 2
2+
3+
[application]
4+
authors = ["Fermyon Engineering <[email protected]>"]
5+
description = "Test using WAGI HTTP."
6+
name = "wagi-http"
7+
version = "1.0.0"
8+
9+
[application.trigger.http]
10+
base = "/base"
11+
12+
[[trigger.http]]
13+
route = "/..."
14+
component = "wagi-http"
15+
executor = { type = "wagi" }
16+
17+
[component.wagi-http]
18+
source = "%{source=integration-wagi}"

0 commit comments

Comments
 (0)