Add StatusAssertions.isForbidden

Includes aligned quoting for header assertion messages.
Also aligns HeaderAssertionTests class name.

Issue: SPR-16129
This commit is contained in:
Juergen Hoeller
2017-10-29 09:51:25 +01:00
parent d9ede5bcca
commit 70ed45020b
6 changed files with 48 additions and 45 deletions

View File

@@ -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) {

View File

@@ -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 {

View File

@@ -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;
}

View File

@@ -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();
}
}