简体   繁体   English

使用 Wiremock 时请求不匹配(测试对外部服务器的请求)

[英]Request was not matched when using Wiremock (testing requests to an external server)

I have configured several stubs using wiremock.我已经使用wiremock 配置了几个存根。 I need to check a client that sends a request to an external server via the Rest API.我需要检查通过 Rest API 向外部服务器发送请求的客户端。 But when I configured the stubs, then for some reason the request when it comes to the stub is truncated and the host (simulating an external server) is truncated.但是当我配置存根时,由于某种原因,涉及到存根的请求被截断并且主机(模拟外部服务器)被截断。

在此处输入图像描述

application.properties应用程序属性

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

    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 configure线模配置
@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));
    }

  • test测试

@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);

       
    }


I have multiple configuration for hosts with different addresses.我对具有不同地址的主机有多种配置。 Each host has its own port.每个主机都有自己的端口。 When I send a request via the endpoint Rest controller, then the host address is not truncated.当我通过端点 Rest controller 发送请求时,主机地址不会被截断。 If I call the method that calls the client from the test loop, then for some reason the host address is formed and the Wiremock stub cannot give a valid answer.如果我从测试循环调用调用客户端的方法,那么由于某种原因,主机地址已形成,Wiremock 存根无法给出有效答案。

Please tell me what's going on.请告诉我发生了什么事。 How can I fix this?我该如何解决这个问题?

I found a solution.我找到了解决方案。

  • 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 - this address must be assigned when creating a stub (without specifying the host address and host port) api.url.fail.header.rec.count.for.stubs - 此地址必须在创建存根时分配主机地址和主机端口

It is important.这很重要。

Use the method - urlPathEqualTo()使用方法 - urlPathEqualTo()

UrlPathPattern urlPathPattern = urlPathEqualTo(this.apiUrlFailHeaderRecCountToExternalServer);

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

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