def test_custom_middleware_updates_response_view_fails(): """ Make sure that a custom middleware is executed as part of the workflow of handling a request. """ app = Minik() app.add_middleware(CustomMiddleware()) @app.get('/event') def get_event(): raise Exception('Something went downhill :(') event = create_api_event('/event', method='GET') response = app(event, MagicMock()) assert response['headers'][ 'x-findme'] == CustomMiddleware.expected_header_value
def test_custom_middleware_updates_response(): """ Make sure that a custom middleware is executed as part of the workflow of handling a request. """ app = Minik() app.add_middleware(CustomMiddleware()) @app.get('/event') def get_event(): return {'data': 'some event'} event = create_api_event('/event', method='GET') response = app(event, MagicMock()) assert response['headers'][ 'x-findme'] == CustomMiddleware.expected_header_value
def test_debug_mode_relays_response(): """ Make sure that if the minik app is in debug mode, the exception trace and error message are sent back to the consumer. """ app_in_debug = Minik(debug=True) @app_in_debug.get('/failme') def fail(): return {'data': app_in_debug.response.field_not_in_response} event = create_api_event('/failme', method='GET') response = app_in_debug(event, context) body = json.loads(response['body']) assert body['trace'] assert body['error_message']
from minik.core import Minik app = Minik() @app.get("/events") def get_events(): return {"data": [{'zip_code': 20902}, {'zip_code': 73071}]} @app.post("/events") def post_event(): event_name = app.request.json_body.get('name') # Save this event somewhere return {'id': 100, 'name': event_name} @app.get("/events/{zip_code}") def get_event(zip_code: int): print(f'{type(zip_code)} - {zip_code}') if zip_code == 20902: return {'events': ['MD Gran fondo', 'Old Busthead']} return {'events': ['other events']}
See the License for the specific language governing permissions and limitations under the License. """ import json import pytest import uuid from unittest.mock import MagicMock from minik.core import Minik from minik.fields import ReStr, BaseRouteField from minik.status_codes import codes from minik.utils import create_api_event sample_app = Minik() context = MagicMock() @sample_app.route('/bio/{username}', methods=['GET']) def get_re_view(username: str): return {'user': username} @pytest.mark.parametrize("username", [('busthead'), ('123'), ('pd_az'), ('hello-world')]) def test_str_route_validation_valid(username): """ The string based route validation will match any valid \w+ regular expression, which is used for unicode patterns [a-zA-Z0-9_]. https://docs.python.org/3/library/re.html
http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. """ import json import pytest from unittest.mock import MagicMock from minik.core import Minik from minik.status_codes import codes from minik.utils import create_api_event sample_app = Minik(debug=True) context = MagicMock() @sample_app.route('/activity_no_method') def no_method_view(): return {'method': sample_app.request.method.lower()} @sample_app.route('/activity', methods=['POST']) def post_view(): return {'id': 2, 'message': 'success'} @sample_app.route('/activity/{activity_id}', methods=['GET']) def get_view(activity_id):