Skip to content

Commit ffc52df

Browse files
Add request.uri.port to rhai engine
1 parent b90786d commit ffc52df

File tree

3 files changed

+21
-2
lines changed

3 files changed

+21
-2
lines changed

apollo-router/src/plugins/rhai/engine.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1147,6 +1147,22 @@ mod router_plugin {
11471147
Ok(())
11481148
}
11491149

1150+
// Uri.port
1151+
#[rhai_fn(get = "port", pure, return_raw)]
1152+
pub(crate) fn uri_port_get(x: &mut Uri) -> Result<Dynamic, Box<EvalAltResult>> {
1153+
to_dynamic(x.port_u16().map(|port| port.to_string()))
1154+
}
1155+
1156+
#[rhai_fn(set = "port", pure, return_raw)]
1157+
pub(crate) fn uri_port_set(x: &mut Uri, value: &str) -> Result<(), Box<EvalAltResult>> {
1158+
let mut parts: Parts = x.clone().into_parts();
1159+
let host = x.host().unwrap_or_default();
1160+
parts.authority =
1161+
Some(Authority::from_str(&format!("{host}:{value}")).map_err(|e| e.to_string())?);
1162+
*x = Uri::from_parts(parts).map_err(|e| e.to_string())?;
1163+
Ok(())
1164+
}
1165+
11501166
// Response.label
11511167
#[rhai_fn(get = "label", pure)]
11521168
pub(crate) fn response_label_get(x: &mut Response) -> Dynamic {

apollo-router/src/plugins/rhai/tests.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -641,7 +641,7 @@ async fn it_can_process_string_subgraph_forbidden() {
641641
if let Err(error) = call_rhai_function("process_subgraph_response_string").await {
642642
let processed_error = process_error(error);
643643
assert_eq!(processed_error.status, StatusCode::INTERNAL_SERVER_ERROR);
644-
assert_eq!(processed_error.message, Some("rhai execution error: 'Runtime error: I have raised an error (line 223, position 5)'".to_string()));
644+
assert_eq!(processed_error.message, Some("rhai execution error: 'Runtime error: I have raised an error (line 226, position 5)'".to_string()));
645645
} else {
646646
// Test failed
647647
panic!("error processed incorrectly");
@@ -669,7 +669,7 @@ async fn it_cannot_process_om_subgraph_missing_message_and_body() {
669669
assert_eq!(
670670
processed_error.message,
671671
Some(
672-
"rhai execution error: 'Runtime error: #{\"status\": 400} (line 234, position 5)'"
672+
"rhai execution error: 'Runtime error: #{\"status\": 400} (line 237, position 5)'"
673673
.to_string()
674674
)
675675
);

apollo-router/tests/fixtures/request_response_test.rhai

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ fn process_common_request(check_context_method_and_id, request) {
3333
if request.uri.path != "/" {
3434
throw(`query: expected: "/", actual: ${request.uri.path}`);
3535
}
36+
if request.uri.port != () {
37+
throw(`query: expected: (), actual: ${request.uri.host}`);
38+
}
3639
}
3740

3841
fn process_supergraph_request(request) {

0 commit comments

Comments
 (0)