更改请求头时,使用Karate Netty服务器作为转发代理服务器。

huangapple go评论91阅读模式
英文:

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

问题

我有一个使用案例,我正在将Karate Netty服务器作为转发代理服务器运行,将请求转发到目标的Tomcat服务器。我注意到在转发请求时,hostname标头被设置为Karate Netty服务器,这导致请求不返回响应。我们是否可以在将请求转发到目标服务器之前更新请求标头。

更广泛的使用案例:在实际的QA环境中进行测试,我们有一个将我们的REST服务从一个基础设施平台移动到另一个基础设施平台的项目。以下是一个示例特性文件:

英文:

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

答案1

得分: 1

首先,关于您的评论 - 获取包括查询字符串的路径应该通过requestUri工作:
https://github.com/karatelabs/karate/tree/master/karate-netty#requesturi

但我们意识到存在一个错误。它将在下一个版本中修复,但如果您可以从develop分支中验证,这将帮助我们加快发布进度。

如果您查看上面链接的问题#2295中的提交,您将看到我们引入了requestPath以更好地控制这些情况。

至于标头,我认为在调用karate.proceed()之前在您的模拟中使用此行将起作用:

  • requestHeaders['host'] = 'myhost:123';

请确认。如果您仍然看到需要调整的内容,请帮助我们 - 您可以查看流程并运行此测试 进行调查。我记得有人遇到了内容长度的问题,所以我们决定在模拟中删除该标头,参见此行

英文:

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.

答案2

得分: 1

感谢 @Peter Thomas 的所有帮助!我能够通过使用以下代码段解决上述问题:

  • 配置 cookies = null
  • requestHeaders['cookie'] = authToken
  • requestHeaders['host'] = firstHost
  • karate.proceed(url1)
  • 定义 response1 = response
  • 配置 cookies = null
  • requestHeaders['cookie'] = authToken
  • requestHeaders['host'] = secondHost
  • karate.proceed(url2)
  • 定义 response2 = response
  • 匹配 response1 = response2
英文:

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

huangapple
  • 本文由 发表于 2023年4月7日 03:40:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/75953196.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定