Search in sources :

Example 26 with ServerWebExchange

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

the class CorsUrlHandlerMappingTests method preFlightRequestWithGlobalCorsConfig.

@Test
public void preFlightRequestWithGlobalCorsConfig() throws Exception {
    CorsConfiguration mappedConfig = new CorsConfiguration();
    mappedConfig.addAllowedOrigin("*");
    this.handlerMapping.setCorsConfigurations(Collections.singletonMap("/welcome.html", mappedConfig));
    String origin = "http://domain2.com";
    ServerWebExchange exchange = createExchange(HttpMethod.OPTIONS, "/welcome.html", origin);
    Object actual = this.handlerMapping.getHandler(exchange).block();
    assertNotNull(actual);
    assertNotSame(this.welcomeController, actual);
    assertEquals("*", exchange.getResponse().getHeaders().getFirst(HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN));
}
Also used : ServerWebExchange(org.springframework.web.server.ServerWebExchange) CorsConfiguration(org.springframework.web.cors.CorsConfiguration) Test(org.junit.Test)

Example 27 with ServerWebExchange

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

the class SimpleUrlHandlerMappingTests method testUrl.

private void testUrl(String url, Object bean, HandlerMapping handlerMapping, String pathWithinMapping) {
    ServerWebExchange exchange = MockServerHttpRequest.method(HttpMethod.GET, URI.create(url)).toExchange();
    Object actual = handlerMapping.getHandler(exchange).block();
    if (bean != null) {
        assertNotNull(actual);
        assertSame(bean, actual);
        //noinspection OptionalGetWithoutIsPresent
        assertEquals(pathWithinMapping, exchange.getAttribute(PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE).get());
    } else {
        assertNull(actual);
    }
}
Also used : ServerWebExchange(org.springframework.web.server.ServerWebExchange)

Example 28 with ServerWebExchange

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

the class VersionResourceResolverTests method resolveResourceSuccess.

@Test
public void resolveResourceSuccess() throws Exception {
    String versionFile = "bar-version.css";
    String version = "version";
    String file = "bar.css";
    Resource expected = new ClassPathResource("test/" + file, getClass());
    ServerWebExchange exchange = MockServerHttpRequest.get("/resources/bar-version.css").toExchange();
    given(this.chain.resolveResource(exchange, versionFile, this.locations)).willReturn(Mono.empty());
    given(this.chain.resolveResource(exchange, file, this.locations)).willReturn(Mono.just(expected));
    given(this.versionStrategy.extractVersion(versionFile)).willReturn(version);
    given(this.versionStrategy.removeVersion(versionFile, version)).willReturn(file);
    given(this.versionStrategy.getResourceVersion(expected)).willReturn(version);
    this.resolver.setStrategyMap(Collections.singletonMap("/**", this.versionStrategy));
    Resource actual = this.resolver.resolveResourceInternal(exchange, versionFile, this.locations, this.chain).block(Duration.ofMillis(5000));
    assertEquals(expected.getFilename(), actual.getFilename());
    verify(this.versionStrategy, times(1)).getResourceVersion(expected);
    assertThat(actual, instanceOf(HttpResource.class));
    assertEquals("\"" + version + "\"", ((HttpResource) actual).getResponseHeaders().getETag());
}
Also used : ServerWebExchange(org.springframework.web.server.ServerWebExchange) ClassPathResource(org.springframework.core.io.ClassPathResource) Resource(org.springframework.core.io.Resource) ClassPathResource(org.springframework.core.io.ClassPathResource) Test(org.junit.Test)

Example 29 with ServerWebExchange

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

the class PatternsRequestConditionTests method compareNumberOfMatchingPatterns.

@Test
public void compareNumberOfMatchingPatterns() throws Exception {
    ServerWebExchange exchange = get("/foo.html").toExchange();
    PatternsRequestCondition c1 = new PatternsRequestCondition("/foo", "*.jpeg");
    PatternsRequestCondition c2 = new PatternsRequestCondition("/foo", "*.html");
    PatternsRequestCondition match1 = c1.getMatchingCondition(exchange);
    PatternsRequestCondition match2 = c2.getMatchingCondition(exchange);
    assertNotNull(match1);
    assertEquals(1, match1.compareTo(match2, exchange));
}
Also used : MockServerWebExchange(org.springframework.mock.http.server.reactive.test.MockServerWebExchange) ServerWebExchange(org.springframework.web.server.ServerWebExchange) Test(org.junit.Test)

Example 30 with ServerWebExchange

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

the class PatternsRequestConditionTests method matchSuffixPattern.

@Test
public void matchSuffixPattern() throws Exception {
    ServerWebExchange exchange = get("/foo.html").toExchange();
    PatternsRequestCondition condition = new PatternsRequestCondition("/{foo}");
    PatternsRequestCondition match = condition.getMatchingCondition(exchange);
    assertNotNull(match);
    assertEquals("/{foo}.*", match.getPatterns().iterator().next());
    condition = new PatternsRequestCondition(new String[] { "/{foo}" }, null, null, false, false, null);
    match = condition.getMatchingCondition(exchange);
    assertNotNull(match);
    assertEquals("/{foo}", match.getPatterns().iterator().next());
}
Also used : MockServerWebExchange(org.springframework.mock.http.server.reactive.test.MockServerWebExchange) ServerWebExchange(org.springframework.web.server.ServerWebExchange) Test(org.junit.Test)

Aggregations

ServerWebExchange (org.springframework.web.server.ServerWebExchange)158 Test (org.junit.Test)138 MockServerWebExchange (org.springframework.mock.http.server.reactive.test.MockServerWebExchange)53 Mono (reactor.core.publisher.Mono)22 ServerHttpResponse (org.springframework.http.server.reactive.ServerHttpResponse)20 MediaType (org.springframework.http.MediaType)18 BindingContext (org.springframework.web.reactive.BindingContext)17 MethodParameter (org.springframework.core.MethodParameter)15 List (java.util.List)14 ResolvableType (org.springframework.core.ResolvableType)14 Map (java.util.Map)12 Collections (java.util.Collections)11 HttpStatus (org.springframework.http.HttpStatus)11 StepVerifier (reactor.test.StepVerifier)11 Assert.assertEquals (org.junit.Assert.assertEquals)10 MockServerHttpRequest (org.springframework.mock.http.server.reactive.test.MockServerHttpRequest)10 ResponseStatusException (org.springframework.web.server.ResponseStatusException)9 ServerWebInputException (org.springframework.web.server.ServerWebInputException)9 ReactiveAdapterRegistry (org.springframework.core.ReactiveAdapterRegistry)8 NotAcceptableStatusException (org.springframework.web.server.NotAcceptableStatusException)8