use of org.springframework.mock.http.server.reactive.test.MockServerWebExchange in project spring-framework by spring-projects.
the class DefaultServerWebExchangeCheckNotModifiedTests method checkModifiedETagAndNotModifiedTimestamp.
@Test
public void checkModifiedETagAndNotModifiedTimestamp() throws Exception {
String currentETag = "\"Foo\"";
String oldEtag = "\"Bar\"";
long time = currentDate.toEpochMilli();
MockServerWebExchange exchange = get("/").ifNoneMatch(oldEtag).ifModifiedSince(time).toExchange();
assertFalse(exchange.checkNotModified(currentETag, currentDate));
assertNull(exchange.getResponse().getStatusCode());
assertEquals(currentETag, exchange.getResponse().getHeaders().getETag());
assertEquals(time, exchange.getResponse().getHeaders().getLastModified());
}
use of org.springframework.mock.http.server.reactive.test.MockServerWebExchange in project spring-framework by spring-projects.
the class DefaultEntityResponseBuilderTests method bodyInserter.
@Test
public void bodyInserter() throws Exception {
String body = "foo";
Publisher<String> publisher = Mono.just(body);
BiFunction<ServerHttpResponse, BodyInserter.Context, Mono<Void>> writer = (response, strategies) -> {
byte[] bodyBytes = body.getBytes(UTF_8);
ByteBuffer byteBuffer = ByteBuffer.wrap(bodyBytes);
DataBuffer buffer = new DefaultDataBufferFactory().wrap(byteBuffer);
return response.writeWith(Mono.just(buffer));
};
Mono<EntityResponse<Publisher<String>>> result = EntityResponse.fromPublisher(publisher, String.class).build();
MockServerWebExchange exchange = MockServerHttpRequest.get("http://localhost").toExchange();
HandlerStrategies strategies = HandlerStrategies.empty().messageWriter(new EncoderHttpMessageWriter<>(new CharSequenceEncoder())).build();
StepVerifier.create(result).consumeNextWith(response -> {
StepVerifier.create(response.entity()).expectNext(body).expectComplete().verify();
response.writeTo(exchange, strategies);
}).expectComplete().verify();
assertNotNull(exchange.getResponse().getBody());
}
use of org.springframework.mock.http.server.reactive.test.MockServerWebExchange in project spring-framework by spring-projects.
the class DefaultRenderingResponseTests method render.
@Test
public void render() throws Exception {
Map<String, Object> model = Collections.singletonMap("foo", "bar");
Mono<RenderingResponse> result = RenderingResponse.create("view").modelAttributes(model).build();
MockServerWebExchange exchange = MockServerHttpRequest.get("http://localhost").toExchange();
ViewResolver viewResolver = mock(ViewResolver.class);
View view = mock(View.class);
when(viewResolver.resolveViewName("view", Locale.ENGLISH)).thenReturn(Mono.just(view));
when(view.render(model, null, exchange)).thenReturn(Mono.empty());
List<ViewResolver> viewResolvers = new ArrayList<>();
viewResolvers.add(viewResolver);
HandlerStrategies mockConfig = mock(HandlerStrategies.class);
when(mockConfig.viewResolvers()).thenReturn(viewResolvers::stream);
StepVerifier.create(result).expectNextMatches(response -> "view".equals(response.name()) && model.equals(response.model())).expectComplete().verify();
}
use of org.springframework.mock.http.server.reactive.test.MockServerWebExchange in project spring-framework by spring-projects.
the class CompositeContentTypeResolverBuilderTests method defaultSettings.
@Test
public void defaultSettings() throws Exception {
RequestedContentTypeResolver resolver = new RequestedContentTypeResolverBuilder().build();
MockServerWebExchange exchange = MockServerHttpRequest.get("/flower.gif").toExchange();
assertEquals("Should be able to resolve file extensions by default", Collections.singletonList(MediaType.IMAGE_GIF), resolver.resolveMediaTypes(exchange));
exchange = MockServerHttpRequest.get("/flower.xyz").toExchange();
assertEquals("Should ignore unknown extensions by default", Collections.<MediaType>emptyList(), resolver.resolveMediaTypes(exchange));
exchange = MockServerHttpRequest.get("/flower?format=gif").toExchange();
assertEquals("Should not resolve request parameters by default", Collections.<MediaType>emptyList(), resolver.resolveMediaTypes(exchange));
exchange = MockServerHttpRequest.get("/flower").accept(MediaType.IMAGE_GIF).toExchange();
assertEquals("Should resolve Accept header by default", Collections.singletonList(MediaType.IMAGE_GIF), resolver.resolveMediaTypes(exchange));
}
use of org.springframework.mock.http.server.reactive.test.MockServerWebExchange in project spring-framework by spring-projects.
the class ResourceHandlerFunctionTests method get.
@Test
public void get() throws IOException {
MockServerWebExchange exchange = MockServerHttpRequest.get("http://localhost").toExchange();
MockServerHttpResponse mockResponse = exchange.getResponse();
ServerRequest request = new DefaultServerRequest(exchange, HandlerStrategies.withDefaults());
Mono<ServerResponse> responseMono = this.handlerFunction.handle(request);
Mono<Void> result = responseMono.then(response -> {
assertEquals(HttpStatus.OK, response.statusCode());
return response.writeTo(exchange, HandlerStrategies.withDefaults());
});
StepVerifier.create(result).expectComplete().verify();
byte[] expectedBytes = Files.readAllBytes(this.resource.getFile().toPath());
StepVerifier.create(mockResponse.getBody()).consumeNextWith(dataBuffer -> {
byte[] resultBytes = new byte[dataBuffer.readableByteCount()];
dataBuffer.read(resultBytes);
assertArrayEquals(expectedBytes, resultBytes);
}).expectComplete().verify();
assertEquals(MediaType.TEXT_PLAIN, mockResponse.getHeaders().getContentType());
assertEquals(this.resource.contentLength(), mockResponse.getHeaders().getContentLength());
}
Aggregations