Skip to content

Commit

Permalink
feat: support 'urlPattern'
Browse files Browse the repository at this point in the history
  • Loading branch information
beltram.maldant committed Mar 12, 2021
1 parent 9637fa2 commit f87eadf
Show file tree
Hide file tree
Showing 10 changed files with 107 additions and 9 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -62,7 +62,7 @@ the latter.
* [x] `url`
* [x] `urlPath`
* [x] `urlPathPattern`
* [ ] `urlPattern`
* [x] `urlPattern`
* [x] headers
* [x] `equalTo`
* [x] `contains`
Expand Down
5 changes: 1 addition & 4 deletions lib/src/model/request/query/mod.rs
Expand Up @@ -10,10 +10,7 @@ use case::QueryCaseInsensitiveMatcher;
use contains::QueryContainsMatcher;
use matches::QueryRegexMatcher;

use super::{
matcher::RequestMatcherDto,
super::request::MockRegistrable,
};
use super::{matcher::RequestMatcherDto, super::request::MockRegistrable};

mod exact;
mod case;
Expand Down
10 changes: 6 additions & 4 deletions lib/src/model/request/url/mod.rs
@@ -1,17 +1,16 @@
use std::convert::TryFrom;

use serde::Deserialize;
use wiremock::{
matchers::{PathExactMatcher, PathRegexMatcher},
MockBuilder,
};
use wiremock::{matchers::{PathExactMatcher, PathRegexMatcher}, MockBuilder};

use just_url::ExactPathAndQueryMatcher;
use url_pattern::UrlPatternMatcher;

use super::MockRegistrable;

mod url_path;
mod url_path_pattern;
mod url_pattern;
mod just_url;

#[derive(Deserialize, Debug, Default)]
Expand All @@ -35,6 +34,9 @@ impl MockRegistrable for HttpUrlDto {
if let Ok(regex) = PathRegexMatcher::try_from(self) {
mock = mock.and(regex);
}
if let Ok(url_pattern_matcher) = UrlPatternMatcher::try_from(self) {
mock = mock.and(url_pattern_matcher);
}
if let Ok(ExactPathAndQueryMatcher(path, queries)) = ExactPathAndQueryMatcher::try_from(self) {
mock = mock.and(path);
for query in queries {
Expand Down
25 changes: 25 additions & 0 deletions lib/src/model/request/url/url_pattern.rs
@@ -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'"))
}
}
1 change: 1 addition & 0 deletions lib/tests/req/url/mod.rs
@@ -1,3 +1,4 @@
pub mod url_path;
pub mod url_pattern;
pub mod url_path_pattern;
pub mod url_query;
37 changes: 37 additions & 0 deletions lib/tests/req/url/url_pattern.rs
@@ -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();
}
9 changes: 9 additions & 0 deletions lib/tests/stubs/req/url-pattern/all.json
@@ -0,0 +1,9 @@
{
"request": {
"urlPattern": "/api/pattern/([a-z]{4})\\?one=([a-z]{4})&two=([a-z]{4})",
"method": "GET"
},
"response": {
"status": 200
}
}
9 changes: 9 additions & 0 deletions lib/tests/stubs/req/url-pattern/just-one-query.json
@@ -0,0 +1,9 @@
{
"request": {
"urlPattern": "/api/pattern\\?one=([a-z]{4})",
"method": "GET"
},
"response": {
"status": 200
}
}
9 changes: 9 additions & 0 deletions lib/tests/stubs/req/url-pattern/just-url.json
@@ -0,0 +1,9 @@
{
"request": {
"urlPattern": "/api/pattern/([a-z]{4})",
"method": "GET"
},
"response": {
"status": 200
}
}
9 changes: 9 additions & 0 deletions lib/tests/stubs/req/url-pattern/many-queries.json
@@ -0,0 +1,9 @@
{
"request": {
"urlPattern": "/api/pattern\\?one=([a-z]{4})&two=([a-z]{4})",
"method": "GET"
},
"response": {
"status": 200
}
}

0 comments on commit f87eadf

Please sign in to comment.