java 如何在响应正文中返回WireMock服务器端口

w8rqjzmb  于 2023-01-11  发布在  Java
关注(0)|答案(3)|浏览(118)

我有一个将WireMock URL返回到另一个WireMock存根的请求。
你有一个问题,对CI工具端口8080是采取,所以我必须使用一个随机端口。@AutoConfigureWireMock(port = 0)十端口是随机的,我不能模拟端口正确。
我MapJSON时出错:

{
  "priority": 1,
  "request": {
    "method": "GET",
    "url": "/api/public/v3/tasks/taskid123"
  },
  "response": {
    "status": 200,
    "jsonBody": {
      "info": {
        "result_callbacks": [{
          "url": "http://localhost:${wiremock.server.port}/download/zip/result"
        }]
      }
    },
    "headers": {
      "Content-Type": "application/json; charset=utf-8"
    }
  }
}

如何在MapJSON时设置服务器端口?

xlpyo6sf

xlpyo6sf1#

您还不能直接获取服务器端口,但可以获取入站端口,这通常具有相同的效果:
{{{request.requestLine.port}}}

i2loujxw

i2loujxw2#

要解决此问题,@Tom的建议是:

{
  "priority": 1,
  "request": {
    "method": "GET",
    "url": "/api/public/v3/tasks/taskid123"
  },
  "response": {
    "status": 200,
    "jsonBody": {
      "info": {
        "result_callbacks": [{
          "url": "http://localhost:{{request.requestLine.port}}/download/zip/result"
        }]
      }
    },
    "headers": {
      "Content-Type": "application/json; charset=utf-8"
    },
    "transformers": [
      "response-template"
    ]
  }
}
hmmo2u0o

hmmo2u0o3#

**注意!**别忘了加上 “变形金刚”[“答复模板”]

"response": {
   ...
   "jsonBody": { "url": "http://localhost:{{port.request}}" },
   "transformers": ["response-template"]
   ...
}

相关问题