mirror of
https://github.com/jupyterhub/jupyterhub.git
synced 2025-10-18 07:23:00 +00:00
allow bypassing proxy in api_request
needed when testing that the proxy is down
This commit is contained in:
@@ -84,7 +84,8 @@ async def test_external_proxy(request):
|
||||
# add user to the db and start a single user server
|
||||
name = 'river'
|
||||
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()
|
||||
|
||||
routes = await app.proxy.get_all_routes()
|
||||
@@ -108,7 +109,7 @@ async def test_external_proxy(request):
|
||||
assert list(routes.keys()) == []
|
||||
|
||||
# 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()
|
||||
|
||||
# check that the routes are correct
|
||||
@@ -135,10 +136,16 @@ async def test_external_proxy(request):
|
||||
|
||||
# tell the hub where the new proxy is
|
||||
new_api_url = 'http://{}:{}'.format(proxy_ip, proxy_port)
|
||||
r = await api_request(app, 'proxy', method='patch', data=json.dumps({
|
||||
r = await api_request(
|
||||
app,
|
||||
'proxy',
|
||||
method='patch',
|
||||
data=json.dumps({
|
||||
'api_url': new_api_url,
|
||||
'auth_token': new_auth_token,
|
||||
}))
|
||||
}),
|
||||
bypass_proxy=True,
|
||||
)
|
||||
r.raise_for_status()
|
||||
assert app.proxy.api_url == new_api_url
|
||||
|
||||
|
@@ -116,14 +116,21 @@ def auth_header(db, name):
|
||||
|
||||
|
||||
@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"""
|
||||
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', {})
|
||||
|
||||
if (
|
||||
'Authorization' not in headers
|
||||
and not kwargs.pop('noauth', False)
|
||||
and not noauth
|
||||
and 'cookies' not in kwargs
|
||||
):
|
||||
# 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'))
|
||||
|
||||
url = ujoin(base_url, 'api', *api_path)
|
||||
method = kwargs.pop('method', 'get')
|
||||
f = getattr(async_requests, method)
|
||||
if app.internal_ssl:
|
||||
kwargs['cert'] = (app.internal_ssl_cert, app.internal_ssl_key)
|
||||
|
Reference in New Issue
Block a user