diff --git a/apollo-router/src/plugins/rhai/engine.rs b/apollo-router/src/plugins/rhai/engine.rs index 062143711f..32732bb435 100644 --- a/apollo-router/src/plugins/rhai/engine.rs +++ b/apollo-router/src/plugins/rhai/engine.rs @@ -1147,6 +1147,22 @@ mod router_plugin { Ok(()) } + // Uri.port + #[rhai_fn(get = "port", pure, return_raw)] + pub(crate) fn uri_port_get(x: &mut Uri) -> Result> { + to_dynamic(x.port_u16().map(|port| port.to_string())) + } + + #[rhai_fn(set = "port", pure, return_raw)] + pub(crate) fn uri_port_set(x: &mut Uri, value: &str) -> Result<(), Box> { + let mut parts: Parts = x.clone().into_parts(); + let host = x.host().unwrap_or_default(); + parts.authority = + Some(Authority::from_str(&format!("{host}:{value}")).map_err(|e| e.to_string())?); + *x = Uri::from_parts(parts).map_err(|e| e.to_string())?; + Ok(()) + } + // Response.label #[rhai_fn(get = "label", pure)] pub(crate) fn response_label_get(x: &mut Response) -> Dynamic { diff --git a/apollo-router/src/plugins/rhai/tests.rs b/apollo-router/src/plugins/rhai/tests.rs index 11c4f3a03a..226349b1d7 100644 --- a/apollo-router/src/plugins/rhai/tests.rs +++ b/apollo-router/src/plugins/rhai/tests.rs @@ -641,7 +641,7 @@ async fn it_can_process_string_subgraph_forbidden() { if let Err(error) = call_rhai_function("process_subgraph_response_string").await { let processed_error = process_error(error); assert_eq!(processed_error.status, StatusCode::INTERNAL_SERVER_ERROR); - assert_eq!(processed_error.message, Some("rhai execution error: 'Runtime error: I have raised an error (line 223, position 5)'".to_string())); + assert_eq!(processed_error.message, Some("rhai execution error: 'Runtime error: I have raised an error (line 226, position 5)'".to_string())); } else { // Test failed panic!("error processed incorrectly"); @@ -669,7 +669,7 @@ async fn it_cannot_process_om_subgraph_missing_message_and_body() { assert_eq!( processed_error.message, Some( - "rhai execution error: 'Runtime error: #{\"status\": 400} (line 234, position 5)'" + "rhai execution error: 'Runtime error: #{\"status\": 400} (line 237, position 5)'" .to_string() ) ); diff --git a/apollo-router/tests/fixtures/request_response_test.rhai b/apollo-router/tests/fixtures/request_response_test.rhai index 4dc88d42ad..c904fcfd8b 100644 --- a/apollo-router/tests/fixtures/request_response_test.rhai +++ b/apollo-router/tests/fixtures/request_response_test.rhai @@ -33,6 +33,9 @@ fn process_common_request(check_context_method_and_id, request) { if request.uri.path != "/" { throw(`query: expected: "/", actual: ${request.uri.path}`); } + if request.uri.port != () { + throw(`query: expected: (), actual: ${request.uri.host}`); + } } fn process_supergraph_request(request) {