Search in sources :

Example 36 with MockServerWebExchange

use of org.springframework.web.testfixture.server.MockServerWebExchange in project spring-framework by spring-projects.

the class ResourceWebHandlerTests method unsupportedHttpMethod.

@Test
public void unsupportedHttpMethod() {
    MockServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest.post(""));
    setPathWithinHandlerMapping(exchange, "foo.css");
    assertThatExceptionOfType(MethodNotAllowedException.class).isThrownBy(() -> this.handler.handle(exchange).block(TIMEOUT));
}
Also used : MethodNotAllowedException(org.springframework.web.server.MethodNotAllowedException) MockServerWebExchange(org.springframework.web.testfixture.server.MockServerWebExchange) Test(org.junit.jupiter.api.Test)

Example 37 with MockServerWebExchange

use of org.springframework.web.testfixture.server.MockServerWebExchange in project spring-framework by spring-projects.

the class ResourceWebHandlerTests method missingResourcePath.

@Test
public void missingResourcePath() {
    MockServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest.get(""));
    setPathWithinHandlerMapping(exchange, "");
    StepVerifier.create(this.handler.handle(exchange)).expectErrorSatisfies(err -> {
        assertThat(err).isInstanceOf(ResponseStatusException.class);
        assertThat(((ResponseStatusException) err).getRawStatusCode()).isEqualTo(404);
    }).verify(TIMEOUT);
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) BeforeEach(org.junit.jupiter.api.BeforeEach) Arrays(java.util.Arrays) StepVerifier(reactor.test.StepVerifier) DefaultDataBufferFactory(org.springframework.core.io.buffer.DefaultDataBufferFactory) UrlResource(org.springframework.core.io.UrlResource) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ClassPathResource(org.springframework.core.io.ClassPathResource) MockServerWebExchange(org.springframework.web.testfixture.server.MockServerWebExchange) AtomicReference(java.util.concurrent.atomic.AtomicReference) CacheControl(org.springframework.http.CacheControl) ArrayList(java.util.ArrayList) ServerWebExchange(org.springframework.web.server.ServerWebExchange) BDDMockito.given(org.mockito.BDDMockito.given) Duration(java.time.Duration) Assertions.assertThatExceptionOfType(org.assertj.core.api.Assertions.assertThatExceptionOfType) DataBufferUtils(org.springframework.core.io.buffer.DataBufferUtils) HandlerMapping(org.springframework.web.reactive.HandlerMapping) Resource(org.springframework.core.io.Resource) ResponseStatusException(org.springframework.web.server.ResponseStatusException) HttpHeaders(org.springframework.http.HttpHeaders) UTF_8(java.nio.charset.StandardCharsets.UTF_8) MediaType(org.springframework.http.MediaType) HttpMethod(org.springframework.http.HttpMethod) PathContainer(org.springframework.http.server.PathContainer) FileSystemResource(org.springframework.core.io.FileSystemResource) IOException(java.io.IOException) Mono(reactor.core.publisher.Mono) DataBuffer(org.springframework.core.io.buffer.DataBuffer) MethodNotAllowedException(org.springframework.web.server.MethodNotAllowedException) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) Flux(reactor.core.publisher.Flux) HttpStatus(org.springframework.http.HttpStatus) MockServerHttpRequest(org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest) List(java.util.List) Assertions.fail(org.assertj.core.api.Assertions.fail) MockServerHttpResponse(org.springframework.web.testfixture.http.server.reactive.MockServerHttpResponse) Assertions.assertThatIllegalArgumentException(org.assertj.core.api.Assertions.assertThatIllegalArgumentException) UriUtils(org.springframework.web.util.UriUtils) Collections(java.util.Collections) StringUtils(org.springframework.util.StringUtils) Mockito.mock(org.mockito.Mockito.mock) MockServerWebExchange(org.springframework.web.testfixture.server.MockServerWebExchange) ResponseStatusException(org.springframework.web.server.ResponseStatusException) Test(org.junit.jupiter.api.Test)

Example 38 with MockServerWebExchange

use of org.springframework.web.testfixture.server.MockServerWebExchange in project spring-framework by spring-projects.

the class ResourceWebHandlerTests method getResourceNoCache.

@Test
public void getResourceNoCache() throws Exception {
    MockServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest.get(""));
    setPathWithinHandlerMapping(exchange, "foo.css");
    this.handler.setCacheControl(CacheControl.noStore());
    this.handler.handle(exchange).block(TIMEOUT);
    MockServerHttpResponse response = exchange.getResponse();
    assertThat(response.getHeaders().getCacheControl()).isEqualTo("no-store");
    assertThat(response.getHeaders().containsKey("Last-Modified")).isTrue();
    assertThat(resourceLastModifiedDate("test/foo.css") / 1000).isEqualTo(response.getHeaders().getLastModified() / 1000);
    assertThat(response.getHeaders().getFirst("Accept-Ranges")).isEqualTo("bytes");
    assertThat(response.getHeaders().get("Accept-Ranges").size()).isEqualTo(1);
}
Also used : MockServerWebExchange(org.springframework.web.testfixture.server.MockServerWebExchange) MockServerHttpResponse(org.springframework.web.testfixture.http.server.reactive.MockServerHttpResponse) Test(org.junit.jupiter.api.Test)

Example 39 with MockServerWebExchange

use of org.springframework.web.testfixture.server.MockServerWebExchange in project spring-framework by spring-projects.

the class ResourceWebHandlerTests method directory.

@Test
public void directory() {
    MockServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest.get(""));
    setPathWithinHandlerMapping(exchange, "js/");
    StepVerifier.create(this.handler.handle(exchange)).expectErrorSatisfies(err -> {
        assertThat(err).isInstanceOf(ResponseStatusException.class);
        assertThat(((ResponseStatusException) err).getRawStatusCode()).isEqualTo(404);
    }).verify(TIMEOUT);
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) BeforeEach(org.junit.jupiter.api.BeforeEach) Arrays(java.util.Arrays) StepVerifier(reactor.test.StepVerifier) DefaultDataBufferFactory(org.springframework.core.io.buffer.DefaultDataBufferFactory) UrlResource(org.springframework.core.io.UrlResource) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ClassPathResource(org.springframework.core.io.ClassPathResource) MockServerWebExchange(org.springframework.web.testfixture.server.MockServerWebExchange) AtomicReference(java.util.concurrent.atomic.AtomicReference) CacheControl(org.springframework.http.CacheControl) ArrayList(java.util.ArrayList) ServerWebExchange(org.springframework.web.server.ServerWebExchange) BDDMockito.given(org.mockito.BDDMockito.given) Duration(java.time.Duration) Assertions.assertThatExceptionOfType(org.assertj.core.api.Assertions.assertThatExceptionOfType) DataBufferUtils(org.springframework.core.io.buffer.DataBufferUtils) HandlerMapping(org.springframework.web.reactive.HandlerMapping) Resource(org.springframework.core.io.Resource) ResponseStatusException(org.springframework.web.server.ResponseStatusException) HttpHeaders(org.springframework.http.HttpHeaders) UTF_8(java.nio.charset.StandardCharsets.UTF_8) MediaType(org.springframework.http.MediaType) HttpMethod(org.springframework.http.HttpMethod) PathContainer(org.springframework.http.server.PathContainer) FileSystemResource(org.springframework.core.io.FileSystemResource) IOException(java.io.IOException) Mono(reactor.core.publisher.Mono) DataBuffer(org.springframework.core.io.buffer.DataBuffer) MethodNotAllowedException(org.springframework.web.server.MethodNotAllowedException) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) Flux(reactor.core.publisher.Flux) HttpStatus(org.springframework.http.HttpStatus) MockServerHttpRequest(org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest) List(java.util.List) Assertions.fail(org.assertj.core.api.Assertions.fail) MockServerHttpResponse(org.springframework.web.testfixture.http.server.reactive.MockServerHttpResponse) Assertions.assertThatIllegalArgumentException(org.assertj.core.api.Assertions.assertThatIllegalArgumentException) UriUtils(org.springframework.web.util.UriUtils) Collections(java.util.Collections) StringUtils(org.springframework.util.StringUtils) Mockito.mock(org.mockito.Mockito.mock) MockServerWebExchange(org.springframework.web.testfixture.server.MockServerWebExchange) ResponseStatusException(org.springframework.web.server.ResponseStatusException) Test(org.junit.jupiter.api.Test)

Example 40 with MockServerWebExchange

use of org.springframework.web.testfixture.server.MockServerWebExchange in project spring-framework by spring-projects.

the class ResourceWebHandlerTests method notModified.

@Test
public void notModified() throws Exception {
    MockServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest.get("").ifModifiedSince(resourceLastModified("test/foo.css")));
    setPathWithinHandlerMapping(exchange, "foo.css");
    this.handler.handle(exchange).block(TIMEOUT);
    assertThat(exchange.getResponse().getStatusCode()).isEqualTo(HttpStatus.NOT_MODIFIED);
}
Also used : MockServerWebExchange(org.springframework.web.testfixture.server.MockServerWebExchange) Test(org.junit.jupiter.api.Test)

Aggregations

MockServerWebExchange (org.springframework.web.testfixture.server.MockServerWebExchange)224 Test (org.junit.jupiter.api.Test)216 MockServerHttpRequest (org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest)61 ClassPathResource (org.springframework.core.io.ClassPathResource)26 HttpHeaders (org.springframework.http.HttpHeaders)25 Resource (org.springframework.core.io.Resource)24 HandlerResult (org.springframework.web.reactive.HandlerResult)23 MethodParameter (org.springframework.core.MethodParameter)22 MockServerHttpResponse (org.springframework.web.testfixture.http.server.reactive.MockServerHttpResponse)22 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)19 MediaType (org.springframework.http.MediaType)19 Mono (reactor.core.publisher.Mono)18 BeforeEach (org.junit.jupiter.api.BeforeEach)17 HttpMethod (org.springframework.http.HttpMethod)15 StepVerifier (reactor.test.StepVerifier)13 Arrays (java.util.Arrays)12 Collections (java.util.Collections)12 List (java.util.List)12 HttpStatus (org.springframework.http.HttpStatus)12 IOException (java.io.IOException)10