use of org.springframework.http.server.reactive.HttpHandler in project spring-framework by spring-projects.
the class WebHttpHandlerBuilderTests method httpHandlerDecorator.
@Test
void httpHandlerDecorator() {
BiFunction<ServerHttpRequest, String, ServerHttpRequest> mutator = (req, value) -> req.mutate().headers(headers -> headers.add("My-Header", value)).build();
AtomicBoolean success = new AtomicBoolean();
HttpHandler httpHandler = WebHttpHandlerBuilder.webHandler(exchange -> {
HttpHeaders headers = exchange.getRequest().getHeaders();
assertThat(headers.get("My-Header")).containsExactlyInAnyOrder("1", "2", "3");
success.set(true);
return Mono.empty();
}).httpHandlerDecorator(handler -> (req, res) -> handler.handle(mutator.apply(req, "1"), res)).httpHandlerDecorator(handler -> (req, res) -> handler.handle(mutator.apply(req, "2"), res)).httpHandlerDecorator(handler -> (req, res) -> handler.handle(mutator.apply(req, "3"), res)).build();
httpHandler.handle(MockServerHttpRequest.get("/").build(), new MockServerHttpResponse()).block();
assertThat(success.get()).isTrue();
}
use of org.springframework.http.server.reactive.HttpHandler in project spring-framework by spring-projects.
the class WebHttpHandlerBuilderTests method orderedWebFilterBeans.
// SPR-15074
@Test
void orderedWebFilterBeans() {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
context.register(OrderedWebFilterBeanConfig.class);
context.refresh();
HttpHandler httpHandler = WebHttpHandlerBuilder.applicationContext(context).build();
boolean condition = httpHandler instanceof HttpWebHandlerAdapter;
assertThat(condition).isTrue();
assertThat(((HttpWebHandlerAdapter) httpHandler).getApplicationContext()).isSameAs(context);
MockServerHttpRequest request = MockServerHttpRequest.get("/").build();
MockServerHttpResponse response = new MockServerHttpResponse();
httpHandler.handle(request, response).block(ofMillis(5000));
assertThat(response.getBodyAsString().block(ofMillis(5000))).isEqualTo("FilterB::FilterA");
}
use of org.springframework.http.server.reactive.HttpHandler 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.http.server.reactive.HttpHandler in project spring-framework by spring-projects.
the class ServerWebExchangeContextFilterTests method extractServerWebExchangeFromContext.
@Test
void extractServerWebExchangeFromContext() {
MyService service = new MyService();
HttpHandler httpHandler = WebHttpHandlerBuilder.webHandler(exchange -> service.service().then()).filter(new ServerWebExchangeContextFilter()).build();
httpHandler.handle(MockServerHttpRequest.get("/path").build(), new MockServerHttpResponse()).block(Duration.ofSeconds(5));
assertThat(service.getExchange()).isNotNull();
}
use of org.springframework.http.server.reactive.HttpHandler in project spring-framework by spring-projects.
the class ContextPathIntegrationTests method multipleWebFluxApps.
@Test
void multipleWebFluxApps() throws Exception {
AnnotationConfigApplicationContext context1 = new AnnotationConfigApplicationContext(WebAppConfig.class);
AnnotationConfigApplicationContext context2 = new AnnotationConfigApplicationContext(WebAppConfig.class);
HttpHandler webApp1Handler = WebHttpHandlerBuilder.applicationContext(context1).build();
HttpHandler webApp2Handler = WebHttpHandlerBuilder.applicationContext(context2).build();
ReactorHttpServer server = new ReactorHttpServer();
server.registerHttpHandler("/webApp1", webApp1Handler);
server.registerHttpHandler("/webApp2", webApp2Handler);
server.afterPropertiesSet();
server.start();
try {
RestTemplate restTemplate = new RestTemplate();
String actual;
String url = "http://localhost:" + server.getPort() + "/webApp1/test";
actual = restTemplate.getForObject(url, String.class);
assertThat(actual).isEqualTo("Tested in /webApp1");
url = "http://localhost:" + server.getPort() + "/webApp2/test";
actual = restTemplate.getForObject(url, String.class);
assertThat(actual).isEqualTo("Tested in /webApp2");
} finally {
server.stop();
}
}
Aggregations