Browse Source
WebTestClient now defines all the steps from setup to performing an exchange and applying expectations. The order of expectations now ensures the response status and headers are verified first since that's available before the body is consumed and also because it determines how the body is to be decoded, i.e. error vs success scenarios. There is now a built-in option for verifying the response as a Map along with Map-specific assertions. There are similar options for verifying the response as a List as well as whether to "collect" the list or "take" the first N elements from the response stream.pull/1330/head
16 changed files with 586 additions and 408 deletions
@ -1,110 +0,0 @@
@@ -1,110 +0,0 @@
|
||||
/* |
||||
* Copyright 2002-2017 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 |
||||
* |
||||
* http://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.io.IOException; |
||||
import java.io.OutputStream; |
||||
import java.io.PrintWriter; |
||||
import java.io.Writer; |
||||
import java.util.function.BiConsumer; |
||||
import java.util.function.Predicate; |
||||
|
||||
import org.apache.commons.logging.Log; |
||||
import org.apache.commons.logging.LogFactory; |
||||
|
||||
/** |
||||
* Provides options for logging diagnostic information about the exchange. |
||||
* |
||||
* @author Rossen Stoyanchev |
||||
* @since 5.0 |
||||
* @see ResponseAssertions#log() |
||||
*/ |
||||
public class LoggingActions<T> { |
||||
|
||||
private static Log logger = LogFactory.getLog(LoggingActions.class); |
||||
|
||||
private final ResponseAssertions<T> resultAssertions; |
||||
|
||||
private final ExchangeResult<T> exchange; |
||||
|
||||
|
||||
public LoggingActions(ExchangeResult<T> exchange, ResponseAssertions<T> resultAssertions) { |
||||
this.resultAssertions = resultAssertions; |
||||
this.exchange = exchange; |
||||
} |
||||
|
||||
|
||||
/** |
||||
* Log with {@link System#out}. |
||||
*/ |
||||
public ResponseAssertions<T> toConsole() { |
||||
return toOutputStream(System.out); |
||||
} |
||||
|
||||
/** |
||||
* Log with a given {@link OutputStream}. |
||||
*/ |
||||
public ResponseAssertions<T> toOutputStream(OutputStream stream) { |
||||
return toWriter(new PrintWriter(stream, true)); |
||||
} |
||||
|
||||
/** |
||||
* Log with a given {@link Writer}. |
||||
*/ |
||||
public ResponseAssertions<T> toWriter(Writer writer) { |
||||
try { |
||||
writer.write(getOutput()); |
||||
} |
||||
catch (IOException ex) { |
||||
throw new IllegalStateException("Failed to print exchange info", ex); |
||||
} |
||||
return this.resultAssertions; |
||||
} |
||||
|
||||
/** |
||||
* Log if TRACE level logging is enabled. |
||||
*/ |
||||
public ResponseAssertions<T> ifTraceEnabled() { |
||||
return doLog(Log::isTraceEnabled, Log::trace); |
||||
} |
||||
|
||||
/** |
||||
* Log if DEBUG level logging is enabled. |
||||
*/ |
||||
public ResponseAssertions<T> ifDebugEnabled() { |
||||
return doLog(Log::isDebugEnabled, Log::debug); |
||||
} |
||||
|
||||
/** |
||||
* Log if INFO level logging is enabled. |
||||
*/ |
||||
public ResponseAssertions<T> ifInfoEnabled() { |
||||
return doLog(Log::isInfoEnabled, Log::info); |
||||
} |
||||
|
||||
|
||||
private ResponseAssertions<T> doLog(Predicate<Log> predicate, BiConsumer<Log, String> consumer) { |
||||
if (predicate.test(logger)) { |
||||
consumer.accept(logger, getOutput()); |
||||
} |
||||
return this.resultAssertions; |
||||
} |
||||
|
||||
private String getOutput() { |
||||
return this.resultAssertions.toString(); |
||||
} |
||||
|
||||
} |
||||
@ -1,66 +0,0 @@
@@ -1,66 +0,0 @@
|
||||
/* |
||||
* Copyright 2002-2017 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 |
||||
* |
||||
* http://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 static org.springframework.test.util.AssertionErrors.assertEquals; |
||||
|
||||
/** |
||||
* Assertions on an {@code ExchangeResult}. |
||||
* |
||||
* <p>Use {@link ExchangeResult#assertThat()} to access these assertions. |
||||
* |
||||
* @author Rossen Stoyanchev |
||||
* @since 5.0 |
||||
*/ |
||||
public class ResponseAssertions<T> { |
||||
|
||||
private final ExchangeResult<T> exchangeResult; |
||||
|
||||
|
||||
ResponseAssertions(ExchangeResult<T> exchangeResult) { |
||||
this.exchangeResult = exchangeResult; |
||||
} |
||||
|
||||
|
||||
/** |
||||
* Assertions on the response status. |
||||
*/ |
||||
public StatusAssertions<T> status() { |
||||
return new StatusAssertions<>(this.exchangeResult.getResponseStatus(), this); |
||||
} |
||||
|
||||
/** |
||||
* Assertions on response headers. |
||||
*/ |
||||
public HeaderAssertions<T> header() { |
||||
return new HeaderAssertions<>(this.exchangeResult.getResponseHeaders(), this); |
||||
} |
||||
|
||||
/** |
||||
* Assert the response body is equal to the given value. |
||||
*/ |
||||
public void bodyEquals(T value) { |
||||
assertEquals("Response body", value, this.exchangeResult.getResponseBody()); |
||||
} |
||||
|
||||
/** |
||||
* Options for logging diagnostic information. |
||||
*/ |
||||
public LoggingActions<T> log() { |
||||
return new LoggingActions<T>(this.exchangeResult, this); |
||||
} |
||||
|
||||
} |
||||
Loading…
Reference in new issue