Search in sources :

Example 11 with MockServerWebExchange

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());
}
Also used : MockServerWebExchange(org.springframework.mock.http.server.reactive.test.MockServerWebExchange) Test(org.junit.Test)

Example 12 with MockServerWebExchange

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());
}
Also used : CharSequenceEncoder(org.springframework.core.codec.CharSequenceEncoder) StepVerifier(reactor.test.StepVerifier) DefaultDataBufferFactory(org.springframework.core.io.buffer.DefaultDataBufferFactory) ServerHttpResponse(org.springframework.http.server.reactive.ServerHttpResponse) ZonedDateTime(java.time.ZonedDateTime) BiFunction(java.util.function.BiFunction) CacheControl(org.springframework.http.CacheControl) ByteBuffer(java.nio.ByteBuffer) BodyInserter(org.springframework.web.reactive.function.BodyInserter) Assert.assertSame(org.junit.Assert.assertSame) MockServerWebExchange(org.springframework.mock.http.server.reactive.test.MockServerWebExchange) EncoderHttpMessageWriter(org.springframework.http.codec.EncoderHttpMessageWriter) ResolvableType(org.springframework.core.ResolvableType) EnumSet(java.util.EnumSet) HttpHeaders(org.springframework.http.HttpHeaders) UTF_8(java.nio.charset.StandardCharsets.UTF_8) Assert.assertNotNull(org.junit.Assert.assertNotNull) Publisher(org.reactivestreams.Publisher) MediaType(org.springframework.http.MediaType) HttpMethod(org.springframework.http.HttpMethod) Set(java.util.Set) Test(org.junit.Test) Mono(reactor.core.publisher.Mono) DataBuffer(org.springframework.core.io.buffer.DataBuffer) Flux(reactor.core.publisher.Flux) HttpStatus(org.springframework.http.HttpStatus) List(java.util.List) MockServerHttpRequest(org.springframework.mock.http.server.reactive.test.MockServerHttpRequest) Collections(java.util.Collections) Mono(reactor.core.publisher.Mono) MockServerWebExchange(org.springframework.mock.http.server.reactive.test.MockServerWebExchange) ByteBuffer(java.nio.ByteBuffer) ServerHttpResponse(org.springframework.http.server.reactive.ServerHttpResponse) EncoderHttpMessageWriter(org.springframework.http.codec.EncoderHttpMessageWriter) DefaultDataBufferFactory(org.springframework.core.io.buffer.DefaultDataBufferFactory) CharSequenceEncoder(org.springframework.core.codec.CharSequenceEncoder) DataBuffer(org.springframework.core.io.buffer.DataBuffer) Test(org.junit.Test)

Example 13 with MockServerWebExchange

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();
}
Also used : ArrayList(java.util.ArrayList) MockServerWebExchange(org.springframework.mock.http.server.reactive.test.MockServerWebExchange) ViewResolver(org.springframework.web.reactive.result.view.ViewResolver) View(org.springframework.web.reactive.result.view.View) Test(org.junit.Test)

Example 14 with MockServerWebExchange

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));
}
Also used : MockServerWebExchange(org.springframework.mock.http.server.reactive.test.MockServerWebExchange) Test(org.junit.Test)

Example 15 with MockServerWebExchange

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());
}
Also used : MockServerWebExchange(org.springframework.mock.http.server.reactive.test.MockServerWebExchange) MockServerHttpResponse(org.springframework.mock.http.server.reactive.test.MockServerHttpResponse) Test(org.junit.Test)

Aggregations

MockServerWebExchange (org.springframework.mock.http.server.reactive.test.MockServerWebExchange)135 Test (org.junit.Test)127 ClassPathResource (org.springframework.core.io.ClassPathResource)19 Resource (org.springframework.core.io.Resource)19 HandlerResult (org.springframework.web.reactive.HandlerResult)17 MethodParameter (org.springframework.core.MethodParameter)15 MediaType (org.springframework.http.MediaType)8 Instant (java.time.Instant)7 HttpHeaders (org.springframework.http.HttpHeaders)5 MockServerHttpResponse (org.springframework.mock.http.server.reactive.test.MockServerHttpResponse)5 FileSystemResource (org.springframework.core.io.FileSystemResource)3 UrlResource (org.springframework.core.io.UrlResource)3 DataBuffer (org.springframework.core.io.buffer.DataBuffer)3 CompositeContentTypeResolver (org.springframework.web.reactive.accept.CompositeContentTypeResolver)3 RequestedContentTypeResolverBuilder (org.springframework.web.reactive.accept.RequestedContentTypeResolverBuilder)3 ArrayList (java.util.ArrayList)2 Collections (java.util.Collections)2 List (java.util.List)2 Assert.assertSame (org.junit.Assert.assertSame)2 DefaultDataBufferFactory (org.springframework.core.io.buffer.DefaultDataBufferFactory)2