Search in sources :

Example 46 with MockServerHttpRequest

use of org.springframework.mock.http.server.reactive.MockServerHttpRequest in project spring-boot by spring-projects.

the class DefaultErrorAttributesTests method defaultStatusCode.

@Test
void defaultStatusCode() {
    Error error = new OutOfMemoryError("Test error");
    MockServerHttpRequest request = MockServerHttpRequest.get("/test").build();
    Map<String, Object> attributes = this.errorAttributes.getErrorAttributes(buildServerRequest(request, error), ErrorAttributeOptions.defaults());
    assertThat(attributes.get("error")).isEqualTo(HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase());
    assertThat(attributes.get("status")).isEqualTo(500);
}
Also used : MockServerHttpRequest(org.springframework.mock.http.server.reactive.MockServerHttpRequest) ObjectError(org.springframework.validation.ObjectError) Test(org.junit.jupiter.api.Test)

Example 47 with MockServerHttpRequest

use of org.springframework.mock.http.server.reactive.MockServerHttpRequest in project spring-boot by spring-projects.

the class DefaultErrorAttributesTests method includeStatusCode.

@Test
void includeStatusCode() {
    MockServerHttpRequest request = MockServerHttpRequest.get("/test").build();
    Map<String, Object> attributes = this.errorAttributes.getErrorAttributes(buildServerRequest(request, NOT_FOUND), ErrorAttributeOptions.defaults());
    assertThat(attributes.get("error")).isEqualTo(HttpStatus.NOT_FOUND.getReasonPhrase());
    assertThat(attributes.get("status")).isEqualTo(404);
}
Also used : MockServerHttpRequest(org.springframework.mock.http.server.reactive.MockServerHttpRequest) Test(org.junit.jupiter.api.Test)

Example 48 with MockServerHttpRequest

use of org.springframework.mock.http.server.reactive.MockServerHttpRequest in project spring-boot by spring-projects.

the class WebFluxAutoConfigurationTests method whenFixedLocalContextResolverIsUsedThenAcceptLanguagesHeaderIsIgnored.

@Test
void whenFixedLocalContextResolverIsUsedThenAcceptLanguagesHeaderIsIgnored() {
    this.contextRunner.withPropertyValues("spring.web.locale:en_UK", "spring.web.locale-resolver=fixed").run((context) -> {
        MockServerHttpRequest request = MockServerHttpRequest.get("/").acceptLanguageAsLocales(StringUtils.parseLocaleString("nl_NL")).build();
        MockServerWebExchange exchange = MockServerWebExchange.from(request);
        LocaleContextResolver localeContextResolver = context.getBean(LocaleContextResolver.class);
        assertThat(localeContextResolver).isInstanceOf(FixedLocaleContextResolver.class);
        LocaleContext localeContext = localeContextResolver.resolveLocaleContext(exchange);
        assertThat(localeContext.getLocale()).isEqualTo(StringUtils.parseLocaleString("en_UK"));
    });
}
Also used : LocaleContext(org.springframework.context.i18n.LocaleContext) MockServerHttpRequest(org.springframework.mock.http.server.reactive.MockServerHttpRequest) MockServerWebExchange(org.springframework.mock.web.server.MockServerWebExchange) FixedLocaleContextResolver(org.springframework.web.server.i18n.FixedLocaleContextResolver) LocaleContextResolver(org.springframework.web.server.i18n.LocaleContextResolver) AcceptHeaderLocaleContextResolver(org.springframework.web.server.i18n.AcceptHeaderLocaleContextResolver) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 49 with MockServerHttpRequest

use of org.springframework.mock.http.server.reactive.MockServerHttpRequest in project spring-boot by spring-projects.

the class WebFluxAutoConfigurationTests method whenAcceptHeaderLocaleContextResolverIsUsedAndHeaderIsAbsentThenConfiguredLocaleIsUsed.

@Test
void whenAcceptHeaderLocaleContextResolverIsUsedAndHeaderIsAbsentThenConfiguredLocaleIsUsed() {
    this.contextRunner.withPropertyValues("spring.web.locale:en_UK").run((context) -> {
        MockServerHttpRequest request = MockServerHttpRequest.get("/").build();
        MockServerWebExchange exchange = MockServerWebExchange.from(request);
        LocaleContextResolver localeContextResolver = context.getBean(LocaleContextResolver.class);
        assertThat(localeContextResolver).isInstanceOf(AcceptHeaderLocaleContextResolver.class);
        LocaleContext localeContext = localeContextResolver.resolveLocaleContext(exchange);
        assertThat(localeContext.getLocale()).isEqualTo(StringUtils.parseLocaleString("en_UK"));
    });
}
Also used : LocaleContext(org.springframework.context.i18n.LocaleContext) MockServerHttpRequest(org.springframework.mock.http.server.reactive.MockServerHttpRequest) MockServerWebExchange(org.springframework.mock.web.server.MockServerWebExchange) FixedLocaleContextResolver(org.springframework.web.server.i18n.FixedLocaleContextResolver) LocaleContextResolver(org.springframework.web.server.i18n.LocaleContextResolver) AcceptHeaderLocaleContextResolver(org.springframework.web.server.i18n.AcceptHeaderLocaleContextResolver) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 50 with MockServerHttpRequest

use of org.springframework.mock.http.server.reactive.MockServerHttpRequest in project spring-cloud-gateway by spring-cloud.

the class LoadBalancerClientFilterTests method unencodedParameters.

@Test
public void unencodedParameters() {
    URI url = URI.create("http://localhost/get?a=b&c=d[]");
    MockServerHttpRequest request = MockServerHttpRequest.method(HttpMethod.GET, url).build();
    URI lbUrl = URI.create("lb://service1?a=b&c=d[]");
    // prove that it is unencoded
    assertThat(lbUrl.getRawQuery()).isEqualTo("a=b&c=d[]");
    ServerWebExchange webExchange = testFilter(request, lbUrl);
    URI uri = webExchange.getRequiredAttribute(GATEWAY_REQUEST_URL_ATTR);
    assertThat(uri).hasScheme("http").hasHost("service1-host1").hasParameter("a", "b").hasParameter("c", "d[]");
    // prove that it is NOT encoded
    assertThat(uri.getRawQuery()).isEqualTo("a=b&c=d[]");
}
Also used : ServerWebExchange(org.springframework.web.server.ServerWebExchange) MockServerWebExchange(org.springframework.mock.web.server.MockServerWebExchange) MockServerHttpRequest(org.springframework.mock.http.server.reactive.MockServerHttpRequest) URI(java.net.URI) Test(org.junit.Test)

Aggregations

MockServerHttpRequest (org.springframework.mock.http.server.reactive.MockServerHttpRequest)75 Test (org.junit.jupiter.api.Test)40 MockServerWebExchange (org.springframework.mock.web.server.MockServerWebExchange)35 Test (org.junit.Test)26 ServerWebExchange (org.springframework.web.server.ServerWebExchange)26 URI (java.net.URI)16 OAuth2AuthorizationRequest (org.springframework.security.oauth2.core.endpoint.OAuth2AuthorizationRequest)11 HttpHeaders (org.springframework.http.HttpHeaders)9 BeforeEach (org.junit.jupiter.api.BeforeEach)7 ResponseStatusException (org.springframework.web.server.ResponseStatusException)7 AcceptHeaderLocaleContextResolver (org.springframework.web.server.i18n.AcceptHeaderLocaleContextResolver)6 Mono (reactor.core.publisher.Mono)6 InetSocketAddress (java.net.InetSocketAddress)5 Assertions.assertThatIllegalStateException (org.assertj.core.api.Assertions.assertThatIllegalStateException)5 Mockito.mock (org.mockito.Mockito.mock)5 ObjectError (org.springframework.validation.ObjectError)5 HashMap (java.util.HashMap)4 Map (java.util.Map)4 ArgumentMatchers.any (org.mockito.ArgumentMatchers.any)4 BDDMockito.given (org.mockito.BDDMockito.given)4