use of io.helidon.webserver.Routing in project helidon by oracle.
the class JsonSupportTest method invalidJson.
@Test
public void invalidJson() throws Exception {
Routing routing = Routing.builder().post("/foo", Handler.create(JsonObject.class, (req, res, json) -> res.send(json))).build();
TestResponse response = TestClient.create(routing, JsonpSupport.create()).path("/foo").post(MediaPublisher.create(MediaType.APPLICATION_JSON.withCharset("UTF-8"), "{ ... invalid ... }"));
assertThat(response.status(), is(equalTo(Http.Status.INTERNAL_SERVER_ERROR_500)));
}
use of io.helidon.webserver.Routing in project helidon by oracle.
the class JsonSupportTest method pingPong.
@Test
public void pingPong() throws Exception {
Routing routing = Routing.builder().post("/foo", Handler.create(JsonObject.class, (req, res, json) -> res.send(json))).build();
JsonObject json = createJson();
TestResponse response = TestClient.create(routing, JsonpSupport.create()).path("/foo").post(MediaPublisher.create(MediaType.APPLICATION_JSON.withCharset("UTF-8"), json.toString()));
assertThat(response.headers().first(Http.Header.CONTENT_TYPE).orElse(null), is(equalTo(MediaType.APPLICATION_JSON.toString())));
byte[] bytes = response.asBytes().toCompletableFuture().get(10, TimeUnit.SECONDS);
JsonObject json2 = Json.createReader(new ByteArrayInputStream(bytes)).readObject();
assertThat(json2, is(equalTo(json)));
}
use of io.helidon.webserver.Routing in project helidon by oracle.
the class JsonSupportTest method pingPongNoCharset.
@Test
public void pingPongNoCharset() throws Exception {
Routing routing = Routing.builder().post("/foo", Handler.create(JsonObject.class, (req, res, json) -> res.send(json))).build();
JsonObject json = createJson();
TestResponse response = TestClient.create(routing, JsonpSupport.create()).path("/foo").post(MediaPublisher.create(MediaType.APPLICATION_JSON, json.toString()));
assertThat(response.headers().first(Http.Header.CONTENT_TYPE).orElse(null), is(equalTo(MediaType.APPLICATION_JSON.toString())));
byte[] bytes = response.asBytes().toCompletableFuture().get(10, TimeUnit.SECONDS);
JsonObject json2 = Json.createReader(new ByteArrayInputStream(bytes)).readObject();
assertThat(json2, is(equalTo(json)));
}
use of io.helidon.webserver.Routing in project helidon by oracle.
the class TestJsonBindingSupport method pingPong.
@Test
public void pingPong() throws Exception {
final Routing routing = Routing.builder().post("/foo", Handler.create(Person.class, (req, res, person) -> res.send(person))).build();
final String personJson = "{\"name\":\"Frank\"}";
final TestResponse response = TestClient.create(routing, JsonbSupport.create()).path("/foo").post(MediaPublisher.create(MediaType.APPLICATION_JSON.withCharset("UTF-8"), personJson));
assertThat(response.headers().first(Http.Header.CONTENT_TYPE).orElse(null), is(MediaType.APPLICATION_JSON.toString()));
final String json = response.asString().get(10, TimeUnit.SECONDS);
assertThat(json, is(personJson));
}
use of io.helidon.webserver.Routing in project helidon by oracle.
the class MetricsSupport method configureVendorMetrics.
/**
* Configure vendor metrics on the provided routing. This method is
* exclusive to {@link #update(io.helidon.webserver.Routing.Rules)} (e.g.
* you should not use both, as otherwise you would duplicate the metrics)
*
* @param routingName name of the routing (may be null)
* @param rules routing builder or routing rules
*/
@Override
public void configureVendorMetrics(String routingName, Routing.Rules rules) {
String metricPrefix = metricsNamePrefix(routingName);
KeyPerformanceIndicatorSupport.Metrics kpiMetrics = KeyPerformanceIndicatorMetricsImpls.get(metricPrefix, metricsSettings.keyPerformanceIndicatorSettings());
rules.any((req, res) -> {
KeyPerformanceIndicatorSupport.Context kpiContext = kpiContext(req);
PostRequestMetricsSupport prms = PostRequestMetricsSupport.create();
req.context().register(prms);
kpiContext.requestHandlingStarted(kpiMetrics);
res.whenSent().thenAccept(r -> postRequestProcessing(prms, req, r, null, kpiContext)).exceptionallyAccept(t -> postRequestProcessing(prms, req, res, t, kpiContext));
Exception exception = null;
try {
req.next();
} catch (Exception e) {
exception = e;
throw e;
} finally {
// Perform updates which depend on completion of request *handling* (after the server has begun request
// *processing* but, in the case of async requests, possibly before processing has finished).
kpiContext.requestHandlingCompleted(exception == null);
}
});
}
Aggregations