Skip to content

Commit

Permalink
Add request.uri.port to rhai engine
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewmcgivery committed Oct 4, 2024
1 parent b90786d commit ffc52df
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
16 changes: 16 additions & 0 deletions apollo-router/src/plugins/rhai/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Dynamic, Box<EvalAltResult>> {
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<EvalAltResult>> {
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 {
Expand Down
4 changes: 2 additions & 2 deletions apollo-router/src/plugins/rhai/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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()
)
);
Expand Down
3 changes: 3 additions & 0 deletions apollo-router/tests/fixtures/request_response_test.rhai
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit ffc52df

Please sign in to comment.