Skip to content

Commit fd24870

Browse files
authored
Merge pull request #2277 from lann/moar-tests
Migrate spin inbound-http test to integration test suite
2 parents 3655b68 + 692025b commit fd24870

File tree

13 files changed

+249
-152
lines changed

13 files changed

+249
-152
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: 69 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
mod testcases;
22

33
mod integration_tests {
4+
use reqwest::Method;
45
use sha2::Digest;
56
use std::collections::HashMap;
67

@@ -23,10 +24,7 @@ mod integration_tests {
2324
let spin = env.runtime_mut();
2425
assert_spin_request(
2526
spin,
26-
testing_framework::Request::new(
27-
reqwest::Method::GET,
28-
&format!("/test?key={test_key}"),
29-
),
27+
testing_framework::Request::new(Method::GET, &format!("/test?key={test_key}")),
3028
testing_framework::Response::new_with_body(200, test_value),
3129
)
3230
},
@@ -46,28 +44,25 @@ mod integration_tests {
4644
let spin = env.runtime_mut();
4745
assert_spin_request(
4846
spin,
49-
testing_framework::Request::new(reqwest::Method::GET, "/test/hello"),
47+
testing_framework::Request::new(Method::GET, "/test/hello"),
5048
testing_framework::Response::new_with_body(200, "I'm a teapot"),
5149
)?;
5250
assert_spin_request(
5351
spin,
5452
testing_framework::Request::new(
55-
reqwest::Method::GET,
53+
Method::GET,
5654
"/test/hello/wildcards/should/be/handled",
5755
),
5856
testing_framework::Response::new_with_body(200, "I'm a teapot"),
5957
)?;
6058
assert_spin_request(
6159
spin,
62-
testing_framework::Request::new(reqwest::Method::GET, "/thishsouldfail"),
60+
testing_framework::Request::new(Method::GET, "/thishsouldfail"),
6361
testing_framework::Response::new(404),
6462
)?;
6563
assert_spin_request(
6664
spin,
67-
testing_framework::Request::new(
68-
reqwest::Method::GET,
69-
"/test/hello/test-placement",
70-
),
65+
testing_framework::Request::new(Method::GET, "/test/hello/test-placement"),
7166
testing_framework::Response::new_with_body(200, "text for test"),
7267
)
7368
},
@@ -152,7 +147,7 @@ mod integration_tests {
152147
let spin = env.runtime_mut();
153148
assert_spin_request(
154149
spin,
155-
testing_framework::Request::new(reqwest::Method::GET, "/env"),
150+
testing_framework::Request::new(Method::GET, "/env"),
156151
testing_framework::Response::full(
157152
200,
158153
[
@@ -185,7 +180,7 @@ mod integration_tests {
185180
assert_spin_request(
186181
spin,
187182
testing_framework::Request::new(
188-
reqwest::Method::GET,
183+
Method::GET,
189184
&format!("/static/thisshouldbemounted/{name}"),
190185
),
191186
testing_framework::Response::new_with_body(200, content),
@@ -203,10 +198,7 @@ mod integration_tests {
203198
let mut assert_not_found = |path: &str| {
204199
assert_spin_request(
205200
spin,
206-
testing_framework::Request::new(
207-
reqwest::Method::GET,
208-
&format!("/static/{path}"),
209-
),
201+
testing_framework::Request::new(Method::GET, &format!("/static/{path}")),
210202
testing_framework::Response::new_with_body(404, "Not Found"),
211203
)
212204
};
@@ -233,7 +225,7 @@ mod integration_tests {
233225
let mut test = |lang: &str, body: &str| {
234226
assert_spin_request(
235227
spin,
236-
testing_framework::Request::new(reqwest::Method::GET, &format!("/{lang}")),
228+
testing_framework::Request::new(Method::GET, &format!("/{lang}")),
237229
testing_framework::Response::new_with_body(200, body),
238230
)
239231
};
@@ -279,16 +271,13 @@ Caused by:
279271
let spin = env.runtime_mut();
280272
assert_spin_request(
281273
spin,
282-
testing_framework::Request::new(reqwest::Method::GET, "/test/outbound-allowed"),
274+
testing_framework::Request::new(Method::GET, "/test/outbound-allowed"),
283275
testing_framework::Response::new_with_body(200, "Hello, Fermyon!\n"),
284276
)?;
285277

286278
assert_spin_request(
287279
spin,
288-
testing_framework::Request::new(
289-
reqwest::Method::GET,
290-
"/test/outbound-not-allowed",
291-
),
280+
testing_framework::Request::new(Method::GET, "/test/outbound-not-allowed"),
292281
testing_framework::Response::new(500),
293282
)?;
294283

@@ -314,7 +303,7 @@ Caused by:
314303
|env| {
315304
let spin = env.runtime_mut();
316305
let mut ensure_success = |uri, expected_status, expected_body| {
317-
let request = testing_framework::Request::new(reqwest::Method::GET, uri);
306+
let request = testing_framework::Request::new(Method::GET, uri);
318307
assert_spin_request(
319308
spin,
320309
request,
@@ -346,7 +335,7 @@ Caused by:
346335
|env| {
347336
let spin = env.runtime_mut();
348337
let mut ensure_success = |uri, expected_status, expected_body| {
349-
let request = testing_framework::Request::new(reqwest::Method::GET, uri);
338+
let request = testing_framework::Request::new(Method::GET, uri);
350339
assert_spin_request(
351340
spin,
352341
request,
@@ -397,7 +386,7 @@ Caused by:
397386
.status();
398387
assert_eq!(status, 200);
399388
let spin = env.runtime_mut();
400-
let request = testing_framework::Request::new(reqwest::Method::GET, "/");
389+
let request = testing_framework::Request::new(Method::GET, "/");
401390
assert_spin_request(
402391
spin,
403392
request,
@@ -596,7 +585,7 @@ Caused by:
596585
)?;
597586
assert_spin_request(
598587
env.runtime_mut(),
599-
testing_framework::Request::new(reqwest::Method::GET, "/"),
588+
testing_framework::Request::new(Method::GET, "/"),
600589
testing_framework::Response::new_with_body(200, "Hello, Fermyon"),
601590
)?;
602591
Ok(())
@@ -627,7 +616,7 @@ Caused by:
627616
assert_spin_request(
628617
env.runtime_mut(),
629618
testing_framework::Request::full(
630-
reqwest::Method::GET,
619+
Method::GET,
631620
"/",
632621
&[("url", &format!("http://127.0.0.1:{port}/",))],
633622
Some(body.as_bytes()),
@@ -936,12 +925,8 @@ route = "/..."
936925
headers.push(("url", &url));
937926
}
938927
let uri = format!("/{uri}");
939-
let request = testing_framework::Request::full(
940-
reqwest::Method::POST,
941-
&uri,
942-
&headers,
943-
Some(body),
944-
);
928+
let request =
929+
testing_framework::Request::full(Method::POST, &uri, &headers, Some(body));
945930
assert_spin_request(
946931
spin,
947932
request,
@@ -1008,7 +993,7 @@ route = "/..."
1008993
.map(|(k, v)| (*k, v.as_str()))
1009994
.collect::<Vec<_>>();
1010995
let request = testing_framework::Request::full(
1011-
reqwest::Method::GET,
996+
Method::GET,
1012997
"/hash-all",
1013998
&headers,
1014999
Option::<Vec<u8>>::None,
@@ -1047,23 +1032,53 @@ route = "/..."
10471032
}
10481033

10491034
#[test]
1050-
fn test_wagi_http() -> anyhow::Result<()> {
1035+
fn test_spin_inbound_http() -> anyhow::Result<()> {
10511036
run_test(
1052-
"wagi-http",
1037+
"spin-inbound-http",
10531038
testing_framework::SpinMode::Http,
10541039
[],
10551040
testing_framework::ServicesConfig::none(),
10561041
move |env| {
10571042
let spin = env.runtime_mut();
10581043
assert_spin_request(
10591044
spin,
1060-
testing_framework::Request::new(reqwest::Method::GET, "/base/hello"),
1061-
testing_framework::Response::new_with_body(200, "I'm a teapot"),
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..."),
10621052
)?;
10631053
assert_spin_request(
10641054
spin,
10651055
testing_framework::Request::full(
1066-
reqwest::Method::GET,
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+
1069+
#[test]
1070+
fn test_wagi_http() -> anyhow::Result<()> {
1071+
run_test(
1072+
"wagi-http",
1073+
testing_framework::SpinMode::Http,
1074+
[],
1075+
testing_framework::ServicesConfig::none(),
1076+
move |env| {
1077+
let spin = env.runtime_mut();
1078+
assert_spin_request(
1079+
spin,
1080+
testing_framework::Request::full(
1081+
Method::GET,
10671082
"/base/echo",
10681083
&[],
10691084
Some("Echo..."),
@@ -1072,19 +1087,25 @@ route = "/..."
10721087
)?;
10731088
assert_spin_request(
10741089
spin,
1075-
testing_framework::Request::new(reqwest::Method::GET, "/base/args?x=y"),
1076-
testing_framework::Response::new_with_body(200, r#"["/base/args", "x=y"]"#),
1090+
testing_framework::Request::full(
1091+
Method::GET,
1092+
"/base/assert-args?x=y",
1093+
&[],
1094+
Some(r#"["/base/assert-args", "x=y"]"#),
1095+
),
1096+
testing_framework::Response::new(200),
10771097
)?;
10781098
assert_spin_request(
10791099
spin,
10801100
testing_framework::Request::full(
1081-
reqwest::Method::GET,
1082-
"/base/env?HTTP_X_CUSTOM_FOO",
1101+
Method::GET,
1102+
"/base/assert-env",
10831103
&[("X-Custom-Foo", "bar")],
1084-
Option::<Vec<u8>>::None,
1104+
Some(r#"{"HTTP_X_CUSTOM_FOO": "bar"}"#),
10851105
),
1086-
testing_framework::Response::new_with_body(200, "bar"),
1087-
)
1106+
testing_framework::Response::new(200),
1107+
)?;
1108+
Ok(())
10881109
},
10891110
)?;
10901111

@@ -1106,7 +1127,7 @@ route = "/..."
11061127
);
11071128
let headers = [("url", service_url.as_str())];
11081129
let request: testing_framework::Request<'_, Vec<u8>> =
1109-
testing_framework::Request::full(reqwest::Method::GET, "/", &headers, None);
1130+
testing_framework::Request::full(Method::GET, "/", &headers, None);
11101131
let expected = testing_framework::Response::new_with_body(200, "Hello, world!");
11111132
assert_spin_request(env.runtime_mut(), request, expected)
11121133
},

0 commit comments

Comments
 (0)