granian/tests/test_asgi.py
Giovanni Barillari c95fd0cba7
Align codestyle (#124)
* Add lint and format tools and config

* Format Python code

* Format Rust code

* Add lint CI workflow
2023-09-25 18:00:09 +02:00

61 lines
2 KiB
Python

import httpx
import pytest
@pytest.mark.asyncio
@pytest.mark.parametrize('threading_mode', ['runtime', 'workers'])
async def test_scope(asgi_server, threading_mode):
async with asgi_server(threading_mode) as port:
res = httpx.get(f'http://localhost:{port}/info?test=true')
assert res.status_code == 200
assert res.headers['content-type'] == 'application/json'
data = res.json()
assert data['asgi'] == {'version': '3.0', 'spec_version': '2.3'}
assert data['type'] == 'http'
assert data['http_version'] == '1.1'
assert data['scheme'] == 'http'
assert data['method'] == 'GET'
assert data['path'] == '/info'
assert data['query_string'] == 'test=true'
assert data['headers']['host'] == f'localhost:{port}'
@pytest.mark.asyncio
@pytest.mark.parametrize('threading_mode', ['runtime', 'workers'])
async def test_body(asgi_server, threading_mode):
async with asgi_server(threading_mode) as port:
res = httpx.post(f'http://localhost:{port}/echo', content='test')
assert res.status_code == 200
assert res.text == 'test'
@pytest.mark.asyncio
@pytest.mark.parametrize('threading_mode', ['runtime', 'workers'])
async def test_body_large(asgi_server, threading_mode):
data = ''.join([f'{idx}test'.zfill(8) for idx in range(0, 5000)])
async with asgi_server(threading_mode) as port:
res = httpx.post(f'http://localhost:{port}/echo', content=data)
assert res.status_code == 200
assert res.text == data
@pytest.mark.asyncio
@pytest.mark.parametrize('threading_mode', ['runtime', 'workers'])
async def test_app_error(asgi_server, threading_mode):
async with asgi_server(threading_mode) as port:
res = httpx.get(f'http://localhost:{port}/err_app')
assert res.status_code == 500
@pytest.mark.asyncio
@pytest.mark.parametrize('threading_mode', ['runtime', 'workers'])
async def test_protocol_error(asgi_server, threading_mode):
async with asgi_server(threading_mode) as port:
res = httpx.get(f'http://localhost:{port}/err_proto')
assert res.status_code == 500