dataset/seed/nginx-reverse-proxy/tests/test_revprox.py

43 lines
1.4 KiB
Python
Raw Normal View History

from yaml import load, SafeLoader
from os import environ
2023-01-17 21:43:32 +01:00
from os.path import join
import warnings
import socket
from requests import get
from requests.exceptions import SSLError
def req(url, ip, verify=True):
# Monkey patch to force IPv4 resolution
old_getaddrinfo = socket.getaddrinfo
def new_getaddrinfo(*args, **kwargs):
ret = old_getaddrinfo(*args, **kwargs)
dns = list(ret[0])
dns[-1] = (ip, dns[-1][1])
return [dns]
socket.getaddrinfo = new_getaddrinfo
2022-07-16 22:16:24 +02:00
if not verify:
with warnings.catch_warnings():
warnings.simplefilter("ignore")
2023-01-17 21:43:32 +01:00
ret = get(url, verify=verify, allow_redirects=False)
2022-07-16 22:16:24 +02:00
else:
2023-01-17 21:43:32 +01:00
ret = get(url, verify=verify, allow_redirects=False)
ret_code = ret.status_code
content = ret.content
socket.getaddrinfo = old_getaddrinfo
return ret_code, content.decode()
def test_revprox():
conf_file = f'{environ["MACHINE_TEST_DIR"]}/reverse-proxy.yml'
with open(conf_file) as yaml:
data = load(yaml, Loader=SafeLoader)
# test known domains
for url in data['urls']:
2023-01-17 21:43:32 +01:00
try:
ret_code, content = req(f'https://{url}', data['address'])
except SSLError:
ret_code, content = req(f'https://{url}', data['address'], verify=join(environ["MACHINE_TEST_DIR"], data["ca_certificate"]))
assert ret_code == 200, f'https://{url} do not returns code 200 but {ret_code}'