-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Factor out and test URI validity function
- Loading branch information
Showing
1 changed file
with
13 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
||
|
@@ -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) | ||
|
@@ -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]")); | ||
} | ||
} |