Changing Request Headers when Karate Netty Server is used as a forward proxy server

179 views Asked by At

I have a use case where I am running the Karate Netty server as a forward proxy server to forward requests to a target Tomcat server. I noted that the hostname header gets set to the Karate Netty Server while forwarding the request which causes the request to not give a response. Can we somehow please update the request header before forwarding the request to target server.

Broader Use Case: Testing in live QA environment where we have a lift and shift project to move our rest services from one infrastructure platform to another infrastructure platform. Here is sample feature file:

Feature:

  Scenario: pathMatches('/myresources/getResource') && methodIs('GET')
    # Sending request to PLATFORM ABC URL
    * karate.proceed('http://localhost:8081')
    * def response1 = response
    # Sending request to PLATFORM XYZ URL
    * karate.proceed('http://localhost:8082')
    * def response2 = response
    * match response1 == response2
2

There are 2 answers

2
Peter Thomas On

First, to your comment - getting the path including the query string is supposed to work via requestUri: https://github.com/karatelabs/karate/tree/master/karate-netty#requesturi

But we realized there's a bug. It will be fixed in the next version, but if you can validate from the develop branch, that will help us expedite a release.

If you see the commits in issue #2295 linked above, you will see we introduced requestPath for more control over these cases.

When it comes to headers, I think this line in your mock before you call karate.proceed() will do the trick:

* requestHeaders['host'] = 'myhost:123'

Do confirm. If you still see things that need to be tweaked, I request your help - you can see the flow and run this test to investigate. I remember someone ran into an issue with the content-length, so we decided to just remove that header in a mock, see this line.

0
ContinuousLearner On

Thanks @Peter Thomas for all the help! I was able to resolve the above issue by using the below snippet:

      * configure cookies = null
      * requestHeaders['cookie'] = authToken
      * requestHeaders['host'] = firstHost
      * karate.proceed(url1)
      * def response1 = response
      * configure cookies = null
      * requestHeaders['cookie'] = authToken
      * requestHeaders['host'] = secondHost
      * karate.proceed(url2)
      * def response2 = response
      * match response1 = response2