async def test_request_files_failure(): client = http.AsyncClient(unit.fake_url) client._AsyncClient__session = MockSessions(resp={}, code=204, content='', failure=True) with pytest.raises(http.RequestException): m = unit.File() m.files = None await client.request(m)
async def test_request_mdws_nc_not_copy(): client = http.AsyncClient(unit.fake_url, mdws_nc=[middleware_not_copy]) client._AsyncClient__session = MockSessions(resp={}, code=204, content='') m = unit.Get() resp, status_code = await client.request(m) assert resp == {} assert unit.Get().__dict__ != m.__dict__ assert status_code == 204
async def test_request_files_ok(): client = http.AsyncClient(unit.fake_url) client._AsyncClient__session = MockSessions(resp={}, code=204, content='') m = unit.File() m.files = None resp, status_code = await client.request(m) assert resp == {} assert status_code == 204
async def test_request_post_failure(): client = http.AsyncClient(tests.fake_url) client._AsyncClient__session = MockSessions(resp={}, code=204, content='', failure=True) with pytest.raises(TestException): await client.request(tests.Post())
async def test_request_get_body_failure(): client = http.AsyncClient(tests.fake_url) client._AsyncClient__session = MockSessions(resp={}, code=204, content='', failure=True) with pytest.raises(AssertionError): await client.request(tests.GetWithBody())
async def test_request_mdws(): client = http.AsyncClient(tests.fake_url, mdws=[middleware]) client._AsyncClient__session = MockSessions(resp={}, code=204, content='') m = tests.Get() resp, status_code = await client.request(m) assert resp == {} assert tests.Get().__dict__ == m.__dict__ assert status_code == 204
async def test_request_get_ok(): client = http.AsyncClient(tests.fake_url) client._AsyncClient__session = MockSessions(resp={}, code=204, content='', failure=False) resp, status_code = await client.request(tests.Get()) assert resp == {} assert status_code == 204
async def test_request_data_ok(): # Mock requests # http.requests = MockRequests(resp=None, code=204, content='') http.requests = req data = {"data1": "data1", "data2": 12345, "data3": False } client = http.AsyncClient(f"http://localhost:{port}") resp, status_code = await client.request(unit.DataDict(data)) assert resp == {'success': True} assert status_code == 200
async def test_request_multiple_files_ok(): # Mock requests # http.requests = MockRequests(resp=None, code=204, content='') http.requests = req src_path = Path.cwd().joinpath("tests", "data") f_json = 'data_file1.json' f1 = 'data_file1.txt' f2 = 'data_file2.txt' multiple_files = [http.AsyncFile('files', open(src_path.joinpath(f_json), 'rb'), f_json, 'application/json'), http.AsyncFile('files', open(src_path.joinpath(f1), 'rb'), f1, None), http.AsyncFile('files', open(src_path.joinpath(f2), 'rb'), f2, None), ] client = http.AsyncClient(f"http://localhost:{port}") resp, status_code = await client.request(unit.ManyFilesAsync(multiple_files)) assert resp == {'success': True} assert status_code == 200
async def test_request_put_ok(): client = http.AsyncClient(unit.fake_url) client._AsyncClient__session = MockSessions(resp={}, code=204, content='') resp, status_code = await client.request(unit.Put()) assert resp == {} assert status_code == 204
async def test_real_request(): client = http.AsyncClient(unit.real_url) resp, status_code = await client.request(unit.Get()) assert len(resp) > 1000 assert status_code == 200
async def test_response_process(): client = http.AsyncClient(unit.fake_url) client._AsyncClient__session = MockSessions(resp={}, code=204, content='') resp, status_code = await client.request(unit.GetResponseProcess()) assert resp is None assert status_code == 200
async def test_request_undefined(): client = http.AsyncClient(unit.fake_url) client._AsyncClient__session = MockSessions(resp={}, code=204, content='', failure=True) with pytest.raises(http.RequestException): await client.request(unit.Undefined())
from clients import http fake_url = 'https://ed.cba' real_url = 'https://yandex.ru' client = http.AsyncClient(real_url) class GetResponseProcess(http.Method): url_ = '/' m_type = 'GET' def response_process(self, resp, status_code): return None, 200 class Get(http.Method): url_ = '/' m_type = 'GET' class GetWithBody(http.Method): url_ = '/' m_type = 'GET' body = {} class Post(http.Method): url_ = '/' m_type = 'POST' body = {}