Skip to content

Added the ability to retrieve recorded requests #26

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,18 @@ client.verify(
times(1)
)
```

### Retrieving recorded requests

```
from mockserver import MockServerClient, request, times
import requests

client = MockServerClient("http://localhost:1080")
requests.post("http://localhost:1080/whatever", json={"foo": "bar"})
result = client.retrieve_recorded_requests(method='POST', path='/whatever')
print(result.get('body')["string"])
```
Verify will check request on MockServer and raise AssertionError if request not found.

## More documentation
Expand Down
11 changes: 11 additions & 0 deletions mockserver_friendly/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,17 @@ def verify_expectations(self):
for req, timing in self.expectations:
self.verify(req, timing)

def retrieve_recorded_requests(
self,
path=None,
method=None,
querystring=None):
return self._call("retrieve", json.dumps(_non_null_options_to_dict(
_Option("method", method),
_Option("path", path),
_Option("queryStringParameters", querystring, formatter=_to_named_values_list),
))).json()


def request(method=None, path=None, querystring=None, body=None, headers=None, cookies=None):
return _non_null_options_to_dict(
Expand Down
72 changes: 72 additions & 0 deletions test/test_retrieve.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
from typing import List

import requests
from mockserver_friendly import request, response, form, json_contains
from test import MOCK_SERVER_URL, MockServerClientTestCase


class TestRetrieveByMethod(MockServerClientTestCase):
def test_retrieve_by_method(self):
self.client.reset()
requests.get(MOCK_SERVER_URL + "/whatever?foo=bar&hellow=world")
requests.post(MOCK_SERVER_URL + "/whatever", json={"foo": "bar"})

get_results: List[dict] = self.client.retrieve_recorded_requests(method='GET')

assert len(get_results) == 1
result = get_results[0]
assert result.get('method') == 'GET'
assert result.get('path') == '/whatever'
assert result.get('queryStringParameters') == {'foo': ['bar'], 'hellow': ['world']}

def test_retrieve_by_path(self):
self.client.reset()
requests.get(MOCK_SERVER_URL + "/whatever?foo=bar&hellow=world")
requests.post(MOCK_SERVER_URL + "/whatever2", json={"foo": "bar"})

get_results: List[dict] = self.client.retrieve_recorded_requests(path='/whatever')

assert len(get_results) == 1
result = get_results[0]
assert result.get('method') == 'GET'
assert result.get('path') == '/whatever'
assert result.get('queryStringParameters') == {'foo': ['bar'], 'hellow': ['world']}

def test_retrieve_by_querystring(self):
self.client.reset()
requests.get(MOCK_SERVER_URL + "/whatever?foo=bar&hellow=world")
requests.post(MOCK_SERVER_URL + "/whatever2?foo=bar&hellow=world3", json={"foo": "bar"})

get_results: List[dict] = self.client.retrieve_recorded_requests(querystring={"hellow": "world3"})

assert len(get_results) == 1
result = get_results[0]
assert result.get('method') == 'POST'
assert result.get('path') == '/whatever2'
assert result.get('queryStringParameters') == {'foo': ['bar'], 'hellow': ['world3']}

def test_retrieve_all(self):
self.client.reset()
requests.get(MOCK_SERVER_URL + "/whatever?foo=bar&hellow=world")
requests.post(MOCK_SERVER_URL + "/whatever2?foo=bar&hellow=world3", json={"foo": "bar"})

get_results: List[dict] = self.client.retrieve_recorded_requests()

assert len(get_results) == 2

def test_retrieve_body(self):
self.client.reset()
requests.get(MOCK_SERVER_URL + "/whatever?foo=bar&hellow=world")
requests.post(MOCK_SERVER_URL + "/whatever2?foo=bar&hellow=world3", json={"foo": "bar"})

get_results: List[dict] = self.client.retrieve_recorded_requests(method='POST')

assert len(get_results) == 1
result = get_results[0]
assert result.get('method') == 'POST'
assert result.get('path') == '/whatever2'
assert result.get('queryStringParameters') == {'foo': ['bar'], 'hellow': ['world3']}
assert result.get('body')["string"] == '{"foo": "bar"}'