Skip to content

Commit

Permalink
HTTP mock urlPattern is no longer default. Closes #33
Browse files Browse the repository at this point in the history
  • Loading branch information
shanejansen committed Aug 23, 2021
1 parent c8ecd55 commit 0bb1f46
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 13 deletions.
2 changes: 1 addition & 1 deletion touchstone-tests/touchstone/tests/test_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ def then(self, given, result) -> bool:

class WildcardMatches(TouchstoneTest):
def given(self) -> object:
self.mocks.http.setup().get('/some-endpoint/([a-z]*)/bar', 'hello http!')
self.mocks.http.setup().get('/some-endpoint/([a-z]*)/bar', 'hello http!', url_pattern=True)
return None

def when(self, given) -> object:
Expand Down
8 changes: 4 additions & 4 deletions touchstone/lib/nodes/mocks/behaviors/i_http_behavior.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,25 @@

class IHttpSetup(object):
@abc.abstractmethod
def get(self, endpoint: str, response: str, response_status: int = 200,
def get(self, endpoint: str, response: str, response_status: int = 200, url_pattern: bool = False,
response_headers: dict = {'Content-Type': 'application/json'}):
"""Returns the given response when a GET request is made to the given endpoint."""
pass

@abc.abstractmethod
def post(self, endpoint: str, response: str, response_status: int = 200,
def post(self, endpoint: str, response: str, response_status: int = 200, url_pattern: bool = False,
response_headers: dict = {'Content-Type': 'application/json'}):
"""Returns the given response when a POST request is made to the given endpoint."""
pass

@abc.abstractmethod
def put(self, endpoint: str, response: str, response_status: int = 200,
def put(self, endpoint: str, response: str, response_status: int = 200, url_pattern: bool = False,
response_headers: dict = {'Content-Type': 'application/json'}):
"""Returns the given response when a PUT request is made to the given endpoint."""
pass

@abc.abstractmethod
def delete(self, endpoint: str, response: str, response_status: int = 200,
def delete(self, endpoint: str, response: str, response_status: int = 200, url_pattern: bool = False,
response_headers: dict = {'Content-Type': 'application/json'}):
"""Returns the given response when a DELETE request is made to the given endpoint."""
pass
Expand Down
21 changes: 13 additions & 8 deletions touchstone/lib/nodes/mocks/docker/http/docker_http_setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@ def init(self, defaults: dict):
for request in defaults.get('requests', []):
self.__submit_mock(request)

def get(self, endpoint: str, response: str, response_status: int = 200,
def get(self, endpoint: str, response: str, response_status: int = 200, url_pattern: bool = False,
response_headers: dict = {'Content-Type': 'application/json'}):
self.__check_mock_response_type(response)
mock = {
'request': {
'method': 'GET',
'urlPattern': endpoint
self.__get_url_type(url_pattern): endpoint
},
'response': {
'status': response_status,
Expand All @@ -50,13 +50,13 @@ def get(self, endpoint: str, response: str, response_status: int = 200,
}
self.__submit_mock(mock)

def post(self, endpoint: str, response: str, response_status: int = 200,
def post(self, endpoint: str, response: str, response_status: int = 200, url_pattern: bool = False,
response_headers: dict = {'Content-Type': 'application/json'}):
self.__check_mock_response_type(response)
mock = {
'request': {
'method': 'POST',
'urlPattern': endpoint
self.__get_url_type(url_pattern): endpoint
},
'response': {
'status': response_status,
Expand All @@ -66,13 +66,13 @@ def post(self, endpoint: str, response: str, response_status: int = 200,
}
self.__submit_mock(mock)

def put(self, endpoint: str, response: str, response_status: int = 200,
def put(self, endpoint: str, response: str, response_status: int = 200, url_pattern: bool = False,
response_headers: dict = {'Content-Type': 'application/json'}):
self.__check_mock_response_type(response)
mock = {
'request': {
'method': 'PUT',
'urlPattern': endpoint
self.__get_url_type(url_pattern): endpoint
},
'response': {
'status': response_status,
Expand All @@ -82,13 +82,13 @@ def put(self, endpoint: str, response: str, response_status: int = 200,
}
self.__submit_mock(mock)

def delete(self, endpoint: str, response: str, response_status: int = 200,
def delete(self, endpoint: str, response: str, response_status: int = 200, url_pattern: bool = False,
response_headers: dict = {'Content-Type': 'application/json'}):
self.__check_mock_response_type(response)
mock = {
'request': {
'method': 'DELETE',
'urlPattern': endpoint
self.__get_url_type(url_pattern): endpoint
},
'response': {
'status': response_status,
Expand All @@ -98,6 +98,11 @@ def delete(self, endpoint: str, response: str, response_status: int = 200,
}
self.__submit_mock(mock)

def __get_url_type(self, url_pattern: bool):
if url_pattern:
return 'urlPattern'
return 'url'

def __check_mock_response_type(self, response):
if type(response) is not str:
raise exceptions.MockException('Response must be of type str.')
Expand Down

0 comments on commit 0bb1f46

Please sign in to comment.