Пример #1
0
def blueprint_app():
    app = Mach9('blueprints')

    first_print = Blueprint('first', url_prefix='/first')
    second_print = Blueprint('second', url_prefix='/second')

    @first_print.route('/foo')
    def foo():
        return text('foo from first')

    @first_print.route('/foo/<param>')
    def foo_with_param(request, param):
        return text('foo from first : {}'.format(param))

    @second_print.route('/foo')  # noqa
    def foo():
        return text('foo from second')

    @second_print.route('/foo/<param>')  # noqa
    def foo_with_param(request, param):
        return text('foo from second : {}'.format(param))

    app.blueprint(first_print)
    app.blueprint(second_print)

    return app
Пример #2
0
def test_with_bp_with_url_prefix():
    app = Mach9('test_with_bp_with_url_prefix')
    bp = Blueprint('test_text', url_prefix='/test1')

    class DummyView(HTTPMethodView):
        def get(self, request):
            return text('I am get method')

    bp.add_route(DummyView.as_view(), '/')

    app.blueprint(bp)
    request, response = app.test_client.get('/test1/')

    assert response.text == 'I am get method'
Пример #3
0
def test_bp_static():
    current_file = inspect.getfile(inspect.currentframe())
    with open(current_file, 'rb') as file:
        current_file_contents = file.read()

    app = Mach9('test_static')
    blueprint = Blueprint('test_static')

    blueprint.static('/testing.file', current_file)

    app.blueprint(blueprint)

    request, response = app.test_client.get('/testing.file')
    assert response.status == 200
    assert response.body == current_file_contents
Пример #4
0
def test_with_bp():
    app = Mach9('test_with_bp')
    bp = Blueprint('test_text')

    class DummyView(HTTPMethodView):
        def get(self, request):
            assert request.stream is None
            return text('I am get method')

    bp.add_route(DummyView.as_view(), '/')

    app.blueprint(bp)
    request, response = app.test_client.get('/')

    assert response.text == 'I am get method'
Пример #5
0
def test_bp_with_host():
    app = Mach9('test_bp_host')
    bp = Blueprint('test_bp_host', url_prefix='/test1', host="example.com")

    @bp.route('/')
    def handler(request):
        return text('Hello')

    @bp.route('/', host="sub.example.com")
    def handler3(request):
        return text('Hello subdomain!')

    app.blueprint(bp)
    headers = {"Host": "example.com"}
    request, response = app.test_client.get(
        '/test1/',
        headers=headers)
    assert response.text == 'Hello'

    headers = {"Host": "sub.example.com"}
    request, response = app.test_client.get(
        '/test1/',
        headers=headers)

    assert response.text == 'Hello subdomain!'
Пример #6
0
def test_bp_strict_slash():
    app = Mach9('test_route_strict_slash')
    bp = Blueprint('test_text')

    @bp.get('/get', strict_slashes=True)
    def handler1(request):
        return text('OK')

    @bp.post('/post/', strict_slashes=True)
    def handler2(request):
        return text('OK')

    app.blueprint(bp)

    request, response = app.test_client.get('/get')
    assert response.text == 'OK'

    request, response = app.test_client.get('/get/')
    assert response.status == 404

    request, response = app.test_client.post('/post/')
    assert response.text == 'OK'

    request, response = app.test_client.post('/post')
    assert response.status == 404
Пример #7
0
def test_bp_listeners():
    app = Mach9('test_middleware')
    blueprint = Blueprint('test_middleware')

    order = []

    @blueprint.listener('before_server_start')
    def handler_1(mach9, loop):
        order.append(1)

    @blueprint.listener('after_server_start')
    def handler_2(mach9, loop):
        order.append(2)

    @blueprint.listener('after_server_start')
    def handler_3(mach9, loop):
        order.append(3)

    @blueprint.listener('before_server_stop')
    def handler_4(mach9, loop):
        order.append(5)

    @blueprint.listener('before_server_stop')
    def handler_5(mach9, loop):
        order.append(4)

    @blueprint.listener('after_server_stop')
    def handler_6(mach9, loop):
        order.append(6)

    app.blueprint(blueprint)

    request, response = app.test_client.get('/')

    assert order == [1, 2, 3, 4, 5, 6]
Пример #8
0
def test_bp_exception_handler():
    app = Mach9('test_middleware')
    blueprint = Blueprint('test_middleware')

    @blueprint.route('/1')
    def handler_1(request):
        raise InvalidUsage("OK")

    @blueprint.route('/2')
    def handler_2(request):
        raise ServerError("OK")

    @blueprint.route('/3')
    def handler_3(request):
        raise NotFound("OK")

    @blueprint.exception(NotFound, ServerError)
    def handler_exception(request, exception):
        return text("OK")

    app.blueprint(blueprint)

    request, response = app.test_client.get('/1')
    assert response.status == 400

    request, response = app.test_client.get('/2')
    assert response.status == 200
    assert response.text == 'OK'

    request, response = app.test_client.get('/3')
    assert response.status == 200
Пример #9
0
def test_several_bp_with_host():
    app = Mach9('test_text')
    bp = Blueprint('test_text',
                   url_prefix='/test',
                   host="example.com")
    bp2 = Blueprint('test_text2',
                    url_prefix='/test',
                    host="sub.example.com")

    @bp.route('/')
    def handler(request):
        return text('Hello')

    @bp2.route('/')
    def handler5(request):
        return text('Hello2')

    @bp2.route('/other/')
    def handler6(request):
        return text('Hello3')

    app.blueprint(bp)
    app.blueprint(bp2)

    assert bp.host == "example.com"
    headers = {"Host": "example.com"}
    request, response = app.test_client.get(
        '/test/',
        headers=headers)
    assert response.text == 'Hello'

    assert bp2.host == "sub.example.com"
    headers = {"Host": "sub.example.com"}
    request, response = app.test_client.get(
        '/test/',
        headers=headers)

    assert response.text == 'Hello2'
    request, response = app.test_client.get(
        '/test/other/',
        headers=headers)
    assert response.text == 'Hello3'
Пример #10
0
def test_several_bp_with_url_prefix():
    app = Mach9('test_text')
    bp = Blueprint('test_text', url_prefix='/test1')
    bp2 = Blueprint('test_text2', url_prefix='/test2')

    @bp.route('/')
    def handler(request):
        return text('Hello')

    @bp2.route('/')
    def handler2(request):
        return text('Hello2')

    app.blueprint(bp)
    app.blueprint(bp2)
    request, response = app.test_client.get('/test1/')
    assert response.text == 'Hello'

    request, response = app.test_client.get('/test2/')
    assert response.text == 'Hello2'
Пример #11
0
def test_bp():
    app = Mach9('test_text')
    bp = Blueprint('test_text')

    @bp.route('/')
    def handler(request):
        return text('Hello')

    app.blueprint(bp)
    request, response = app.test_client.get('/')

    assert response.text == 'Hello'
Пример #12
0
def test_bp_middleware():
    app = Mach9('test_middleware')
    blueprint = Blueprint('test_middleware')

    @blueprint.middleware('response')
    async def process_response(request, response):
        return text('OK')

    @app.route('/')
    async def handler(request):
        return text('FAIL')

    app.blueprint(blueprint)

    request, response = app.test_client.get('/')

    assert response.status == 200
    assert response.text == 'OK'
Пример #13
0
def test_request_stream():
    '''test for complex application'''

    bp = Blueprint('test_blueprint_request_stream')
    app = Mach9('test_request_stream')

    class SimpleView(HTTPMethodView):
        def get(self, request):
            assert request.stream is None
            return text('OK')

        @stream_decorator
        async def post(self, request):
            assert isinstance(request.stream, BodyChannel)
            result = ''
            while True:
                body_chunk = await request.stream.receive()
                if body_chunk['more_content'] is False:
                    break
                result += body_chunk['content'].decode('utf-8')
            return text(result)

    @app.post('/stream', stream=True)
    async def handler(request):
        assert isinstance(request.stream, BodyChannel)
        result = ''
        while True:
            body_chunk = await request.stream.receive()
            if body_chunk['more_content'] is False:
                break
            result += body_chunk['content'].decode('utf-8')
        return text(result)

    @app.get('/get')
    async def get(request):
        assert request.stream is None
        return text('OK')

    @bp.post('/bp_stream', stream=True)
    async def bp_stream(request):
        assert isinstance(request.stream, BodyChannel)
        result = ''
        while True:
            body_chunk = await request.stream.receive()
            if body_chunk['more_content'] is False:
                break
            result += body_chunk['content'].decode('utf-8')
        return text(result)

    @bp.get('/bp_get')
    async def bp_get(request):
        assert request.stream is None
        return text('OK')

    def get_handler(request):
        assert request.stream is None
        return text('OK')

    async def post_handler(request):
        assert isinstance(request.stream, BodyChannel)
        result = ''
        while True:
            body_chunk = await request.stream.receive()
            if body_chunk['more_content'] is False:
                break
            result += body_chunk['content'].decode('utf-8')
        return text(result)

    app.add_route(SimpleView.as_view(), '/method_view')

    view = CompositionView()
    view.add(['GET'], get_handler)
    view.add(['POST'], post_handler, stream=True)

    app.blueprint(bp)

    app.add_route(view, '/composition_view')

    request, response = app.test_client.get('/method_view')
    assert response.status == 200
    assert response.text == 'OK'

    request, response = app.test_client.post('/method_view', data=data)
    assert response.status == 200
    assert response.text == data

    request, response = app.test_client.get('/composition_view')
    assert response.status == 200
    assert response.text == 'OK'

    request, response = app.test_client.post('/composition_view', data=data)
    assert response.status == 200
    assert response.text == data

    request, response = app.test_client.get('/get')
    assert response.status == 200
    assert response.text == 'OK'

    request, response = app.test_client.post('/stream', data=data)
    assert response.status == 200
    assert response.text == data

    request, response = app.test_client.get('/bp_get')
    assert response.status == 200
    assert response.text == 'OK'

    request, response = app.test_client.post('/bp_stream', data=data)
    assert response.status == 200
    assert response.text == data
Пример #14
0
def test_request_stream_blueprint():

    app = Mach9('test_request_stream_blueprint')
    bp = Blueprint('test_blueprint_request_stream_blueprint')

    @app.get('/get')
    async def get(request):
        assert request.stream is None
        return text('GET')

    @bp.head('/head')
    async def head(request):
        assert request.stream is None
        return text('HEAD')

    @bp.delete('/delete')
    async def delete(request):
        assert request.stream is None
        return text('DELETE')

    @bp.options('/options')
    async def options(request):
        assert request.stream is None
        return text('OPTIONS')

    @bp.post('/_post/<id>')
    async def _post(request, id):
        assert request.stream is None
        return text('_POST')

    @bp.post('/post/<id>', stream=True)
    async def post(request, id):
        assert isinstance(request.stream, BodyChannel)
        result = ''
        while True:
            body_chunk = await request.stream.receive()
            if body_chunk['more_content'] is False:
                break
            result += body_chunk['content'].decode('utf-8')
        return text(result)

    @bp.put('/_put')
    async def _put(request):
        assert request.stream is None
        return text('_PUT')

    @bp.put('/put', stream=True)
    async def put(request):
        assert isinstance(request.stream, BodyChannel)
        result = ''
        while True:
            body_chunk = await request.stream.receive()
            if body_chunk['more_content'] is False:
                break
            result += body_chunk['content'].decode('utf-8')
        return text(result)

    @bp.patch('/_patch')
    async def _patch(request):
        assert request.stream is None
        return text('_PATCH')

    @bp.patch('/patch', stream=True)
    async def patch(request):
        assert isinstance(request.stream, BodyChannel)
        result = ''
        while True:
            body_chunk = await request.stream.receive()
            if body_chunk['more_content'] is False:
                break
            result += body_chunk['content'].decode('utf-8')
        return text(result)

    app.blueprint(bp)

    request, response = app.test_client.get('/get')
    assert response.status == 200
    assert response.text == 'GET'

    request, response = app.test_client.head('/head')
    assert response.status == 200
    assert response.text == ''

    request, response = app.test_client.delete('/delete')
    assert response.status == 200
    assert response.text == 'DELETE'

    request, response = app.test_client.options('/options')
    assert response.status == 200
    assert response.text == 'OPTIONS'

    request, response = app.test_client.post('/_post/1', data=data)
    assert response.status == 200
    assert response.text == '_POST'

    request, response = app.test_client.post('/post/1', data=data)
    assert response.status == 200
    assert response.text == data

    request, response = app.test_client.put('/_put', data=data)
    assert response.status == 200
    assert response.text == '_PUT'

    request, response = app.test_client.put('/put', data=data)
    assert response.status == 200
    assert response.text == data

    request, response = app.test_client.patch('/_patch', data=data)
    assert response.status == 200
    assert response.text == '_PATCH'

    request, response = app.test_client.patch('/patch', data=data)
    assert response.status == 200
    assert response.text == data
Пример #15
0
def test_bp_shorthand():
    app = Mach9('test_shorhand_routes')
    blueprint = Blueprint('test_shorhand_routes')

    @blueprint.get('/get')
    def handler8(request):
        assert request.stream is None
        return text('OK')

    @blueprint.put('/put')
    def handler9(request):
        assert request.stream is None
        return text('OK')

    @blueprint.post('/post')
    def handler10(request):
        assert request.stream is None
        return text('OK')

    @blueprint.head('/head')
    def handler11(request):
        assert request.stream is None
        return text('OK')

    @blueprint.options('/options')
    def handler12(request):
        assert request.stream is None
        return text('OK')

    @blueprint.patch('/patch')
    def handler13(request):
        assert request.stream is None
        return text('OK')

    @blueprint.delete('/delete')
    def handler14(request):
        assert request.stream is None
        return text('OK')

    app.blueprint(blueprint)

    request, response = app.test_client.get('/get')
    assert response.text == 'OK'

    request, response = app.test_client.post('/get')
    assert response.status == 405

    request, response = app.test_client.put('/put')
    assert response.text == 'OK'

    request, response = app.test_client.get('/post')
    assert response.status == 405

    request, response = app.test_client.post('/post')
    assert response.text == 'OK'

    request, response = app.test_client.get('/post')
    assert response.status == 405

    request, response = app.test_client.head('/head')
    assert response.status == 200

    request, response = app.test_client.get('/head')
    assert response.status == 405

    request, response = app.test_client.options('/options')
    assert response.text == 'OK'

    request, response = app.test_client.get('/options')
    assert response.status == 405

    request, response = app.test_client.patch('/patch')
    assert response.text == 'OK'

    request, response = app.test_client.get('/patch')
    assert response.status == 405

    request, response = app.test_client.delete('/delete')
    assert response.text == 'OK'

    request, response = app.test_client.get('/delete')
    assert response.status == 405