Search in sources :

Example 1 with MockServerHttpResponse

use of org.springframework.mock.http.server.reactive.MockServerHttpResponse in project spring-security by spring-projects.

the class PathMatcherServerWebExchangeMatcherTests method setup.

@BeforeEach
public void setup() {
    MockServerHttpRequest request = MockServerHttpRequest.post("/path").build();
    MockServerHttpResponse response = new MockServerHttpResponse();
    DefaultWebSessionManager sessionManager = new DefaultWebSessionManager();
    this.exchange = MockServerWebExchange.from(request);
    this.path = "/path";
    this.matcher = new PathPatternParserServerWebExchangeMatcher(this.pattern);
}
Also used : MockServerHttpRequest(org.springframework.mock.http.server.reactive.MockServerHttpRequest) DefaultWebSessionManager(org.springframework.web.server.session.DefaultWebSessionManager) MockServerHttpResponse(org.springframework.mock.http.server.reactive.MockServerHttpResponse) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 2 with MockServerHttpResponse

use of org.springframework.mock.http.server.reactive.MockServerHttpResponse in project spring-security by spring-projects.

the class BearerTokenServerAccessDeniedHandlerTests method handleWhenNotOAuth2AuthenticatedThenStatus403.

@Test
public void handleWhenNotOAuth2AuthenticatedThenStatus403() {
    Authentication token = new TestingAuthenticationToken("user", "pass");
    ServerWebExchange exchange = mock(ServerWebExchange.class);
    given(exchange.getPrincipal()).willReturn(Mono.just(token));
    given(exchange.getResponse()).willReturn(new MockServerHttpResponse());
    this.accessDeniedHandler.handle(exchange, null).block();
    assertThat(exchange.getResponse().getStatusCode()).isEqualTo(HttpStatus.FORBIDDEN);
    assertThat(exchange.getResponse().getHeaders().get("WWW-Authenticate")).isEqualTo(Arrays.asList("Bearer"));
}
Also used : ServerWebExchange(org.springframework.web.server.ServerWebExchange) Authentication(org.springframework.security.core.Authentication) TestingAuthenticationToken(org.springframework.security.authentication.TestingAuthenticationToken) MockServerHttpResponse(org.springframework.mock.http.server.reactive.MockServerHttpResponse) Test(org.junit.jupiter.api.Test)

Example 3 with MockServerHttpResponse

use of org.springframework.mock.http.server.reactive.MockServerHttpResponse in project spring-framework by spring-projects.

the class HttpHandlerConnector method doConnect.

private Mono<ClientHttpResponse> doConnect(HttpMethod httpMethod, URI uri, Function<? super ClientHttpRequest, Mono<Void>> requestCallback) {
    // unsafe(): we're intercepting, already serialized Publisher signals
    Sinks.Empty<Void> requestWriteSink = Sinks.unsafe().empty();
    Sinks.Empty<Void> handlerSink = Sinks.unsafe().empty();
    ClientHttpResponse[] savedResponse = new ClientHttpResponse[1];
    MockClientHttpRequest mockClientRequest = new MockClientHttpRequest(httpMethod, uri);
    MockServerHttpResponse mockServerResponse = new MockServerHttpResponse();
    mockClientRequest.setWriteHandler(requestBody -> {
        log("Invoking HttpHandler for ", httpMethod, uri);
        ServerHttpRequest mockServerRequest = adaptRequest(mockClientRequest, requestBody);
        ServerHttpResponse responseToUse = prepareResponse(mockServerResponse, mockServerRequest);
        this.handler.handle(mockServerRequest, responseToUse).subscribe(aVoid -> {
        }, // Ignore result: signals cannot compete
        handlerSink::tryEmitError, handlerSink::tryEmitEmpty);
        return Mono.empty();
    });
    mockServerResponse.setWriteHandler(responseBody -> Mono.fromRunnable(() -> {
        log("Creating client response for ", httpMethod, uri);
        savedResponse[0] = adaptResponse(mockServerResponse, responseBody);
    }));
    log("Writing client request for ", httpMethod, uri);
    requestCallback.apply(mockClientRequest).subscribe(aVoid -> {
    }, // Ignore result: signals cannot compete
    requestWriteSink::tryEmitError, requestWriteSink::tryEmitEmpty);
    return Mono.when(requestWriteSink.asMono(), handlerSink.asMono()).onErrorMap(ex -> {
        ClientHttpResponse response = savedResponse[0];
        return response != null ? new FailureAfterResponseCompletedException(response, ex) : ex;
    }).then(Mono.fromCallable(() -> savedResponse[0] != null ? savedResponse[0] : adaptResponse(mockServerResponse, Flux.empty())));
}
Also used : Sinks(reactor.core.publisher.Sinks) ServerHttpResponse(org.springframework.http.server.reactive.ServerHttpResponse) Function(java.util.function.Function) ClientHttpResponse(org.springframework.http.client.reactive.ClientHttpResponse) HttpCookie(org.springframework.http.HttpCookie) Schedulers(reactor.core.scheduler.Schedulers) ClientHttpRequest(org.springframework.http.client.reactive.ClientHttpRequest) URI(java.net.URI) ServerHttpRequest(org.springframework.http.server.reactive.ServerHttpRequest) MockClientHttpResponse(org.springframework.mock.http.client.reactive.MockClientHttpResponse) MockServerHttpRequest(org.springframework.mock.http.server.reactive.MockServerHttpRequest) HttpHeaders(org.springframework.http.HttpHeaders) Publisher(org.reactivestreams.Publisher) HttpMethod(org.springframework.http.HttpMethod) MultiValueMap(org.springframework.util.MultiValueMap) Mono(reactor.core.publisher.Mono) HttpHeadResponseDecorator(org.springframework.http.server.reactive.HttpHeadResponseDecorator) DataBuffer(org.springframework.core.io.buffer.DataBuffer) Flux(reactor.core.publisher.Flux) HttpHandler(org.springframework.http.server.reactive.HttpHandler) Log(org.apache.commons.logging.Log) LogFactory(org.apache.commons.logging.LogFactory) MockServerHttpResponse(org.springframework.mock.http.server.reactive.MockServerHttpResponse) ClientHttpConnector(org.springframework.http.client.reactive.ClientHttpConnector) MockClientHttpRequest(org.springframework.mock.http.client.reactive.MockClientHttpRequest) Assert(org.springframework.util.Assert) Sinks(reactor.core.publisher.Sinks) ServerHttpRequest(org.springframework.http.server.reactive.ServerHttpRequest) MockServerHttpRequest(org.springframework.mock.http.server.reactive.MockServerHttpRequest) MockClientHttpRequest(org.springframework.mock.http.client.reactive.MockClientHttpRequest) MockServerHttpResponse(org.springframework.mock.http.server.reactive.MockServerHttpResponse) ServerHttpResponse(org.springframework.http.server.reactive.ServerHttpResponse) MockServerHttpResponse(org.springframework.mock.http.server.reactive.MockServerHttpResponse) ClientHttpResponse(org.springframework.http.client.reactive.ClientHttpResponse) MockClientHttpResponse(org.springframework.mock.http.client.reactive.MockClientHttpResponse)

Example 4 with MockServerHttpResponse

use of org.springframework.mock.http.server.reactive.MockServerHttpResponse in project spring-framework by spring-projects.

the class HttpHandlerConnector method connect.

@Override
public Mono<ClientHttpResponse> connect(HttpMethod httpMethod, URI uri, Function<? super ClientHttpRequest, Mono<Void>> requestCallback) {
    MonoProcessor<ClientHttpResponse> result = MonoProcessor.create();
    MockClientHttpRequest mockClientRequest = new MockClientHttpRequest(httpMethod, uri);
    MockServerHttpResponse mockServerResponse = new MockServerHttpResponse();
    mockClientRequest.setWriteHandler(requestBody -> {
        log("Invoking HttpHandler for ", httpMethod, uri);
        ServerHttpRequest mockServerRequest = adaptRequest(mockClientRequest, requestBody);
        this.handler.handle(mockServerRequest, mockServerResponse).subscribe(aVoid -> {
        }, result::onError);
        return Mono.empty();
    });
    mockServerResponse.setWriteHandler(responseBody -> {
        log("Creating client response for ", httpMethod, uri);
        result.onNext(adaptResponse(mockServerResponse, responseBody));
        return Mono.empty();
    });
    log("Writing client request for ", httpMethod, uri);
    requestCallback.apply(mockClientRequest).subscribe(aVoid -> {
    }, result::onError);
    return result;
}
Also used : ServerHttpRequest(org.springframework.http.server.reactive.ServerHttpRequest) MockServerHttpRequest(org.springframework.mock.http.server.reactive.MockServerHttpRequest) MockClientHttpRequest(org.springframework.mock.http.client.reactive.MockClientHttpRequest) ClientHttpResponse(org.springframework.http.client.reactive.ClientHttpResponse) MockClientHttpResponse(org.springframework.mock.http.client.reactive.MockClientHttpResponse) MockServerHttpResponse(org.springframework.mock.http.server.reactive.MockServerHttpResponse)

Example 5 with MockServerHttpResponse

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

the class ReactiveManagementWebSecurityAutoConfigurationTests method performFilter.

private ServerWebExchange performFilter(AssertableReactiveWebApplicationContext context, String path) {
    ServerWebExchange exchange = webHandler(context).createExchange(MockServerHttpRequest.get(path).build(), new MockServerHttpResponse());
    WebFilterChainProxy proxy = context.getBean(WebFilterChainProxy.class);
    proxy.filter(exchange, (serverWebExchange) -> Mono.empty()).block(Duration.ofSeconds(30));
    return exchange;
}
Also used : ReactiveUserDetailsServiceAutoConfiguration(org.springframework.boot.autoconfigure.security.reactive.ReactiveUserDetailsServiceAutoConfiguration) HttpWebHandlerAdapter(org.springframework.web.server.adapter.HttpWebHandlerAdapter) ServerHttpResponse(org.springframework.http.server.reactive.ServerHttpResponse) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) EnvironmentEndpointAutoConfiguration(org.springframework.boot.actuate.autoconfigure.env.EnvironmentEndpointAutoConfiguration) WebHandler(org.springframework.web.server.WebHandler) ReactiveOAuth2ResourceServerAutoConfiguration(org.springframework.boot.autoconfigure.security.oauth2.resource.reactive.ReactiveOAuth2ResourceServerAutoConfiguration) ServerWebExchange(org.springframework.web.server.ServerWebExchange) HealthEndpointAutoConfiguration(org.springframework.boot.actuate.autoconfigure.health.HealthEndpointAutoConfiguration) ServerHttpSecurity(org.springframework.security.config.web.server.ServerHttpSecurity) ReactiveWebApplicationContextRunner(org.springframework.boot.test.context.runner.ReactiveWebApplicationContextRunner) Duration(java.time.Duration) HealthContributorAutoConfiguration(org.springframework.boot.actuate.autoconfigure.health.HealthContributorAutoConfiguration) WebEndpointAutoConfiguration(org.springframework.boot.actuate.autoconfigure.endpoint.web.WebEndpointAutoConfiguration) ReactiveAuthenticationManager(org.springframework.security.authentication.ReactiveAuthenticationManager) URI(java.net.URI) AssertableReactiveWebApplicationContext(org.springframework.boot.test.context.assertj.AssertableReactiveWebApplicationContext) InfoEndpointAutoConfiguration(org.springframework.boot.actuate.autoconfigure.info.InfoEndpointAutoConfiguration) ServerHttpRequest(org.springframework.http.server.reactive.ServerHttpRequest) ReactiveSecurityAutoConfiguration(org.springframework.boot.autoconfigure.security.reactive.ReactiveSecurityAutoConfiguration) AutoConfigurations(org.springframework.boot.autoconfigure.AutoConfigurations) MockServerHttpRequest(org.springframework.mock.http.server.reactive.MockServerHttpRequest) HttpHeaders(org.springframework.http.HttpHeaders) Customizer(org.springframework.security.config.Customizer) Mono(reactor.core.publisher.Mono) EndpointAutoConfiguration(org.springframework.boot.actuate.autoconfigure.endpoint.EndpointAutoConfiguration) ApplicationContext(org.springframework.context.ApplicationContext) WebFilterChainProxy(org.springframework.security.web.server.WebFilterChainProxy) Test(org.junit.jupiter.api.Test) Configuration(org.springframework.context.annotation.Configuration) List(java.util.List) SecurityWebFilterChain(org.springframework.security.web.server.SecurityWebFilterChain) WebFluxAutoConfiguration(org.springframework.boot.autoconfigure.web.reactive.WebFluxAutoConfiguration) Bean(org.springframework.context.annotation.Bean) MockServerHttpResponse(org.springframework.mock.http.server.reactive.MockServerHttpResponse) Collections(java.util.Collections) ApplicationContextAware(org.springframework.context.ApplicationContextAware) Mockito.mock(org.mockito.Mockito.mock) ServerWebExchange(org.springframework.web.server.ServerWebExchange) WebFilterChainProxy(org.springframework.security.web.server.WebFilterChainProxy) MockServerHttpResponse(org.springframework.mock.http.server.reactive.MockServerHttpResponse)

Aggregations

MockServerHttpResponse (org.springframework.mock.http.server.reactive.MockServerHttpResponse)15 Test (org.junit.jupiter.api.Test)10 ServerWebExchange (org.springframework.web.server.ServerWebExchange)9 MockServerHttpRequest (org.springframework.mock.http.server.reactive.MockServerHttpRequest)8 ServerHttpRequest (org.springframework.http.server.reactive.ServerHttpRequest)7 BeforeEach (org.junit.jupiter.api.BeforeEach)6 Mono (reactor.core.publisher.Mono)6 Mockito.mock (org.mockito.Mockito.mock)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 Mockito.spy (org.mockito.Mockito.spy)4 Mockito.times (org.mockito.Mockito.times)4 Mockito.verify (org.mockito.Mockito.verify)4 ServerCodecConfigurer (org.springframework.http.codec.ServerCodecConfigurer)4 OAuth2AuthorizationRequest (org.springframework.security.oauth2.core.endpoint.OAuth2AuthorizationRequest)4 OAuth2ParameterNames (org.springframework.security.oauth2.core.endpoint.OAuth2ParameterNames)4 WebSession (org.springframework.web.server.WebSession)4 DefaultServerWebExchange (org.springframework.web.server.adapter.DefaultServerWebExchange)4