From dd7369df4820b3418ccfdeaf5ee79e5ebdf63940 Mon Sep 17 00:00:00 2001 From: Rossen Stoyanchev Date: Mon, 17 Aug 2020 14:53:19 +0100 Subject: [PATCH] WiretapConnector.Info is private The claimRequest method was not intended to be public and couldn't have been used since the Info type it returned was package private. This change completely hides the Info. See gh-19647 --- .../reactive/server/DefaultWebTestClient.java | 12 ++++---- .../web/reactive/server/WiretapConnector.java | 30 +++++++++++-------- .../server/WiretapConnectorTests.java | 5 ++-- 3 files changed, 24 insertions(+), 23 deletions(-) diff --git a/spring-test/src/main/java/org/springframework/test/web/reactive/server/DefaultWebTestClient.java b/spring-test/src/main/java/org/springframework/test/web/reactive/server/DefaultWebTestClient.java index 65e2e36614..c9dce22f2a 100644 --- a/spring-test/src/main/java/org/springframework/test/web/reactive/server/DefaultWebTestClient.java +++ b/spring-test/src/main/java/org/springframework/test/web/reactive/server/DefaultWebTestClient.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 the original author or authors. + * Copyright 2002-2020 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -306,8 +306,8 @@ class DefaultWebTestClient implements WebTestClient { public ResponseSpec exchange() { ClientResponse clientResponse = this.bodySpec.exchange().block(getTimeout()); Assert.state(clientResponse != null, "No ClientResponse"); - WiretapConnector.Info info = wiretapConnector.claimRequest(this.requestId); - return new DefaultResponseSpec(info, clientResponse, this.uriTemplate, getTimeout()); + ExchangeResult result = wiretapConnector.getExchangeResult(this.requestId, this.uriTemplate, getTimeout()); + return new DefaultResponseSpec(result, clientResponse, getTimeout()); } } @@ -321,10 +321,8 @@ class DefaultWebTestClient implements WebTestClient { private final Duration timeout; - DefaultResponseSpec(WiretapConnector.Info wiretapInfo, ClientResponse response, - @Nullable String uriTemplate, Duration timeout) { - - this.exchangeResult = wiretapInfo.createExchangeResult(timeout, uriTemplate); + DefaultResponseSpec(ExchangeResult exchangeResult, ClientResponse response, Duration timeout) { + this.exchangeResult = exchangeResult; this.response = response; this.timeout = timeout; } diff --git a/spring-test/src/main/java/org/springframework/test/web/reactive/server/WiretapConnector.java b/spring-test/src/main/java/org/springframework/test/web/reactive/server/WiretapConnector.java index b904da963d..fcc8fc48f9 100644 --- a/spring-test/src/main/java/org/springframework/test/web/reactive/server/WiretapConnector.java +++ b/spring-test/src/main/java/org/springframework/test/web/reactive/server/WiretapConnector.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2020 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -53,7 +53,7 @@ class WiretapConnector implements ClientHttpConnector { private final ClientHttpConnector delegate; - private final Map exchanges = new ConcurrentHashMap<>(); + private final Map exchanges = new ConcurrentHashMap<>(); WiretapConnector(ClientHttpConnector delegate) { @@ -79,43 +79,47 @@ class WiretapConnector implements ClientHttpConnector { String requestId = wrappedRequest.getHeaders().getFirst(header); Assert.state(requestId != null, () -> "No \"" + header + "\" header"); WiretapClientHttpResponse wrappedResponse = new WiretapClientHttpResponse(response); - this.exchanges.put(requestId, new Info(wrappedRequest, wrappedResponse)); + this.exchanges.put(requestId, new ClientExchangeInfo(wrappedRequest, wrappedResponse)); return wrappedResponse; }); } /** - * Retrieve the {@link Info} for the given "request-id" header value. + * Create the {@link ExchangeResult} for the given "request-id" header value. */ - public Info claimRequest(String requestId) { - Info info = this.exchanges.remove(requestId); + ExchangeResult getExchangeResult(String requestId, @Nullable String uriTemplate, Duration timeout) { + ClientExchangeInfo info = this.exchanges.remove(requestId); Assert.state(info != null, () -> { String header = WebTestClient.WEBTESTCLIENT_REQUEST_ID; return "No match for " + header + "=" + requestId; }); - return info; + return new ExchangeResult(info.getRequest(), info.getResponse(), + info.getRequest().getRecorder().getContent(), + info.getResponse().getRecorder().getContent(), + timeout, uriTemplate); } /** * Holder for {@link WiretapClientHttpRequest} and {@link WiretapClientHttpResponse}. */ - class Info { + private static class ClientExchangeInfo { private final WiretapClientHttpRequest request; private final WiretapClientHttpResponse response; - - public Info(WiretapClientHttpRequest request, WiretapClientHttpResponse response) { + public ClientExchangeInfo(WiretapClientHttpRequest request, WiretapClientHttpResponse response) { this.request = request; this.response = response; } + public WiretapClientHttpRequest getRequest() { + return this.request; + } - public ExchangeResult createExchangeResult(Duration timeout, @Nullable String uriTemplate) { - return new ExchangeResult(this.request, this.response, this.request.getRecorder().getContent(), - this.response.getRecorder().getContent(), timeout, uriTemplate); + public WiretapClientHttpResponse getResponse() { + return this.response; } } diff --git a/spring-test/src/test/java/org/springframework/test/web/reactive/server/WiretapConnectorTests.java b/spring-test/src/test/java/org/springframework/test/web/reactive/server/WiretapConnectorTests.java index 60a3ba15e3..000f2f1417 100644 --- a/spring-test/src/test/java/org/springframework/test/web/reactive/server/WiretapConnectorTests.java +++ b/spring-test/src/test/java/org/springframework/test/web/reactive/server/WiretapConnectorTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 the original author or authors. + * Copyright 2002-2020 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -57,8 +57,7 @@ public class WiretapConnectorTests { ExchangeFunction function = ExchangeFunctions.create(wiretapConnector); function.exchange(clientRequest).block(ofMillis(0)); - WiretapConnector.Info actual = wiretapConnector.claimRequest("1"); - ExchangeResult result = actual.createExchangeResult(Duration.ZERO, null); + ExchangeResult result = wiretapConnector.getExchangeResult("1", null, Duration.ZERO); assertThat(result.getMethod()).isEqualTo(HttpMethod.GET); assertThat(result.getUrl().toString()).isEqualTo("/test"); }