Skip to content

Commit

Permalink
Merge branch 'webtestclient-experiment'
Browse files Browse the repository at this point in the history
  • Loading branch information
rstoyanchev committed Aug 19, 2020
2 parents 443e9ee + 3426e62 commit ccb719e
Show file tree
Hide file tree
Showing 57 changed files with 5,856 additions and 118 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public int getRawStatusCode() {
public HttpHeaders getHeaders() {
if (!getCookies().isEmpty() && this.headers.get(HttpHeaders.SET_COOKIE) == null) {
getCookies().values().stream().flatMap(Collection::stream)
.forEach(cookie -> getHeaders().add(HttpHeaders.SET_COOKIE, cookie.toString()));
.forEach(cookie -> this.headers.add(HttpHeaders.SET_COOKIE, cookie.toString()));
}
return this.headers;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,220 @@
/*
* Copyright 2002-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.test.web.reactive.server;

import java.time.Duration;
import java.util.function.Consumer;

import org.hamcrest.Matcher;
import org.hamcrest.MatcherAssert;

import org.springframework.http.ResponseCookie;
import org.springframework.test.util.AssertionErrors;

import static org.hamcrest.MatcherAssert.assertThat;

/**
* Assertions on cookies of the response.
* @author Rossen Stoyanchev
*/
public class CookieAssertions {

private final ExchangeResult exchangeResult;

private final WebTestClient.ResponseSpec responseSpec;


public CookieAssertions(ExchangeResult exchangeResult, WebTestClient.ResponseSpec responseSpec) {
this.exchangeResult = exchangeResult;
this.responseSpec = responseSpec;
}


/**
* Expect a header with the given name to match the specified values.
*/
public WebTestClient.ResponseSpec valueEquals(String name, String value) {
this.exchangeResult.assertWithDiagnostics(() -> {
String message = getMessage(name);
AssertionErrors.assertEquals(message, value, getCookie(name).getValue());
});
return this.responseSpec;
}

/**
* Assert the first value of the response cookie with a Hamcrest {@link Matcher}.
*/
public WebTestClient.ResponseSpec value(String name, Matcher<? super String> matcher) {
String value = getCookie(name).getValue();
this.exchangeResult.assertWithDiagnostics(() -> {
String message = getMessage(name);
MatcherAssert.assertThat(message, value, matcher);
});
return this.responseSpec;
}

/**
* Consume the value of the response cookie.
*/
public WebTestClient.ResponseSpec value(String name, Consumer<String> consumer) {
String value = getCookie(name).getValue();
this.exchangeResult.assertWithDiagnostics(() -> consumer.accept(value));
return this.responseSpec;
}

/**
* Expect that the cookie with the given name is present.
*/
public WebTestClient.ResponseSpec exists(String name) {
getCookie(name);
return this.responseSpec;
}

/**
* Expect that the cookie with the given name is not present.
*/
public WebTestClient.ResponseSpec doesNotExist(String name) {
ResponseCookie cookie = this.exchangeResult.getResponseCookies().getFirst(name);
if (cookie != null) {
String message = getMessage(name) + " exists with value=[" + cookie.getValue() + "]";
this.exchangeResult.assertWithDiagnostics(() -> AssertionErrors.fail(message));
}
return this.responseSpec;
}

/**
* Assert a cookie's maxAge attribute.
*/
public WebTestClient.ResponseSpec maxAge(String name, Duration expected) {
Duration maxAge = getCookie(name).getMaxAge();
this.exchangeResult.assertWithDiagnostics(() -> {
String message = getMessage(name) + " maxAge";
AssertionErrors.assertEquals(message, expected, maxAge);
});
return this.responseSpec;
}

/**
* Assert a cookie's maxAge attribute with a Hamcrest {@link Matcher}.
*/
public WebTestClient.ResponseSpec maxAge(String name, Matcher<? super Long> matcher) {
long maxAge = getCookie(name).getMaxAge().getSeconds();
this.exchangeResult.assertWithDiagnostics(() -> {
String message = getMessage(name) + " maxAge";
assertThat(message, maxAge, matcher);
});
return this.responseSpec;
}

/**
* Assert a cookie's path attribute.
*/
public WebTestClient.ResponseSpec path(String name, String expected) {
String path = getCookie(name).getPath();
this.exchangeResult.assertWithDiagnostics(() -> {
String message = getMessage(name) + " path";
AssertionErrors.assertEquals(message, expected, path);
});
return this.responseSpec;
}

/**
* Assert a cookie's path attribute with a Hamcrest {@link Matcher}.
*/
public WebTestClient.ResponseSpec path(String name, Matcher<? super String> matcher) {
String path = getCookie(name).getPath();
this.exchangeResult.assertWithDiagnostics(() -> {
String message = getMessage(name) + " path";
assertThat(message, path, matcher);
});
return this.responseSpec;
}

/**
* Assert a cookie's domain attribute.
*/
public WebTestClient.ResponseSpec domain(String name, String expected) {
String path = getCookie(name).getDomain();
this.exchangeResult.assertWithDiagnostics(() -> {
String message = getMessage(name) + " domain";
AssertionErrors.assertEquals(message, expected, path);
});
return this.responseSpec;
}

/**
* Assert a cookie's domain attribute with a Hamcrest {@link Matcher}.
*/
public WebTestClient.ResponseSpec domain(String name, Matcher<? super String> matcher) {
String domain = getCookie(name).getDomain();
this.exchangeResult.assertWithDiagnostics(() -> {
String message = getMessage(name) + " domain";
assertThat(message, domain, matcher);
});
return this.responseSpec;
}

/**
* Assert a cookie's secure attribute.
*/
public WebTestClient.ResponseSpec secure(String name, boolean expected) {
boolean isSecure = getCookie(name).isSecure();
this.exchangeResult.assertWithDiagnostics(() -> {
String message = getMessage(name) + " secure";
AssertionErrors.assertEquals(message, expected, isSecure);
});
return this.responseSpec;
}

/**
* Assert a cookie's httpOnly attribute.
*/
public WebTestClient.ResponseSpec httpOnly(String name, boolean expected) {
boolean isHttpOnly = getCookie(name).isHttpOnly();
this.exchangeResult.assertWithDiagnostics(() -> {
String message = getMessage(name) + " secure";
AssertionErrors.assertEquals(message, expected, isHttpOnly);
});
return this.responseSpec;
}

/**
* Assert a cookie's sameSite attribute.
*/
public WebTestClient.ResponseSpec sameSite(String name, String expected) {
String sameSite = getCookie(name).getSameSite();
this.exchangeResult.assertWithDiagnostics(() -> {
String message = getMessage(name) + " secure";
AssertionErrors.assertEquals(message, expected, sameSite);
});
return this.responseSpec;
}


private ResponseCookie getCookie(String name) {
ResponseCookie cookie = this.exchangeResult.getResponseCookies().getFirst(name);
if (cookie == null) {
String message = "No cookie with name '" + name + "'";
this.exchangeResult.assertWithDiagnostics(() -> AssertionErrors.fail(message));
}
return cookie;
}

private String getMessage(String cookie) {
return "Response cookie '" + cookie + "'";
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -306,8 +306,8 @@ public RequestHeadersSpec<?> syncBody(Object body) {
public ResponseSpec exchange() {
ClientResponse clientResponse = this.bodySpec.exchange().block(getTimeout());
Assert.state(clientResponse != null, "No ClientResponse");
WiretapConnector.Info info = wiretapConnector.claimRequest(this.requestId);
return new DefaultResponseSpec(info, clientResponse, this.uriTemplate, getTimeout());
ExchangeResult result = wiretapConnector.getExchangeResult(this.requestId, this.uriTemplate, getTimeout());
return new DefaultResponseSpec(result, clientResponse, getTimeout());
}
}

Expand All @@ -321,10 +321,8 @@ private static class DefaultResponseSpec implements ResponseSpec {
private final Duration timeout;


DefaultResponseSpec(WiretapConnector.Info wiretapInfo, ClientResponse response,
@Nullable String uriTemplate, Duration timeout) {

this.exchangeResult = wiretapInfo.createExchangeResult(timeout, uriTemplate);
DefaultResponseSpec(ExchangeResult exchangeResult, ClientResponse response, Duration timeout) {
this.exchangeResult = exchangeResult;
this.response = response;
this.timeout = timeout;
}
Expand All @@ -339,6 +337,11 @@ public HeaderAssertions expectHeader() {
return new HeaderAssertions(this.exchangeResult, this);
}

@Override
public CookieAssertions expectCookie() {
return new CookieAssertions(this.exchangeResult, this);
}

@Override
public <B> BodySpec<B, ?> expectBody(Class<B> bodyType) {
B body = this.response.bodyToMono(bodyType).block(this.timeout);
Expand Down Expand Up @@ -380,7 +383,14 @@ public BodyContentSpec expectBody() {

@Override
public <T> FluxExchangeResult<T> returnResult(Class<T> elementClass) {
Flux<T> body = this.response.bodyToFlux(elementClass);
Flux<T> body;
if (elementClass.equals(Void.class)) {
this.response.releaseBody().block();
body = Flux.empty();
}
else {
body = this.response.bodyToFlux(elementClass);
}
return new FluxExchangeResult<>(this.exchangeResult, body);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -72,6 +72,9 @@ public class ExchangeResult {
@Nullable
private final String uriTemplate;

@Nullable
final Object mockServerResult;


/**
* Create an instance with an HTTP request and response along with promises
Expand All @@ -83,9 +86,11 @@ public class ExchangeResult {
* @param responseBody capture of serialized response body content
* @param timeout how long to wait for content to materialize
* @param uriTemplate the URI template used to set up the request, if any
* @param serverResult the result of a mock server exchange if applicable.
*/
ExchangeResult(ClientHttpRequest request, ClientHttpResponse response,
Mono<byte[]> requestBody, Mono<byte[]> responseBody, Duration timeout, @Nullable String uriTemplate) {
Mono<byte[]> requestBody, Mono<byte[]> responseBody, Duration timeout, @Nullable String uriTemplate,
@Nullable Object serverResult) {

Assert.notNull(request, "ClientHttpRequest is required");
Assert.notNull(response, "ClientHttpResponse is required");
Expand All @@ -98,6 +103,7 @@ public class ExchangeResult {
this.responseBody = responseBody;
this.timeout = timeout;
this.uriTemplate = uriTemplate;
this.mockServerResult = serverResult;
}

/**
Expand All @@ -110,6 +116,7 @@ public class ExchangeResult {
this.responseBody = other.responseBody;
this.timeout = other.timeout;
this.uriTemplate = other.uriTemplate;
this.mockServerResult = other.mockServerResult;
}


Expand Down Expand Up @@ -195,6 +202,16 @@ public byte[] getResponseBodyContent() {
return this.responseBody.block(this.timeout);
}

/**
* Return the result from the mock server exchange, if applicable, for
* further assertions on the state of the server response.
* @since 5.3
* @see org.springframework.test.web.servlet.client.MockMvcTestClient#resultActionsFor(ExchangeResult)
*/
@Nullable
public Object getMockServerResult() {
return this.mockServerResult;
}

/**
* Execute the given Runnable, catch any {@link AssertionError}, decorate
Expand Down Expand Up @@ -222,7 +239,8 @@ public String toString() {
"< " + getStatus() + " " + getStatus().getReasonPhrase() + "\n" +
"< " + formatHeaders(getResponseHeaders(), "\n< ") + "\n" +
"\n" +
formatBody(getResponseHeaders().getContentType(), this.responseBody) +"\n";
formatBody(getResponseHeaders().getContentType(), this.responseBody) +"\n" +
formatMockServerResult();
}

private String formatHeaders(HttpHeaders headers, String delimiter) {
Expand Down Expand Up @@ -252,4 +270,10 @@ private String formatBody(@Nullable MediaType contentType, Mono<byte[]> body) {
.block(this.timeout);
}

private String formatMockServerResult() {
return (this.mockServerResult != null ?
"\n====================== MockMvc (Server) ===============================\n" +
this.mockServerResult + "\n" : "");
}

}
Loading

0 comments on commit ccb719e

Please sign in to comment.