Add StatusAssertions.isForbidden
Includes aligned quoting for header assertion messages. Also aligns HeaderAssertionTests class name. Issue: SPR-16129
This commit is contained in:
@@ -66,7 +66,7 @@ public class HeaderAssertions {
|
||||
fail(getMessage(name) + " not found");
|
||||
}
|
||||
boolean match = Pattern.compile(pattern).matcher(value).matches();
|
||||
String message = getMessage(name) + "=\'" + value + "\' does not match \'" + pattern + "\'";
|
||||
String message = getMessage(name) + "=[" + value + "] does not match [" + pattern + "]";
|
||||
this.exchangeResult.assertWithDiagnostics(() -> assertTrue(message, match));
|
||||
return this.responseSpec;
|
||||
}
|
||||
@@ -114,14 +114,12 @@ public class HeaderAssertions {
|
||||
}
|
||||
|
||||
|
||||
// Private methods
|
||||
|
||||
private HttpHeaders getHeaders() {
|
||||
return this.exchangeResult.getResponseHeaders();
|
||||
}
|
||||
|
||||
private String getMessage(String headerName) {
|
||||
return "Response header [" + headerName + "]";
|
||||
return "Response header '" + headerName + "'";
|
||||
}
|
||||
|
||||
private WebTestClient.ResponseSpec assertHeader(String name, @Nullable Object expected, @Nullable Object actual) {
|
||||
|
||||
@@ -24,6 +24,7 @@ import org.springframework.test.util.JsonPathExpectationsHelper;
|
||||
* @author Rossen Stoyanchev
|
||||
* @since 5.0
|
||||
* @see <a href="https://github.com/jayway/JsonPath">https://github.com/jayway/JsonPath</a>
|
||||
* @see JsonPathExpectationsHelper
|
||||
*/
|
||||
public class JsonPathAssertions {
|
||||
|
||||
|
||||
@@ -18,7 +18,7 @@ package org.springframework.test.web.reactive.server;
|
||||
|
||||
import org.springframework.http.HttpStatus;
|
||||
|
||||
import static org.springframework.test.util.AssertionErrors.assertEquals;
|
||||
import static org.springframework.test.util.AssertionErrors.*;
|
||||
|
||||
/**
|
||||
* Assertions on the response status.
|
||||
@@ -134,6 +134,14 @@ public class StatusAssertions {
|
||||
return assertStatusAndReturn(HttpStatus.UNAUTHORIZED);
|
||||
}
|
||||
|
||||
/**
|
||||
* Assert the response status code is {@code HttpStatus.FORBIDDEN} (403).
|
||||
* @since 5.0.2
|
||||
*/
|
||||
public WebTestClient.ResponseSpec isForbidden() {
|
||||
return assertStatusAndReturn(HttpStatus.FORBIDDEN);
|
||||
}
|
||||
|
||||
/**
|
||||
* Assert the response status code is {@code HttpStatus.NOT_FOUND} (404).
|
||||
*/
|
||||
@@ -187,7 +195,6 @@ public class StatusAssertions {
|
||||
return assertSeriesAndReturn(expected);
|
||||
}
|
||||
|
||||
// Private methods
|
||||
|
||||
private WebTestClient.ResponseSpec assertStatusAndReturn(HttpStatus expected) {
|
||||
HttpStatus actual = this.exchangeResult.getStatus();
|
||||
@@ -197,8 +204,8 @@ public class StatusAssertions {
|
||||
|
||||
private WebTestClient.ResponseSpec assertSeriesAndReturn(HttpStatus.Series expected) {
|
||||
HttpStatus status = this.exchangeResult.getStatus();
|
||||
String message = "Range for response status value " + status;
|
||||
this.exchangeResult.assertWithDiagnostics(() -> assertEquals(message, expected, status.series()));
|
||||
this.exchangeResult.assertWithDiagnostics(() ->
|
||||
assertEquals("Range for response status value " + status, expected, status.series()));
|
||||
return this.responseSpec;
|
||||
}
|
||||
|
||||
|
||||
@@ -75,6 +75,9 @@ import org.springframework.web.util.UriBuilderFactory;
|
||||
*
|
||||
* @author Rossen Stoyanchev
|
||||
* @since 5.0
|
||||
* @see StatusAssertions
|
||||
* @see HeaderAssertions
|
||||
* @see JsonPathAssertions
|
||||
*/
|
||||
public interface WebTestClient {
|
||||
|
||||
@@ -154,7 +157,7 @@ public interface WebTestClient {
|
||||
WebTestClient mutateWith(WebTestClientConfigurer configurer);
|
||||
|
||||
|
||||
// Static, factory methods
|
||||
// Static factory methods
|
||||
|
||||
/**
|
||||
* Use this server setup to test one `@Controller` at a time.
|
||||
@@ -261,9 +264,9 @@ public interface WebTestClient {
|
||||
* Shortcut to build the test client.
|
||||
*/
|
||||
WebTestClient build();
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Specification for customizing controller configuration equivalent to, and
|
||||
* internally delegating to, a {@link WebFluxConfigurer}.
|
||||
@@ -324,9 +327,9 @@ public interface WebTestClient {
|
||||
* @see WebFluxConfigurer#configureViewResolvers
|
||||
*/
|
||||
ControllerSpec viewResolvers(Consumer<ViewResolverRegistry> consumer);
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Specification for customizing router function configuration.
|
||||
*/
|
||||
@@ -336,9 +339,9 @@ public interface WebTestClient {
|
||||
* Configure handler strategies.
|
||||
*/
|
||||
RouterFunctionSpec handlerStrategies(HandlerStrategies handlerStrategies);
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Steps for customizing the {@link WebClient} used to test with
|
||||
* internally delegating to a {@link WebClient.Builder}.
|
||||
@@ -433,7 +436,6 @@ public interface WebTestClient {
|
||||
* Build the {@link WebTestClient} instance.
|
||||
*/
|
||||
WebTestClient build();
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -470,9 +472,9 @@ public interface WebTestClient {
|
||||
* @return spec to add headers or perform the exchange
|
||||
*/
|
||||
S uri(Function<UriBuilder, URI> uriFunction);
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Specification for adding request headers and performing an exchange.
|
||||
*/
|
||||
@@ -570,9 +572,9 @@ public interface WebTestClient {
|
||||
* @return spec for decoding the response
|
||||
*/
|
||||
ResponseSpec exchange();
|
||||
|
||||
}
|
||||
|
||||
|
||||
interface RequestBodySpec extends RequestHeadersSpec<RequestBodySpec> {
|
||||
/**
|
||||
* Set the length of the body in bytes, as specified by the
|
||||
@@ -617,13 +619,13 @@ public interface WebTestClient {
|
||||
* @return a {@code Mono} with the response
|
||||
*/
|
||||
RequestHeadersSpec<?> syncBody(Object body);
|
||||
|
||||
}
|
||||
|
||||
interface RequestHeadersUriSpec<S extends RequestHeadersSpec<S>>
|
||||
extends UriSpec<S>, RequestHeadersSpec<S> {
|
||||
|
||||
interface RequestHeadersUriSpec<S extends RequestHeadersSpec<S>> extends UriSpec<S>, RequestHeadersSpec<S> {
|
||||
}
|
||||
|
||||
|
||||
interface RequestBodyUriSpec extends RequestBodySpec, RequestHeadersUriSpec<RequestBodySpec> {
|
||||
}
|
||||
|
||||
@@ -698,6 +700,7 @@ public interface WebTestClient {
|
||||
<T> FluxExchangeResult<T> returnResult(ParameterizedTypeReference<T> elementType);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Spec for expectations on the response body decoded to a single Object.
|
||||
*/
|
||||
@@ -718,9 +721,9 @@ public interface WebTestClient {
|
||||
* decoded response content.
|
||||
*/
|
||||
EntityExchangeResult<B> returnResult();
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Spec for expectations on the response body decoded to a List.
|
||||
*/
|
||||
@@ -745,9 +748,9 @@ public interface WebTestClient {
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
ListBodySpec<E> doesNotContain(E... elements);
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Spec for expectations on the response body content.
|
||||
*/
|
||||
@@ -791,7 +794,6 @@ public interface WebTestClient {
|
||||
* raw response content.
|
||||
*/
|
||||
EntityExchangeResult<byte[]> returnResult();
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -30,10 +30,8 @@ import org.springframework.http.MediaType;
|
||||
import org.springframework.mock.http.client.reactive.MockClientHttpRequest;
|
||||
import org.springframework.mock.http.client.reactive.MockClientHttpResponse;
|
||||
|
||||
import static junit.framework.TestCase.assertNotNull;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.fail;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link HeaderAssertions}.
|
||||
@@ -41,11 +39,10 @@ import static org.mockito.Mockito.mock;
|
||||
* @author Rossen Stoyanchev
|
||||
* @since 5.0
|
||||
*/
|
||||
public class HeaderAssertionsTests {
|
||||
|
||||
public class HeaderAssertionTests {
|
||||
|
||||
@Test
|
||||
public void valueEquals() throws Exception {
|
||||
public void valueEquals() {
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
headers.add("foo", "bar");
|
||||
HeaderAssertions assertions = headerAssertions(headers);
|
||||
@@ -79,7 +76,7 @@ public class HeaderAssertionsTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void valueEqualsWithMultipeValues() throws Exception {
|
||||
public void valueEqualsWithMultipeValues() {
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
headers.add("foo", "bar");
|
||||
headers.add("foo", "baz");
|
||||
@@ -107,7 +104,7 @@ public class HeaderAssertionsTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void valueMatches() throws Exception {
|
||||
public void valueMatches() {
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
|
||||
HeaderAssertions assertions = headerAssertions(headers);
|
||||
@@ -122,14 +119,13 @@ public class HeaderAssertionsTests {
|
||||
catch (AssertionError error) {
|
||||
Throwable cause = error.getCause();
|
||||
assertNotNull(cause);
|
||||
assertEquals("Response header [Content-Type]='application/json;charset=UTF-8' " +
|
||||
"does not match '.*ISO-8859-1.*'", cause.getMessage());
|
||||
assertEquals("Response header 'Content-Type'=[application/json;charset=UTF-8] " +
|
||||
"does not match [.*ISO-8859-1.*]", cause.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void cacheControl() throws Exception {
|
||||
|
||||
public void cacheControl() {
|
||||
CacheControl control = CacheControl.maxAge(1, TimeUnit.HOURS).noTransform();
|
||||
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
@@ -148,6 +144,7 @@ public class HeaderAssertionsTests {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private HeaderAssertions headerAssertions(HttpHeaders responseHeaders) {
|
||||
MockClientHttpRequest request = new MockClientHttpRequest(HttpMethod.GET, URI.create("/"));
|
||||
MockClientHttpResponse response = new MockClientHttpResponse(HttpStatus.OK);
|
||||
@@ -26,8 +26,8 @@ import org.springframework.http.HttpStatus;
|
||||
import org.springframework.mock.http.client.reactive.MockClientHttpRequest;
|
||||
import org.springframework.mock.http.client.reactive.MockClientHttpResponse;
|
||||
|
||||
import static org.junit.Assert.fail;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link StatusAssertions}.
|
||||
@@ -38,8 +38,7 @@ import static org.mockito.Mockito.mock;
|
||||
public class StatusAssertionTests {
|
||||
|
||||
@Test
|
||||
public void isEqualTo() throws Exception {
|
||||
|
||||
public void isEqualTo() {
|
||||
StatusAssertions assertions = statusAssertions(HttpStatus.CONFLICT);
|
||||
|
||||
// Success
|
||||
@@ -64,8 +63,7 @@ public class StatusAssertionTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void reasonEquals() throws Exception {
|
||||
|
||||
public void reasonEquals() {
|
||||
StatusAssertions assertions = statusAssertions(HttpStatus.CONFLICT);
|
||||
|
||||
// Success
|
||||
@@ -81,7 +79,7 @@ public class StatusAssertionTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void statusSerius1xx() throws Exception {
|
||||
public void statusSerius1xx() {
|
||||
StatusAssertions assertions = statusAssertions(HttpStatus.CONTINUE);
|
||||
|
||||
// Success
|
||||
@@ -97,7 +95,7 @@ public class StatusAssertionTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void statusSerius2xx() throws Exception {
|
||||
public void statusSerius2xx() {
|
||||
StatusAssertions assertions = statusAssertions(HttpStatus.OK);
|
||||
|
||||
// Success
|
||||
@@ -113,7 +111,7 @@ public class StatusAssertionTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void statusSerius3xx() throws Exception {
|
||||
public void statusSerius3xx() {
|
||||
StatusAssertions assertions = statusAssertions(HttpStatus.PERMANENT_REDIRECT);
|
||||
|
||||
// Success
|
||||
@@ -129,7 +127,7 @@ public class StatusAssertionTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void statusSerius4xx() throws Exception {
|
||||
public void statusSerius4xx() {
|
||||
StatusAssertions assertions = statusAssertions(HttpStatus.BAD_REQUEST);
|
||||
|
||||
// Success
|
||||
@@ -145,7 +143,7 @@ public class StatusAssertionTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void statusSerius5xx() throws Exception {
|
||||
public void statusSerius5xx() {
|
||||
StatusAssertions assertions = statusAssertions(HttpStatus.INTERNAL_SERVER_ERROR);
|
||||
|
||||
// Success
|
||||
|
||||
Reference in New Issue
Block a user