2022-02-07 12:12:06 +03:00
|
|
|
from time import sleep, perf_counter
|
2019-12-03 23:56:59 +03:00
|
|
|
import requests
|
|
|
|
import pytest
|
|
|
|
from context import PytestConf
|
|
|
|
|
|
|
|
if not PytestConf.config.getoption("--test-jwk-url"):
|
|
|
|
pytest.skip("--test-jwk-url flag is missing, skipping tests", allow_module_level=True)
|
|
|
|
|
|
|
|
# assumes the JWK server is running on 127.0.0.1:5001
|
|
|
|
|
2022-02-07 12:12:06 +03:00
|
|
|
def wait_until_request_count_reaches(num_requests, state_key, max_wait_secs):
|
|
|
|
start_time = perf_counter()
|
2022-01-28 03:17:53 +03:00
|
|
|
requests.post('http://localhost:5001/reset-state')
|
2022-02-07 12:12:06 +03:00
|
|
|
request_count = 0
|
|
|
|
time_elapsed = 0
|
|
|
|
|
|
|
|
while request_count < num_requests:
|
|
|
|
time_elapsed = perf_counter() - start_time
|
|
|
|
if time_elapsed > max_wait_secs:
|
|
|
|
raise Exception(f'Waited {time_elapsed} seconds for {state_key} JWK requests to reach {num_requests}. Only received {request_count}.')
|
|
|
|
|
|
|
|
sleep(0.2)
|
|
|
|
state = requests.get('http://localhost:5001/state').json()
|
|
|
|
request_count = state[state_key]
|
|
|
|
|
|
|
|
return time_elapsed
|
|
|
|
|
|
|
|
def test_cache_control_header_max_age(hge_ctx):
|
|
|
|
# The test uses max-age=3, so we are expecting one request (timing out after 6 seconds)
|
|
|
|
time_elapsed = wait_until_request_count_reaches(1, 'cache-control', 6)
|
|
|
|
print(f"time_elapsed: {time_elapsed}")
|
2022-01-28 03:17:53 +03:00
|
|
|
|
|
|
|
def test_cache_control_header_no_caching(hge_ctx):
|
2022-02-07 12:12:06 +03:00
|
|
|
# HGE should refresh the JWK once a second, so we are expecting three requests in at least two seconds
|
|
|
|
# (timing out after 10 seconds)
|
|
|
|
time_elapsed = wait_until_request_count_reaches(3, 'cache-control', 10)
|
|
|
|
print(f"time_elapsed: {time_elapsed}")
|
|
|
|
assert(time_elapsed >= 2)
|
2019-12-03 23:56:59 +03:00
|
|
|
|
|
|
|
def test_expires_header(hge_ctx):
|
2022-02-07 12:12:06 +03:00
|
|
|
# The test uses a three second jwk expiry so we are expecting one request (timing out after 6 seconds)
|
|
|
|
time_elapsed = wait_until_request_count_reaches(1, 'expires', 6)
|
|
|
|
print(f"time_elapsed: {time_elapsed}")
|