Skip to content

Commit

Permalink
Factor out and test URI validity function
Browse files Browse the repository at this point in the history
  • Loading branch information
glyn committed Jun 28, 2024
1 parent 64fbba2 commit 15afb57
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@ fn create_router(jm: jrdmap::JrdMap) -> Router {
.with_state(state)
}

fn valid_uri(uri: &str) -> bool {
let uri_reference = Uri::parse(uri);
!uri_reference.is_err() && !uri_reference.unwrap().is_relative()
}

async fn handler(State(state): State<ServerState>, Query(params): Query<Params>) -> Response {
let uri = params.resource;

Expand All @@ -94,8 +99,7 @@ async fn handler(State(state): State<ServerState>, Query(params): Query<Params>)
.unwrap()
} else {
let uri = uri.get(0).unwrap();
let uri_reference = Uri::parse(uri);
if uri_reference.is_err() || uri_reference.unwrap().is_relative() {
if !valid_uri(&uri) {
// Malformed "resource" parameter
Response::builder()
.status(StatusCode::BAD_REQUEST)
Expand Down Expand Up @@ -491,4 +495,11 @@ mod tests {
});
assert_eq!(actual, expected);
}

#[test]
fn test_valid_uri() {
assert_eq!(false, valid_uri(""));
assert_eq!(false, valid_uri("[email protected]"));
assert_eq!(true, valid_uri("acct:[email protected]"));
}
}

0 comments on commit 15afb57

Please sign in to comment.