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);
}
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"));
}
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())));
}
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;
}
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;
}
Aggregations