Beispiel #1
0
def test_exception_handler():
    app = Sanic('test_text')

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

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

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

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

    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

    request, response = sanic_endpoint_test(app, uri='/random')
    assert response.status == 200
    assert response.text == 'OK'
Beispiel #2
0
def test_exception_response():
    app = Sanic('test_text')

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

    @app.route('/error')
    def handler_error(request):
        raise ServerError("OK")

    @app.route('/404')
    def handler_404(request):
        raise NotFound("OK")

    @app.route('/invalid')
    def handler_invalid(request):
        raise InvalidUsage("OK")

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

    request, response = sanic_endpoint_test(app, uri='/error')
    assert response.status == 500

    request, response = sanic_endpoint_test(app, uri='/invalid')
    assert response.status == 400

    request, response = sanic_endpoint_test(app, uri='/404')
    assert response.status == 404
Beispiel #3
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
Beispiel #4
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') == '✓'
Beispiel #5
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 == '✓'
Beispiel #6
0
def test_dynamic_route_regex():
    app = Sanic('test_dynamic_route_int')

    @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
Beispiel #7
0
def skip_test_utf8_route():
	app = Sanic('skip_test_utf8_route')

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

	# UTF-8 Paths are not supported
	request, response = sanic_endpoint_test(app, route='/✓', uri='/✓')
	assert response.text == 'OK'
Beispiel #8
0
def test_dynamic_route_number():
    app = Sanic('test_dynamic_route_int')

    results = []

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

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

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

    request, response = sanic_endpoint_test(app, uri='/weight/1234-56')
    assert response.status == 404
Beispiel #9
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'
Beispiel #10
0
def test_utf8_post_json():
	app = Sanic('test_utf8_post_json')

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

	payload = {'test': '✓'}
	headers = {'content-type': 'application/json'}

	request, response = sanic_endpoint_test(app, data=json_dumps(payload), headers=headers)

	assert request.json.get('test') == '✓'
	assert response.text == 'OK'
Beispiel #11
0
def test_query_string():
    app = Sanic('test_query_string')

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

    request, response = sanic_endpoint_test(app,
                                            params=[("test1", 1),
                                                    ("test2", "false"),
                                                    ("test2", "true")])

    assert request.args.get('test1') == '1'
    assert request.args.get('test2') == 'false'
Beispiel #12
0
def test_dynamic_route_string():
    app = Sanic('test_dynamic_route_string')

    results = []

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

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

    assert response.text == 'OK'
    assert results[0] == 'test123'
Beispiel #13
0
def test_middleware_override_response():
    app = Sanic('test_middleware_override_response')

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

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

    request, response = sanic_endpoint_test(app)

    assert response.status == 200
    assert response.text == 'OK'
Beispiel #14
0
def test_middleware_override_request():
    app = Sanic('test_middleware_override_request')

    @app.middleware
    async def halt_request(request):
        return text('OK')

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

    response = sanic_endpoint_test(app, gather_request=False)

    assert response.status == 200
    assert response.text == 'OK'
Beispiel #15
0
def test_json():
    app = Sanic('test_json')

    @app.route('/')
    async def handler(request):
        return json({"test": True})

    request, response = sanic_endpoint_test(app)

    try:
        results = json_loads(response.text)
    except:
        raise ValueError(
            "Expected JSON response but got '{}'".format(response))

    assert results.get('test') == True
Beispiel #16
0
def test_middleware_request():
    app = Sanic('test_middleware_request')

    results = []

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

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

    request, response = sanic_endpoint_test(app)

    assert response.text == 'OK'
    assert type(results[0]) is Request
Beispiel #17
0
def test_middleware_response():
    app = Sanic('test_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)

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

    request, response = sanic_endpoint_test(app)

    assert response.text == 'OK'
    assert type(results[0]) is Request
    assert type(results[1]) is Request
    assert issubclass(type(results[2]), HTTPResponse)