-
Notifications
You must be signed in to change notification settings - Fork 1
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
Amazia Gur
authored and
Amazia Gur
committed
Oct 31, 2024
1 parent
b40c963
commit b88dc44
Showing
5 changed files
with
96 additions
and
33 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import re | ||
from typing import Pattern, Dict, Tuple, Any, Optional, Callable, Union | ||
|
||
|
||
class StubGroup: | ||
def __init__(self): | ||
self.stubs: Dict[ | ||
str, | ||
Dict[ | ||
Pattern, | ||
Tuple[int, Any, Optional[ | ||
Callable[[], Tuple[int, Any]]] | ||
]]] = {} | ||
|
||
def add(self, method: str, | ||
pattern: Union[str, Pattern], status_code: int, response: Any, | ||
response_func: Optional[Callable[[], Tuple[int, Any]]] = None): | ||
if method not in self.stubs: | ||
self.stubs[method] = {} | ||
|
||
if isinstance(pattern, str): | ||
pattern = re.compile(f"^{re.sub(r'\\{(\\w+)\\}', | ||
r'(?P<\\1>[^/]+)', pattern)}$") | ||
|
||
self.stubs[method][pattern] = (status_code, response, response_func) | ||
|
||
def match(self, method: str, path: str): | ||
matched_stub = None | ||
path_params = {} | ||
|
||
for compiled_path, (status_code, response, response_func)\ | ||
in self.stubs.get(method, {}).items(): | ||
match = compiled_path.match(path) | ||
if match: | ||
matched_stub = (status_code, response, response_func) | ||
path_params = match.groupdict() | ||
break | ||
|
||
return matched_stub, path_params |
This file was deleted.
Oops, something went wrong.
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,46 @@ | ||
from hamcrest import assert_that, none, is_ | ||
from mockingbird.stub_group import StubGroup | ||
|
||
|
||
def test_no_match(): | ||
stub_group = StubGroup() | ||
stub_group.add("GET", "/hi", 200, {"message": "hello"}) | ||
matched, _ = stub_group.match("GET", "/hello/kuku") | ||
assert_that(matched, none()) | ||
|
||
|
||
def test_match(): | ||
stub_group = StubGroup() | ||
stub_group.add("GET", "/hi", 200, {"message": "hello"}) | ||
matched, _ = stub_group.match("GET", "/hi") | ||
assert_that(matched, is_((200, {"message": "hello"}, None))) | ||
|
||
|
||
def test_none_on_partial_match(): | ||
stub_group = StubGroup() | ||
stub_group.add("POST", "/hi", 200, {"message": "hello"}) | ||
matched, _ = stub_group.match("POST", "/hi/hello") | ||
assert_that(matched, none()) | ||
|
||
|
||
def test_match_w_path_param(): | ||
stub_group = StubGroup() | ||
stub_group.add("GET", r"^/hello/(?P<name>\w+)$", | ||
200, {"message": "Hello, {name}!"}) | ||
|
||
matched, path_param = stub_group.match("GET", "/hello/mockingbird") | ||
|
||
assert_that(matched, is_((200, {"message": "Hello, {name}!"}, None))) | ||
assert_that(path_param, is_({"name": "mockingbird"})) | ||
|
||
|
||
def test_match_stub_with_response_func(): | ||
stub_group = StubGroup() | ||
|
||
def dynamic_response(): | ||
return 202, {"status": "dynamic"} | ||
|
||
stub_group.add("GET", r"^/dynamic$", | ||
200, {}, response_func=dynamic_response) | ||
matched, _ = stub_group.match("GET", "/dynamic") | ||
assert_that(matched, is_((200, {}, dynamic_response))) |