Skip to content

Commit b20de75

Browse files
committed
Support custom status code in ExchangeResult for WebTestClient
Prior to this commit, ExchangeResult.assertWithDiagnostics() threw an IllegalArgumentException for a custom HTTP status code since toString() invoked getStatus() without a try-catch block. This commit addresses this issue by introducing a formatStatus() method that defensively formats the response status, initially trying to format the HttpStatus and falling back to formatting the raw integer status code. Closes gh-29283
1 parent 1c1a0af commit b20de75

File tree

2 files changed

+29
-5
lines changed

2 files changed

+29
-5
lines changed

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

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2021 the original author or authors.
2+
* Copyright 2002-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -50,6 +50,7 @@
5050
* respectively.
5151
*
5252
* @author Rossen Stoyanchev
53+
* @author Sam Brannen
5354
* @since 5.0
5455
* @see EntityExchangeResult
5556
* @see FluxExchangeResult
@@ -171,6 +172,8 @@ public byte[] getRequestBodyContent() {
171172

172173
/**
173174
* Return the HTTP status code as an {@link HttpStatus} enum value.
175+
* @throws IllegalArgumentException in case of an unknown HTTP status code
176+
* @see #getRawStatusCode()
174177
*/
175178
public HttpStatus getStatus() {
176179
return this.response.getStatusCode();
@@ -248,13 +251,22 @@ public String toString() {
248251
"\n" +
249252
formatBody(getRequestHeaders().getContentType(), this.requestBody) + "\n" +
250253
"\n" +
251-
"< " + getStatus() + " " + getStatus().getReasonPhrase() + "\n" +
254+
"< " + formatStatus() + "\n" +
252255
"< " + formatHeaders(getResponseHeaders(), "\n< ") + "\n" +
253256
"\n" +
254257
formatBody(getResponseHeaders().getContentType(), this.responseBody) +"\n" +
255258
formatMockServerResult();
256259
}
257260

261+
private String formatStatus() {
262+
try {
263+
return getStatus() + " " + getStatus().getReasonPhrase();
264+
}
265+
catch (Exception ex) {
266+
return Integer.toString(getRawStatusCode());
267+
}
268+
}
269+
258270
private String formatHeaders(HttpHeaders headers, String delimiter) {
259271
return headers.entrySet().stream()
260272
.map(entry -> entry.getKey() + ": " + entry.getValue())

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

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2021 the original author or authors.
2+
* Copyright 2002-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -36,6 +36,7 @@
3636
* Unit tests for {@link StatusAssertions}.
3737
*
3838
* @author Rossen Stoyanchev
39+
* @author Sam Brannen
3940
*/
4041
class StatusAssertionTests {
4142

@@ -56,9 +57,20 @@ void isEqualTo() {
5657
assertions.isEqualTo(408));
5758
}
5859

59-
@Test // gh-23630
60+
@Test // gh-23630, gh-29283
6061
void isEqualToWithCustomStatus() {
61-
statusAssertions(600).isEqualTo(600);
62+
StatusAssertions assertions = statusAssertions(600);
63+
64+
// Success
65+
assertions.isEqualTo(600);
66+
67+
// Wrong status
68+
assertThatExceptionOfType(AssertionError.class).isThrownBy(() ->
69+
assertions.isEqualTo(HttpStatus.REQUEST_TIMEOUT));
70+
71+
// Wrong status value
72+
assertThatExceptionOfType(AssertionError.class).isThrownBy(() ->
73+
assertions.isEqualTo(408));
6274
}
6375

6476
@Test

0 commit comments

Comments
 (0)