async def async_client(app) -> AsyncClient: async with AsyncClient(app=app, base_url="http://test") as ac: yield ac
async def client() -> typing.AsyncGenerator[AsyncClient, None]: async with AsyncClient(app=app, base_url='http://test') as client: yield client
def setUp(self) -> None: super().setUp() self.client = AsyncClient(app=web.app, base_url='http://test')
async def test_read_root(): async with AsyncClient(app=app, base_url="http://test") as ac: response = await ac.get("/") assert response.status_code == 200 assert response.json() == {"msg": "Hello World"}
async def test_ping_async(): async with AsyncClient(app=app, base_url="http://test") as ac: response = await ac.get("/api/ping") assert response.status_code == 200 assert response.json()["response"] == "pong"
def test_client_queryparams(): client = AsyncClient(params={"a": "b"}) assert isinstance(client.params, QueryParams) assert client.params["a"] == "b"
import pytest from httpx import AsyncClient from app.main import app client = AsyncClient() @pytest.mark.asyncio async def test_read_root(): async with AsyncClient(app=app, base_url="http://test") as ac: response = await ac.get("/") assert response.status_code == 200 assert response.json() == {"msg": "Hello World"} @pytest.mark.asyncio async def test_read_item(): async with AsyncClient(app=app, base_url="http://test") as ac: response = await ac.get("/items/1") assert response.status_code == 200 assert response.json() == {"item_id": 1, "q": None} @pytest.mark.asyncio async def test_update_item(): async with AsyncClient(app=app, base_url="http://test") as ac: response = await ac.put("/items/1", json={ "name": "foobar", "price": 122.11 })
async def test_redirect_loop(): client = AsyncClient(transport=AsyncMockTransport()) with pytest.raises(TooManyRedirects): await client.get("https://example.org/redirect_loop")
async def test_redirect_custom_scheme(): client = AsyncClient(transport=AsyncMockTransport()) with pytest.raises(UnsupportedProtocol) as e: await client.post("https://example.org/redirect_custom_scheme") assert str(e.value) == "Scheme b'market' not supported."
async def test_relative_redirect(): client = AsyncClient(transport=AsyncMockTransport()) response = await client.get("https://example.org/relative_redirect") assert response.status_code == codes.OK assert response.url == URL("https://example.org/") assert len(response.history) == 1
async def test_async_too_many_redirects(): client = AsyncClient(transport=AsyncMockTransport()) with pytest.raises(TooManyRedirects): await client.get("https://example.org/multiple_redirects?count=21")
async def client(event_loop): async with AsyncClient(app=app, base_url="http://test") as client: yield client
def app_client(app): return AsyncClient(transport=ASGITransport(app=app), base_url="http://app")
async def test_server(): with override_settings( ASYNC_TASKS=False, INTERNAL_DATABASE_FILE=settings.INTERNAL_DATABASE_FILE + '.test'): from cloudcopy.server.app import api as app, tasks try: async with AsyncExitStack() as stack: client = await stack.enter_async_context( AsyncClient(app=app, base_url='http://test')) test0 = await stack.enter_async_context( setup_test_database('test0', url='file:test0')) test1 = await stack.enter_async_context( setup_test_database('test1', url='file:test1')) test2 = await stack.enter_async_context( setup_test_database('test2', url='file:test2')) await setup_db(test0, tables=('test1', 'test2', 'test3')) await setup_db(test1, tables=('test2', 'test3'), rows=5) await setup_db(test2, tables=('test3', )) databases = [{ 'name': 'test0', 'url': 'file:test0', 'scope': { 'schemas': { 'main': True } } }, { 'name': 'test2', 'url': 'file:test2' }, { 'name': 'test3', 'url': 'file:test3', 'scope': { 'schemas': { 'main': True } } }] # POST for value in databases: response = await client.post('/v0/databases/', data=json.dumps( {'data': value})) assert response.status_code == 201 response = response.json()['data'] for k, v in value.items(): assert response[k] == v value['id'] = response['id'] # GET (collection) response = await client.get('/v0/databases/') assert response.status_code == 200 response = response.json()['data'] for i, value in enumerate(databases): for k, v in value.items(): assert response[i][k] == v # GET (record) modify_index = 2 id = databases[modify_index]['id'] response = await client.get(f'/v0/databases/{id}/') assert response.status_code == 200 response = response.json()['data'] for k, v in databases[modify_index].items(): assert response[k] == v assert response['created'] created = response['created'] assert response['updated'] updated = response['updated'] # PUT # all writable fields not in response # should be reset to defaults response = await client.put(f'/v0/databases/{id}/', data=json.dumps({ 'data': { 'name': 'foo', 'url': 'file:test1' } })) assert response.status_code == 200 response = response.json()['data'] assert response['name'] == 'foo' assert response['url'] == 'file:test1' # unset "scope" was set to null assert response['scope'] == None # unchanged assert response['created'] == created assert response['updated'] >= updated updated = response['updated'] # PATCH (by name) response = await client.patch(f'/v0/databases/foo/', data=json.dumps( {'data': { 'name': 'test1' }})) assert response.status_code == 200 response = response.json()['data'] assert response['name'] == 'test1' # unchanged assert response['url'] == 'file:test1' assert response['updated'] >= updated # Workflows response = await client.get(f'/v0/workflows') assert response.status_code == 200 response = response.json()['data'] assert response == [] workflows = [ { "name": "diff-0-1", "schedule": { "immediate": True }, "steps": [{ "type": "compare", "hashes": True, "source": databases[0]['id'], # by id (test0) "target": "test1" # by name reference (test1) }] }, { "name": "info-2", "steps": [{ "type": "info", "source": "file:test2" }], "schedule": { "delay": "1 minute" } } ] # add workflow for workflow in workflows: response = await client.post('/v0/workflows/', data=json.dumps( {'data': workflow})) assert response.status_code == 201 response = response.json()['data'] for k, v in workflow.items(): assert response[k] == v workflow['id'] = response['id'] # DELETE (by newly changed name) response = await client.delete(f'/v0/databases/test1/') assert response.status_code == 204 response = await client.get('/v0/databases/') assert len(response.json()['data']) == len(databases) - 1 immediate_workflow = workflows[0] delayed_workflow = workflows[1] scheduled = tasks.scheduled() assert len(scheduled) == 1 assert scheduled[0].name == 'workflow-execute' args, kwargs = scheduled[0].data assert args[0] == delayed_workflow['id'] immediate_id = immediate_workflow['id'] response = await client.get( f'/v0/jobs/?f__workflow_id={immediate_id}') assert response.status_code == 200 data = response.json()['data'] assert len(data) == 1 data = data[0] assert data['status'] == 'Succeeded' result = data['result'] assert result == diff_0_1_result finally: # clean up test sqlite DB if os.path.exists(settings.INTERNAL_DATABASE_FILE): os.remove(settings.INTERNAL_DATABASE_FILE)
async def test_read_timeout(server, backend): timeout = TimeoutConfig(read_timeout=1e-6) async with AsyncClient(timeout=timeout, backend=backend) as client: with pytest.raises(ReadTimeout): await client.get("http://127.0.0.1:8000/slow_response")
class Endpoint: __is_endpoint__ = True client: AgnosticClient = Client() _async_client = AsyncClient() def __init__(self, *, path: str, method: str, call: Callable, client: AgnosticClient = None): self.path = path self.method = method.strip().upper() self.is_async = is_coroutine_callable(call) if client is not None: assert self.is_async == isinstance( client, AsyncClient), "async function should use `AsyncClient`" self.client = client elif self.is_async: self.client = self._async_client self.call = call self.transmuter = transmuter(call) self.raw_signature = inspect.signature(call) self.dependant = get_dependant(path=path, call=call, http_method=method) self.is_method = False self.is_classmethod = False def __call__(self, *args, **kwargs): from .service import Service args, kwargs = self.transmuter(*args, **kwargs) if self.is_classmethod: args = args[1:] elif self.is_method and len(args) != 0: first_param = next(iter(self.raw_signature.parameters.values()), None) if first_param is not None: name = first_param.name if name in kwargs: kwargs.pop(name) elif isinstance(args[0], Service): args = args[1:] request = self._prepare_request(*args, **kwargs) if self.is_async and isinstance(self.client, AsyncClient): async def send_request(): return self._response_to_return(await self.client.send(request)) return asyncio.ensure_future(send_request()) elif self.is_async: async def send_request(): return self._response_to_return(self.client.send(request)) return asyncio.ensure_future(send_request()) else: return self._response_to_return(self.client.send(request)) def __get__(self, instance, owner): if instance is not None: self.client = getattr(instance, 'client', self.client) return self def rebuild_dependant(self, is_method: bool, is_classmethod: bool): self.is_method = is_method self.is_classmethod = is_classmethod self.dependant = get_dependant(path=self.path, call=self.call, http_method=self.method, is_method_or_classmethod=is_method or is_classmethod) def _prepare_request(self, args: Tuple[..., Any], kwargs: Dict[str, Any]) -> Request: dependant = self.dependant bound_args = dependant.bind(args, kwargs) path_values = bound_args.get('Path', {}) queries = bound_args.get('Query', None) headers = bound_args.get('Header', None) cookies = bound_args.get('Cookie', None) body = bound_args.get('Body', None) form = bound_args.get('Form', None) url = self.client.base_url.join(self.path.format(**path_values)) request = Request(self.method, url, params=queries, headers=headers, cookies=cookies, json=body, data=form) return request def _response_to_return(self, response: Response): return_annotation = self.dependant.signature.return_annotation if return_annotation is inspect.Parameter.empty or return_annotation is Response: return response elif return_annotation is None: return None elif return_annotation is str: return response.text elif return_annotation is bytes: return response.content else: try: return typic.transmute(return_annotation, response.json()) except ValueError: try: return typic.transmute(return_annotation, response.text) except ValueError: pass return response
async def start(self): """Initialize the client.""" self.client = AsyncClient(timeout=config.TIMEOUT, max_redirects=config.MAX_REDIRECTS)
async def default_client(app): async with AsyncClient(app=app, base_url="http://test") as ac: yield ac
async def base_client(app_with_test_overrides: FastAPI) -> AsyncClient: async with LifespanManager(app_with_test_overrides): async with AsyncClient(app=app_with_test_overrides, base_url="http://testserver") as client: yield client
async def test_no_scheme_redirect(): client = AsyncClient(dispatch=MockDispatch()) response = await client.get("https://example.org/no_scheme_redirect") assert response.status_code == codes.OK assert response.url == URL("https://example.org/") assert len(response.history) == 1
async def test_read_item(): async with AsyncClient(app=app, base_url="http://test") as ac: response = await ac.get("/items/1") assert response.status_code == 200 assert response.json() == {"item_id": 1, "q": None}
async def test_too_many_redirects(): client = AsyncClient(dispatch=MockDispatch()) with pytest.raises(TooManyRedirects): await client.get("https://example.org/multiple_redirects?count=21")
def async_test_client_with_auth(app_with_auth): return AsyncClient(app=app_with_auth, base_url="http://test")
async def test_redirect_loop(): client = AsyncClient(dispatch=MockDispatch()) with pytest.raises(RedirectLoop): await client.get("https://example.org/redirect_loop")
async def test_get_devices(): async with AsyncClient(app=app, base_url="http://test") as client: response = await client.get("/api/devices") assert response.status_code == 200 assert len(response.json()) == len(INVENTORY)
async def test_cross_dubdomain_redirect(): client = AsyncClient(dispatch=MockDispatch()) url = "https://example.com/cross_subdomain" response = await client.get(url) assert response.url == URL("https://www.example.org/cross_subdomain")
async def client() -> AsyncGenerator["AsyncClient", None]: async with LifespanManager(app): async with AsyncClient(app=app, base_url="http://test") as client: yield client
if result.result_id in stat_set: stat_set.remove(result.result_id) return await logger.log( answer_map.get(result.result_id, "-1"), hashlib.md5(result.from_user.id.to_bytes(16, "little", signed=False)).hexdigest()) @dp.message_handler(commands=["stat"]) async def stat_handler(message: types.Message): stat = await logger.get_stat() top_sentences = "\n".join( [f"{k}:{v} 次" for k, v in stat.top_sentences.items()]) await message.answer( f"总共已经有 {stat.users} 名迟化人卖了 {stat.total_requests} 句菜\n其中最迟的人卖了 {stat.top_user_count} 句\n\n" f"被卖得最多次的句子:\n{top_sentences}") logger = InlineLogger(os.environ["LOG_FILE"]) scheduler = AsyncIOScheduler() scheduler.add_job(update_corpus, "interval", minutes=10) scheduler.add_job(update_corpus) corpus = Corpus() seller = Seller(corpus) http = AsyncClient() scheduler.start() if __name__ == '__main__': executor.start_polling(dp, skip_updates=True)
def __init__(self, session=None, **kwargs): from httpx import AsyncClient, HTTPError self.session = session self.session_class = AsyncClient(**kwargs) self.error = (HTTPError, InvalidSchemaError)
async def supervisor(cc_list: list[str]) -> int: async with AsyncClient() as client: to_do = [download_one(client, cc) for cc in sorted(cc_list)] res = await asyncio.gather(*to_do) return len(res)