Search in sources :

Example 21 with Routing

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)));
}
Also used : TestResponse(io.helidon.webserver.testsupport.TestResponse) Routing(io.helidon.webserver.Routing) Test(org.junit.jupiter.api.Test)

Example 22 with Routing

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)));
}
Also used : TestResponse(io.helidon.webserver.testsupport.TestResponse) ByteArrayInputStream(java.io.ByteArrayInputStream) Routing(io.helidon.webserver.Routing) JsonObject(jakarta.json.JsonObject) Test(org.junit.jupiter.api.Test)

Example 23 with Routing

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)));
}
Also used : TestResponse(io.helidon.webserver.testsupport.TestResponse) ByteArrayInputStream(java.io.ByteArrayInputStream) Routing(io.helidon.webserver.Routing) JsonObject(jakarta.json.JsonObject) Test(org.junit.jupiter.api.Test)

Example 24 with Routing

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));
}
Also used : TestResponse(io.helidon.webserver.testsupport.TestResponse) Routing(io.helidon.webserver.Routing) Test(org.junit.jupiter.api.Test)

Example 25 with Routing

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);
        }
    });
}
Also used : Arrays(java.util.Arrays) JsonValue(jakarta.json.JsonValue) KeyPerformanceIndicatorSupport(io.helidon.webserver.KeyPerformanceIndicatorSupport) HelidonRestServiceSupport(io.helidon.servicecommon.rest.HelidonRestServiceSupport) DeprecatedConfig(io.helidon.config.DeprecatedConfig) MediaType(io.helidon.common.http.MediaType) BigDecimal(java.math.BigDecimal) PostRequestMetricsSupport(io.helidon.metrics.serviceapi.PostRequestMetricsSupport) Map(java.util.Map) JsonObject(jakarta.json.JsonObject) BigInteger(java.math.BigInteger) RestServiceSettings(io.helidon.servicecommon.rest.RestServiceSettings) JsonArrayBuilder(jakarta.json.JsonArrayBuilder) Set(java.util.Set) Logger(java.util.logging.Logger) MetricID(org.eclipse.microprofile.metrics.MetricID) Collectors(java.util.stream.Collectors) MetricsSettings(io.helidon.metrics.api.MetricsSettings) ServerRequest(io.helidon.webserver.ServerRequest) Objects(java.util.Objects) List(java.util.List) Stream(java.util.stream.Stream) Handler(io.helidon.webserver.Handler) Optional(java.util.Optional) MetricRegistry(org.eclipse.microprofile.metrics.MetricRegistry) JsonArray(jakarta.json.JsonArray) JsonBuilderFactory(jakarta.json.JsonBuilderFactory) Metric(org.eclipse.microprofile.metrics.Metric) HashMap(java.util.HashMap) Function(java.util.function.Function) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) JsonpSupport(io.helidon.media.jsonp.JsonpSupport) JsonStructure(jakarta.json.JsonStructure) BiConsumer(java.util.function.BiConsumer) ServerResponse(io.helidon.webserver.ServerResponse) Http(io.helidon.common.http.Http) MessageBodyWriter(io.helidon.media.common.MessageBodyWriter) Config(io.helidon.config.Config) MinimalMetricsSupport(io.helidon.metrics.serviceapi.MinimalMetricsSupport) RequestHeaders(io.helidon.webserver.RequestHeaders) Json(jakarta.json.Json) JsonObjectBuilder(jakarta.json.JsonObjectBuilder) KeyPerformanceIndicatorMetricsSettings(io.helidon.metrics.api.KeyPerformanceIndicatorMetricsSettings) RegistryFactory(io.helidon.metrics.api.RegistryFactory) SystemTagsManager(io.helidon.metrics.api.SystemTagsManager) Comparator(java.util.Comparator) Routing(io.helidon.webserver.Routing) Collections(java.util.Collections) KeyPerformanceIndicatorSupport(io.helidon.webserver.KeyPerformanceIndicatorSupport) PostRequestMetricsSupport(io.helidon.metrics.serviceapi.PostRequestMetricsSupport)

Aggregations

Routing (io.helidon.webserver.Routing)86 WebServer (io.helidon.webserver.WebServer)38 Config (io.helidon.config.Config)33 Test (org.junit.jupiter.api.Test)32 Http (io.helidon.common.http.Http)23 TimeUnit (java.util.concurrent.TimeUnit)21 LogConfig (io.helidon.common.LogConfig)19 MediaType (io.helidon.common.http.MediaType)19 CoreMatchers.is (org.hamcrest.CoreMatchers.is)17 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)17 SecurityContext (io.helidon.security.SecurityContext)16 HttpException (io.helidon.webserver.HttpException)15 Optional (java.util.Optional)15 CountDownLatch (java.util.concurrent.CountDownLatch)15 WebSecurity (io.helidon.security.integration.webserver.WebSecurity)13 SERVICE_UNAVAILABLE_503 (io.helidon.common.http.Http.Status.SERVICE_UNAVAILABLE_503)11 JsonpSupport (io.helidon.media.jsonp.JsonpSupport)11 Security (io.helidon.security.Security)11 StaticContentSupport (io.helidon.webserver.staticcontent.StaticContentSupport)10 TestResponse (io.helidon.webserver.testsupport.TestResponse)10