Skip to content

Commit 692025b

Browse files
committed
tests: Migrate spin inbound-http test to integration test suite
Signed-off-by: Lann Martin <[email protected]>
1 parent ae7752a commit 692025b

File tree

11 files changed

+152
-91
lines changed

11 files changed

+152
-91
lines changed

build.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,6 @@ error: the `wasm32-wasi` target is not installed
6464
std::fs::create_dir_all("target/test-programs").unwrap();
6565

6666
build_wasm_test_program("core-wasi-test.wasm", "crates/core/tests/core-wasi-test");
67-
build_wasm_test_program(
68-
"rust-http-test.wasm",
69-
"crates/trigger-http/tests/rust-http-test",
70-
);
7167
build_wasm_test_program("redis-rust.wasm", "crates/trigger-redis/tests/rust");
7268

7369
build_wasm_test_program(

crates/trigger-http/src/lib.rs

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -542,7 +542,6 @@ impl OutboundWasiHttpHandler for HttpRuntimeData {
542542
#[cfg(test)]
543543
mod tests {
544544
use anyhow::Result;
545-
use spin_testing::test_socket_addr;
546545

547546
use super::*;
548547

@@ -668,31 +667,6 @@ mod tests {
668667
res
669668
}
670669

671-
#[tokio::test]
672-
async fn test_spin_http() -> Result<()> {
673-
let trigger: HttpTrigger = spin_testing::HttpTestConfig::default()
674-
.test_program("rust-http-test.wasm")
675-
.http_spin_trigger("/test")
676-
.build_trigger()
677-
.await;
678-
679-
let body = body::full(Bytes::from_static("Fermyon".as_bytes()));
680-
let req = http::Request::post("https://myservice.fermyon.dev/test?abc=def")
681-
.header("x-custom-foo", "bar")
682-
.header("x-custom-foo2", "bar2")
683-
.body(body)
684-
.unwrap();
685-
686-
let res = trigger
687-
.handle(req, Scheme::HTTPS, test_socket_addr())
688-
.await?;
689-
assert_eq!(res.status(), StatusCode::OK);
690-
let body_bytes = res.into_body().collect().await.unwrap().to_bytes();
691-
assert_eq!(body_bytes.to_vec(), "Hello, Fermyon".as_bytes());
692-
693-
Ok(())
694-
}
695-
696670
#[test]
697671
fn parse_listen_addr_prefers_ipv4() {
698672
let addr = parse_listen_addr("localhost:12345").unwrap();

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

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

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

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

crates/trigger-http/tests/rust-http-test/src/lib.rs

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

tests/integration.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1031,6 +1031,41 @@ route = "/..."
10311031
Ok(())
10321032
}
10331033

1034+
#[test]
1035+
fn test_spin_inbound_http() -> anyhow::Result<()> {
1036+
run_test(
1037+
"spin-inbound-http",
1038+
testing_framework::SpinMode::Http,
1039+
[],
1040+
testing_framework::ServicesConfig::none(),
1041+
move |env| {
1042+
let spin = env.runtime_mut();
1043+
assert_spin_request(
1044+
spin,
1045+
testing_framework::Request::full(
1046+
Method::GET,
1047+
"/base/echo",
1048+
&[],
1049+
Some("Echo..."),
1050+
),
1051+
testing_framework::Response::new_with_body(200, "Echo..."),
1052+
)?;
1053+
assert_spin_request(
1054+
spin,
1055+
testing_framework::Request::full(
1056+
Method::GET,
1057+
"/base/assert-headers?k=v",
1058+
&[("X-Custom-Foo", "bar")],
1059+
Some(r#"{"x-custom-foo": "bar"}"#),
1060+
),
1061+
testing_framework::Response::new(200),
1062+
)?;
1063+
Ok(())
1064+
},
1065+
)?;
1066+
Ok(())
1067+
}
1068+
10341069
#[test]
10351070
fn test_wagi_http() -> anyhow::Result<()> {
10361071
run_test(

tests/test-components/components/Cargo.lock

Lines changed: 32 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[package]
2+
name = "integration-spin-inbound-http"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[lib]
7+
crate-type = ["cdylib"]
8+
9+
[dependencies]
10+
miniserde = "0.1.36"
11+
wit-bindgen = "0.13"
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
wit_bindgen::generate!({
2+
world: "http-trigger",
3+
path: "../../../../wit-0.2.0/deps/spin@unversioned",
4+
exports: {
5+
"fermyon:spin/inbound-http": SpinHttp,
6+
}
7+
});
8+
9+
use std::collections::HashMap;
10+
11+
use exports::fermyon::spin::inbound_http::{self, Request, Response};
12+
use miniserde::json;
13+
14+
struct SpinHttp;
15+
16+
impl inbound_http::Guest for SpinHttp {
17+
fn handle_request(req: Request) -> Response {
18+
let (status, body) = match handle_request(req) {
19+
Ok(body) => (200, body),
20+
Err(err) => (500, format!("{err:?}").into_bytes()),
21+
};
22+
Response {
23+
status,
24+
headers: None,
25+
body: Some(body),
26+
}
27+
}
28+
}
29+
30+
fn handle_request(req: Request) -> Result<Vec<u8>, Box<dyn std::error::Error>> {
31+
if !req.params.is_empty() {
32+
return Err("request params field is deprecated".into());
33+
}
34+
35+
let headers = req.headers.into_iter().collect::<HashMap<_, _>>();
36+
let path = headers
37+
.get("spin-path-info")
38+
.map(|path| path.as_str())
39+
.unwrap_or("");
40+
match path {
41+
"/echo" => Ok(req.body.unwrap_or_default()),
42+
"/assert-headers" => {
43+
let body = String::from_utf8(req.body.unwrap_or_default())?;
44+
let expected: HashMap<String, String> = json::from_str(&body)?;
45+
for (key, val) in expected {
46+
let got = headers
47+
.get(&key)
48+
.ok_or_else(|| format!("missing header {key:?}"))?;
49+
if got != &val {
50+
return Err(format!("expected header {key}: {val:?}, got {got:?}").into());
51+
}
52+
}
53+
54+
Ok(vec![])
55+
}
56+
other => Err(format!("unknown test route spin-path-info={other:?}").into()),
57+
}
58+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
spin_manifest_version = 2
2+
3+
[application]
4+
authors = ["Fermyon Engineering <[email protected]>"]
5+
description = "Test using the spin inbound-http interface."
6+
name = "spin-inbound-http"
7+
version = "1.0.0"
8+
9+
[application.trigger.http]
10+
base = "/base"
11+
12+
[[trigger.http]]
13+
route = "/..."
14+
[trigger.http.component]
15+
source = "%{source=integration-spin-inbound-http}"

0 commit comments

Comments
 (0)