Search in sources :

Example 31 with MockServerWebExchange

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

the class ReactiveCloudFoundrySecurityInterceptorTests method preHandleSuccessfulWithFullAccess.

@Test
void preHandleSuccessfulWithFullAccess() {
    String accessToken = mockAccessToken();
    given(this.securityService.getAccessLevel(accessToken, "my-app-id")).willReturn(Mono.just(AccessLevel.FULL));
    given(this.tokenValidator.validate(any())).willReturn(Mono.empty());
    MockServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest.get("/a").header(HttpHeaders.AUTHORIZATION, "bearer " + mockAccessToken()).build());
    StepVerifier.create(this.interceptor.preHandle(exchange, "/a")).consumeNextWith((response) -> {
        assertThat(response.getStatus()).isEqualTo(HttpStatus.OK);
        assertThat((AccessLevel) exchange.getAttribute("cloudFoundryAccessLevel")).isEqualTo(AccessLevel.FULL);
    }).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 32 with MockServerWebExchange

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

the class ReactiveCloudFoundrySecurityInterceptorTests method preHandleWhenTokenIsMissingShouldReturnMissingAuthorization.

@Test
void preHandleWhenTokenIsMissingShouldReturnMissingAuthorization() {
    MockServerWebExchange request = MockServerWebExchange.from(MockServerHttpRequest.get("/a").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 33 with MockServerWebExchange

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

the class ReactiveCloudFoundrySecurityInterceptorTests method preHandleWhenApplicationIdIsNullShouldReturnError.

@Test
void preHandleWhenApplicationIdIsNullShouldReturnError() {
    this.interceptor = new CloudFoundrySecurityInterceptor(this.tokenValidator, this.securityService, null);
    MockServerWebExchange request = MockServerWebExchange.from(MockServerHttpRequest.get("/a").header(HttpHeaders.AUTHORIZATION, "bearer " + 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 34 with MockServerWebExchange

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

the class ReactiveCloudFoundrySecurityInterceptorTests method preHandleWhenRequestIsPreFlightShouldBeOk.

@Test
void preHandleWhenRequestIsPreFlightShouldBeOk() {
    MockServerWebExchange request = MockServerWebExchange.from(MockServerHttpRequest.options("/a").header(HttpHeaders.ORIGIN, "https://example.com").header(HttpHeaders.ACCESS_CONTROL_REQUEST_METHOD, "GET").build());
    StepVerifier.create(this.interceptor.preHandle(request, "/a")).consumeNextWith((response) -> assertThat(response.getStatus()).isEqualTo(HttpStatus.OK)).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 35 with MockServerWebExchange

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

the class WebFluxAutoConfigurationTests method whenAcceptHeaderLocaleContextResolverIsUsedThenAcceptLanguagesHeaderIsHonoured.

@Test
void whenAcceptHeaderLocaleContextResolverIsUsedThenAcceptLanguagesHeaderIsHonoured() {
    this.contextRunner.withPropertyValues("spring.web.locale:en_UK").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(AcceptHeaderLocaleContextResolver.class);
        LocaleContext localeContext = localeContextResolver.resolveLocaleContext(exchange);
        assertThat(localeContext.getLocale()).isEqualTo(StringUtils.parseLocaleString("nl_NL"));
    });
}
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)

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