ExchangeResult exposes URI template used if any

Issue: SPR-15589
This commit is contained in:
Rossen Stoyanchev
2017-05-25 17:07:50 -04:00
parent fd51893a44
commit 0287a74d69
2 changed files with 41 additions and 14 deletions

View File

@@ -151,22 +151,22 @@ class DefaultWebTestClient implements WebTestClient {
@Override
public S uri(URI uri) {
return (S) new DefaultRequestBodySpec(this.uriSpec.uri(uri));
return (S) new DefaultRequestBodySpec(this.uriSpec.uri(uri), null);
}
@Override
public S uri(String uriTemplate, Object... uriVariables) {
return (S) new DefaultRequestBodySpec(this.uriSpec.uri(uriTemplate, uriVariables));
return (S) new DefaultRequestBodySpec(this.uriSpec.uri(uriTemplate, uriVariables), uriTemplate);
}
@Override
public S uri(String uriTemplate, Map<String, ?> uriVariables) {
return (S) new DefaultRequestBodySpec(this.uriSpec.uri(uriTemplate, uriVariables));
return (S) new DefaultRequestBodySpec(this.uriSpec.uri(uriTemplate, uriVariables), uriTemplate);
}
@Override
public S uri(Function<UriBuilder, URI> uriBuilder) {
return (S) new DefaultRequestBodySpec(this.uriSpec.uri(uriBuilder));
return (S) new DefaultRequestBodySpec(this.uriSpec.uri(uriBuilder), null);
}
}
@@ -174,11 +174,14 @@ class DefaultWebTestClient implements WebTestClient {
private final WebClient.RequestBodySpec bodySpec;
private final String uriTemplate;
private final String requestId;
DefaultRequestBodySpec(WebClient.RequestBodySpec spec) {
DefaultRequestBodySpec(WebClient.RequestBodySpec spec, String uriTemplate) {
this.bodySpec = spec;
this.uriTemplate = uriTemplate;
this.requestId = String.valueOf(requestIndex.incrementAndGet());
this.bodySpec.header(WebTestClient.WEBTESTCLIENT_REQUEST_ID, this.requestId);
}
@@ -270,7 +273,7 @@ class DefaultWebTestClient implements WebTestClient {
private DefaultResponseSpec toResponseSpec(Mono<ClientResponse> mono) {
ClientResponse clientResponse = mono.block(getTimeout());
ExchangeResult exchangeResult = wiretapConnector.claimRequest(this.requestId);
return new DefaultResponseSpec(exchangeResult, clientResponse, getTimeout());
return new DefaultResponseSpec(exchangeResult, clientResponse, this.uriTemplate, getTimeout());
}
}
@@ -283,8 +286,10 @@ class DefaultWebTestClient implements WebTestClient {
private final Duration timeout;
UndecodedExchangeResult(ExchangeResult result, ClientResponse response, Duration timeout) {
super(result);
UndecodedExchangeResult(ExchangeResult result, ClientResponse response,
String uriTemplate, Duration timeout) {
super(result, uriTemplate);
this.response = response;
this.timeout = timeout;
}
@@ -321,8 +326,8 @@ class DefaultWebTestClient implements WebTestClient {
private final UndecodedExchangeResult result;
DefaultResponseSpec(ExchangeResult result, ClientResponse response, Duration timeout) {
this.result = new UndecodedExchangeResult(result, response, timeout);
DefaultResponseSpec(ExchangeResult result, ClientResponse response, String uriTemplate, Duration timeout) {
this.result = new UndecodedExchangeResult(result, response, uriTemplate, timeout);
}
@Override

View File

@@ -60,21 +60,36 @@ public class ExchangeResult {
private final WiretapClientHttpResponse response;
private final String uriTemplate;
/**
* Constructor used when the {@code ClientHttpResponse} becomes available.
* Constructor to use after the server response is first received in the
* {@link WiretapConnector} and the {@code ClientHttpResponse} created.
*/
protected ExchangeResult(WiretapClientHttpRequest request, WiretapClientHttpResponse response) {
ExchangeResult(WiretapClientHttpRequest request, WiretapClientHttpResponse response) {
this.request = request;
this.response = response;
this.uriTemplate = null;
}
/**
* Copy constructor used when the body is decoded or consumed.
* Constructor to copy the from the yet undecoded ExchangeResult with extra
* information to expose such as the original URI template used, if any.
*/
protected ExchangeResult(ExchangeResult other) {
ExchangeResult(ExchangeResult other, String uriTemplate) {
this.request = other.request;
this.response = other.response;
this.uriTemplate = uriTemplate;
}
/**
* Copy constructor to use after body is decoded and/or consumed.
*/
ExchangeResult(ExchangeResult other) {
this.request = other.request;
this.response = other.response;
this.uriTemplate = other.uriTemplate;
}
@@ -92,6 +107,13 @@ public class ExchangeResult {
return this.request.getURI();
}
/**
* Return the original URI template used to prepare the request, if any.
*/
public String getUriTemplate() {
return this.uriTemplate;
}
/**
* Return the request headers sent to the server.
*/