Skip to content

Commit

Permalink
Adding support for HTTP PATCH method in mock server (#829)
Browse files Browse the repository at this point in the history
* Adding support for HTTP PATCH method in mock server

* Adding tests for put and patch in mock server
  • Loading branch information
KevinSJ authored Feb 28, 2024
1 parent c841bfa commit fe75b7f
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 4 deletions.
4 changes: 2 additions & 2 deletions lyrebird/mock/blueprints/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
core = Blueprint('mock', __name__, url_prefix='/mock')


@core.route('/', methods=['GET', 'POST', 'HEAD', 'PUT', 'DELETE', 'OPTIONS'])
@core.route('/<path:path>', methods=['GET', 'POST', 'HEAD', 'PUT', 'DELETE', 'OPTIONS'])
@core.route('/', methods=['GET', 'POST', 'HEAD', 'PUT', 'DELETE', 'OPTIONS', 'PATCH'])
@core.route('/<path:path>', methods=['GET', 'POST', 'HEAD', 'PUT', 'DELETE', 'OPTIONS', 'PATCH'])
def index(path=''):
logger.info(f'<Core> On request {request.url}')

Expand Down
4 changes: 2 additions & 2 deletions lyrebird/mock/handlers/handler_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ def _parse_request(self):
_request.update(request_info)

# handle request data
if self.request.method in ['POST', 'PUT']:
if self.request.method in ['POST', 'PUT', 'PATCH']:
DataHelper.origin2flow(self.request, output=_request, chain=self.request_chain)

if self.request.headers.get('Lyrebird-Client-Address'):
Expand All @@ -106,7 +106,7 @@ def _parse_request(self):

self.flow['request'] = _request

if self.request.method in ['POST', 'PUT'] and application.config.get('mock.request.keep_origin_data'):
if self.request.method in ['POST', 'PUT', 'PATCH'] and application.config.get('mock.request.keep_origin_data'):
origin_data = DataHelper.origin2string(self.request)
self.flow['origin_request'] = {
'data': origin_data
Expand Down
37 changes: 37 additions & 0 deletions tests/test_mock_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,42 @@ def test_mock_api_with_query(client, clear):
assert cache_list._cache[0]['request']['url'] == f'?url={url}'


def test_mock_api_put(client, clear):
url = '/mock/http://www.bing.com'
origin_json = {
'name': 'Lyrebird'
}
origin_body = json.dumps(origin_json)
headers = {'Content-Type': 'application/json'}

client.put(url, headers=headers, data=origin_body)

cache_list = context.application.cache
assert len(cache_list._cache) == 1

flow = cache_list._cache[0]
flow_body = flow['request']['data']
assert origin_json == flow_body


def test_mock_api_patch(client, clear):
url = '/mock/http://www.bing.com'
origin_json = {
'name': 'Lyrebird'
}
origin_body = json.dumps(origin_json)
headers = {'Content-Type': 'application/json'}

client.patch(url, headers=headers, data=origin_body)

cache_list = context.application.cache
assert len(cache_list._cache) == 1

flow = cache_list._cache[0]
flow_body = flow['request']['data']
assert origin_json == flow_body


def test_mock_content_type_json(client, clear):
url = '/mock/http://www.bing.com'
origin_json = {
Expand Down Expand Up @@ -224,6 +260,7 @@ def test_mock_content_type_form_keep_origin_request(client, clear, keep_origin_d

assert origin_data == flow_body


def test_mock_content_type_default_keep_origin_request(client, clear, keep_origin_data):
url = '/mock/http://www.bing.com'
origin_data = b'Lyrebird'
Expand Down

0 comments on commit fe75b7f

Please sign in to comment.