wiremock 配置后浏览器访问显示Request was not matched


问题描述:

Java代码:

@BeforeAll
static void beforeAll(){
  WireMockServer wireMockServer = new WireMockServer(wireMockConfig().port(8080));
  wireMockServer.start();
  configureFor("localhost",8080);
}

@Test
void stub() throws InterruptedException {
  stubFor(get(urlEqualTo("/2020"))
    .withHeader("Accept",equalTo("text/xml"))
    .willReturn(aResponse()
      .withStatus(200)
      .withHeader("Content-Type","text/plain")
      .withBody("exception")));
  Thread.sleep(20000);  //wiremock服务维持时间20s
}

在服务维持期间去访问浏览器 http://127.0.0.1:8080/2020

若页面返回 Request was not matched,即如下图

 解决方法:

1.用cmd命令窗口访问

curl -H "Accept:text/xml" http://127.0.0.1:8080/2020

2.修改java代码中请求参数Accept的值 为图Request列Accept的值,即代码中红色体改为如下

.withHeader("Accept",equalTo("text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9"))

 注意:Accept的值跟访问的浏览器有关。