Search in sources :

Example 91 with MockServerWebExchange

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

the class DispatcherHandlerTests method preFlightRequest.

@Test
void preFlightRequest() {
    WebHandler webHandler = mock(WebHandler.class);
    HandlerMapping handlerMapping = mock(HandlerMapping.class);
    given((handlerMapping).getHandler(any())).willReturn(Mono.just(webHandler));
    StaticApplicationContext context = new StaticApplicationContext();
    context.registerBean("handlerMapping", HandlerMapping.class, () -> handlerMapping);
    context.registerBean(HandlerAdapter.class, SimpleHandlerAdapter::new);
    context.registerBean(HandlerResultHandler.class, StringHandlerResultHandler::new);
    context.refresh();
    MockServerHttpRequest request = MockServerHttpRequest.options("/").header(HttpHeaders.ORIGIN, "https://domain.com").header(HttpHeaders.ACCESS_CONTROL_REQUEST_METHOD, "GET").build();
    MockServerWebExchange exchange = MockServerWebExchange.from(request);
    new DispatcherHandler(context).handle(exchange).block(Duration.ofSeconds(0));
    verifyNoInteractions(webHandler);
}
Also used : StaticApplicationContext(org.springframework.context.support.StaticApplicationContext) MockServerHttpRequest(org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest) WebHandler(org.springframework.web.server.WebHandler) MockServerWebExchange(org.springframework.web.testfixture.server.MockServerWebExchange) SimpleHandlerAdapter(org.springframework.web.reactive.result.SimpleHandlerAdapter) Test(org.junit.jupiter.api.Test)

Example 92 with MockServerWebExchange

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

the class AcceptHeaderLocaleContextResolverTests method resolveMissingAcceptLanguageHeaderWithDefault.

@Test
public void resolveMissingAcceptLanguageHeaderWithDefault() {
    this.resolver.setDefaultLocale(US);
    MockServerHttpRequest request = MockServerHttpRequest.get("/").build();
    MockServerWebExchange exchange = MockServerWebExchange.from(request);
    assertThat(this.resolver.resolveLocaleContext(exchange).getLocale()).isEqualTo(US);
}
Also used : MockServerHttpRequest(org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest) MockServerWebExchange(org.springframework.web.testfixture.server.MockServerWebExchange) Test(org.junit.jupiter.api.Test)

Example 93 with MockServerWebExchange

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

the class AcceptHeaderLocaleContextResolverTests method defaultLocale.

@Test
public void defaultLocale() {
    this.resolver.setDefaultLocale(JAPANESE);
    MockServerHttpRequest request = MockServerHttpRequest.get("/").build();
    MockServerWebExchange exchange = MockServerWebExchange.from(request);
    assertThat(this.resolver.resolveLocaleContext(exchange).getLocale()).isEqualTo(JAPANESE);
    request = MockServerHttpRequest.get("/").acceptLanguageAsLocales(US).build();
    exchange = MockServerWebExchange.from(request);
    assertThat(this.resolver.resolveLocaleContext(exchange).getLocale()).isEqualTo(US);
}
Also used : MockServerHttpRequest(org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest) MockServerWebExchange(org.springframework.web.testfixture.server.MockServerWebExchange) Test(org.junit.jupiter.api.Test)

Example 94 with MockServerWebExchange

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

the class AcceptHeaderLocaleContextResolverTests method resolveEmptyAcceptLanguageHeader.

@Test
public void resolveEmptyAcceptLanguageHeader() {
    MockServerHttpRequest request = MockServerHttpRequest.get("/").header(HttpHeaders.ACCEPT_LANGUAGE, "").build();
    MockServerWebExchange exchange = MockServerWebExchange.from(request);
    assertThat(this.resolver.resolveLocaleContext(exchange).getLocale()).isNull();
}
Also used : MockServerHttpRequest(org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest) MockServerWebExchange(org.springframework.web.testfixture.server.MockServerWebExchange) Test(org.junit.jupiter.api.Test)

Example 95 with MockServerWebExchange

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

the class AcceptHeaderLocaleContextResolverTests method resolveEmptyAcceptLanguageHeaderWithDefault.

@Test
public void resolveEmptyAcceptLanguageHeaderWithDefault() {
    this.resolver.setDefaultLocale(US);
    MockServerHttpRequest request = MockServerHttpRequest.get("/").header(HttpHeaders.ACCEPT_LANGUAGE, "").build();
    MockServerWebExchange exchange = MockServerWebExchange.from(request);
    assertThat(this.resolver.resolveLocaleContext(exchange).getLocale()).isEqualTo(US);
}
Also used : MockServerHttpRequest(org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest) 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