Search in sources :

Example 61 with MockServerWebExchange

use of org.springframework.mock.web.server.MockServerWebExchange 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 62 with MockServerWebExchange

use of org.springframework.mock.web.server.MockServerWebExchange in project spring-boot by spring-projects.

the class ApplicationContextServerWebExchangeMatcherTests method matchesWhenContextIsNull.

@Test
void matchesWhenContextIsNull() {
    MockServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest.get("/path").build());
    assertThatIllegalStateException().isThrownBy(() -> new TestApplicationContextServerWebExchangeMatcher<>(ExistingBean.class).callMatchesAndReturnProvidedContext(exchange)).withMessageContaining("No ApplicationContext found on ServerWebExchange.");
}
Also used : MockServerWebExchange(org.springframework.mock.web.server.MockServerWebExchange) Test(org.junit.jupiter.api.Test)

Example 63 with MockServerWebExchange

use of org.springframework.mock.web.server.MockServerWebExchange in project spring-boot by spring-projects.

the class ReactiveCloudFoundrySecurityInterceptorTests method preHandleWhenCloudFoundrySecurityServiceIsNullShouldReturnError.

@Test
void preHandleWhenCloudFoundrySecurityServiceIsNullShouldReturnError() {
    this.interceptor = new CloudFoundrySecurityInterceptor(this.tokenValidator, null, "my-app-id");
    MockServerWebExchange request = MockServerWebExchange.from(MockServerHttpRequest.get("/a").header(HttpHeaders.AUTHORIZATION, mockAccessToken()).build());
    StepVerifier.create(this.interceptor.preHandle(request, "/a")).consumeErrorWith((ex) -> assertThat(((CloudFoundryAuthorizationException) ex).getReason()).isEqualTo(Reason.SERVICE_UNAVAILABLE)).verify();
}
Also used : Base64Utils(org.springframework.util.Base64Utils) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) BeforeEach(org.junit.jupiter.api.BeforeEach) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) StepVerifier(reactor.test.StepVerifier) MockServerHttpRequest(org.springframework.mock.http.server.reactive.MockServerHttpRequest) Mock(org.mockito.Mock) HttpHeaders(org.springframework.http.HttpHeaders) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Mono(reactor.core.publisher.Mono) Test(org.junit.jupiter.api.Test) HttpStatus(org.springframework.http.HttpStatus) AccessLevel(org.springframework.boot.actuate.autoconfigure.cloudfoundry.AccessLevel) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) BDDMockito.given(org.mockito.BDDMockito.given) Reason(org.springframework.boot.actuate.autoconfigure.cloudfoundry.CloudFoundryAuthorizationException.Reason) CloudFoundryAuthorizationException(org.springframework.boot.actuate.autoconfigure.cloudfoundry.CloudFoundryAuthorizationException) MockServerWebExchange(org.springframework.mock.web.server.MockServerWebExchange) CloudFoundryAuthorizationException(org.springframework.boot.actuate.autoconfigure.cloudfoundry.CloudFoundryAuthorizationException) MockServerWebExchange(org.springframework.mock.web.server.MockServerWebExchange) Test(org.junit.jupiter.api.Test)

Example 64 with MockServerWebExchange

use of org.springframework.mock.web.server.MockServerWebExchange in project spring-boot by spring-projects.

the class ReactiveCloudFoundrySecurityInterceptorTests method preHandleWhenTokenIsNotBearerShouldReturnMissingAuthorization.

@Test
void preHandleWhenTokenIsNotBearerShouldReturnMissingAuthorization() {
    MockServerWebExchange request = MockServerWebExchange.from(MockServerHttpRequest.get("/a").header(HttpHeaders.AUTHORIZATION, mockAccessToken()).build());
    StepVerifier.create(this.interceptor.preHandle(request, "/a")).consumeNextWith((response) -> assertThat(response.getStatus()).isEqualTo(Reason.MISSING_AUTHORIZATION.getStatus())).verifyComplete();
}
Also used : Base64Utils(org.springframework.util.Base64Utils) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) BeforeEach(org.junit.jupiter.api.BeforeEach) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) StepVerifier(reactor.test.StepVerifier) MockServerHttpRequest(org.springframework.mock.http.server.reactive.MockServerHttpRequest) Mock(org.mockito.Mock) HttpHeaders(org.springframework.http.HttpHeaders) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Mono(reactor.core.publisher.Mono) Test(org.junit.jupiter.api.Test) HttpStatus(org.springframework.http.HttpStatus) AccessLevel(org.springframework.boot.actuate.autoconfigure.cloudfoundry.AccessLevel) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) BDDMockito.given(org.mockito.BDDMockito.given) Reason(org.springframework.boot.actuate.autoconfigure.cloudfoundry.CloudFoundryAuthorizationException.Reason) CloudFoundryAuthorizationException(org.springframework.boot.actuate.autoconfigure.cloudfoundry.CloudFoundryAuthorizationException) MockServerWebExchange(org.springframework.mock.web.server.MockServerWebExchange) MockServerWebExchange(org.springframework.mock.web.server.MockServerWebExchange) Test(org.junit.jupiter.api.Test)

Example 65 with MockServerWebExchange

use of org.springframework.mock.web.server.MockServerWebExchange in project spring-boot by spring-projects.

the class ReactiveCloudFoundrySecurityInterceptorTests method preHandleSuccessfulWithRestrictedAccess.

@Test
void preHandleSuccessfulWithRestrictedAccess() {
    String accessToken = mockAccessToken();
    given(this.securityService.getAccessLevel(accessToken, "my-app-id")).willReturn(Mono.just(AccessLevel.RESTRICTED));
    given(this.tokenValidator.validate(any())).willReturn(Mono.empty());
    MockServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest.get("/info").header(HttpHeaders.AUTHORIZATION, "bearer " + mockAccessToken()).build());
    StepVerifier.create(this.interceptor.preHandle(exchange, "info")).consumeNextWith((response) -> {
        assertThat(response.getStatus()).isEqualTo(HttpStatus.OK);
        assertThat((AccessLevel) exchange.getAttribute("cloudFoundryAccessLevel")).isEqualTo(AccessLevel.RESTRICTED);
    }).verifyComplete();
}
Also used : Base64Utils(org.springframework.util.Base64Utils) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) BeforeEach(org.junit.jupiter.api.BeforeEach) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) StepVerifier(reactor.test.StepVerifier) MockServerHttpRequest(org.springframework.mock.http.server.reactive.MockServerHttpRequest) Mock(org.mockito.Mock) HttpHeaders(org.springframework.http.HttpHeaders) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Mono(reactor.core.publisher.Mono) Test(org.junit.jupiter.api.Test) HttpStatus(org.springframework.http.HttpStatus) AccessLevel(org.springframework.boot.actuate.autoconfigure.cloudfoundry.AccessLevel) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) BDDMockito.given(org.mockito.BDDMockito.given) Reason(org.springframework.boot.actuate.autoconfigure.cloudfoundry.CloudFoundryAuthorizationException.Reason) CloudFoundryAuthorizationException(org.springframework.boot.actuate.autoconfigure.cloudfoundry.CloudFoundryAuthorizationException) MockServerWebExchange(org.springframework.mock.web.server.MockServerWebExchange) MockServerWebExchange(org.springframework.mock.web.server.MockServerWebExchange) Test(org.junit.jupiter.api.Test)

Aggregations

MockServerWebExchange (org.springframework.mock.web.server.MockServerWebExchange)94 Test (org.junit.jupiter.api.Test)81 MockServerHttpRequest (org.springframework.mock.http.server.reactive.MockServerHttpRequest)44 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)27 Mono (reactor.core.publisher.Mono)26 BeforeEach (org.junit.jupiter.api.BeforeEach)22 StepVerifier (reactor.test.StepVerifier)21 ServerWebExchange (org.springframework.web.server.ServerWebExchange)15 Duration (java.time.Duration)14 ErrorAttributes (org.springframework.boot.web.reactive.error.ErrorAttributes)13 HandlerMethod (org.springframework.web.method.HandlerMethod)13 Timed (io.micrometer.core.annotation.Timed)12 MockClock (io.micrometer.core.instrument.MockClock)12 Tag (io.micrometer.core.instrument.Tag)12 SimpleConfig (io.micrometer.core.instrument.simple.SimpleConfig)12 SimpleMeterRegistry (io.micrometer.core.instrument.simple.SimpleMeterRegistry)12 EOFException (java.io.EOFException)12 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)12 AutoTimer (org.springframework.boot.actuate.metrics.AutoTimer)12 ResponseCookie (org.springframework.http.ResponseCookie)12