10-production_config.py 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. """
  2. Example test using production (static) HAProxy configuration
  3. """
  4. from pathlib import Path
  5. from httphound.main import BaseProxyTest, BackendConfig, ProxyConfig
  6. class ProductionConfigBasicTest(BaseProxyTest):
  7. """Test using a production haproxy config with automatic
  8. backend patching
  9. """
  10. def __init__(self):
  11. super().__init__()
  12. self.description = "Production config with backend patching"
  13. # backend will run on port 9999
  14. self.backend_config = BackendConfig(
  15. host='127.0.0.1',
  16. port=9999,
  17. response_status=200,
  18. response_body='Production test response',
  19. response_headers={"X-Test-Mode":"production"}
  20. )
  21. # configure haproxy to use production config
  22. self.proxy_config = ProxyConfig(
  23. binary_path=Path.home() / "bin/haproxy",
  24. config_mode="production",
  25. production_config_file_path="example_config/haproxy.cfg",
  26. backend_name_to_patch="app_backend",
  27. bind_address_override="*:4242",
  28. skip_backend_injection=False,
  29. )
  30. self.url = "http://127.0.0.1:4242"
  31. self.expected_status = 200
  32. self.expected_headers = {
  33. "x-test-mode": "production",
  34. }
  35. async def run_test(self):
  36. """Run the test"""
  37. await self.make_request()
  38. assert self.backend.request_count == 1
  39. assert "Host" in self.backend.received_headers
  40. return True
  41. class ProductionConfigWithExtraArgsTest(BaseProxyTest):
  42. """Test with custom HAProxy command-line arguments"""
  43. def __init__(self):
  44. super().__init__()
  45. self.description = "Production config with extra HAProxy args"
  46. self.backend_config = BackendConfig(
  47. host="127.0.0.1",
  48. port=9999,
  49. )
  50. self.proxy_config = ProxyConfig(
  51. binary_path=Path.home() / "bin/haproxy",
  52. config_mode="production",
  53. production_config_file_path="example_config/haproxy.cfg",
  54. backend_name_to_patch="app_backend",
  55. bind_address_override="*:4242",
  56. # Add custom HAProxy arguments
  57. extra_args=[
  58. "-dM",
  59. ]
  60. )
  61. self.url = "http://127.0.0.1:4242/"
  62. self.expected_status = 200
  63. async def run_test(self):
  64. await self.make_request()
  65. return True