Mono<ServerResponse> retrieveExceptionInfo(ServerRequest serverRequest) { String exceptionName = serverRequest.attribute(HYSTRIX_EXECUTION_EXCEPTION_ATTR) .map(exception -> exception.getClass().getName()) .orElse(""); return ServerResponse.ok().header(RETRIEVED_EXCEPTION, exceptionName) .build(); } }
@Test public void from() { ServerResponse other = ServerResponse.ok().header("foo", "bar").build().block(); Mono<ServerResponse> result = ServerResponse.from(other).build(); StepVerifier.create(result) .expectNextMatches(response -> HttpStatus.OK.equals(response.statusCode()) && "bar".equals(response.headers().getFirst("foo"))) .expectComplete() .verify(); }
@Test public void build() { ResponseCookie cookie = ResponseCookie.from("name", "value").build(); Mono<ServerResponse> result = ServerResponse.status(HttpStatus.CREATED) .header("MyKey", "MyValue") .cookie(cookie).build(); MockServerHttpRequest request = MockServerHttpRequest.get("http://example.com").build(); MockServerWebExchange exchange = MockServerWebExchange.from(request); result.flatMap(res -> res.writeTo(exchange, EMPTY_CONTEXT)).block(); MockServerHttpResponse response = exchange.getResponse(); assertEquals(HttpStatus.CREATED, response.getStatusCode()); assertEquals("MyValue", response.getHeaders().getFirst("MyKey")); assertEquals("value", response.getCookies().getFirst("name").getValue()); StepVerifier.create(response.getBody()).expectComplete().verify(); }
public static HandlerFunction<ServerResponse> scrape(PrometheusMeterRegistry registry) { return (request) -> ServerResponse.ok().header("Content-Type", TextFormat.CONTENT_TYPE_004) .body(Mono.fromSupplier(() -> { Writer writer = new StringWriter(); try { TextFormat.write004(writer, registry.getPrometheusRegistry().metricFamilySamples()); } catch (IOException e) { // This actually never happens since StringWriter::write() doesn't throw any IOException throw new RuntimeException(e); } return writer.toString(); }), String.class); } }