"""Exercise the shipped Nginx routes using disposable files and an isolated port."""
from html.parser import HTMLParser
from pathlib import Path
import shutil
import socket
import subprocess
import tempfile
import time
import unittest
import urllib.error
import urllib.parse
import urllib.request
class NginxDirectoryTests(unittest.TestCase):
@classmethod
def setUpClass(cls):
nginx = shutil.which('nginx')
if not nginx:
raise unittest.SkipTest('Nginx is required for the integration fixture')
cls.temp = tempfile.TemporaryDirectory(prefix='mirrorouc-nginx-test-')
cls.root = Path(cls.temp.name)
cls.mirrors = cls.root / 'mirrors'
cls.dist = cls.root / 'dist'
(cls.mirrors / 'repo' / 'sub').mkdir(parents=True)
(cls.mirrors / 'repo' / 'empty').mkdir()
build = Path(__file__).resolve().parent.parent / 'dist'
if not (build / 'fancy-index' / 'header.html').is_file():
raise RuntimeError('Run npm run build before the Nginx integration fixture')
shutil.copytree(build, cls.dist)
(cls.dist / 'assets' / 'test.js').write_text('window.fixture = true', encoding='utf-8')
(cls.mirrors / 'repo' / 'index.html').write_text('repository index must not replace listing', encoding='utf-8')
(cls.mirrors / 'repo' / '中文 #+%.txt').write_bytes(b'0123456789abcdef')
(cls.mirrors / 'repo' / 'zero').write_bytes(b'')
(cls.mirrors / 'repo' / '.hidden').write_text('hidden', encoding='utf-8')
(cls.mirrors / 'repo' / '\">.txt').write_text('escaped name', encoding='utf-8')
(cls.root / 'private-secret').write_text('outside-secret', encoding='utf-8')
with socket.socket() as available:
available.bind(('127.0.0.1', 0))
port = available.getsockname()[1]
cls.base = f'http://127.0.0.1:{port}'
server = Path(__file__).with_name('nginx-preview.conf').read_text(encoding='utf-8')
server = server.replace('listen 100.69.2.2:8080;', f'listen 127.0.0.1:{port};')
server = server.replace('/data/mirrors/', cls.mirrors.as_posix() + '/')
server = server.replace('/opt/mirrorouc/current/dist', cls.dist.as_posix())
# The Go upstream is deliberately unavailable throughout these tests.
server = server.replace('http://127.0.0.1:8081', 'http://127.0.0.1:9')
config = cls.root / 'nginx.conf'
config.write_text(f'load_module /usr/lib/nginx/modules/ngx_http_fancyindex_module.so;\nworker_processes 1;\npid {cls.root}/nginx.pid;\nerror_log {cls.root}/error.log;\nevents {{ worker_connections 32; }}\nhttp {{ access_log off; types {{ text/html html; text/plain txt; application/javascript js; }}\n{server}\n}}\n', encoding='utf-8')
validation = subprocess.run([nginx, '-p', str(cls.root), '-c', str(config), '-t'], capture_output=True, text=True)
if validation.returncode:
raise RuntimeError(validation.stderr)
cls.process = subprocess.Popen([nginx, '-p', str(cls.root), '-c', str(config), '-g', 'daemon off; master_process off;'], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
for _ in range(100):
if cls.process.poll() is not None:
raise RuntimeError((cls.root / 'error.log').read_text())
try:
with urllib.request.urlopen(cls.base + '/assets/test.js', timeout=1):
return
except (OSError, urllib.error.URLError):
time.sleep(0.03)
cls.process.terminate()
raise RuntimeError('Isolated Nginx did not become ready')
@classmethod
def tearDownClass(cls):
cls.process.terminate()
cls.process.wait(timeout=5)
cls.temp.cleanup()
def request(self, path, method='GET', headers=None):
request = urllib.request.Request(self.base + path, method=method, headers=headers or {})
try:
response = urllib.request.build_opener(NoRedirect).open(request, timeout=5)
except urllib.error.HTTPError as response:
return response.code, response.headers, response.read()
with response:
return response.status, response.headers, response.read()
def test_complete_server_html_without_go_or_javascript(self):
code, headers, body = self.request('/mirrors/repo/')
self.assertEqual(code, 200)
self.assertIn('text/html', headers['Content-Type'])
html = body.decode('utf-8')
self.assertIn('