使用 WireMock 测试带有 HttpResponse 的 HttpRequest 的问题

问题描述 投票:0回答:1

使用下面的代码,会出现错误:

<h1>Bad Message 400</h1><pre>reason: Bad Request</pre>

    @Test
    public void basicWireMockTest() throws IOException, InterruptedException {
        // Setup the WireMock mapping stub for the test
        CriticNotification r = CriticNotificationServiceUnitTest.getCriticNotification();
        String filterCreated = (filterBuilder.createFilter(r));
        String url = filterBuilder.getUri(false, r.getVirtualPair());
        String completeUrl = filterBuilder.generateCompleteFilter(url, filterCreated).replaceFirst(url, "");
        wireMockServer.stubFor(get(FilterBuilderImpl.URI_PATH)
                .withHeader("Connection", containing("close"))
                .withHeader("Content-Type", containing("application/json"))
                .withHeader("Authorization", containing(filterBuilder.getAuthorization()))
                .willReturn(aResponse()
                        .withHeader("Connection", "close")
                        .withHeader("Content-Type", "application/json")
                        .withBodyFile("**responseGet.json**")));
        // Setup HTTP GET request (with HTTP Client embedded in Java 17+)
        final HttpClient client = HttpClient.newBuilder().build();
        final HttpRequest request = HttpRequest.newBuilder()
                .header("Connection", "close")
                .header("Content-Type", "application/json")
                .header("Authorization", filterBuilder.getAuthorization())
                .uri(URI.create(wireMockServer.url(completeUrl)))
                .GET()
                .build();
        // Send the request and receive the response
        final HttpResponse<String> response =
                client.send(request, HttpResponse.BodyHandlers.ofString());

        System.out.println(response.request().uri().toURL().toString() + "[" + **response.headers().toString()** + "]");
        System.out.println(response.body());

我正在使用:

import java.net.http.HttpClient; import java.net.http.HttpRequest; import java.net.http.HttpResponse; 

错误消息 400

原因:错误请求

这是在response.body()中,我期望配置文件中定义的内容(特别是body.json):

__files目录下有两个文件:responseGet.json和body.json

responseGet.json内容:

{
  "request": {
    "method": "GET",
    "url": "/objectserver/restapi/alerts/status",
    "headers": {
      "Connection": {
        "contains": "close"
      },
      "Content-Type": {
        "contains": "application/json"
      },
    }
  },
  "response": {
    "headers": {"Connection": "close","Content-Type": "application/json"},
    "status": 200,
    "bodyFileName": "body.json"
  }
}

body.json内容:

{
  "rowset": {
    "osname": "CNSAADP",
    "tblname": "status",
    "rows": [
      {
        "Serial": 2114264833,
        "ServerName": "CNSPCC2"
      }
    ],
    "affectedRows": 1,
    "coldesc": [
      {
        "name": "Serial",
        "type": "integer",
        "size": 4
      },
      {
        "name": "ServerName",
        "type": "string",
        "size": 64
      }
    ],
    "dbname": "alerts"
  }
}

我认为问题与响应标头有关,因为这是我像他们一样收到的:

java.net.http.HttpHeaders@161e87ad { {connection=[close], content-length=[54], content-type=[text/html;charset=iso-8859-1]} }

此输出来自: response.headers().toString()

任何想法,为什么我没有收到像 body.json 文件内容?

java wiremock
1个回答
0
投票

我找到了解决办法。

我没有使用responseGet.json 文件,而是使用body.json。而且,当创建请求时,我添加了版本,如下所示:

final HttpRequest request = HttpRequest.newBuilder()
            .header("Connection", "close")
            .header("Content-Type", "application/json")
            .header("Authorization", filterBuilder.getAuthorization())
            .version(HttpClient.Version.HTTP_1_1)
            .uri(URI.create(wireMockServer.url(completeUrl)))
            .GET()
            .build();
© www.soinside.com 2019 - 2024. All rights reserved.