0

I am testing on an app that is making calls to a public backend.

I want to use wiremock to mock reponses from the public backends.

I was looking at something like

  1. the app makes a request to api.example.com which is forced through the wiremock proxy on localhost:<port>
  2. wiremock matches the URL based on the rules I provide and returns a mock response

The only examples I could find map localhost requests to real backends or mock responses to localhost.

4

1 回答 1

0

如果我理解您的问题,该应用程序的行为如下:

  • 应用程序向您的后端发送请求
  • 您的后端向公共后端发送转发请求
  • 应用程序向公共后端发送请求
  • 公共后端响应

据我所知,WireMock 只有在模拟来自后端的响应并将该响应的转发请求指向另一个模拟的响应时才能实现这一点。WireMock 仅在向 WireMock 发出请求时才知道重定向和代理。WireMock 不充当中间人代理,监听所有发出的请求(不管 url)并有选择地返回模拟响应。WireMock 仅在您直接点击 WireMock 时才知道代理/转发/响应。所以你需要有类似的东西......

{
    "request" : {
        "url" : "/my-backend",
        "method": GET
    }, 
    "response" : {
        "status" : 302,
        "headers" : {
            "location" : "/public-backend"
        }
    }
}

这会将转发请求返回到/public-backendurl。(取决于您的实际 API 在返回该转发请求时的样子,上述响应可能不准确。)然后您需要模拟该请求/响应映射的样子

{
    "request" : {
        "url" : "/public-backend",
        "method" : "GET"
    },
    "response" : {
        // response
    }
}

根据您用于运行测试的内容,最好利用现有的拦截/代理功能,运行程序必须侦听对某些 url 的请求并以这种方式返回模拟响应。例如,TestCafe 可以拦截 HTTP 请求并返回您的自定义响应。如果您以这种方式进行测试,那么您只需要模拟来自公共 API 的响应,而不必模拟来自后端的响应。

于 2020-12-23T16:27:52.390 回答