async def do(): conf = Configuration("http://bing.com/") request = ClientRequest("GET", "http://bing.com/") policies = [ UserAgentPolicy("myusergant") ] async with AsyncPipeline(policies, AsyncPipelineRequestsHTTPSender(AsyncTrioRequestsHTTPSender(conf))) as pipeline: return await pipeline.run(request)
async def test_conf_async_requests(): conf = Configuration("http://bing.com/") request = ClientRequest("GET", "http://bing.com/") async with AsyncRequestsHTTPSender(conf) as sender: response = await sender.send(request) assert response.body() is not None assert response.status_code == 200
async def test_conf_async_requests(): conf = Configuration("http://bing.com/") request = ClientRequest("GET", "http://bing.com/") policies = [ UserAgentPolicy("myusergant") ] async with AsyncPipeline(policies, AsyncPipelineRequestsHTTPSender(AsyncRequestsHTTPSender(conf))) as pipeline: response = await pipeline.run(request) assert response.http_response.status_code == 200
def client(): # We need a ServiceClient instance, but the poller itself don't use it, so we don't need # Something functionnal return ServiceClient(None, Configuration("http://example.org"))
async def test_client_send(self): cfg = Configuration("/") cfg.headers = {'Test': 'true'} cfg.credentials = mock.create_autospec(OAuthTokenAuthentication) client = ServiceClientAsync(cfg) req_response = requests.Response() req_response._content = br'{"real": true}' # Has to be valid bytes JSON req_response._content_consumed = True req_response.status_code = 200 def side_effect(*args, **kwargs): return req_response session = mock.create_autospec(requests.Session) session.request.side_effect = side_effect session.adapters = { "http://": HTTPAdapter(), "https://": HTTPAdapter(), } # Be sure the mock does not trick me assert not hasattr(session.resolve_redirects, 'is_msrest_patched') client.config.pipeline._sender.driver.session = session client.config.credentials.signed_session.return_value = session client.config.credentials.refresh_session.return_value = session request = ClientRequest('GET', '/') await client.async_send(request, stream=False) session.request.call_count = 0 session.request.assert_called_with( 'GET', '/', allow_redirects=True, cert=None, headers={ 'User-Agent': cfg.user_agent, 'Test': 'true' # From global config }, stream=False, timeout=100, verify=True) assert session.resolve_redirects.is_msrest_patched request = client.get('/', headers={'id': '1234'}, content={'Test': 'Data'}) await client.async_send(request, stream=False) session.request.assert_called_with( 'GET', '/', data='{"Test": "Data"}', allow_redirects=True, cert=None, headers={ 'User-Agent': cfg.user_agent, 'Content-Length': '16', 'id': '1234', 'Accept': 'application/json', 'Test': 'true' # From global config }, stream=False, timeout=100, verify=True) assert session.request.call_count == 1 session.request.call_count = 0 assert session.resolve_redirects.is_msrest_patched request = client.get('/', headers={'id': '1234'}, content={'Test': 'Data'}) session.request.side_effect = requests.RequestException("test") with pytest.raises(ClientRequestError): await client.async_send(request, test='value', stream=False) session.request.assert_called_with( 'GET', '/', data='{"Test": "Data"}', allow_redirects=True, cert=None, headers={ 'User-Agent': cfg.user_agent, 'Content-Length': '16', 'id': '1234', 'Accept': 'application/json', 'Test': 'true' # From global config }, stream=False, timeout=100, verify=True) assert session.request.call_count == 1 session.request.call_count = 0 assert session.resolve_redirects.is_msrest_patched session.request.side_effect = oauth2.rfc6749.errors.InvalidGrantError( "test") with pytest.raises(TokenExpiredError): await client.async_send(request, headers={'id': '1234'}, content={'Test': 'Data'}, test='value') assert session.request.call_count == 2 session.request.call_count = 0 session.request.side_effect = ValueError("test") with pytest.raises(ValueError): await client.async_send(request, headers={'id': '1234'}, content={'Test': 'Data'}, test='value')
async def do(): conf = Configuration("http://bing.com/") request = ClientRequest("GET", "http://bing.com/") async with AsyncTrioRequestsHTTPSender(conf) as sender: return await sender.send(request) assert response.body() is not None