async def group(number, duration): acurl_el = acurl.EventLoop() requests = duration_generator('http://localhost:9003', duration) results = await asyncio.gather( *[runner(acurl_el, requests) for i in range(number)]) acurl_el.stop() return sum(results)
async def test_max_redirects_raises(httpserver): httpserver.expect_oneshot_request("/test", "GET").respond_with_response( WZResponse(status=301, headers={"Location": "/test2"})) loop = acurl.EventLoop() s = loop.session() with pytest.raises(acurl.RequestError): await s.get(httpserver.url_for("/test"), max_redirects=0)
async def test_get(httpserver): httpserver.expect_oneshot_request("/test", "GET").respond_with_data("hi") loop = acurl.EventLoop() s = loop.session() r = await s.get(httpserver.url_for("/test")) assert isinstance(r, acurl.Response) assert r.request.method == "GET" assert r.body == b"hi" httpserver.check_assertions()
async def test_disallow_redirects(httpserver): httpserver.expect_oneshot_request("/test", "GET").respond_with_response( WZResponse(status=301, headers={"Location": "/test2"})) loop = acurl.EventLoop() s = loop.session() r = await s.get(httpserver.url_for("/test"), allow_redirects=False) assert r.status_code == 301 assert r.headers["Location"].endswith("/test2")
def __init__(self, use_new_acurl_implementation=False): if use_new_acurl_implementation: import acurl_ng self._wrapper = acurl_ng.CurlWrapper(asyncio.get_event_loop()) else: import acurl self._wrapper = acurl.EventLoop() self._pool = deque()
async def test_response_cookies(httpserver): hdrs = Headers() hdrs.add("Set-Cookie", "foo=bar") hdrs.add("Set-Cookie", "quux=xyzzy") httpserver.expect_request("/foo").respond_with_response( Response(response="", status=200, headers=hdrs)) el = acurl.EventLoop() el._run_in_thread() s = el.session() r = await s.get(httpserver.url_for("/foo")) assert r.cookies == {"foo": "bar", "quux": "xyzzy"}
async def test_post_json(httpserver): httpserver.expect_oneshot_request( "/test", "POST", data='{"foo":"bar"}').respond_with_data("hi") loop = acurl.EventLoop() s = loop.session() r = await s.post(httpserver.url_for("/test"), json={"foo": "bar"}) assert isinstance(r, acurl.Response) assert r.request.method == "POST" assert r.body == b"hi" httpserver.check_assertions()
async def test_redirect(httpserver): httpserver.expect_oneshot_request("/test", "GET").respond_with_response( WZResponse(status=301, headers={"Location": "/test2"})) httpserver.expect_oneshot_request("/test2", "GET").respond_with_data("hi") loop = acurl.EventLoop() s = loop.session() r = await s.get(httpserver.url_for("/test")) assert r.body == b"hi" assert r.url == httpserver.url_for("/test2") assert r._prev.url == httpserver.url_for("/test") assert len(r.history) == 1
async def test_response_callback(httpserver): httpserver.expect_oneshot_request("/test", "GET").respond_with_data("hi") loop = acurl.EventLoop() s = loop.session() called = False def response_cb(resp): nonlocal called if called: raise Exception("called too many times") assert isinstance(resp, acurl.Response) called = True s.set_response_callback(response_cb) await s.get(httpserver.url_for("/test")) assert called httpserver.check_assertions()
def session(): el = acurl.EventLoop() return el.session()
async def test_request_no_data_raises(): loop = acurl.EventLoop() s = loop.session() with pytest.raises(ValueError): await s.request("GET", "http://foo.com", json={}, data="")
async def test_invalid_method_raises(): loop = acurl.EventLoop() s = loop.session() with pytest.raises(ValueError): await s.request("FOO", "http://foo.com")