Search code examples
automated-testshaproxy

How do I locally test haproxy when routing based on host name?


I have a fairly complex haproxy configuration that routes to backends based on the host of the request (via acl + hdr_dom). If I want to test the configuration locally, I have to change the resolution of the hosts I want to test (for example, by changing hosts file to resolve host to 127.0.0.1). I can then use wget or curl to test the haproxy configuration locally, and reset the hosts file afterward.

However, I want to have automated tests, and changing system level things in an automated test makes me nervous. Before I go down the path of changing the hosts file in an automatic way, is there a way to tell haproxy "pretend this request was made to xxx.yyy.com"? Is there some other approach I can take?

Thanks


Solution

  • is there a way to tell haproxy "pretend this request was made to xxx.yyy.com"?

    I believe that's the wrong question. The correct question is "How can I tell curl that example.com's IP address is 127.0.0.1?"

    The answer is --resolve.

    curl -v --resolve example.com:80:127.0.0.1 http://example.com
    curl -v --resolve example.com:443:127.0.0.1 https://example.com
    

    This preloads the answer (127.0.0.1) into curl's DNS cache for that invocation only, which apparently tricks it into not actually doing a DNS lookup, because it thinks it already did. Instead, it will connect to the IP address you specify.