allow bypassing proxy in api_request

needed when testing that the proxy is down
This commit is contained in:
Min RK
2019-02-05 12:40:55 +01:00
parent 2715607361
commit a6d0c36594
2 changed files with 23 additions and 10 deletions

View File

@@ -84,7 +84,8 @@ async def test_external_proxy(request):
# add user to the db and start a single user server # add user to the db and start a single user server
name = 'river' name = 'river'
add_user(app.db, app, name=name) add_user(app.db, app, name=name)
r = await api_request(app, 'users', name, 'server', method='post') r = await api_request(app, 'users', name, 'server', method='post',
bypass_proxy=True)
r.raise_for_status() r.raise_for_status()
routes = await app.proxy.get_all_routes() routes = await app.proxy.get_all_routes()
@@ -108,7 +109,7 @@ async def test_external_proxy(request):
assert list(routes.keys()) == [] assert list(routes.keys()) == []
# poke the server to update the proxy # poke the server to update the proxy
r = await api_request(app, 'proxy', method='post') r = await api_request(app, 'proxy', method='post', bypass_proxy=True)
r.raise_for_status() r.raise_for_status()
# check that the routes are correct # check that the routes are correct
@@ -135,10 +136,16 @@ async def test_external_proxy(request):
# tell the hub where the new proxy is # tell the hub where the new proxy is
new_api_url = 'http://{}:{}'.format(proxy_ip, proxy_port) new_api_url = 'http://{}:{}'.format(proxy_ip, proxy_port)
r = await api_request(app, 'proxy', method='patch', data=json.dumps({ r = await api_request(
'api_url': new_api_url, app,
'auth_token': new_auth_token, 'proxy',
})) method='patch',
data=json.dumps({
'api_url': new_api_url,
'auth_token': new_auth_token,
}),
bypass_proxy=True,
)
r.raise_for_status() r.raise_for_status()
assert app.proxy.api_url == new_api_url assert app.proxy.api_url == new_api_url

View File

@@ -116,14 +116,21 @@ def auth_header(db, name):
@check_db_locks @check_db_locks
async def api_request(app, *api_path, **kwargs): async def api_request(app, *api_path, method='get',
noauth=False, bypass_proxy=False,
**kwargs):
"""Make an API request""" """Make an API request"""
base_url = public_url(app, path='hub') if bypass_proxy:
# make a direct request to the hub,
# skipping the proxy
base_url = app.hub.url
else:
base_url = public_url(app, path='hub')
headers = kwargs.setdefault('headers', {}) headers = kwargs.setdefault('headers', {})
if ( if (
'Authorization' not in headers 'Authorization' not in headers
and not kwargs.pop('noauth', False) and not noauth
and 'cookies' not in kwargs and 'cookies' not in kwargs
): ):
# make a copy to avoid modifying arg in-place # make a copy to avoid modifying arg in-place
@@ -138,7 +145,6 @@ async def api_request(app, *api_path, **kwargs):
headers.setdefault('Referer', ujoin(base_url, 'test')) headers.setdefault('Referer', ujoin(base_url, 'test'))
url = ujoin(base_url, 'api', *api_path) url = ujoin(base_url, 'api', *api_path)
method = kwargs.pop('method', 'get')
f = getattr(async_requests, method) f = getattr(async_requests, method)
if app.internal_ssl: if app.internal_ssl:
kwargs['cert'] = (app.internal_ssl_cert, app.internal_ssl_key) kwargs['cert'] = (app.internal_ssl_cert, app.internal_ssl_key)