简体   繁体   English

Wiremock 为存根 url 返回 404

[英]Wiremock returning 404 for a stubbed url

I have defined wireMock server as follows:-我已经定义了wireMock服务器如下:-

    private WireMockServer wireMockServer;
        @Before
        public void preSetup() throws Exception {
          wireMockServer = new WireMockServer(56789);
          wireMockServer.start();
        };

        @Override
        @After
        public void tearDown() {
          wireMockServer.stop();
        }

        @Test
        public void testSendMatchingMessage() throws Exception {

          wireMockServer.stubFor(get(urlEqualTo("/orders/v1/ordersearch/"))
            .willReturn(aResponse().withStatus(200).withBody("<response>Some content</response>")));

       }

But whenever I am hitting the url something like below但是每当我点击以下网址时

http://0.0.0.0:56789/orders/v1/ordersearch/?field=address%2Cfinance%2Cshipping&limit=10&page=2&q=createdFrom.gt%7E2016-01-11T10%3A12%3A13

I am getting the below error:-我收到以下错误:-

    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"/>
    <title>Error 404 NOT_FOUND</title>
    </head>
    <body><h2>HTTP ERROR 404</h2>
    <p>Problem accessing /__files/orders/v1/ordersearch/. Reason:
    <pre>    NOT_FOUND</pre></p><hr /><i><small>Powered by Jetty://</small></i><br/>                                                
    <br/>                                                
    <br/>                                                
    <br/>                                                
    <br/>                                                
    <br/>                                                
    <br/>                                                
    <br/>                                                
    <br/>     
    </body>
    </html>

Can some one let me know what I am doing wrong?有人可以让我知道我做错了什么吗?

As per Stubbing - Wiremock (the 1st in Google on "wiremockserver urlequalto"):根据Stubbing - Wiremock (Google 中“wiremockserver urlequalto”的第一个):

Note: you must use urlPathEqualTo or urlPathMatching to specify the path, as urlEqualTo or urlMatching will attempt to match the whole request URL, including the query parameters.注意:您必须使用urlPathEqualTourlPathMatching来指定路径,因为urlEqualTourlMatching将尝试匹配整个请求 URL,包括查询参数。

For anyone who is trying to add Wiremock to an Android app and stumbles upon this question:对于任何试图将 Wiremock 添加到 Android 应用程序并偶然发现这个问题的人:

If you run your mocking after the network call is made, it won't work.如果在进行网络调用运行模拟,它将不起作用。 This may seem obvious but I was tripped up on it.这似乎很明显,但我被它绊倒了。

When you run an Espresso test, by default the activity test rule launches the activity right away, and so the activity was firing and pulling it's config data before my mocking code was actually run.当您运行 Espresso 测试时,默认情况下,活动测试规则会立即启动活动,因此活动会在我的模拟代码实际运行之前触发并提取其配置数据。 I was seeing the same error as OP.我看到了与 OP 相同的错误。

The fix is to make your activity test rule not launch initially, then mock your data, and tell the activity to start once you've done all that.解决方法是让您的活动测试规则最初不启动,然后模拟您的数据,并在您完成所有操作后告诉活动开始。

The one I usually use is urlPathMatching that you can get when importing:我通常使用的是urlPathMatching ,您可以在导入时获得:

import static com.github.tomakehurst.wiremock.client.WireMock.*;

So in your test, you would have your service of class MyService that would make a rest call to an external api "/orders/v1/ordersearch/" which is that call that will be mocked with the wiremock stubFor .因此,在您的测试中,您将拥有MyService类的service ,该service将对外部 api "/orders/v1/ordersearch/"进行休息调用,该调用将使用 wiremock stubFor

Then the mockServer.verify that the mockServer has made a get to the external api when the service service.sendToExternalUrl() .然后mockServer.verify的mockServer作出了获取对外部API时的服务service.sendToExternalUrl()

  private WireMockServer mockServer;
  private MyService service;

  @Before
  public void preSetup() {
    mockServer = new WireMockServer(56789);
    mockServer.start();
  }

  @After
  public void tearDown() {
    mockServer.stop();
  }

  @Test
  public void testSendMatchingMessage() {
    UrlPattern externalUrl = urlPathMatching("/orders/v1/ordersearch/");
    stubFor(get(externalUrl).willReturn(aResponse().withStatus(200)));

    service.sendToExternalUrl();

    mockServer.verify(1, getRequestedFor(externalUrl)
        .withRequestBody(containing(new JSONObject().put("orders", "results").toString())));
  }

Just to add a different scenario which in my case resulted in the exact same problem:只是添加一个不同的场景,在我的情况下导致完全相同的问题:

make sure that nothing else is using the Wiremock port .确保没有其他东西在使用 Wiremock 端口

I had a Docker container running in the background which had a mapping to the same port that was being used by my Wiremock.我有一个在后台运行的 Docker 容器,它映射到我的 Wiremock 正在使用的同一端口。 This was easy to miss because no binding errors (or any other kind) were thrown by Wiremock when running my tests - it started normally and my test requests would just return 404.这很容易错过,因为在运行我的测试时 Wiremock 没有抛出任何绑定错误(或任何其他类型)——它正常启动,我的测试请求只会返回 404。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM