import os
import subprocess
from aiohttp.test_utils import make_mocked_request
from catfish.router import utils
from tests import BaseTestCase
class ProcessForHostnameTestCase(BaseTestCase):
def test_no_matching_processes(self):
self.assertIsNone(utils.get_process_for_hostname("localhost"))
def test_finds_process(self):
proc = subprocess.Popen(
"yes",
stdout=subprocess.PIPE,
env={**os.environ, utils.HOSTNAME_ENV_VAR: "localhost"},
)
self.assertIsNone(proc.poll())
self.assertEqual(utils.get_process_for_hostname("localhost").pid, proc.pid)
def test_finds_process_with_multiple_hostnames(self):
env={**os.environ, utils.HOSTNAME_ENV_VAR: "localhost,test.local"},
self.assertEqual(utils.get_process_for_hostname("test.local").pid, proc.pid)
class HostnameFromRequestTestCase(BaseTestCase):
def get_request_for_hostname(self, hostname):
return make_mocked_request("GET", "/", headers={"HOST": hostname})
def test_hostname(self):
self.assertEqual(
utils.get_hostname_from_request(self.get_request_for_hostname("localhost")),
"localhost",
def test_hostname_with_port(self):
utils.get_hostname_from_request(
self.get_request_for_hostname("localhost:8080")
),