Skip to content

Commit ae7752a

Browse files
committed
Update integration-wagi component
Change approach to do test assertions in the test component. Signed-off-by: Lann Martin <[email protected]>
1 parent 6445f70 commit ae7752a

File tree

4 files changed

+76
-25
lines changed

4 files changed

+76
-25
lines changed

tests/integration.rs

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1040,11 +1040,6 @@ route = "/..."
10401040
testing_framework::ServicesConfig::none(),
10411041
move |env| {
10421042
let spin = env.runtime_mut();
1043-
assert_spin_request(
1044-
spin,
1045-
testing_framework::Request::new(reqwest::Method::GET, "/base/hello"),
1046-
testing_framework::Response::new_with_body(200, "I'm a teapot"),
1047-
)?;
10481043
assert_spin_request(
10491044
spin,
10501045
testing_framework::Request::full(
@@ -1057,19 +1052,25 @@ route = "/..."
10571052
)?;
10581053
assert_spin_request(
10591054
spin,
1060-
testing_framework::Request::new(reqwest::Method::GET, "/base/args?x=y"),
1061-
testing_framework::Response::new_with_body(200, r#"["/base/args", "x=y"]"#),
1055+
testing_framework::Request::full(
1056+
Method::GET,
1057+
"/base/assert-args?x=y",
1058+
&[],
1059+
Some(r#"["/base/assert-args", "x=y"]"#),
1060+
),
1061+
testing_framework::Response::new(200),
10621062
)?;
10631063
assert_spin_request(
10641064
spin,
10651065
testing_framework::Request::full(
1066-
reqwest::Method::GET,
1067-
"/base/env?HTTP_X_CUSTOM_FOO",
1066+
Method::GET,
1067+
"/base/assert-env",
10681068
&[("X-Custom-Foo", "bar")],
1069-
Option::<Vec<u8>>::None,
1069+
Some(r#"{"HTTP_X_CUSTOM_FOO": "bar"}"#),
10701070
),
1071-
testing_framework::Response::new_with_body(200, "bar"),
1072-
)
1071+
testing_framework::Response::new(200),
1072+
)?;
1073+
Ok(())
10731074
},
10741075
)?;
10751076

tests/test-components/components/Cargo.lock

Lines changed: 25 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 & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
[package]
22
name = "integration_wagi"
33
version = "0.1.0"
4-
edition = "2021"
4+
edition = "2021"
5+
6+
[dependencies]
7+
miniserde = "0.1.36"
Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,43 @@
1-
use std::error::Error;
1+
use std::{collections::HashMap, error::Error};
22

33
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)?
4+
let mut body = "".to_string();
5+
match std::env::var("PATH_INFO")?.as_str() {
6+
// Echos request body to response body
7+
"/echo" => {
8+
body = std::io::read_to_string(std::io::stdin())?;
119
}
10+
11+
// Asserts that WAGI args match the JSON array in the request body
12+
"/assert-args" => {
13+
let expected: Vec<String> = stdin_json()?;
14+
let args = std::env::args().collect::<Vec<_>>();
15+
if args != expected {
16+
return Err(format!("expected args {expected:?} got {args:?}").into());
17+
}
18+
}
19+
20+
// Asserts that env vars contains the JSON object entries in the request body
21+
"/assert-env" => {
22+
let expected: HashMap<String, String> = stdin_json()?;
23+
for (key, val) in expected {
24+
let got = std::env::var(&key)?;
25+
if got != val {
26+
return Err(format!("expected env var {key}={val:?}, got {got:?}").into());
27+
}
28+
}
29+
}
30+
1231
other => {
13-
println!("Content-Type: text/plain");
14-
println!("Status: 404\n\n");
15-
println!("Not Found (PATH_INFO={other:?})");
16-
return Ok(());
32+
return Err(format!("unknown test route {other:?}").into());
1733
}
1834
};
35+
1936
print!("Content-Type: text/plain\n\n{body}");
2037
Ok(())
2138
}
39+
40+
fn stdin_json<T: miniserde::Deserialize>() -> Result<T, Box<dyn Error>> {
41+
let body = std::io::read_to_string(std::io::stdin())?;
42+
Ok(miniserde::json::from_str(&body)?)
43+
}

0 commit comments

Comments
 (0)