Skip to content

Commit b731289

Browse files
committed
refactor(weld): rename policy_enforced_http to http and fix clippy warnings
Signed-off-by: Jiaxiao Zhou <duibao55328@gmail.com>
1 parent 5b3c73a commit b731289

File tree

6 files changed

+31
-80
lines changed

6 files changed

+31
-80
lines changed

crates/component2json/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -695,7 +695,7 @@ mod tests {
695695
#[test]
696696
fn test_vals_to_json_single() {
697697
let val = Val::Bool(true);
698-
let json_val = vals_to_json(&[val.clone()]);
698+
let json_val = vals_to_json(std::slice::from_ref(&val));
699699
assert_eq!(json_val, val_to_json(&val));
700700
}
701701

crates/weld/src/lib.rs

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ use wasmtime_wasi_http::{WasiHttpCtx, WasiHttpView};
2525
mod wasistate;
2626
pub use wasistate::{create_wasi_state_template_from_policy, WasiStateTemplate};
2727

28-
mod policy_enforced_http;
29-
pub use policy_enforced_http::WeldWasiState;
28+
mod http;
29+
pub use http::WeldWasiState;
3030

3131
const DOWNLOADS_DIR: &str = "downloads";
3232

@@ -1556,22 +1556,6 @@ permissions: {}
15561556
Ok(())
15571557
}
15581558

1559-
#[test(tokio::test)]
1560-
async fn test_get_wasi_state_for_component_default_policy() -> Result<()> {
1561-
let manager = create_test_manager().await?;
1562-
1563-
// Test getting WASI state for component without attached policy (should use default)
1564-
let _wasi_state = manager
1565-
.get_wasi_state_for_component("test-component")
1566-
.await?;
1567-
1568-
// Should not fail and return a valid WasiState
1569-
// We can't directly inspect the WasiState, but we can verify it was created successfully
1570-
assert!(true); // If we get here without error, the test passes
1571-
1572-
Ok(())
1573-
}
1574-
15751559
#[test(tokio::test)]
15761560
async fn test_get_wasi_state_for_component_with_policy() -> Result<()> {
15771561
let manager = create_test_manager().await?;
@@ -1599,9 +1583,6 @@ permissions:
15991583
.get_wasi_state_for_component(TEST_COMPONENT_ID)
16001584
.await?;
16011585

1602-
// Should not fail and return a valid WasiState with the policy applied
1603-
assert!(true); // If we get here without error, the test passes
1604-
16051586
Ok(())
16061587
}
16071588

tests/fetch_integration_test.rs

Lines changed: 17 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,7 @@ async fn test_fetch_with_network_policy_enforcement() -> Result<()> {
2222

2323
let target_url = "https://example.com/";
2424

25-
println!(
26-
"Attempting to fetch {} without network permissions...",
27-
target_url
28-
);
25+
println!("Attempting to fetch {target_url} without network permissions...");
2926

3027
let result = manager
3128
.execute_component_call(
@@ -37,23 +34,19 @@ async fn test_fetch_with_network_policy_enforcement() -> Result<()> {
3734

3835
match result {
3936
Ok(response) => {
40-
println!("Component response: {}", response);
37+
println!("Component response: {response}");
4138

4239
// Check if the response contains an error indicating the request was blocked
4340
if response.contains("HttpRequestDenied") {
4441
println!("✅ Network request properly blocked by policy!");
4542
} else {
4643
panic!(
47-
"Expected network request to be blocked, but got successful response: {}",
48-
response
44+
"Expected network request to be blocked, but got successful response: {response}"
4945
);
5046
}
5147
}
5248
Err(e) => {
53-
panic!(
54-
"Expected network request to be blocked, but got successful response: {}",
55-
e
56-
);
49+
panic!("Expected network request to be blocked, but got successful response: {e}");
5750
}
5851
}
5952

@@ -70,10 +63,7 @@ async fn test_fetch_with_network_policy_enforcement() -> Result<()> {
7063
assert!(grant_result.is_ok(), "Failed to grant network permission");
7164

7265
// Then try to fetch with network permissions - should succeed
73-
println!(
74-
"Attempting to fetch {} with network permissions...",
75-
target_url
76-
);
66+
println!("Attempting to fetch {target_url} with network permissions...");
7767

7868
let result = manager
7969
.execute_component_call(
@@ -85,27 +75,20 @@ async fn test_fetch_with_network_policy_enforcement() -> Result<()> {
8575

8676
match result {
8777
Ok(response) => {
88-
println!("Fetch response after granting permission: {}", response);
78+
println!("Fetch response after granting permission: {response}");
8979

9080
if response.contains("HttpRequestDenied") {
91-
panic!(
92-
"Network request still being blocked after granting permission: {}",
93-
response
94-
);
81+
panic!("Network request still being blocked after granting permission: {response}");
9582
} else {
9683
assert!(
9784
response.contains("Example Domain") || response.contains("example"),
98-
"Expected response to contain example.com content, got: {}",
99-
response
85+
"Expected response to contain example.com content, got: {response}"
10086
);
10187
println!("✅ Network request succeeded after granting permission!");
10288
}
10389
}
10490
Err(e) => {
105-
panic!(
106-
"Expected network request to be blocked, but got successful response: {}",
107-
e
108-
);
91+
panic!("Expected network request to be blocked, but got successful response: {e}");
10992
}
11093
}
11194

@@ -142,13 +125,13 @@ async fn test_fetch_with_different_host_still_denied() -> Result<()> {
142125

143126
match result {
144127
Err(e) => {
145-
panic!("Expected request to httpbin.org to be denied when only example.com is allowed, got: {}", e);
128+
panic!("Expected request to httpbin.org to be denied when only example.com is allowed, got: {e}");
146129
}
147130
Ok(response) => {
148131
if response.contains("HttpRequestDenied") {
149132
println!("✅ Request to unauthorized host properly blocked!");
150133
} else {
151-
panic!("Expected request to httpbin.org to be denied when only example.com is allowed, got: {}", response);
134+
panic!("Expected request to httpbin.org to be denied when only example.com is allowed, got: {response}");
152135
}
153136
}
154137
}
@@ -186,13 +169,10 @@ async fn test_fetch_with_scheme_specific_permissions() -> Result<()> {
186169
// HTTPS should succeed or fail for non-policy reasons
187170
match https_result {
188171
Ok(response) => {
189-
println!("HTTPS fetch response: {}", response);
172+
println!("HTTPS fetch response: {response}");
190173

191174
if response.contains("HttpRequestDenied") {
192-
panic!(
193-
"HTTPS request should not be blocked by policy, got: {}",
194-
response
195-
);
175+
panic!("HTTPS request should not be blocked by policy, got: {response}");
196176
} else {
197177
println!("✅ HTTPS request allowed as expected");
198178
}
@@ -203,8 +183,7 @@ async fn test_fetch_with_scheme_specific_permissions() -> Result<()> {
203183
!error_msg.contains("denied")
204184
&& !error_msg.contains("HttpRequestUriInvalid")
205185
&& !error_msg.contains("HttpRequestDenied"),
206-
"HTTPS request should not be denied by policy: {}",
207-
error_msg
186+
"HTTPS request should not be denied by policy: {error_msg}"
208187
);
209188
}
210189
}
@@ -221,21 +200,19 @@ async fn test_fetch_with_scheme_specific_permissions() -> Result<()> {
221200
match http_result {
222201
Err(e) => {
223202
let error_msg = e.to_string();
224-
println!("Expected HTTP denial: {}", error_msg);
203+
println!("Expected HTTP denial: {error_msg}");
225204

226205
assert!(
227206
error_msg.contains("HttpRequestDenied"),
228-
"Expected HTTP request to be denied when only HTTPS is allowed: {}",
229-
error_msg
207+
"Expected HTTP request to be denied when only HTTPS is allowed: {error_msg}"
230208
);
231209
}
232210
Ok(response) => {
233211
if response.contains("HttpRequestDenied") {
234212
println!("✅ HTTP request properly blocked when only HTTPS allowed!");
235213
} else {
236214
panic!(
237-
"Expected HTTP request to be denied when only HTTPS is allowed, got: {}",
238-
response
215+
"Expected HTTP request to be denied when only HTTPS is allowed, got: {response}"
239216
);
240217
}
241218
}

tests/file_integration_test.rs

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -206,9 +206,8 @@ async fn test_filesystem_component_integration() -> Result<()> {
206206
let project_dir = std::env::var("CARGO_MANIFEST_DIR").context("CARGO_MANIFEST_DIR not set")?;
207207

208208
let execute_request = format!(
209-
r#"{{"jsonrpc": "2.0", "method": "tools/call", "params": {{"name": "list-directory", "arguments": {{"path": "{}"}}}}, "id": 4}}
210-
"#,
211-
project_dir
209+
r#"{{"jsonrpc": "2.0", "method": "tools/call", "params": {{"name": "list-directory", "arguments": {{"path": "{project_dir}"}}}}, "id": 4}}
210+
"#
212211
);
213212

214213
stdin.write_all(execute_request.as_bytes()).await?;
@@ -247,9 +246,8 @@ async fn test_filesystem_component_integration() -> Result<()> {
247246
.contains("Failed to read directory"));
248247

249248
let grant_permission_request = format!(
250-
r#"{{"jsonrpc": "2.0", "method": "tools/call", "params": {{"name": "grant-storage-permission", "arguments": {{"component_id": "filesystem", "details": {{"uri": "fs://{}", "access": ["read"]}}}}}}, "id": 5}}
251-
"#,
252-
project_dir
249+
r#"{{"jsonrpc": "2.0", "method": "tools/call", "params": {{"name": "grant-storage-permission", "arguments": {{"component_id": "filesystem", "details": {{"uri": "fs://{project_dir}", "access": ["read"]}}}}}}, "id": 5}}
250+
"#
253251
);
254252

255253
stdin.write_all(grant_permission_request.as_bytes()).await?;
@@ -308,9 +306,8 @@ async fn test_filesystem_component_integration() -> Result<()> {
308306
assert!(policy_info["policy_info"]["policy_id"].is_string());
309307

310308
let execute_with_permission_request = format!(
311-
r#"{{"jsonrpc": "2.0", "method": "tools/call", "params": {{"name": "list-directory", "arguments": {{"path": "{}"}}}}, "id": 7}}
312-
"#,
313-
project_dir
309+
r#"{{"jsonrpc": "2.0", "method": "tools/call", "params": {{"name": "list-directory", "arguments": {{"path": "{project_dir}"}}}}, "id": 7}}
310+
"#
314311
);
315312

316313
stdin
@@ -401,7 +398,7 @@ async fn test_filesystem_component_lifecycle_manager() -> Result<()> {
401398
.execute_component_call(
402399
&id,
403400
"list-directory",
404-
&format!(r#"{{"path": "{}"}}"#, project_dir),
401+
&format!(r#"{{"path": "{project_dir}"}}"#),
405402
)
406403
.await;
407404

@@ -411,10 +408,7 @@ async fn test_filesystem_component_lifecycle_manager() -> Result<()> {
411408
Ok(response) => {
412409
// If it succeeds, it should be because the component has some default access
413410
// but it might still benefit from explicit permissions
414-
println!(
415-
"Component succeeded without explicit permissions: {}",
416-
response
417-
);
411+
println!("Component succeeded without explicit permissions: {response}");
418412
}
419413
Err(error) => {
420414
// If it fails, verify it's the expected permission error
@@ -441,14 +435,14 @@ async fn test_filesystem_component_lifecycle_manager() -> Result<()> {
441435
let policy_info = policy_info.unwrap();
442436
let policy_content = tokio::fs::read_to_string(&policy_info.local_path).await?;
443437
assert!(policy_content.contains("storage"));
444-
assert!(policy_content.contains(&format!("fs://{}", project_dir)));
438+
assert!(policy_content.contains(&format!("fs://{project_dir}")));
445439
assert!(policy_content.contains("read"));
446440

447441
let result_with_permission = manager
448442
.execute_component_call(
449443
&id,
450444
"list-directory",
451-
&format!(r#"{{"path": "{}"}}"#, project_dir),
445+
&format!(r#"{{"path": "{project_dir}"}}"#),
452446
)
453447
.await;
454448

tests/transport_integration_test.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use std::net::{Ipv4Addr, SocketAddr, SocketAddrV4};
2-
use std::path::PathBuf;
32
use std::process::Stdio;
43
use std::sync::Arc;
54
use std::time::Duration;
@@ -284,7 +283,7 @@ async fn test_load_component_from_oci() -> Result<()> {
284283
|| error_msg.contains("docker client")
285284
|| error_msg.contains("Failed to start docker registry")
286285
{
287-
println!("Skipping OCI test: Docker is not available - {}", error_msg);
286+
println!("Skipping OCI test: Docker is not available - {error_msg}");
288287
return Ok(());
289288
}
290289
return Err(e);

0 commit comments

Comments
 (0)