Search in sources :

Example 31 with MockServerWebExchange

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

the class CssLinkResourceTransformerTests method transformEmptyUrlFunction.

// https://github.com/spring-projects/spring-framework/issues/22602
@Test
public void transformEmptyUrlFunction() throws Exception {
    MockServerWebExchange exchange = MockServerWebExchange.from(get("/static/empty_url_function.css"));
    Resource css = getResource("empty_url_function.css");
    String expected = ".fooStyle {\n" + "\tbackground: transparent url() no-repeat left top;\n" + "}";
    StepVerifier.create(this.transformerChain.transform(exchange, css).cast(TransformedResource.class)).consumeNextWith(transformedResource -> {
        String result = new String(transformedResource.getByteArray(), StandardCharsets.UTF_8);
        result = StringUtils.deleteAny(result, "\r");
        assertThat(result).isEqualTo(expected);
    }).expectComplete().verify();
}
Also used : ClassPathResource(org.springframework.core.io.ClassPathResource) EncodedResource(org.springframework.web.reactive.resource.EncodedResourceResolver.EncodedResource) Resource(org.springframework.core.io.Resource) MockServerWebExchange(org.springframework.web.testfixture.server.MockServerWebExchange) Test(org.junit.jupiter.api.Test)

Example 32 with MockServerWebExchange

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

the class HandlerMethodMappingTests method ambiguousMatchOnPreFlightRequestWithCorsConfig.

// gh-26490
@Test
public void ambiguousMatchOnPreFlightRequestWithCorsConfig() throws Exception {
    this.mapping.registerMapping("/f?o", this.handler, this.method1);
    this.mapping.registerMapping("/fo?", this.handler, this.handler.getClass().getMethod("corsHandlerMethod"));
    MockServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest.options("https://example.org/foo").header(HttpHeaders.ORIGIN, "https://domain.com").header(HttpHeaders.ACCESS_CONTROL_REQUEST_METHOD, "GET"));
    this.mapping.getHandler(exchange).block();
    MockServerHttpResponse response = exchange.getResponse();
    assertThat(response.getStatusCode()).isNull();
    assertThat(response.getHeaders().getAccessControlAllowOrigin()).isEqualTo("https://domain.com");
    assertThat(response.getHeaders().getAccessControlAllowMethods()).containsExactly(HttpMethod.GET);
}
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 33 with MockServerWebExchange

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

the class HandlerMethodMappingTests method ambiguousMatch.

@Test
public void ambiguousMatch() {
    this.mapping.registerMapping("/f?o", this.handler, this.method1);
    this.mapping.registerMapping("/fo?", this.handler, this.method2);
    MockServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest.get("/foo"));
    Mono<Object> result = this.mapping.getHandler(exchange);
    StepVerifier.create(result).expectError(IllegalStateException.class).verify();
}
Also used : Assertions.assertThatIllegalStateException(org.assertj.core.api.Assertions.assertThatIllegalStateException) MockServerWebExchange(org.springframework.web.testfixture.server.MockServerWebExchange) Test(org.junit.jupiter.api.Test)

Example 34 with MockServerWebExchange

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

the class HandlerMethodMappingTests method directMatch.

@Test
public void directMatch() {
    this.mapping.registerMapping("/foo", this.handler, this.method1);
    this.mapping.registerMapping("/fo*", this.handler, this.method2);
    MockServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest.get("/foo"));
    Mono<Object> result = this.mapping.getHandler(exchange);
    assertThat(((HandlerMethod) result.block()).getMethod()).isEqualTo(this.method1);
    assertThat(this.mapping.getMatches()).containsExactly("/foo");
}
Also used : MockServerWebExchange(org.springframework.web.testfixture.server.MockServerWebExchange) HandlerMethod(org.springframework.web.method.HandlerMethod) Test(org.junit.jupiter.api.Test)

Example 35 with MockServerWebExchange

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

the class ResourceWebHandlerTests method noPathWithinHandlerMappingAttribute.

@Test
public void noPathWithinHandlerMappingAttribute() {
    MockServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest.get(""));
    assertThatIllegalArgumentException().isThrownBy(() -> this.handler.handle(exchange).block(TIMEOUT));
}
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