Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
beltram.maldant
committed
Mar 12, 2021
1 parent
9637fa2
commit f87eadf
Showing
10 changed files
with
107 additions
and
9 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
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
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
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 |
---|---|---|
@@ -0,0 +1,25 @@ | ||
use std::{convert::TryFrom, str::FromStr}; | ||
|
||
use regex::Regex; | ||
use wiremock::{Match, Request}; | ||
|
||
use super::HttpUrlDto; | ||
|
||
pub struct UrlPatternMatcher(Regex); | ||
|
||
impl Match for UrlPatternMatcher { | ||
fn matches(&self, req: &Request) -> bool { | ||
self.0.is_match(req.url.as_str()) | ||
} | ||
} | ||
|
||
impl TryFrom<&HttpUrlDto> for UrlPatternMatcher { | ||
type Error = anyhow::Error; | ||
|
||
fn try_from(http_url: &HttpUrlDto) -> anyhow::Result<Self> { | ||
http_url.url_pattern.as_ref() | ||
.and_then(|it| Regex::from_str(it).ok()) | ||
.map(|it| Self(it)) | ||
.ok_or_else(|| anyhow::Error::msg("No 'urlPattern'")) | ||
} | ||
} |
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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
pub mod url_path; | ||
pub mod url_pattern; | ||
pub mod url_path_pattern; | ||
pub mod url_query; |
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 |
---|---|---|
@@ -0,0 +1,37 @@ | ||
use surf::get; | ||
|
||
use crate::utils::*; | ||
|
||
#[async_std::test] | ||
async fn should_map_request_url_pattern_with_just_url() { | ||
let srv = given("req/url-pattern/just-url"); | ||
get(&srv.path("/api/pattern/abcd")).await.unwrap().assert_ok(); | ||
get(&srv.path("/api/pattern/1234")).await.unwrap().assert_not_found(); | ||
} | ||
|
||
#[async_std::test] | ||
async fn should_map_request_url_pattern_with_just_one_query() { | ||
let srv = given("req/url-pattern/just-one-query"); | ||
get(&srv.path_query("/api/pattern", "one", "abcd")).await.unwrap().assert_ok(); | ||
get(&srv.path_query("/api/pattern", "one", "1234")).await.unwrap().assert_not_found(); | ||
} | ||
|
||
#[async_std::test] | ||
async fn should_map_request_url_pattern_with_many_queries() { | ||
let srv = given("req/url-pattern/many-queries"); | ||
get(&srv.path_queries("/api/pattern", ("one", "abcd"), ("two", "abcd"))).await.unwrap().assert_ok(); | ||
get(&srv.path_queries("/api/pattern", ("one", "1234"), ("two", "abcd"))).await.unwrap().assert_not_found(); | ||
get(&srv.path_queries("/api/pattern", ("one", "abcd"), ("two", "1234"))).await.unwrap().assert_not_found(); | ||
get(&srv.path_queries("/api/pattern", ("one", "1234"), ("two", "1234"))).await.unwrap().assert_not_found(); | ||
} | ||
|
||
#[async_std::test] | ||
async fn should_map_request_url_pattern_with_both_url_and_queries() { | ||
let srv = given("req/url-pattern/all"); | ||
get(&srv.path_queries("/api/pattern/abcd", ("one", "abcd"), ("two", "abcd"))).await.unwrap().assert_ok(); | ||
get(&srv.path_queries("/api/pattern/1234", ("one", "abcd"), ("two", "abcd"))).await.unwrap().assert_not_found(); | ||
get(&srv.path_queries("/api/pattern/abcd", ("one", "1234"), ("two", "abcd"))).await.unwrap().assert_not_found(); | ||
get(&srv.path_queries("/api/pattern/abcd", ("one", "abcd"), ("two", "1234"))).await.unwrap().assert_not_found(); | ||
get(&srv.path_queries("/api/pattern/abcd", ("one", "1234"), ("two", "1234"))).await.unwrap().assert_not_found(); | ||
get(&srv.path_queries("/api/pattern/1234", ("one", "1234"), ("two", "1234"))).await.unwrap().assert_not_found(); | ||
} |
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 |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"request": { | ||
"urlPattern": "/api/pattern/([a-z]{4})\\?one=([a-z]{4})&two=([a-z]{4})", | ||
"method": "GET" | ||
}, | ||
"response": { | ||
"status": 200 | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"request": { | ||
"urlPattern": "/api/pattern\\?one=([a-z]{4})", | ||
"method": "GET" | ||
}, | ||
"response": { | ||
"status": 200 | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"request": { | ||
"urlPattern": "/api/pattern/([a-z]{4})", | ||
"method": "GET" | ||
}, | ||
"response": { | ||
"status": 200 | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"request": { | ||
"urlPattern": "/api/pattern\\?one=([a-z]{4})&two=([a-z]{4})", | ||
"method": "GET" | ||
}, | ||
"response": { | ||
"status": 200 | ||
} | ||
} |