03-basic_header_regex.py 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. from pathlib import Path
  2. from proxytester.main import BaseProxyTest, BackendConfig, ProxyConfig
  3. class FailHeaderRegexTest(BaseProxyTest):
  4. """Simple test to demonstrate how to match for header regexes.
  5. This test should fail
  6. """
  7. def __init__(self):
  8. super().__init__()
  9. self.description = "Header regex match (Fail)"
  10. # The expected_header_patterns should contain
  11. # the header name and a pattern that is used to
  12. # match the received header value
  13. self.expected_header_patterns = {
  14. "x-test": r"\d{1}$",
  15. }
  16. self.proxy_config = ProxyConfig(
  17. binary_path=Path.home() / "bin/haproxy"
  18. )
  19. self.backend_config = BackendConfig(
  20. response_headers={"X-Test": "1234"},
  21. )
  22. async def run_test(self):
  23. await self.make_request()
  24. return True
  25. class PassHeaderRegexTest(BaseProxyTest):
  26. """Simple test to demonstrate how to match for header regexes.
  27. """
  28. def __init__(self):
  29. super().__init__()
  30. self.description = "Header regex match (Pass)"
  31. # The expected_header_patterns should contain
  32. # the header name and a pattern that is used to
  33. # match the received header value
  34. self.expected_header_patterns = {
  35. "x-test": r"\d{4}$",
  36. }
  37. self.proxy_config = ProxyConfig(
  38. binary_path=Path.home() / "bin/haproxy"
  39. )
  40. self.backend_config = BackendConfig(
  41. response_headers={"X-Test": "1234"},
  42. )
  43. async def run_test(self):
  44. await self.make_request()
  45. return True