async def run(): replies.add(replies.GET, "http://example.com", body=b"test", match_querystring=True) resp = await asks.get("http://example.com") assert_response(resp, "test") resp = await asks.get("http://example.com/") assert_response(resp, "test") with pytest.raises(ConnectionError): asks.get("http://example.com?query=foo")
def run(): url = re.compile(r"https?://(.*\.)?example.com") replies.add(replies.GET, url, body=b"test") resp = asks.get("http://example.com") assert_response(resp, "test") resp = asks.get("https://example.com") assert_response(resp, "test") resp = asks.get("https://uk.example.com") assert_response(resp, "test") with pytest.raises(ConnectionError): asks.get("https://uk.exaaample.com")
def run(): replies.add_callback(replies.GET, url, request_callback, content_type=None) resp = asks.get(url) assert resp.text == "test callback" assert resp.status_code == status assert resp.reason == reason assert "foo" in resp.headers assert "Content-Type" not in resp.headers
async def run(): replies.add(replies.GET, "http://example.com/zero") replies.add(replies.GET, "http://example.com/one") replies.add(replies.GET, "http://example.com/two") replies.add(replies.GET, re.compile(r"http://example\.com/three")) replies.add(replies.GET, re.compile(r"http://example\.com/four")) re.purge() replies.remove(replies.GET, "http://example.com/two") replies.remove(Reply(method=replies.GET, url="http://example.com/zero")) replies.remove(replies.GET, re.compile(r"http://example\.com/four")) with pytest.raises(ConnectionError): await asks.get("http://example.com/zero") asks.get("http://example.com/one") with pytest.raises(ConnectionError): await asks.get("http://example.com/two") asks.get("http://example.com/three") with pytest.raises(ConnectionError): await asks.get("http://example.com/four")
async def run(): with replies.mock: replies.add(replies.GET, "http://example.com", body=b"test") resp = await asks.get("http://example.com") assert_response(resp, "test") assert len(replies.calls) == 1 assert replies.calls[0].request.url == "http://example.com/" assert replies.calls[0].response.content == b"test" resp = asks.get("http://example.com?foo=bar") assert_response(resp, "test") assert len(replies.calls) == 2 assert replies.calls[ 1].request.url == "http://example.com/?foo=bar" assert replies.calls[1].response.content == b"test"
"""isort:skip_file""" import asks import lxml.html from prop import Promise from asyncio import get_event_loop loop = get_event_loop() p = ( Promise(asks.get("https://en.wikipedia.org/wiki/Main_Page"), loop=loop). then(lambda response: lxml.html.fromstring(response.text).xpath( '//*[contains(text(), "Did you know...")]/../following-sibling::*/ul//li' )).catch( # In case the request fails or lxml can't parse the response, continues with empty list lambda _: []).then(lambda lis: "\n".join(("Did you know:", *( li.text_content() for li in lis)))).then(print)) loop.run_until_complete(p)