【问题标题】:Request was not matched when using Wiremock (testing requests to an external server)使用 Wiremock 时请求不匹配(测试对外部服务器的请求)
【发布时间】:2021-02-03 13:15:36
【问题描述】:

我已经使用wiremock 配置了几个存根。我需要检查通过 Rest API 向外部服务器发送请求的客户端。但是当我配置存根时,由于某种原因,涉及到存根的请求被截断,主机(模拟外部服务器)被截断。

application.properties

api.url.fail.header.rec.count.external.server=http://127.0.0.1:8282/dataByUnloadPlanPaysSet/failRecCount
  • 休息客户端

    public ResponseEntity<InfoFromExternalServerDto>
    sendRequestFromExternalServer(String blockId, String urlToExternalServer) {

        UriComponentsBuilder uriComponentsBuilder = buildUriToExternalServer(blockId, urlToExternalServer);
        String uriWithParamsToExternalServer = uriComponentsBuilder.toUriString();

        HttpHeaders requestHttpHeaders = getHeadersHttpHeaders();
        HttpEntity<Object> requestHttpEntity = new HttpEntity<>(null, requestHttpHeaders);

        return restTemplate.exchange(
                uriWithParamsToExternalServer,
                HttpMethod.GET,
                requestHttpEntity,
                InfoFromExternalServerDto.class
        );
    }


    private UriComponentsBuilder buildUriToExternalServer(String blockId, String urlToExternalServer) {

        return UriComponentsBuilder.fromHttpUrl(urlToExternalServer)
                .queryParam("format", "json")
                .queryParam("block", blockId);
    }

    private HttpHeaders getHeadersHttpHeaders() {

        var requestHttpHeaders = new HttpHeaders();
        requestHttpHeaders.add("Accept", MediaType.APPLICATION_JSON_VALUE);

        return requestHttpHeaders;
    }

  • wiremock 配置
@Value("${api.url.fail.header.rec.count.external.server}")
    private String apiUrlFailHeaderRecCountToExternalServer;


    public void setupStubForProcessingRequest(int portExternalServerMock) {

        String addressHost = "127.0.0.1";

        configureFor(addressHost, portExternalServerMock);

        setupResponseInCaseFailRecCount();
    }

    private void setupResponseInCaseFailRecCount(){

        String countEntriesIntoHeaderRecCount = "1";

        UrlPattern urlPattern = urlEqualTo(this.apiUrlFailHeaderRecCountToExternalServer);

        MappingBuilder mappingBuilder = get(urlPattern);

        MappingBuilder mappingBuilderWithHeader = serverMockUtils.makeMappingBuilderSuccess(mappingBuilder);

        int statusOk = HttpStatus.OK.value();

        ResponseDefinitionBuilder responseDefinitionBuilder = aResponse().
                withStatus(statusOk)
                .withHeader("Content-Type", "application/json")
                .withHeader("rec_count", countEntriesIntoHeaderRecCount)
                .withBodyFile("json/infoFromExternalServer.json");

        MappingBuilder responseForReturn = mappingBuilderWithHeader.willReturn(responseDefinitionBuilder);

        stubFor(responseForReturn);
    }

 public MappingBuilder makeMappingBuilderSuccess(MappingBuilder mappingBuilder){

        return mappingBuilder
                .withHeader("Accept", matching("application/json"))
                .withQueryParam("format", equalTo("json"))
                .withQueryParam("block", equalTo(blockId));
    }

  • 测试

@Value("${api.url.fail.header.rec.count.external.server}")
    private String apiUrlFailHeaderRecCountToExternalServer;

    @Value("${blockId.param.query}")
    private String blockId;

    private WireMockServer wireMockServer;

    private final int portExternalServerMock = 8282;

    @BeforeEach
    void setup() {

        WireMockConfiguration mockConfigurationPort = wireMockConfig()
                .port(this.portExternalServerMock);

        wireMockServer = new WireMockServer(mockConfigurationPort);
        wireMockServer.start();

        restServerMockForClient.setupStubForProcessingRequest(this.portExternalServerMock) ;
    }

@AfterEach
    void teardown() {
        wireMockServer.stop();
    }

    @Test
    void getDataFromExternalServer() {
        InfoFromExternalServerDto dataFromExternalServer =
                clientToExternalServer.getDataFromExternalServer(this.blockId,
                        this.apiUrlFailHeaderRecCountToExternalServer);

       
    }


我对不同地址的主机有多种配置。每个主机都有自己的端口。当我通过端点 Rest 控制器发送请求时,主机地址不会被截断。如果我从测试循环中调用调用客户端的方法,那么由于某种原因会形成主机地址并且 Wiremock 存根无法给出有效答案。

请告诉我发生了什么事。我该如何修复这个?

【问题讨论】:

  • 您能否添加您的解决方案作为问题的答案?这将使人们在未来更容易搜索。

标签: java spring-boot rest wiremock


【解决方案1】:

我找到了解决办法。

  • application.properties
api.url.fail.header.rec.count.external.server=http://127.0.0.1:8282/dataByUnloadPlanPaysSet/failRecCount

api.url.fail.header.rec.count.for.stubs=/dataByUnloadPlanPaysSet/failRecCount

api.url.fail.header.rec.count.for.stubs - 此地址必须在创建存根时分配(不指定主机地址和主机端口)

这很重要。

使用方法——urlPathEqualTo()

UrlPathPattern urlPathPattern = urlPathEqualTo(this.apiUrlFailHeaderRecCountToExternalServer);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-22
    • 1970-01-01
    相关资源
    最近更新 更多