Browse Source
Put assertBodyXyz options behind a common assertBody() entry point currently including "isEmpty" and "asMap" but in the future others related to JSON content or XPath for example. Now that ExchangeActions provides method to access the ExchangeInfo it has been removed from constructors of assertion classes that already have ExchangeActions.pull/1323/merge
8 changed files with 117 additions and 62 deletions
@ -0,0 +1,74 @@ |
|||||||
|
/* |
||||||
|
* 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.nio.ByteBuffer; |
||||||
|
import java.time.Duration; |
||||||
|
import java.util.Map; |
||||||
|
|
||||||
|
import reactor.core.publisher.Flux; |
||||||
|
import reactor.core.publisher.Mono; |
||||||
|
import reactor.test.StepVerifier; |
||||||
|
|
||||||
|
import org.springframework.core.ResolvableType; |
||||||
|
import org.springframework.web.reactive.function.client.ClientResponse; |
||||||
|
|
||||||
|
import static org.springframework.web.reactive.function.BodyExtractors.toMono; |
||||||
|
|
||||||
|
/** |
||||||
|
* |
||||||
|
* @author Rossen Stoyanchev |
||||||
|
* @since 5.0 |
||||||
|
*/ |
||||||
|
public class ResponseBodyAssertions { |
||||||
|
|
||||||
|
private final ExchangeActions exchangeActions; |
||||||
|
|
||||||
|
|
||||||
|
public ResponseBodyAssertions(ExchangeActions exchangeActions) { |
||||||
|
this.exchangeActions = exchangeActions; |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
/** |
||||||
|
* Assert the response does not have any content. |
||||||
|
*/ |
||||||
|
public ExchangeActions isEmpty() { |
||||||
|
Flux<?> body = getResponse().bodyToFlux(ByteBuffer.class); |
||||||
|
StepVerifier.create(body).expectComplete().verify(getTimeout()); |
||||||
|
return this.exchangeActions; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Assert the response decoded as a Map of the given key and value types. |
||||||
|
*/ |
||||||
|
public <K, V> MapAssertions<K, V> asMap(Class<K> keyType, Class<V> valueType) { |
||||||
|
ResolvableType type = ResolvableType.forClassWithGenerics(Map.class, keyType, valueType); |
||||||
|
Mono<Map<K, V>> mono = getResponse().body(toMono(type)); |
||||||
|
Map<K, V> map = mono.block(getTimeout()); |
||||||
|
return new MapAssertions<>(this.exchangeActions, map, "Response body map"); |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
private ClientResponse getResponse() { |
||||||
|
return this.exchangeActions.andReturn().getResponse(); |
||||||
|
} |
||||||
|
|
||||||
|
private Duration getTimeout() { |
||||||
|
return this.exchangeActions.andReturn().getResponseTimeout(); |
||||||
|
} |
||||||
|
|
||||||
|
} |
Loading…
Reference in new issue