Browse Source

Use getRawStatusCode() for custom status code support in value methods

Closes gh-26658
pull/27107/head
Juergen Hoeller 5 years ago
parent
commit
a6ab7164a3
  1. 26
      spring-test/src/main/java/org/springframework/test/web/reactive/server/StatusAssertions.java
  2. 21
      spring-test/src/test/java/org/springframework/test/web/reactive/server/StatusAssertionTests.java

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

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2019 the original author or authors. * Copyright 2002-2021 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -71,8 +71,7 @@ public class StatusAssertions {
* Assert the response status code is {@code HttpStatus.CREATED} (201). * Assert the response status code is {@code HttpStatus.CREATED} (201).
*/ */
public WebTestClient.ResponseSpec isCreated() { public WebTestClient.ResponseSpec isCreated() {
HttpStatus expected = HttpStatus.CREATED; return assertStatusAndReturn(HttpStatus.CREATED);
return assertStatusAndReturn(expected);
} }
/** /**
@ -158,8 +157,8 @@ public class StatusAssertions {
*/ */
public WebTestClient.ResponseSpec reasonEquals(String reason) { public WebTestClient.ResponseSpec reasonEquals(String reason) {
String actual = this.exchangeResult.getStatus().getReasonPhrase(); String actual = this.exchangeResult.getStatus().getReasonPhrase();
String message = "Response status reason"; this.exchangeResult.assertWithDiagnostics(() ->
this.exchangeResult.assertWithDiagnostics(() -> AssertionErrors.assertEquals(message, reason, actual)); AssertionErrors.assertEquals("Response status reason", reason, actual));
return this.responseSpec; return this.responseSpec;
} }
@ -195,8 +194,7 @@ public class StatusAssertions {
* Assert the response status code is in the 5xx range. * Assert the response status code is in the 5xx range.
*/ */
public WebTestClient.ResponseSpec is5xxServerError() { public WebTestClient.ResponseSpec is5xxServerError() {
HttpStatus.Series expected = HttpStatus.Series.SERVER_ERROR; return assertSeriesAndReturn(HttpStatus.Series.SERVER_ERROR);
return assertSeriesAndReturn(expected);
} }
/** /**
@ -205,8 +203,8 @@ public class StatusAssertions {
* @since 5.1 * @since 5.1
*/ */
public WebTestClient.ResponseSpec value(Matcher<Integer> matcher) { public WebTestClient.ResponseSpec value(Matcher<Integer> matcher) {
int value = this.exchangeResult.getStatus().value(); int actual = this.exchangeResult.getRawStatusCode();
this.exchangeResult.assertWithDiagnostics(() -> MatcherAssert.assertThat("Response status", value, matcher)); this.exchangeResult.assertWithDiagnostics(() -> MatcherAssert.assertThat("Response status", actual, matcher));
return this.responseSpec; return this.responseSpec;
} }
@ -216,8 +214,8 @@ public class StatusAssertions {
* @since 5.1 * @since 5.1
*/ */
public WebTestClient.ResponseSpec value(Consumer<Integer> consumer) { public WebTestClient.ResponseSpec value(Consumer<Integer> consumer) {
int value = this.exchangeResult.getStatus().value(); int actual = this.exchangeResult.getRawStatusCode();
this.exchangeResult.assertWithDiagnostics(() -> consumer.accept(value)); this.exchangeResult.assertWithDiagnostics(() -> consumer.accept(actual));
return this.responseSpec; return this.responseSpec;
} }
@ -230,10 +228,8 @@ public class StatusAssertions {
private WebTestClient.ResponseSpec assertSeriesAndReturn(HttpStatus.Series expected) { private WebTestClient.ResponseSpec assertSeriesAndReturn(HttpStatus.Series expected) {
HttpStatus status = this.exchangeResult.getStatus(); HttpStatus status = this.exchangeResult.getStatus();
this.exchangeResult.assertWithDiagnostics(() -> { this.exchangeResult.assertWithDiagnostics(() ->
String message = "Range for response status value " + status; AssertionErrors.assertEquals("Range for response status value " + status, expected, status.series()));
AssertionErrors.assertEquals(message, expected, status.series());
});
return this.responseSpec; return this.responseSpec;
} }

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

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2019 the original author or authors. * Copyright 2002-2021 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -34,6 +34,7 @@ import static org.mockito.Mockito.mock;
/** /**
* Unit tests for {@link StatusAssertions}. * Unit tests for {@link StatusAssertions}.
*
* @author Rossen Stoyanchev * @author Rossen Stoyanchev
*/ */
public class StatusAssertionTests { public class StatusAssertionTests {
@ -73,20 +74,19 @@ public class StatusAssertionTests {
} }
@Test @Test
public void statusSerius1xx() { public void statusSeries1xx() {
StatusAssertions assertions = statusAssertions(HttpStatus.CONTINUE); StatusAssertions assertions = statusAssertions(HttpStatus.CONTINUE);
// Success // Success
assertions.is1xxInformational(); assertions.is1xxInformational();
// Wrong series // Wrong series
assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> assertThatExceptionOfType(AssertionError.class).isThrownBy(() ->
assertions.is2xxSuccessful()); assertions.is2xxSuccessful());
} }
@Test @Test
public void statusSerius2xx() { public void statusSeries2xx() {
StatusAssertions assertions = statusAssertions(HttpStatus.OK); StatusAssertions assertions = statusAssertions(HttpStatus.OK);
// Success // Success
@ -98,7 +98,7 @@ public class StatusAssertionTests {
} }
@Test @Test
public void statusSerius3xx() { public void statusSeries3xx() {
StatusAssertions assertions = statusAssertions(HttpStatus.PERMANENT_REDIRECT); StatusAssertions assertions = statusAssertions(HttpStatus.PERMANENT_REDIRECT);
// Success // Success
@ -110,7 +110,7 @@ public class StatusAssertionTests {
} }
@Test @Test
public void statusSerius4xx() { public void statusSeries4xx() {
StatusAssertions assertions = statusAssertions(HttpStatus.BAD_REQUEST); StatusAssertions assertions = statusAssertions(HttpStatus.BAD_REQUEST);
// Success // Success
@ -122,7 +122,7 @@ public class StatusAssertionTests {
} }
@Test @Test
public void statusSerius5xx() { public void statusSeries5xx() {
StatusAssertions assertions = statusAssertions(HttpStatus.INTERNAL_SERVER_ERROR); StatusAssertions assertions = statusAssertions(HttpStatus.INTERNAL_SERVER_ERROR);
// Success // Success
@ -134,7 +134,7 @@ public class StatusAssertionTests {
} }
@Test @Test
public void matches() { public void matchesStatusValue() {
StatusAssertions assertions = statusAssertions(HttpStatus.CONFLICT); StatusAssertions assertions = statusAssertions(HttpStatus.CONFLICT);
// Success // Success
@ -146,6 +146,11 @@ public class StatusAssertionTests {
assertions.value(equalTo(200))); assertions.value(equalTo(200)));
} }
@Test
public void matchesCustomStatus() {
statusAssertions(600).value(equalTo(600));
}
private StatusAssertions statusAssertions(HttpStatus status) { private StatusAssertions statusAssertions(HttpStatus status) {
return statusAssertions(status.value()); return statusAssertions(status.value());

Loading…
Cancel
Save