def test_sessions(): a = Malt() @a.get('/') def dump_session(request): old_sess = dict(request.session) request.session['a'] = 2 return jsonify(old_sess) wsgi = a.wsgi_app({'SESSIONS': True, 'SECRET_KEY': 'abc'}) # Capture response headers headers = [None] def start_response(_, h): headers[0] = h # Send with a valid session session_token = 'eyJhIjoxfQ.2XFQKMS-erhoKkSGsezDxFsim6YctUnzxaiiMP1wzFs' environ = { 'REQUEST_METHOD': 'GET', 'PATH_INFO': '/', 'HTTP_COOKIE': 'session=' + session_token, } rv = wsgi(environ, start_response) rv_text = next(iter(rv)).decode('utf-8') assert json.loads(rv_text) == {'a': 1} # Check response session cookie set_cookie = next(v for k, v in headers[0] if k.lower() == 'set-cookie') expected = 'eyJhIjoyfQ.WW2DtOr1pkaM9nrC9sw2kHW3Cxd57hDhLie--g46DjE' assert set_cookie == 'session=' + expected
def test_config(): a = Malt() @a.get('/') def dump_config(request): return jsonify(request.config) wsgi = a.wsgi_app({'a': 'b'}) environ = { 'REQUEST_METHOD': 'GET', 'PATH_INFO': '/', } rv = wsgi(environ, lambda status, headers: None) rv_text = next(iter(rv)).decode('utf-8') assert json.loads(rv_text)['a'] == 'b'
def test_default_handler(): app = Malt() def divide(a, b): return a / b def raise_exc(status=None, msg=None): raise HTTPException(status, msg) for args in ((divide, 1, 0), (raise_exc, ), (raise_exc, -1), (raise_exc, 418.), (raise_exc, 500)): response = app.get_response(*args) assert '<h1>500 Internal Server Error</h1>' in response.response[0] response = app.get_response(raise_exc, 418) assert '<h1>418 I\'m a teapot</h1>' in response.response[0] response = app.get_response(raise_exc, 418, 'Good Stuff') assert '<h1>418 Good Stuff</h1>' in response.response[0] response = app.get_response(raise_exc, 418., 'Bad Stuff') assert '<h1>500 Bad Stuff</h1>' in response.response[0] response = app.get_response(raise_exc, -1, 'Worse Stuff') assert '<h1>500 Worse Stuff</h1>' in response.response[0]
def test_custom_handler(): app = Malt() @app.error_handler def handle(exc): return Response('%d\n%s' % (exc.status_code, exc.message), code=exc.status_code) def divide(a, b): return a / b def raise_exc(status=None, msg=None): raise HTTPException(status, msg) for args in ((divide, 1, 0), (raise_exc, ), (raise_exc, -1), (raise_exc, 418.), (raise_exc, 500)): response = app.get_response(*args) assert '500\nInternal Server Error' in response.response[0] response = app.get_response(raise_exc, 418) assert '418\nI\'m a teapot' in response.response[0] response = app.get_response(raise_exc, 418, 'Good Stuff') assert '418\nGood Stuff' in response.response[0] response = app.get_response(raise_exc, 418., 'Bad Stuff') assert '500\nBad Stuff' in response.response[0] response = app.get_response(raise_exc, -1, 'Worse Stuff') assert '500\nWorse Stuff' in response.response[0]
def test_error_in_response(): """A response that errors when iterated should still get returned. We'll let the WSGI server deal with errors mid-way through iterating through a response object. """ app = Malt() def secretly_bad_response(): def gen(): for i in range(10): if i == 9: raise Exception() yield str(i) return Response(gen()) response = app.get_response(secretly_bad_response) with pytest.raises(Exception): list(response)
def test_bad_custom_handler(): app = Malt() @app.error_handler def dont_actually_handle(exc): raise exc def divide(a, b): return a / b def raise_exc(status=None, msg=None): raise HTTPException(status, msg) for args in ((divide, 1, 0), (raise_exc, ), (raise_exc, -1), (raise_exc, 418.), (raise_exc, 500), (raise_exc, 418), (raise_exc, 418, 'Good Stuff'), (raise_exc, 418., 'Bad Stuff'), (raise_exc, -1, 'Worse Stuff')): response = app.get_response(*args) assert '<h1>500 Internal Server Error</h1>' in response.response[0]
def test_default_handler(): app = Malt() def divide(a, b): return a / b def raise_exc(status=None, msg=None): raise HTTPException(status, msg) for args in ((divide, 1, 0), (raise_exc,), (raise_exc, -1), (raise_exc, 418.), (raise_exc, 500)): response = app.get_response(*args) assert '<h1>500 Internal Server Error</h1>' in response.response[0] response = app.get_response(raise_exc, 418) assert '<h1>418 I\'m a teapot</h1>' in response.response[0] response = app.get_response(raise_exc, 418, 'Good Stuff') assert '<h1>418 Good Stuff</h1>' in response.response[0] response = app.get_response(raise_exc, 418., 'Bad Stuff') assert '<h1>500 Bad Stuff</h1>' in response.response[0] response = app.get_response(raise_exc, -1, 'Worse Stuff') assert '<h1>500 Worse Stuff</h1>' in response.response[0]
def test_custom_handler(): app = Malt() @app.error_handler def handle(exc): return Response('%d\n%s' % (exc.status_code, exc.message), code=exc.status_code) def divide(a, b): return a / b def raise_exc(status=None, msg=None): raise HTTPException(status, msg) for args in ((divide, 1, 0), (raise_exc,), (raise_exc, -1), (raise_exc, 418.), (raise_exc, 500)): response = app.get_response(*args) assert '500\nInternal Server Error' in response.response[0] response = app.get_response(raise_exc, 418) assert '418\nI\'m a teapot' in response.response[0] response = app.get_response(raise_exc, 418, 'Good Stuff') assert '418\nGood Stuff' in response.response[0] response = app.get_response(raise_exc, 418., 'Bad Stuff') assert '500\nBad Stuff' in response.response[0] response = app.get_response(raise_exc, -1, 'Worse Stuff') assert '500\nWorse Stuff' in response.response[0]
def test_bad_custom_handler(): app = Malt() @app.error_handler def dont_actually_handle(exc): raise exc def divide(a, b): return a / b def raise_exc(status=None, msg=None): raise HTTPException(status, msg) for args in ((divide, 1, 0), (raise_exc,), (raise_exc, -1), (raise_exc, 418.), (raise_exc, 500), (raise_exc, 418), (raise_exc, 418, 'Good Stuff'), (raise_exc, 418., 'Bad Stuff'), (raise_exc, -1, 'Worse Stuff')): response = app.get_response(*args) assert '<h1>500 Internal Server Error</h1>' in response.response[0]
from malt import Malt, json from wsgiref.simple_server import make_server app = Malt() @app.error_handler def err_response(exc): return json({ 'msg': exc.message, }, pretty=True, code=exc.status_code) def headers_dict(headers): data = {} for key in headers: data[key] = headers[key] return data @app.get('^/ip$') def ip(request): return json({ 'origin': request.remote_addr, }, pretty=True) @app.get('^/user-agent$') def user_agent(request): return json({
# -*- coding: utf-8 -*- from malt import Malt, Response, HTTPException, json from wsgiref.simple_server import make_server app = Malt() tasks = [ { 'id': 0, 'name': 'Buy groceries' }, { 'id': 1, 'name': 'Clean the patio' }, { 'id': 2, 'name': 'Take over the world' }, ] @app.get('^/$') def print_url(request): return Response(request.url + '\n') @app.get('^/tasks$') def list_tasks(request): return json({'tasks': tasks})
# -*- coding: utf-8 -*- """Test that the package exists and has specified metadata.""" from malt import Malt, Request, Response, HTTPException, json as jsonify import json import pytest app = Malt() @app.before_request def before(request): if request.path == '/before': return Response(u'before_request returned\n') @app.after_request def after(request, response): if request.path == '/after': return Response(u'after_request returned\n') return response @app.error_handler def handle(error): return Response('%s\n' % error.message, code=error.status_code) @app.get('^/$') def root(request): return Response('Hello World!\n')