Exemple #1
0
def test_methods():
    app = Sanic('test_methods')

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

        def post(self, request):
            return text('I am post method')

        def put(self, request):
            return text('I am put method')

        def patch(self, request):
            return text('I am patch method')

        def delete(self, request):
            return text('I am delete method')

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

    request, response = sanic_endpoint_test(app, method="get")
    assert response.text == 'I am get method'
    request, response = sanic_endpoint_test(app, method="post")
    assert response.text == 'I am post method'
    request, response = sanic_endpoint_test(app, method="put")
    assert response.text == 'I am put method'
    request, response = sanic_endpoint_test(app, method="patch")
    assert response.text == 'I am patch method'
    request, response = sanic_endpoint_test(app, method="delete")
    assert response.text == 'I am delete method'
Exemple #2
0
def test_overload_routes():
    app = Sanic('test_dynamic_route')

    @app.route('/overload', methods=['GET'])
    async def handler1(request):
        return text('OK1')

    @app.route('/overload', methods=['POST', 'PUT'])
    async def handler2(request):
        return text('OK2')

    request, response = sanic_endpoint_test(app, 'get', uri='/overload')
    assert response.text == 'OK1'

    request, response = sanic_endpoint_test(app, 'post', uri='/overload')
    assert response.text == 'OK2'

    request, response = sanic_endpoint_test(app, 'put', uri='/overload')
    assert response.text == 'OK2'

    request, response = sanic_endpoint_test(app, 'delete', uri='/overload')
    assert response.status == 405

    with pytest.raises(RouteExists):
        @app.route('/overload', methods=['PUT', 'DELETE'])
        async def handler3(request):
            return text('Duplicated')
Exemple #3
0
def test_remove_route_without_clean_cache():
    app = Sanic('test_remove_static_route')

    async def handler(request):
        return text('OK')

    app.add_route(handler, '/test')

    request, response = sanic_endpoint_test(app, uri='/test')
    assert response.status == 200

    app.remove_route('/test', clean_cache=True)

    request, response = sanic_endpoint_test(app, uri='/test')
    assert response.status == 404

    app.add_route(handler, '/test')

    request, response = sanic_endpoint_test(app, uri='/test')
    assert response.status == 200

    app.remove_route('/test', clean_cache=False)

    request, response = sanic_endpoint_test(app, uri='/test')
    assert response.status == 200
Exemple #4
0
def test_unmergeable_overload_routes():
    app = Sanic('test_dynamic_route')

    @app.route('/overload_whole', methods=None)
    async def handler1(request):
        return text('OK1')

    with pytest.raises(RouteExists):
        @app.route('/overload_whole', methods=['POST', 'PUT'])
        async def handler2(request):
            return text('Duplicated')

    request, response = sanic_endpoint_test(app, 'get', uri='/overload_whole')
    assert response.text == 'OK1'

    request, response = sanic_endpoint_test(app, 'post', uri='/overload_whole')
    assert response.text == 'OK1'


    @app.route('/overload_part', methods=['GET'])
    async def handler1(request):
        return text('OK1')

    with pytest.raises(RouteExists):
        @app.route('/overload_part')
        async def handler2(request):
            return text('Duplicated')

    request, response = sanic_endpoint_test(app, 'get', uri='/overload_part')
    assert response.text == 'OK1'

    request, response = sanic_endpoint_test(app, 'post', uri='/overload_part')
    assert response.status == 405
Exemple #5
0
def test_remove_static_route():
    app = Sanic('test_remove_static_route')

    async def handler1(request):
        return text('OK1')

    async def handler2(request):
        return text('OK2')

    app.add_route(handler1, '/test')
    app.add_route(handler2, '/test2')

    request, response = sanic_endpoint_test(app, uri='/test')
    assert response.status == 200

    request, response = sanic_endpoint_test(app, uri='/test2')
    assert response.status == 200

    app.remove_route('/test')
    app.remove_route('/test2')

    request, response = sanic_endpoint_test(app, uri='/test')
    assert response.status == 404

    request, response = sanic_endpoint_test(app, uri='/test2')
    assert response.status == 404
Exemple #6
0
def test_remove_route_without_clean_cache():
    app = Sanic('test_remove_static_route')

    async def handler(request):
        return text('OK')

    app.add_route(handler, '/test')

    request, response = sanic_endpoint_test(app, uri='/test')
    assert response.status == 200

    app.remove_route('/test', clean_cache=True)

    request, response = sanic_endpoint_test(app, uri='/test')
    assert response.status == 404

    app.add_route(handler, '/test')

    request, response = sanic_endpoint_test(app, uri='/test')
    assert response.status == 200

    app.remove_route('/test', clean_cache=False)

    request, response = sanic_endpoint_test(app, uri='/test')
    assert response.status == 200
Exemple #7
0
def test_bp_with_host():
    app = Sanic('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 handler(request):
        return text('Hello subdomain!')

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

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

    assert response.text == 'Hello subdomain!'
Exemple #8
0
def test_bp_exception_handler():
    app = Sanic('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 = sanic_endpoint_test(app, uri='/1')
    assert response.status == 400


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

    request, response = sanic_endpoint_test(app, uri='/3')
    assert response.status == 200
Exemple #9
0
def test_bp_exception_handler():
    app = Sanic('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 = sanic_endpoint_test(app, uri='/1')
    assert response.status == 400


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

    request, response = sanic_endpoint_test(app, uri='/3')
    assert response.status == 200
Exemple #10
0
def test_remove_static_route():
    app = Sanic('test_remove_static_route')

    async def handler1(request):
        return text('OK1')

    async def handler2(request):
        return text('OK2')

    app.add_route(handler1, '/test')
    app.add_route(handler2, '/test2')

    request, response = sanic_endpoint_test(app, uri='/test')
    assert response.status == 200

    request, response = sanic_endpoint_test(app, uri='/test2')
    assert response.status == 200

    app.remove_route('/test')
    app.remove_route('/test2')

    request, response = sanic_endpoint_test(app, uri='/test')
    assert response.status == 404

    request, response = sanic_endpoint_test(app, uri='/test2')
    assert response.status == 404
Exemple #11
0
def test_several_bp_with_host():
    app = Sanic('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 handler2(request):
        return text('Hello2')

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

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

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

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

    assert response.text == 'Hello2'
    request, response = sanic_endpoint_test(app,
                                            uri='/test/other/',
                                            headers=headers)
    assert response.text == 'Hello3'
Exemple #12
0
def test_unexisting_methods():
    app = Sanic('test_unexisting_methods')

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

    app.add_route(DummyView.as_view(), '/')
    request, response = sanic_endpoint_test(app, method="get")
    assert response.text == 'I am get method'
    request, response = sanic_endpoint_test(app, method="post")
    assert response.text == 'Error: Method POST not allowed for URL /'
Exemple #13
0
def test_shorthand_routes_options():
    app = Sanic('test_shorhand_routes_options')

    @app.options('/options')
    def handler(request):
        return text('OK')

    request, response = sanic_endpoint_test(app, uri='/options', method='options')
    assert response.status == 200

    request, response = sanic_endpoint_test(app, uri='/options', method='get')
    assert response.status == 405
Exemple #14
0
def test_shorthand_routes_patch():
    app = Sanic('test_shorhand_routes_patch')

    @app.patch('/patch')
    def handler(request):
        return text('OK')

    request, response = sanic_endpoint_test(app, uri='/patch', method='patch')
    assert response.text == 'OK'

    request, response = sanic_endpoint_test(app, uri='/patch', method='get')
    assert response.status == 405
Exemple #15
0
def test_method_not_allowed():
    app = Sanic('test_method_not_allowed')

    @app.route('/test', methods=['GET'])
    async def handler(request):
        return text('OK')

    request, response = sanic_endpoint_test(app, uri='/test')
    assert response.status == 200

    request, response = sanic_endpoint_test(app, method='post', uri='/test')
    assert response.status == 405
Exemple #16
0
def test_custom_pagination(app):
    request, response = sanic_endpoint_test(app, uri='/job', method='get')
    page_one = json.loads(response.text)
    request, response = sanic_endpoint_test(app, uri='/job?page=2', method='get')
    page_two = json.loads(response.text)

    assert len(page_one.get('data')) == 1
    assert page_one.get('total_pages') == 2

    assert len(page_two.get('data')) == 1
    assert page_two.get('total_pages') == 2
    assert page_two.get('page') == 2
def test_method_not_allowed():
    app = Sanic('test_method_not_allowed')

    @app.route('/test', methods=['GET'])
    async def handler(request):
        return text('OK')

    request, response = sanic_endpoint_test(app, uri='/test')
    assert response.status == 200

    request, response = sanic_endpoint_test(app, method='post', uri='/test')
    assert response.status == 405
Exemple #18
0
def test_unexisting_methods():
    app = Sanic('test_unexisting_methods')

    class DummyView(HTTPMethodView):

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

    app.add_route(DummyView.as_view(), '/')
    request, response = sanic_endpoint_test(app, method="get")
    assert response.text == 'I am get method'
    request, response = sanic_endpoint_test(app, method="post")
    assert response.text == 'Error: Method POST not allowed for URL /'
Exemple #19
0
def test_vhosts_with_list():
    app = Sanic('test_vhosts')

    @app.route('/', host=["hello.com", "world.com"])
    async def handler(request):
        return text("Hello, world!")

    headers = {"Host": "hello.com"}
    request, response = sanic_endpoint_test(app, headers=headers)
    assert response.text == "Hello, world!"

    headers = {"Host": "world.com"}
    request, response = sanic_endpoint_test(app, headers=headers)
    assert response.text == "Hello, world!"
Exemple #20
0
def test_remove_dynamic_route():
    app = Sanic('test_remove_dynamic_route')

    async def handler(request, name):
        return text('OK')

    app.add_route(handler, '/folder/<name>')

    request, response = sanic_endpoint_test(app, uri='/folder/test123')
    assert response.status == 200

    app.remove_route('/folder/<name>')
    request, response = sanic_endpoint_test(app, uri='/folder/test123')
    assert response.status == 404
Exemple #21
0
def test_remove_dynamic_route():
    app = Sanic('test_remove_dynamic_route')

    async def handler(request, name):
        return text('OK')

    app.add_route(handler, '/folder/<name>')

    request, response = sanic_endpoint_test(app, uri='/folder/test123')
    assert response.status == 200

    app.remove_route('/folder/<name>')
    request, response = sanic_endpoint_test(app, uri='/folder/test123')
    assert response.status == 404
Exemple #22
0
def test_use_custom_protocol():
    server_kwargs = {'protocol': CustomHttpProtocol}
    request, response = sanic_endpoint_test(app,
                                            uri='/1',
                                            server_kwargs=server_kwargs)
    assert response.status == 200
    assert response.text == 'OK'
Exemple #23
0
def test_static_routes():
    app = Sanic('test_dynamic_route')

    @app.route('/test')
    async def handler1(request):
        return text('OK1')

    @app.route('/pizazz')
    async def handler2(request):
        return text('OK2')

    request, response = sanic_endpoint_test(app, uri='/test')
    assert response.text == 'OK1'

    request, response = sanic_endpoint_test(app, uri='/pizazz')
    assert response.text == 'OK2'
Exemple #24
0
def test_update_record(app):
    payload = {'email': '*****@*****.**'}
    headers = {'content-type': 'application/json'}
    request, response = sanic_endpoint_test(app,
                                            uri='/person/1',
                                            data=json.dumps(payload),
                                            headers=headers,
                                            method='put')

    expected_response = {
        'data': {
            'id': 1,
            'name': 'Sanic the Hedgehog',
            'job': {
                'id': 1,
                'name': 'Space garbage man',
                'description': 'Collects garbage in space',
                'base_pay': 15
            },
            'email': '*****@*****.**'
        },
        'status_code': 200,
        'message': 'OK'
    }

    assert json.loads(response.text) == expected_response
Exemple #25
0
def test_with_middleware_response():
    app = Sanic('test_with_middleware_response')

    results = []

    @app.middleware('request')
    async def process_response(request):
        results.append(request)

    @app.middleware('response')
    async def process_response(request, response):
        results.append(request)
        results.append(response)

    class DummyView(HTTPMethodView):

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

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

    request, response = sanic_endpoint_test(app)

    assert response.text == 'I am get method'
    assert type(results[0]) is Request
    assert type(results[1]) is Request
    assert isinstance(results[2], HTTPResponse)
Exemple #26
0
def test_methods(method):
    app = Sanic('test_methods')

    class DummyView(HTTPMethodView):

        def get(self, request):
            return text('', headers={'method': 'GET'})

        def post(self, request):
            return text('', headers={'method': 'POST'})

        def put(self, request):
            return text('', headers={'method': 'PUT'})

        def head(self, request):
            return text('', headers={'method': 'HEAD'})

        def options(self, request):
            return text('', headers={'method': 'OPTIONS'})

        def patch(self, request):
            return text('', headers={'method': 'PATCH'})

        def delete(self, request):
            return text('', headers={'method': 'DELETE'})

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

    request, response = sanic_endpoint_test(app, method=method)
    assert response.headers['method'] == method
Exemple #27
0
def test_get_backrefs(app):
    request, response = sanic_endpoint_test(app,
                                            uri='/job/1?backrefs=true',
                                            method='get')
    expected_repsonse = {
        'data': {
            'id':
            1,
            'name':
            "Space garbage man",
            'description':
            "Collects garbage in space",
            'base_pay':
            15,
            'person_job': [{
                'id': 1,
                'name': "Sanic the Hedgehog",
                'email': "*****@*****.**"
            }]
        },
        'status_code': 200,
        'message': "OK"
    }

    assert expected_repsonse == json.loads(response.text)
Exemple #28
0
def test_chained_redirect(redirect_app):
    """Test sanic_endpoint_test is working for redirection"""
    request, response = sanic_endpoint_test(redirect_app, uri='/1')
    assert request.url.endswith('/1')
    assert response.status == 200
    assert response.text == 'OK'
    assert response.url.endswith('/3')
Exemple #29
0
def test_create_new_record(app):
    payload = {
        'name': 'Knackles the Echidna',
        'email': '*****@*****.**',
        'job': 1
    }
    headers = {'content-type': 'application/json'}
    request, response = sanic_endpoint_test(app,
                                            uri='/person',
                                            data=json.dumps(payload),
                                            headers=headers,
                                            method='post')
    expected_response = {
        'data': {
            'id': 2,
            'name': 'Knackles the Echidna',
            'job': {
                'id': 1,
                'name': 'Space garbage man',
                'description': 'Collects garbage in space',
                'base_pay': 15
            },
            'email': '*****@*****.**'
        },
        'status_code': 200,
        'message': "Resource with id '2' was created!"
    }

    assert json.loads(response.text) == expected_response
Exemple #30
0
def test_bp_listeners():
    app = Sanic('test_middleware')
    blueprint = Blueprint('test_middleware')

    order = []

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

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

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

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

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

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

    app.blueprint(blueprint)

    request, response = sanic_endpoint_test(app, uri='/')

    assert order == [1,2,3,4,5,6]
Exemple #31
0
def test_chained_redirect(redirect_app):
    """Test sanic_endpoint_test is working for redirection"""
    request, response = sanic_endpoint_test(redirect_app, uri='/1')
    assert request.url.endswith('/1')
    assert response.status == 200
    assert response.text == 'OK'
    assert response.url.endswith('/3')
def test_dynamic_route_int():
    app = Sanic('test_dynamic_route_int')

    results = []

    @app.route('/folder/<folder_id:int>')
    async def handler(request, folder_id):
        results.append(folder_id)
        return text('OK')

    request, response = sanic_endpoint_test(app, uri='/folder/12345')
    assert response.text == 'OK'
    assert type(results[0]) is int

    request, response = sanic_endpoint_test(app, uri='/folder/asdf')
    assert response.status == 404
Exemple #33
0
def test_bp_listeners():
    app = Sanic('test_middleware')
    blueprint = Blueprint('test_middleware')

    order = []

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

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

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

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

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

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

    app.blueprint(blueprint)

    request, response = sanic_endpoint_test(app, uri='/')

    assert order == [1,2,3,4,5,6]
Exemple #34
0
def test_static_file(static_file_path, static_file_content):
    app = Sanic('test_static')
    app.static('/testing.file', static_file_path)

    request, response = sanic_endpoint_test(app, uri='/testing.file')
    assert response.status == 200
    assert response.body == static_file_content
Exemple #35
0
def test_with_middleware_response():
    app = Sanic('test_with_middleware_response')

    results = []

    @app.middleware('request')
    async def process_response(request):
        results.append(request)

    @app.middleware('response')
    async def process_response(request, response):
        results.append(request)
        results.append(response)

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

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

    request, response = sanic_endpoint_test(app)

    assert response.text == 'I am get method'
    assert type(results[0]) is Request
    assert type(results[1]) is Request
    assert issubclass(type(results[2]), HTTPResponse)
def test_payload_too_large_at_on_header_default():
    data = 'a' * 1000
    response = sanic_endpoint_test(
        on_header_default_app, method='post', uri='/1',
        gather_request=False, data=data)
    assert response.status == 413
    assert response.text == 'Error: Payload Too Large'
def test_static_routes():
    app = Sanic('test_dynamic_route')

    @app.route('/test')
    async def handler1(request):
        return text('OK1')

    @app.route('/pizazz')
    async def handler2(request):
        return text('OK2')

    request, response = sanic_endpoint_test(app, uri='/test')
    assert response.text == 'OK1'

    request, response = sanic_endpoint_test(app, uri='/pizazz')
    assert response.text == 'OK2'
Exemple #38
0
def test_dynamic_route_int():
    app = Sanic('test_dynamic_route_int')

    results = []

    @app.route('/folder/<folder_id:int>')
    async def handler(request, folder_id):
        results.append(folder_id)
        return text('OK')

    request, response = sanic_endpoint_test(app, uri='/folder/12345')
    assert response.text == 'OK'
    assert type(results[0]) is int

    request, response = sanic_endpoint_test(app, uri='/folder/asdf')
    assert response.status == 404
Exemple #39
0
def test_get_docs():
    app = Sanic('test_get')

    app.blueprint(openapi_blueprint)

    request, response = sanic_endpoint_test(app, 'get', '/openapi/spec.json')

    assert response.status == 200
def test_static_add_route():
    app = Sanic('test_static_add_route')

    async def handler1(request):
        return text('OK1')

    async def handler2(request):
        return text('OK2')

    app.add_route(handler1, '/test')
    app.add_route(handler2, '/test2')

    request, response = sanic_endpoint_test(app, uri='/test')
    assert response.text == 'OK1'

    request, response = sanic_endpoint_test(app, uri='/test2')
    assert response.text == 'OK2'
def test_exception_in_exception_handler_debug_off(exception_app):
    """Test that an exception thrown in an error handler is handled"""
    request, response = sanic_endpoint_test(
        exception_app,
        uri='/error_in_error_handler_handler',
        debug=True)
    assert response.status == 500
    assert response.body.startswith(b'Exception raised in exception ')
def test_use_custom_protocol():
    server_kwargs = {
        'protocol': CustomHttpProtocol
    }
    request, response = sanic_endpoint_test(app, uri='/1',
                                            server_kwargs=server_kwargs)
    assert response.status == 200
    assert response.text == 'OK'
Exemple #43
0
def test_static_add_route():
    app = Sanic('test_static_add_route')

    async def handler1(request):
        return text('OK1')

    async def handler2(request):
        return text('OK2')

    app.add_route(handler1, '/test')
    app.add_route(handler2, '/test2')

    request, response = sanic_endpoint_test(app, uri='/test')
    assert response.text == 'OK1'

    request, response = sanic_endpoint_test(app, uri='/test2')
    assert response.text == 'OK2'
def test_exception_in_exception_handler_debug_off(exception_app):
    """Test that an exception thrown in an error handler is handled"""
    request, response = sanic_endpoint_test(
        exception_app,
        uri='/error_in_error_handler_handler',
        debug=False)
    assert response.status == 500
    assert response.body == b'An error occurred while handling an error'
Exemple #45
0
def test_dynamic_route_unhashable():
    app = Sanic('test_dynamic_route_unhashable')

    @app.route('/folder/<unhashable:[A-Za-z0-9/]+>/end/')
    async def handler(request, unhashable):
        return text('OK')

    request, response = sanic_endpoint_test(app, uri='/folder/test/asdf/end/')
    assert response.status == 200

    request, response = sanic_endpoint_test(app, uri='/folder/test///////end/')
    assert response.status == 200

    request, response = sanic_endpoint_test(app, uri='/folder/test/end/')
    assert response.status == 200

    request, response = sanic_endpoint_test(app, uri='/folder/test/nope/')
    assert response.status == 404
Exemple #46
0
def test_redirect_headers_none(redirect_app):
    request, response = sanic_endpoint_test(
        redirect_app, method="get",
        uri="/redirect_init",
        headers=None,
        allow_redirects=False)

    assert response.status == 302
    assert response.headers["Location"] == "/redirect_target"
Exemple #47
0
def test_utf8_query_string():
    app = Sanic('test_utf8_query_string')

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

    request, response = sanic_endpoint_test(app, params=[("utf8", '✓')])
    assert request.args.get('utf8') == '✓'
Exemple #48
0
def test_utf8_response():
    app = Sanic('test_utf8_response')

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

    request, response = sanic_endpoint_test(app)
    assert response.text == '✓'
Exemple #49
0
def test_vhosts():
    app = Sanic('test_vhosts')

    @app.route('/', host="example.com")
    async def handler(request):
        return text("You're at example.com!")

    @app.route('/', host="subdomain.example.com")
    async def handler(request):
        return text("You're at subdomain.example.com!")

    headers = {"Host": "example.com"}
    request, response = sanic_endpoint_test(app, headers=headers)
    assert response.text == "You're at example.com!"

    headers = {"Host": "subdomain.example.com"}
    request, response = sanic_endpoint_test(app, headers=headers)
    assert response.text == "You're at subdomain.example.com!"
Exemple #50
0
def test_dynamic_route_regex():
    app = Sanic('test_dynamic_route_regex')

    @app.route('/folder/<folder_id:[A-Za-z0-9]{0,4}>')
    async def handler(request, folder_id):
        return text('OK')

    request, response = sanic_endpoint_test(app, uri='/folder/test')
    assert response.status == 200

    request, response = sanic_endpoint_test(app, uri='/folder/test1')
    assert response.status == 404

    request, response = sanic_endpoint_test(app, uri='/folder/test-123')
    assert response.status == 404

    request, response = sanic_endpoint_test(app, uri='/folder/')
    assert response.status == 200
Exemple #51
0
def test_simple_url_for_getting(simple_app):
    for letter in string.ascii_letters:
        url = simple_app.url_for(letter)

        assert url == '/{}'.format(letter)
        request, response = sanic_endpoint_test(
            simple_app, uri=url)
        assert response.status == 200
        assert response.text == letter
Exemple #52
0
def test_sync():
    app = Sanic('test_text')

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

    request, response = sanic_endpoint_test(app)

    assert response.text == 'Hello'