03-basic_header_regex.py 1.8 KB

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