1

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
  • I tried another approach where I am using the below snippet to forward the request to target servers but i am not sure how to get the query parameters from the URL and pass them on to target server: """ Scenario: pathMatches('/myresources/getResource') && methodIs('GET') * Given url 'http://localhost:8081' And path "/myresources/getResource" When method GET * def response1 = response * Given url 'http://localhost:8082' And path "/myresources/getResource" When method GET * def response1 = response * match response1 == response2 """ – ContinuousLearner Apr 06 '23 at 19:55

2 Answers2

1

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.

Peter Thomas
  • 54,465
  • 21
  • 84
  • 248
  • 1
    Thanks @PeterThomas I was able to set the Host correctly with the above syntax. However, I now ran into another issue where the cookie used in the first call gets updated with a session cookie and then even If I try to update the cookie with the original value it doesnt allow to do so. Now I get a 401 unauthorized error. I used the same syntax : * requestHeaders['cookie'] = originalCookie Is there a way to use the original cookie from the first call? Or update the requestHeaders for the second call to use the original cookie or can i asynshronously submit both the request in parallel? – ContinuousLearner Apr 14 '23 at 06:02
  • @ContinuousLearner a cookie is just a header, so it should have been available. I think this has gotten to the point where it is too specific to your situation. all the details you need are in the above answer, my suggestion is please dive into the code, debug the flow if needed and contribute fixes to karate if needed – Peter Thomas Apr 14 '23 at 06:20
1

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