Skip to content

Commit 0287a74

Browse files
committed
ExchangeResult exposes URI template used if any
Issue: SPR-15589
1 parent fd51893 commit 0287a74

File tree

2 files changed

+41
-14
lines changed

2 files changed

+41
-14
lines changed

spring-test/src/main/java/org/springframework/test/web/reactive/server/DefaultWebTestClient.java

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -151,34 +151,37 @@ private class DefaultUriSpec<S extends RequestHeadersSpec<?>> implements UriSpec
151151

152152
@Override
153153
public S uri(URI uri) {
154-
return (S) new DefaultRequestBodySpec(this.uriSpec.uri(uri));
154+
return (S) new DefaultRequestBodySpec(this.uriSpec.uri(uri), null);
155155
}
156156

157157
@Override
158158
public S uri(String uriTemplate, Object... uriVariables) {
159-
return (S) new DefaultRequestBodySpec(this.uriSpec.uri(uriTemplate, uriVariables));
159+
return (S) new DefaultRequestBodySpec(this.uriSpec.uri(uriTemplate, uriVariables), uriTemplate);
160160
}
161161

162162
@Override
163163
public S uri(String uriTemplate, Map<String, ?> uriVariables) {
164-
return (S) new DefaultRequestBodySpec(this.uriSpec.uri(uriTemplate, uriVariables));
164+
return (S) new DefaultRequestBodySpec(this.uriSpec.uri(uriTemplate, uriVariables), uriTemplate);
165165
}
166166

167167
@Override
168168
public S uri(Function<UriBuilder, URI> uriBuilder) {
169-
return (S) new DefaultRequestBodySpec(this.uriSpec.uri(uriBuilder));
169+
return (S) new DefaultRequestBodySpec(this.uriSpec.uri(uriBuilder), null);
170170
}
171171
}
172172

173173
private class DefaultRequestBodySpec implements RequestBodySpec {
174174

175175
private final WebClient.RequestBodySpec bodySpec;
176176

177+
private final String uriTemplate;
178+
177179
private final String requestId;
178180

179181

180-
DefaultRequestBodySpec(WebClient.RequestBodySpec spec) {
182+
DefaultRequestBodySpec(WebClient.RequestBodySpec spec, String uriTemplate) {
181183
this.bodySpec = spec;
184+
this.uriTemplate = uriTemplate;
182185
this.requestId = String.valueOf(requestIndex.incrementAndGet());
183186
this.bodySpec.header(WebTestClient.WEBTESTCLIENT_REQUEST_ID, this.requestId);
184187
}
@@ -270,7 +273,7 @@ public RequestHeadersSpec<?> syncBody(Object body) {
270273
private DefaultResponseSpec toResponseSpec(Mono<ClientResponse> mono) {
271274
ClientResponse clientResponse = mono.block(getTimeout());
272275
ExchangeResult exchangeResult = wiretapConnector.claimRequest(this.requestId);
273-
return new DefaultResponseSpec(exchangeResult, clientResponse, getTimeout());
276+
return new DefaultResponseSpec(exchangeResult, clientResponse, this.uriTemplate, getTimeout());
274277
}
275278

276279
}
@@ -283,8 +286,10 @@ private static class UndecodedExchangeResult extends ExchangeResult {
283286
private final Duration timeout;
284287

285288

286-
UndecodedExchangeResult(ExchangeResult result, ClientResponse response, Duration timeout) {
287-
super(result);
289+
UndecodedExchangeResult(ExchangeResult result, ClientResponse response,
290+
String uriTemplate, Duration timeout) {
291+
292+
super(result, uriTemplate);
288293
this.response = response;
289294
this.timeout = timeout;
290295
}
@@ -321,8 +326,8 @@ private static class DefaultResponseSpec implements ResponseSpec {
321326
private final UndecodedExchangeResult result;
322327

323328

324-
DefaultResponseSpec(ExchangeResult result, ClientResponse response, Duration timeout) {
325-
this.result = new UndecodedExchangeResult(result, response, timeout);
329+
DefaultResponseSpec(ExchangeResult result, ClientResponse response, String uriTemplate, Duration timeout) {
330+
this.result = new UndecodedExchangeResult(result, response, uriTemplate, timeout);
326331
}
327332

328333
@Override

spring-test/src/main/java/org/springframework/test/web/reactive/server/ExchangeResult.java

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,21 +60,36 @@ public class ExchangeResult {
6060

6161
private final WiretapClientHttpResponse response;
6262

63+
private final String uriTemplate;
64+
6365

6466
/**
65-
* Constructor used when the {@code ClientHttpResponse} becomes available.
67+
* Constructor to use after the server response is first received in the
68+
* {@link WiretapConnector} and the {@code ClientHttpResponse} created.
6669
*/
67-
protected ExchangeResult(WiretapClientHttpRequest request, WiretapClientHttpResponse response) {
70+
ExchangeResult(WiretapClientHttpRequest request, WiretapClientHttpResponse response) {
6871
this.request = request;
6972
this.response = response;
73+
this.uriTemplate = null;
7074
}
7175

7276
/**
73-
* Copy constructor used when the body is decoded or consumed.
77+
* Constructor to copy the from the yet undecoded ExchangeResult with extra
78+
* information to expose such as the original URI template used, if any.
7479
*/
75-
protected ExchangeResult(ExchangeResult other) {
80+
ExchangeResult(ExchangeResult other, String uriTemplate) {
7681
this.request = other.request;
7782
this.response = other.response;
83+
this.uriTemplate = uriTemplate;
84+
}
85+
86+
/**
87+
* Copy constructor to use after body is decoded and/or consumed.
88+
*/
89+
ExchangeResult(ExchangeResult other) {
90+
this.request = other.request;
91+
this.response = other.response;
92+
this.uriTemplate = other.uriTemplate;
7893
}
7994

8095

@@ -92,6 +107,13 @@ public URI getUrl() {
92107
return this.request.getURI();
93108
}
94109

110+
/**
111+
* Return the original URI template used to prepare the request, if any.
112+
*/
113+
public String getUriTemplate() {
114+
return this.uriTemplate;
115+
}
116+
95117
/**
96118
* Return the request headers sent to the server.
97119
*/

0 commit comments

Comments
 (0)