Skip to content

Commit 94d51dc

Browse files
authored
Features: Optional strict mode, expectation / function call tracing, optional expectation setting. (#8)
Signed-off-by: Christopher Agia <chrisagia@google.com>
1 parent 52b8093 commit 94d51dc

File tree

11 files changed

+1040
-483
lines changed

11 files changed

+1040
-483
lines changed

.github/workflows/rust.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,10 +180,10 @@ jobs:
180180
run: target/release/examples/hello_world proxy-wasm-rust-sdk/target/wasm32-unknown-unknown/release/examples/hello_world.wasm
181181

182182
- name: Test (http_auth_random)
183-
run: target/release/examples/http_auth_random proxy-wasm-rust-sdk/target/wasm32-unknown-unknown/release/examples/http_auth_random.wasm
183+
run: target/release/examples/http_auth_random proxy-wasm-rust-sdk/target/wasm32-unknown-unknown/release/examples/http_auth_random.wasm -a
184184

185185
- name: Test (http_headers)
186-
run: target/release/examples/http_headers proxy-wasm-rust-sdk/target/wasm32-unknown-unknown/release/examples/http_headers.wasm
186+
run: target/release/examples/http_headers proxy-wasm-rust-sdk/target/wasm32-unknown-unknown/release/examples/http_headers.wasm -a
187187

188188
outdated:
189189
runs-on: ubuntu-latest

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@ anyhow = "1.0.31"
1212
lazy_static = "1.4.0"
1313
more-asserts = "0.2.1"
1414
rand = "0.7.3"
15+
structopt = "0.3.16"

examples/hello_world.rs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,13 @@
1313
// limitations under the License.
1414

1515
use anyhow::Result;
16-
use proxy_wasm_test_framework::{tester, types::*};
17-
use std::env;
16+
use proxy_wasm_test_framework::tester;
17+
use proxy_wasm_test_framework::types::*;
18+
use structopt::StructOpt;
1819

1920
fn main() -> Result<()> {
20-
let args: Vec<String> = env::args().collect();
21-
assert_eq!(args.len(), 2);
22-
let hello_world_path = &args[1];
23-
let mut hello_world_test = tester::test(hello_world_path)?;
21+
let args = tester::MockSettings::from_args();
22+
let mut hello_world_test = tester::mock(args)?;
2423

2524
hello_world_test
2625
.call_start()
@@ -33,18 +32,22 @@ fn main() -> Result<()> {
3332

3433
hello_world_test
3534
.call_proxy_on_vm_start(root_context, 0)
36-
.expect_log(LogLevel::Info, "Hello, World!")
37-
.expect_set_tick_period_millis(5 * 10u64.pow(3))
35+
.expect_log(Some(LogLevel::Info), Some("Hello, World!"))
36+
.expect_set_tick_period_millis(Some(5 * 10u64.pow(3)))
3837
.execute_and_expect(ReturnType::Bool(true))?;
3938

4039
hello_world_test
4140
.call_proxy_on_tick(root_context)
4241
.expect_get_current_time_nanos()
43-
.returning(0 * 10u64.pow(9))
42+
.returning(Some(0 * 10u64.pow(9)))
43+
.expect_log(Some(LogLevel::Info), Some("It's 1970-01-01 00:00:00 UTC"))
4444
.execute_and_expect(ReturnType::None)?;
4545

4646
hello_world_test
4747
.call_proxy_on_tick(root_context)
48+
.expect_get_current_time_nanos()
49+
.returning(None)
50+
.expect_log(Some(LogLevel::Info), None)
4851
.execute_and_expect(ReturnType::None)?;
4952

5053
return Ok(());

examples/http_auth_random.rs

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,13 @@
1313
// limitations under the License.
1414

1515
use anyhow::Result;
16-
use proxy_wasm_test_framework::{tester, types::*};
17-
use std::env;
16+
use proxy_wasm_test_framework::tester;
17+
use proxy_wasm_test_framework::types::*;
18+
use structopt::StructOpt;
1819

1920
fn main() -> Result<()> {
20-
let args: Vec<String> = env::args().collect();
21-
assert_eq!(args.len(), 2);
22-
let http_auth_random_path = &args[1];
23-
let mut http_auth_random = tester::test(http_auth_random_path)?;
21+
let args = tester::MockSettings::from_args();
22+
let mut http_auth_random = tester::mock(args)?;
2423

2524
http_auth_random
2625
.call_start()
@@ -37,37 +36,41 @@ fn main() -> Result<()> {
3736
.execute_and_expect(ReturnType::None)?;
3837

3938
http_auth_random
40-
.call_proxy_on_request_headers(http_context, 0)
39+
.call_proxy_on_request_headers(http_context, 0, false)
4140
.expect_http_call(
42-
"httpbin",
43-
vec![
41+
Some("httpbin"),
42+
Some(vec![
4443
(":method", "GET"),
4544
(":path", "/bytes/1"),
4645
(":authority", "httpbin.org"),
47-
],
46+
]),
4847
None,
49-
vec![],
50-
5 * 10u64.pow(3),
48+
Some(vec![]),
49+
Some(5 * 10u64.pow(3)),
5150
)
52-
.returning(0)
51+
.returning(Some(0))
5352
.execute_and_expect(ReturnType::Action(Action::Pause))?;
5453

5554
let buffer_data = "custom_developer_body";
5655
http_auth_random
5756
.call_proxy_on_http_call_response(http_context, 0, 0, buffer_data.len() as i32, 0)
58-
.expect_get_buffer_bytes(BufferType::HttpCallResponseBody)
59-
.returning(buffer_data)
57+
.expect_get_buffer_bytes(Some(BufferType::HttpCallResponseBody))
58+
.returning(Some(buffer_data))
6059
.expect_send_local_response(
61-
403,
60+
Some(403),
6261
Some("Access forbidden.\n"),
63-
vec![("Powered-By", "proxy-wasm")],
64-
-1,
62+
Some(vec![("Powered-By", "proxy-wasm")]),
63+
Some(-1),
6564
)
6665
.execute_and_expect(ReturnType::None)?;
6766

6867
http_auth_random
69-
.call_proxy_on_response_headers(http_context, 0)
70-
.expect_replace_header_map_value(MapType::HttpResponseHeaders, "Powered-By", "proxy-wasm")
68+
.call_proxy_on_response_headers(http_context, 0, false)
69+
.expect_replace_header_map_value(
70+
Some(MapType::HttpResponseHeaders),
71+
Some("Powered-By"),
72+
Some("proxy-wasm"),
73+
)
7174
.execute_and_expect(ReturnType::Action(Action::Continue))?;
7275

7376
return Ok(());

examples/http_headers.rs

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,13 @@
1313
// limitations under the License.
1414

1515
use anyhow::Result;
16-
use proxy_wasm_test_framework::{tester, types::*};
17-
use std::env;
16+
use proxy_wasm_test_framework::tester;
17+
use proxy_wasm_test_framework::types::*;
18+
use structopt::StructOpt;
1819

1920
fn main() -> Result<()> {
20-
let args: Vec<String> = env::args().collect();
21-
assert_eq!(args.len(), 2);
22-
let http_headers_path = &args[1];
23-
let mut http_headers_test = tester::test(http_headers_path)?;
21+
let args = tester::MockSettings::from_args();
22+
let mut http_headers_test = tester::mock(args)?;
2423

2524
http_headers_test
2625
.call_start()
@@ -37,34 +36,34 @@ fn main() -> Result<()> {
3736
.execute_and_expect(ReturnType::None)?;
3837

3938
http_headers_test
40-
.call_proxy_on_request_headers(http_context, 0)
41-
.expect_get_header_map_pairs(MapType::HttpRequestHeaders)
42-
.returning(vec![
39+
.call_proxy_on_request_headers(http_context, 0, false)
40+
.expect_get_header_map_pairs(Some(MapType::HttpRequestHeaders))
41+
.returning(Some(vec![
4342
(":method", "GET"),
4443
(":path", "/hello"),
4544
(":authority", "developer"),
46-
])
47-
.expect_get_header_map_value(MapType::HttpRequestHeaders, ":path")
48-
.returning("/hello")
45+
]))
46+
.expect_get_header_map_value(Some(MapType::HttpRequestHeaders), Some(":path"))
47+
.returning(Some("/hello"))
4948
.expect_send_local_response(
50-
200,
49+
Some(200),
5150
Some("Hello, World!\n"),
52-
vec![("Hello", "World"), ("Powered-By", "proxy-wasm")],
53-
-1,
51+
Some(vec![("Hello", "World"), ("Powered-By", "proxy-wasm")]),
52+
Some(-1),
5453
)
5554
.execute_and_expect(ReturnType::Action(Action::Pause))?;
5655

5756
http_headers_test
58-
.call_proxy_on_response_headers(http_context, 0)
59-
.expect_get_header_map_pairs(MapType::HttpResponseHeaders)
60-
.returning(vec![(":status", "200"), ("Powered-By", "proxy-wasm")])
61-
.expect_log(LogLevel::Trace, "#2 <- :status: 200")
62-
.expect_log(LogLevel::Trace, "#2 <- Powered-By: proxy-wasm")
57+
.call_proxy_on_response_headers(http_context, 0, false)
58+
.expect_get_header_map_pairs(Some(MapType::HttpResponseHeaders))
59+
.returning(Some(vec![(":status", "200"), ("Powered-By", "proxy-wasm")]))
60+
.expect_log(Some(LogLevel::Trace), Some("#2 <- :status: 200"))
61+
.expect_log(Some(LogLevel::Trace), Some("#2 <- Powered-By: proxy-wasm"))
6362
.execute_and_expect(ReturnType::Action(Action::Continue))?;
6463

6564
http_headers_test
6665
.call_proxy_on_log(http_context)
67-
.expect_log(LogLevel::Trace, "#2 completed.")
66+
.expect_log(Some(LogLevel::Trace), Some("#2 completed."))
6867
.execute_and_expect(ReturnType::None)?;
6968

7069
return Ok(());

src/expect_interface.rs

Lines changed: 24 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@ use crate::tester::Tester;
1616

1717
// As of now, the following expectations do not require "fn returning()" implementations and hence
1818
// no structure is provided for them. Setting of these expectations are built directly into tester.rs:
19-
// proxy_log(), proxy_set_tick_period_millis(), proxy_set_buffer_bytes(), proxy_replace_header_map_value(),
20-
// proxy_remove_header_map_value(), proxy_add_header_map_value(), proxy_send_local_response(), etc.
19+
// proxy_log(), proxy_set_tick_period_millis(), proxy_set_buffer_bytes(), proxy_set_header_map_pairs,
20+
// proxy_replace_header_map_value(), proxy_remove_header_map_value(), proxy_add_header_map_value(),
21+
// proxy_send_local_response(), etc.
2122

2223
pub struct ExpectGetCurrentTimeNanos<'a> {
2324
tester: &'a mut Tester,
@@ -28,7 +29,7 @@ impl<'a> ExpectGetCurrentTimeNanos<'a> {
2829
ExpectGetCurrentTimeNanos { tester: tester }
2930
}
3031

31-
pub fn returning(&mut self, current_time_nanos: u64) -> &mut Tester {
32+
pub fn returning(&mut self, current_time_nanos: Option<u64>) -> &mut Tester {
3233
self.tester
3334
.get_expect_handle()
3435
.staged
@@ -39,18 +40,18 @@ impl<'a> ExpectGetCurrentTimeNanos<'a> {
3940

4041
pub struct ExpectGetBufferBytes<'a> {
4142
tester: &'a mut Tester,
42-
buffer_type: i32,
43+
buffer_type: Option<i32>,
4344
}
4445

4546
impl<'a> ExpectGetBufferBytes<'a> {
46-
pub fn expecting(tester: &'a mut Tester, buffer_type: i32) -> ExpectGetBufferBytes {
47+
pub fn expecting(tester: &'a mut Tester, buffer_type: Option<i32>) -> ExpectGetBufferBytes {
4748
ExpectGetBufferBytes {
4849
tester: tester,
4950
buffer_type: buffer_type,
5051
}
5152
}
5253

53-
pub fn returning(&mut self, buffer_data: &str) -> &mut Tester {
54+
pub fn returning(&mut self, buffer_data: Option<&str>) -> &mut Tester {
5455
self.tester
5556
.get_expect_handle()
5657
.staged
@@ -61,18 +62,18 @@ impl<'a> ExpectGetBufferBytes<'a> {
6162

6263
pub struct ExpectGetHeaderMapPairs<'a> {
6364
tester: &'a mut Tester,
64-
map_type: i32,
65+
map_type: Option<i32>,
6566
}
6667

6768
impl<'a> ExpectGetHeaderMapPairs<'a> {
68-
pub fn expecting(tester: &'a mut Tester, map_type: i32) -> ExpectGetHeaderMapPairs {
69+
pub fn expecting(tester: &'a mut Tester, map_type: Option<i32>) -> ExpectGetHeaderMapPairs {
6970
ExpectGetHeaderMapPairs {
7071
tester: tester,
7172
map_type: map_type,
7273
}
7374
}
7475

75-
pub fn returning(&mut self, header_map_pairs: Vec<(&str, &str)>) -> &mut Tester {
76+
pub fn returning(&mut self, header_map_pairs: Option<Vec<(&str, &str)>>) -> &mut Tester {
7677
self.tester
7778
.get_expect_handle()
7879
.staged
@@ -81,39 +82,17 @@ impl<'a> ExpectGetHeaderMapPairs<'a> {
8182
}
8283
}
8384

84-
pub struct ExpectSetHeaderMapPairs<'a> {
85-
tester: &'a mut Tester,
86-
map_type: i32,
87-
}
88-
89-
impl<'a> ExpectSetHeaderMapPairs<'a> {
90-
pub fn expecting(tester: &'a mut Tester, map_type: i32) -> ExpectSetHeaderMapPairs {
91-
ExpectSetHeaderMapPairs {
92-
tester: tester,
93-
map_type: map_type,
94-
}
95-
}
96-
97-
pub fn returning(&mut self, header_map_pairs: Vec<(&str, &str)>) -> &mut Tester {
98-
self.tester
99-
.get_expect_handle()
100-
.staged
101-
.set_expect_set_header_map_pairs(self.map_type, header_map_pairs);
102-
self.tester
103-
}
104-
}
105-
10685
pub struct ExpectGetHeaderMapValue<'a> {
10786
tester: &'a mut Tester,
108-
map_type: i32,
109-
header_map_key: &'static str,
87+
map_type: Option<i32>,
88+
header_map_key: Option<&'static str>,
11089
}
11190

11291
impl<'a> ExpectGetHeaderMapValue<'a> {
11392
pub fn expecting(
11493
tester: &'a mut Tester,
115-
map_type: i32,
116-
header_map_key: &'static str,
94+
map_type: Option<i32>,
95+
header_map_key: Option<&'static str>,
11796
) -> ExpectGetHeaderMapValue<'a> {
11897
ExpectGetHeaderMapValue {
11998
tester: tester,
@@ -122,7 +101,7 @@ impl<'a> ExpectGetHeaderMapValue<'a> {
122101
}
123102
}
124103

125-
pub fn returning(&mut self, header_map_value: &str) -> &mut Tester {
104+
pub fn returning(&mut self, header_map_value: Option<&str>) -> &mut Tester {
126105
self.tester
127106
.get_expect_handle()
128107
.staged
@@ -133,21 +112,21 @@ impl<'a> ExpectGetHeaderMapValue<'a> {
133112

134113
pub struct ExpectHttpCall<'a> {
135114
tester: &'a mut Tester,
136-
upstream: &'a str,
137-
headers: Option<Vec<(&'a str, &'a str)>>,
115+
upstream: Option<&'a str>,
116+
headers: Option<Option<Vec<(&'a str, &'a str)>>>,
138117
body: Option<&'a str>,
139-
trailers: Option<Vec<(&'a str, &'a str)>>,
140-
timeout: u64,
118+
trailers: Option<Option<Vec<(&'a str, &'a str)>>>,
119+
timeout: Option<u64>,
141120
}
142121

143122
impl<'a> ExpectHttpCall<'a> {
144123
pub fn expecting(
145124
tester: &'a mut Tester,
146-
upstream: &'a str,
147-
headers: Vec<(&'a str, &'a str)>,
125+
upstream: Option<&'a str>,
126+
headers: Option<Vec<(&'a str, &'a str)>>,
148127
body: Option<&'a str>,
149-
trailers: Vec<(&'a str, &'a str)>,
150-
timeout: u64,
128+
trailers: Option<Vec<(&'a str, &'a str)>>,
129+
timeout: Option<u64>,
151130
) -> ExpectHttpCall<'a> {
152131
ExpectHttpCall {
153132
tester: tester,
@@ -159,7 +138,7 @@ impl<'a> ExpectHttpCall<'a> {
159138
}
160139
}
161140

162-
pub fn returning(&mut self, token_id: u32) -> &mut Tester {
141+
pub fn returning(&mut self, token_id: Option<u32>) -> &mut Tester {
163142
self.tester.get_expect_handle().staged.set_expect_http_call(
164143
self.upstream,
165144
self.headers.take().unwrap(),

0 commit comments

Comments
 (0)