Browse Source

Added some example tests for reference

Fabrizio Furnari 9 months ago
parent
commit
d5843a36e7

+ 27 - 0
example_tests/01-simple.py

@@ -0,0 +1,27 @@
+from pathlib import Path
+from proxytester.main import BaseProxyTest, ProxyConfig
+
+
+class BasicPassTest(BaseProxyTest):
+    """Minimal test that will always pass.
+
+    As for other tests the haproxy binary must be present somewhere
+    on the filesystem.
+    """
+    def __init__(self):
+        super().__init__()
+        self.description = "Basic request"
+        # Default address of proxy to listen is 127.0.0.1:4242
+        self.url = "http://localhost:4242/"
+
+        # Customize the haproxy path
+        self.proxy_config = ProxyConfig(
+            binary_path=Path.home() / "bin/haproxy"
+        )
+
+    async def run_test(self):
+        """This is the bare minimum to run the test
+        """
+        await self.make_request()
+        # Returning True as this must always pass
+        return True

+ 62 - 0
example_tests/02-basic_header_check.py

@@ -0,0 +1,62 @@
+from pathlib import Path
+from proxytester.main import BaseProxyTest, BackendConfig, ProxyConfig
+
+
+class FailHeaderCheck(BaseProxyTest):
+    """Minimal test to demonstrate the basic header checks functionality
+
+    This test should fail as the backend or the reverse proxy aren't configured
+    to answer this header.
+    """
+    def __init__(self):
+        super().__init__()
+        self.description = "Client custom header (Fail)"
+        self.url = "http://localhost:4242/"
+
+        # expected_headers must match with the headers received by
+        # the client or will fail
+        self.expected_headers = {
+            "x-test": "1234",
+        }
+
+        self.proxy_config = ProxyConfig(
+            binary_path=Path.home() / "bin/haproxy"
+        )
+
+    async def run_test(self):
+        await self.make_request()
+        return True
+
+
+class PassHeaderCheck(BaseProxyTest):
+    """Minimal test to demonstrate the basic header checks functionality
+
+    This test should succeed.
+    """
+    def __init__(self):
+        super().__init__()
+        self.description = "Client custom header (Pass)"
+        self.url = "http://localhost:4242/"
+
+        # expected_headers must match with the headers received by
+        # the client or will fail
+        self.expected_headers = {
+            "x-test": "1234",
+        }
+
+        self.proxy_config = ProxyConfig(
+            binary_path=Path.home() / "bin/haproxy"
+        )
+
+        # Defining a backend configuration with headers
+        # to answer to the client
+        self.backend_config = BackendConfig(
+            response_headers={"X-Test": "1234"},
+        )
+
+    async def run_test(self):
+        """The run_test() method is always the same as all logic
+        is defined entirely in the classes configuration.
+        """
+        await self.make_request()
+        return True

+ 56 - 0
example_tests/03-basic_header_regex.py

@@ -0,0 +1,56 @@
+from pathlib import Path
+from proxytester.main import BaseProxyTest, BackendConfig, ProxyConfig
+
+
+class FailHeaderRegexTest(BaseProxyTest):
+    """Simple test to demonstrate how to match for header regexes.
+
+    This test should fail
+    """
+    def __init__(self):
+        super().__init__()
+        self.description = "Header regex match (Fail)"
+        # The expected_header_patterns should contain
+        # the header name and a pattern that is used to
+        # match the received header value
+        self.expected_header_patterns = {
+            "x-test": r"\d{1}$",
+        }
+
+        self.proxy_config = ProxyConfig(
+            binary_path=Path.home() / "bin/haproxy"
+        )
+
+        self.backend_config = BackendConfig(
+            response_headers={"X-Test": "1234"},
+        )
+
+    async def run_test(self):
+        await self.make_request()
+        return True
+
+
+class PassHeaderRegexTest(BaseProxyTest):
+    """Simple test to demonstrate how to match for header regexes.
+    """
+    def __init__(self):
+        super().__init__()
+        self.description = "Header regex match (Pass)"
+        # The expected_header_patterns should contain
+        # the header name and a pattern that is used to
+        # match the received header value
+        self.expected_header_patterns = {
+            "x-test": r"\d{4}$",
+        }
+
+        self.proxy_config = ProxyConfig(
+            binary_path=Path.home() / "bin/haproxy"
+        )
+
+        self.backend_config = BackendConfig(
+            response_headers={"X-Test": "1234"},
+        )
+
+    async def run_test(self):
+        await self.make_request()
+        return True

+ 50 - 0
example_tests/04-backend_headers.py

@@ -0,0 +1,50 @@
+from pathlib import Path
+from proxytester.main import BaseProxyTest, ProxyConfig
+
+
+class FailHeaderBackendTest(BaseProxyTest):
+    """A test to check the headers received by the backend.
+
+    This test should fail
+    """
+    def __init__(self):
+        super().__init__()
+        self.description = "Backend headers check (Fail)"
+
+        # We send an header but expect another one on the backend
+        self.headers = {
+            "X-Test-1": "1234",
+        }
+        self.expected_backend_headers = {
+            "x-test-2": "abcd",
+        }
+
+        self.proxy_config = ProxyConfig(
+            binary_path=Path.home() / "bin/haproxy"
+        )
+
+    async def run_test(self):
+        await self.make_request()
+        return True
+
+
+class PassHeaderBackendTest(BaseProxyTest):
+    """A test to check the headers received by the backend.
+    """
+    def __init__(self):
+        super().__init__()
+        self.description = "Backend headers check (Pass)"
+        self.headers = {
+            "X-Test-1": "abcd",
+        }
+        self.expected_backend_headers = {
+            "x-test-1": "abcd",
+        }
+
+        self.proxy_config = ProxyConfig(
+            binary_path=Path.home() / "bin/haproxy"
+        )
+
+    async def run_test(self):
+        await self.make_request()
+        return True

+ 104 - 0
example_tests/05-forbidden_headers.py

@@ -0,0 +1,104 @@
+from pathlib import Path
+from proxytester.main import BaseProxyTest, BackendConfig, ProxyConfig
+
+
+class FailForbiddenHeadersTest(BaseProxyTest):
+    """Test that headers received by the client aren't the ones explicitly
+    listed as forbidden ones.
+
+    This test should fail
+    """
+    def __init__(self):
+        super().__init__()
+        self.description = "Forbidden client headers check (Fail)"
+
+        # We don't expect these headers
+        self.forbidden_client_headers = ['x-forbidden']
+
+        self.proxy_config = ProxyConfig(
+            binary_path=Path.home() / "bin/haproxy"
+        )
+
+        # Configure the backend to send the forbidden header and fail
+        # the test
+        self.backend_config = BackendConfig(
+            response_headers={"X-Forbidden": "1234"},
+        )
+
+    async def run_test(self):
+        await self.make_request()
+        return True
+
+
+class PassForbiddenHeadersTest(BaseProxyTest):
+    """Test that headers received by the client aren't the ones explicitly
+    listed as forbidden ones.
+    """
+    def __init__(self):
+        super().__init__()
+        self.description = "Forbidden client headers check (Pass)"
+
+        # We don't expect these headers
+        self.forbidden_client_headers = ['x-forbidden']
+
+        self.proxy_config = ProxyConfig(
+            binary_path=Path.home() / "bin/haproxy"
+        )
+
+        self.backend_config = BackendConfig(
+            response_headers={"X-Test": "1234"},
+        )
+
+    async def run_test(self):
+        await self.make_request()
+        return True
+
+
+class FailBackendForbiddenHeadersTest(BaseProxyTest):
+    """Test that headers received by the backend aren't the ones explicitly
+    listed as forbidden ones.
+
+    This test should fail
+    """
+    def __init__(self):
+        super().__init__()
+        self.description = "Forbidden backend headers check (Fail)"
+
+        # Backend doesn't expect these headers
+        self.forbidden_backend_headers = ['x-forbidden']
+
+        self.headers = {
+            "X-Forbidden": "1234",
+        }
+
+        self.proxy_config = ProxyConfig(
+            binary_path=Path.home() / "bin/haproxy"
+        )
+
+    async def run_test(self):
+        await self.make_request()
+        return True
+
+
+class PassBackendForbiddenHeadersTest(BaseProxyTest):
+    """Test that headers received by the backend aren't the ones explicitly
+    listed as forbidden ones.
+    """
+    def __init__(self):
+        super().__init__()
+        self.description = "Forbidden backend headers check (Pass)"
+
+        # Backend doesn't expect these headers
+        self.forbidden_backend_headers = ['x-forbidden']
+
+        self.headers = {
+            "X-Test": "1234",
+        }
+
+        self.proxy_config = ProxyConfig(
+            binary_path=Path.home() / "bin/haproxy"
+        )
+
+    async def run_test(self):
+        await self.make_request()
+        return True

+ 48 - 0
example_tests/06-backend_configuration.py

@@ -0,0 +1,48 @@
+from pathlib import Path
+from proxytester.main import BaseProxyTest, BackendConfig, ProxyConfig
+
+
+class ExampleBackendConfigurationTest(BaseProxyTest):
+    """A simple test to demonstrate some advanced backend configuration options
+    """
+
+    def __init__(self):
+        super().__init__()
+        self.description = "Example backend configuration"
+
+        self.backend_config = BackendConfig(
+            # Host and port are used both by the HTTP backend and in the
+            # reverse proxy configuration
+            host="127.0.0.1",
+            port="9999",
+            # Response headers, body and status are sent to the reverse proxy
+            # at each request
+            response_headers={
+                "X-Test-1": "1234",
+                "X-Test-2": "5678",
+            },
+            response_body="This is just a test body",
+            response_status=301,
+        )
+
+        # Now we check some headers, body (regex) and status code
+        self.expected_headers = {
+            "x-test-2": "5678",
+        }
+        self.forbidden_client_headers = {
+            "x-test-3": "abcd",
+        }
+
+        self.expected_header_patterns = {
+            "x-test-1": r"^[1-4]{4}$",
+        }
+        self.expected_body_pattern = r".*just.*"
+        self.expected_status = 301
+
+        self.proxy_config = ProxyConfig(
+            binary_path=Path.home() / "bin/haproxy"
+        )
+
+    async def run_test(self):
+        await self.make_request()
+        return True

+ 40 - 0
example_tests/07-multiple_fail.py

@@ -0,0 +1,40 @@
+from pathlib import Path
+from proxytester.main import BaseProxyTest, BackendConfig, ProxyConfig
+
+
+class FailExampleMultipleFailTest(BaseProxyTest):
+    """A simple test to demonstrate multiple failures
+    """
+    def __init__(self):
+        super().__init__()
+        self.description = "Example multiple failures (Fail)"
+
+        self.backend_config = BackendConfig(
+            # Response headers, body and status are sent to the reverse proxy
+            # at each request
+            response_headers={
+                "X-Test-1": "1234",
+                "X-Test-2": "5678",
+            },
+            response_body="This is just a test body",
+            response_status=301,
+        )
+
+        # This should fail
+        self.expected_headers = {
+            "x-test-2": "42",
+        }
+        # This should fail
+        self.expected_status = 200
+        # This should pass
+        self.forbidden_client_headers = {
+            "x-test-3": "abcd",
+        }
+
+        self.proxy_config = ProxyConfig(
+            binary_path=Path.home() / "bin/haproxy"
+        )
+
+    async def run_test(self):
+        await self.make_request()
+        return True

+ 37 - 0
example_tests/08-errors.py

@@ -0,0 +1,37 @@
+from pathlib import Path
+from proxytester.main import BaseProxyTest, ProxyConfig
+
+
+class ErroredTest(BaseProxyTest):
+    """A test that raise an exception to demonstrate the difference
+    between failed test and errored test
+    """
+    def __init__(self):
+        super().__init__()
+        self.description = "A test that produces an error (Error)"
+        self.proxy_config = ProxyConfig(
+            binary_path=Path.home() / "bin/haproxy"
+        )
+
+    async def run_test(self):
+        await self.make_request()
+        # This will fail with the standard error message
+        return False
+
+
+class ErroredExceptionTest(BaseProxyTest):
+    """Also raising an exception in run_test() produces an errored
+    test
+    """
+
+    def __init__(self):
+        super().__init__()
+        self.description = "A test that produces an exception (Error)"
+        self.proxy_config = ProxyConfig(
+            binary_path=Path.home() / "bin/haproxy"
+        )
+
+    async def run_test(self):
+        await self.make_request()
+        # There's no need of returning bool here
+        raise RuntimeError("This must fail with custom message")