def test_do_timeout(): "Do timeout." # Tried with pytest.raises(Exception), but no luck: # https://stackoverflow.com/questions/23337471/how-to-properly-assert-that-an-exception-gets-raised-in-pytest#29855337 api = Api(f'{server}/get_slow/sleep/2.0', timeout=1) try: api.click_send() except timeout_decorator.TimeoutError: assert True
def test_here_maptile_image(): "Get maptile image from HERE.com." from ipyrest import Api url = 'https://1.{maptype}.maps.api.here.com/' \ 'maptile/2.1/{tiletype}/newest/{scheme}/{zoom}/{xtile}/{ytile}/{size}/{format}' args = dict( maptype='base', tiletype='maptile', scheme='normal.day', zoom='11', xtile='525', ytile='761', size='256', format='png8', ) params = dict( app_id=os.getenv('HEREMAPS_APP_ID'), app_code=os.getenv('HEREMAPS_APP_CODE'), ppi='320', ) # This needs to build the final URL, first, from the args and params # before sending the request! We simulate this here: url = url.format(**args) query = '&'.join(f'{k}={v}' for (k, v) in params.items()) url = f'{url}?{query}' api = Api(url, click_send=True) # This should do it all by itself: # api = Api(url, args=args, params=params, click_send=True) img = api.resp_pane.get_child_named('Content').get_child_named( 'Image').value assert img.startswith(b'\x89PNG\r\n')
def test_vcr(): "Make simple request, record in a VCR cassete." cassette_path = 'cassette3.yaml' Api(f'{server}/get_header', cassette_path=cassette_path, click_send=True) # get_json assert exists(join(recorder.cassette_library_dir, cassette_path))
def test_jupyter(): "Get body of jupyter.org." from ipyrest import Api url = 'https://jupyter.org' api = Api(url, click_send=True) assert api.url_txt.value == url assert api.resp.status_code == 200
def test_osm_tile(): "Get PNG tile from tile.osm.org." from ipyrest import Api url = 'http://tile.osm.org/0/0/0.png' api = Api(url, click_send=True) assert api.url_txt.value == url assert api.resp.status_code == 200
def test_google_header(): "Get response header of google.com." import json from ipyrest import Api url = 'http://google.com' api = Api(url, click_send=True) assert api.url_txt.value == url h = json.loads(api.resp_pane.get_child_named('Headers').value) assert h["Content-Type"] == "text/html; charset=ISO-8859-1"
def test_py_req_template_api(): """Test create Python code with requests from Api instance.""" from ipyrest import Api from ipyrest.codegen import create_code_from_api, python_requests_template config = dict( url='http://apple.com', method='POST', headers={'X-Foo': 'foo.bar'}, params={'foo': '42'}, # data='XXX' ) api = Api(**config) snippet = create_code_from_api(python_requests_template, api) # Test if correct assignements are contained in the snippet assert "method = '{method}'".format(**config) in snippet assert "url = '{url}'".format(**config) in snippet assert "headers = {headers}".format(**config) in snippet assert "params = {params}".format(**config) in snippet
import os from urllib.parse import quote from ipyrest import Api url = 'https://geocoder.api.here.com/6.2/geocode.json' params = dict( searchtext=quote('Invalidenstr. 116, 10115 Berlin, Germany'), app_id=os.getenv('HEREMAPS_APP_ID'), app_code=os.getenv('HEREMAPS_APP_CODE') ) api = Api(url, params=params, click_send=True)
def test_timeout(): "Timeout." Api(f'{server}/get_slow/sleep/1.0', click_send=True) assert True
def test_api_local(): "Create empty box, do nothing else." Api(f'{server}/get_json') assert True
def test_gpx(): "Make simple GPX request." Api(f'{server}/get_gpx', click_send=True)
def test_empty(): "Create empty box, do not send any request." Api() assert True
from ipyrest import Api url = 'http://jupyter.org' api = Api(url, click_send=True)