SpectoLabs / hoverfly-java

Java binding for Hoverfly
Apache License 2.0
168 stars 58 forks source link

Create support for custom headers #259

Closed italomg closed 3 years ago

italomg commented 3 years ago

WHAT This PR allows the user to set custom headers on the HoverflyClient.

WHY This was something I had to implement due to the fact that I am using a Kubernetes stack. To be more specific I am using an Ingress in front of my service where Hoverfly runs. The problem is that Ingress does not allow for a connection on any port, according to the docs:

An Ingress does not expose arbitrary ports or protocols

It would be fairly hard for me to change my stack, so I choose instead to pass specific headers which then get mapped to the Hoverfly admin API or to the Hoverfly simulation.

HOW Instead of having only a method to set the Authorization header, now there is also a method to set any headers using a HashMap. Also, the original functionality of adding an Authorization header was kept for retro compatibility, but underneath it is using the new structure.

I see this might be a particular use case, but it would be nice if this is something that would be welcome to the library as it would enable me to use the official library directly instead of my fork :smile:

tommysitu commented 3 years ago

I think what you are doing is quite specific to your environment, and not part of hoverfly. To allow flexibility for configuring the underlying httpclient, I would recommend adding a new Builder method which accepts your own customized OkHttpClient (you can create one with the header interceptor). Something like this:

HoverflyClient.custom()
      .overrideDefaultHttpClient(yourHttpClient)
      .build()