Skip to content

Commit a6ab716

Browse files
committed
Use getRawStatusCode() for custom status code support in value methods
Closes gh-26658
1 parent a7db92b commit a6ab716

File tree

2 files changed

+24
-23
lines changed

2 files changed

+24
-23
lines changed

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

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2021 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.
@@ -71,8 +71,7 @@ public WebTestClient.ResponseSpec isOk() {
7171
* Assert the response status code is {@code HttpStatus.CREATED} (201).
7272
*/
7373
public WebTestClient.ResponseSpec isCreated() {
74-
HttpStatus expected = HttpStatus.CREATED;
75-
return assertStatusAndReturn(expected);
74+
return assertStatusAndReturn(HttpStatus.CREATED);
7675
}
7776

7877
/**
@@ -158,8 +157,8 @@ public WebTestClient.ResponseSpec isNotFound() {
158157
*/
159158
public WebTestClient.ResponseSpec reasonEquals(String reason) {
160159
String actual = this.exchangeResult.getStatus().getReasonPhrase();
161-
String message = "Response status reason";
162-
this.exchangeResult.assertWithDiagnostics(() -> AssertionErrors.assertEquals(message, reason, actual));
160+
this.exchangeResult.assertWithDiagnostics(() ->
161+
AssertionErrors.assertEquals("Response status reason", reason, actual));
163162
return this.responseSpec;
164163
}
165164

@@ -195,8 +194,7 @@ public WebTestClient.ResponseSpec is4xxClientError() {
195194
* Assert the response status code is in the 5xx range.
196195
*/
197196
public WebTestClient.ResponseSpec is5xxServerError() {
198-
HttpStatus.Series expected = HttpStatus.Series.SERVER_ERROR;
199-
return assertSeriesAndReturn(expected);
197+
return assertSeriesAndReturn(HttpStatus.Series.SERVER_ERROR);
200198
}
201199

202200
/**
@@ -205,8 +203,8 @@ public WebTestClient.ResponseSpec is5xxServerError() {
205203
* @since 5.1
206204
*/
207205
public WebTestClient.ResponseSpec value(Matcher<Integer> matcher) {
208-
int value = this.exchangeResult.getStatus().value();
209-
this.exchangeResult.assertWithDiagnostics(() -> MatcherAssert.assertThat("Response status", value, matcher));
206+
int actual = this.exchangeResult.getRawStatusCode();
207+
this.exchangeResult.assertWithDiagnostics(() -> MatcherAssert.assertThat("Response status", actual, matcher));
210208
return this.responseSpec;
211209
}
212210

@@ -216,8 +214,8 @@ public WebTestClient.ResponseSpec value(Matcher<Integer> matcher) {
216214
* @since 5.1
217215
*/
218216
public WebTestClient.ResponseSpec value(Consumer<Integer> consumer) {
219-
int value = this.exchangeResult.getStatus().value();
220-
this.exchangeResult.assertWithDiagnostics(() -> consumer.accept(value));
217+
int actual = this.exchangeResult.getRawStatusCode();
218+
this.exchangeResult.assertWithDiagnostics(() -> consumer.accept(actual));
221219
return this.responseSpec;
222220
}
223221

@@ -230,10 +228,8 @@ private WebTestClient.ResponseSpec assertStatusAndReturn(HttpStatus expected) {
230228

231229
private WebTestClient.ResponseSpec assertSeriesAndReturn(HttpStatus.Series expected) {
232230
HttpStatus status = this.exchangeResult.getStatus();
233-
this.exchangeResult.assertWithDiagnostics(() -> {
234-
String message = "Range for response status value " + status;
235-
AssertionErrors.assertEquals(message, expected, status.series());
236-
});
231+
this.exchangeResult.assertWithDiagnostics(() ->
232+
AssertionErrors.assertEquals("Range for response status value " + status, expected, status.series()));
237233
return this.responseSpec;
238234
}
239235

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

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2021 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.
@@ -34,6 +34,7 @@
3434

3535
/**
3636
* Unit tests for {@link StatusAssertions}.
37+
*
3738
* @author Rossen Stoyanchev
3839
*/
3940
public class StatusAssertionTests {
@@ -73,20 +74,19 @@ public void reasonEquals() {
7374
}
7475

7576
@Test
76-
public void statusSerius1xx() {
77+
public void statusSeries1xx() {
7778
StatusAssertions assertions = statusAssertions(HttpStatus.CONTINUE);
7879

7980
// Success
8081
assertions.is1xxInformational();
8182

8283
// Wrong series
83-
8484
assertThatExceptionOfType(AssertionError.class).isThrownBy(() ->
8585
assertions.is2xxSuccessful());
8686
}
8787

8888
@Test
89-
public void statusSerius2xx() {
89+
public void statusSeries2xx() {
9090
StatusAssertions assertions = statusAssertions(HttpStatus.OK);
9191

9292
// Success
@@ -98,7 +98,7 @@ public void statusSerius2xx() {
9898
}
9999

100100
@Test
101-
public void statusSerius3xx() {
101+
public void statusSeries3xx() {
102102
StatusAssertions assertions = statusAssertions(HttpStatus.PERMANENT_REDIRECT);
103103

104104
// Success
@@ -110,7 +110,7 @@ public void statusSerius3xx() {
110110
}
111111

112112
@Test
113-
public void statusSerius4xx() {
113+
public void statusSeries4xx() {
114114
StatusAssertions assertions = statusAssertions(HttpStatus.BAD_REQUEST);
115115

116116
// Success
@@ -122,7 +122,7 @@ public void statusSerius4xx() {
122122
}
123123

124124
@Test
125-
public void statusSerius5xx() {
125+
public void statusSeries5xx() {
126126
StatusAssertions assertions = statusAssertions(HttpStatus.INTERNAL_SERVER_ERROR);
127127

128128
// Success
@@ -134,7 +134,7 @@ public void statusSerius5xx() {
134134
}
135135

136136
@Test
137-
public void matches() {
137+
public void matchesStatusValue() {
138138
StatusAssertions assertions = statusAssertions(HttpStatus.CONFLICT);
139139

140140
// Success
@@ -146,6 +146,11 @@ public void matches() {
146146
assertions.value(equalTo(200)));
147147
}
148148

149+
@Test
150+
public void matchesCustomStatus() {
151+
statusAssertions(600).value(equalTo(600));
152+
}
153+
149154

150155
private StatusAssertions statusAssertions(HttpStatus status) {
151156
return statusAssertions(status.value());

0 commit comments

Comments
 (0)