无法验证对WireMock服务器的请求:“没有请求完全匹配”

时间:2018-04-25 10:17:26

标签: java wiremock

我正在尝试验证我对WireMock服务器发出的请求,但是我收到一条错误消息,指出请求不完全匹配。唯一不匹配的是“Content-Type:plain \ text”位。

请参阅下面的代码:

public class WeatherApplicationTest {


    @Rule
    public  WireMockRule wireMockRule = new WireMockRule(8080);
  //  public   WireMockServer wireMockServer = new WireMockServer(8090); //No-args constructor will start on port 8080, no HTTPS



    @BeforeClass
    public  static void setUpClass() {
        //wireMockServer.start();
    }

    @AfterClass
    public static  void tearDownClass() {
        //wireMockServer.stop();

    }


    @Test
    public void statusMessage() throws IOException{

                wireMockRule.start();


         stubFor(get(urlEqualTo("/user/1"))
        .willReturn(aResponse()
        .withStatus(200)
        .withStatusMessage("Everything is fine")
        .withHeader("Content-Type","text/plain")));

       HttpClient client = new DefaultHttpClient();
       HttpClient client2 = new DefaultHttpClient();

        HttpGet request = new HttpGet("http://localhost:" + wireMockRule.port() + 
        "/user/1");
       HttpResponse response = client.execute(request);

       HttpGet request2 = new HttpGet("http://localhost:" + wireMockRule.port() + 
        "/some/else");
       HttpResponse response2 = client2.execute(request2);

        assertThat(response.getStatusLine().getStatusCode(), is(200));
        assertThat(response2.getStatusLine().getStatusCode(), is(404));


        verify(getRequestedFor(urlEqualTo("/user/1"))
        .withHeader("Content-Type", equalTo("text/plain")));


        wireMockRule.stop();



}
}

这是我收到的错误消息:

  

没有完全匹配的请求。最相似的要求是:预期:<得到   /用户/ 1

     

Content-Type:text / plain

     
    

但是:< GET / user / 1

  

2 个答案:

答案 0 :(得分:0)

问题在于您尝试验证请求是否有Content-Type: text/plain标头,而您只是将其作为响应标头发送。

删除.withHeader("Content-Type", equalTo("text/plain")),它应该有用。

答案 1 :(得分:0)

将标头设置为两个HttpGet请求

 request.setHeader(HttpHeaders.CONTENT_TYPE, "text/plain");

它应该可以正常工作