Search in sources :

Example 6 with TestResponse

use of io.helidon.webserver.testsupport.TestResponse in project helidon by oracle.

the class JsonSupportTest method acceptHeaders.

@Test
public void acceptHeaders() throws Exception {
    Routing routing = Routing.builder().post("/foo", Handler.create(JsonObject.class, (req, res, json) -> res.send(json))).build();
    JsonObject json = createJson();
    // Has accept
    TestResponse response = TestClient.create(routing, JsonpSupport.create()).path("/foo").header("Accept", "text/plain; q=.8, application/json; q=.1").post(MediaPublisher.create(MediaType.APPLICATION_JSON.withCharset("UTF-8"), json.toString()));
    assertThat(response.status(), is(equalTo(Http.Status.OK_200)));
    assertThat(response.headers().first(Http.Header.CONTENT_TYPE).orElse(null), is(equalTo(MediaType.APPLICATION_JSON.toString())));
    // Has accept with +json
    response = TestClient.create(routing, JsonpSupport.create()).path("/foo").header("Accept", "text/plain; q=.8, application/specific+json; q=.1").post(MediaPublisher.create(MediaType.APPLICATION_JSON.withCharset("UTF-8"), json.toString()));
    assertThat(response.status(), is(equalTo(Http.Status.OK_200)));
    assertThat(response.headers().first(Http.Header.CONTENT_TYPE).orElse(null), is(equalTo(MediaType.parse("application/specific+json").toString())));
    // With start
    response = TestClient.create(routing, JsonpSupport.create()).path("/foo").header("Accept", "text/plain; q=.8, application/*; q=.1").post(MediaPublisher.create(MediaType.APPLICATION_JSON.withCharset("UTF-8"), json.toString()));
    assertThat(response.status(), is(equalTo(Http.Status.OK_200)));
    assertThat(response.headers().first(Http.Header.CONTENT_TYPE).orElse(null), is(equalTo(MediaType.APPLICATION_JSON.toString())));
    // With JSONP standard application/javascript
    response = TestClient.create(routing, JsonpSupport.create()).path("/foo").header("Accept", "application/javascript").post(MediaPublisher.create(MediaType.APPLICATION_JSON.withCharset("UTF-8"), json.toString()));
    assertThat(response.status(), is(equalTo(Http.Status.INTERNAL_SERVER_ERROR_500)));
    // Without start
    response = TestClient.create(routing, JsonpSupport.create()).path("/foo").header("Accept", "text/plain; q=.8, application/specific; q=.1").post(MediaPublisher.create(MediaType.APPLICATION_JSON.withCharset("UTF-8"), json.toString()));
    assertThat(response.status(), is(equalTo(Http.Status.INTERNAL_SERVER_ERROR_500)));
}
Also used : TestResponse(io.helidon.webserver.testsupport.TestResponse) Routing(io.helidon.webserver.Routing) JsonObject(jakarta.json.JsonObject) Test(org.junit.jupiter.api.Test)

Example 7 with TestResponse

use of io.helidon.webserver.testsupport.TestResponse 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 8 with TestResponse

use of io.helidon.webserver.testsupport.TestResponse 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 9 with TestResponse

use of io.helidon.webserver.testsupport.TestResponse 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 10 with TestResponse

use of io.helidon.webserver.testsupport.TestResponse 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)

Aggregations

TestResponse (io.helidon.webserver.testsupport.TestResponse)26 Test (org.junit.jupiter.api.Test)25 Routing (io.helidon.webserver.Routing)10 JsonObject (jakarta.json.JsonObject)4 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)3 TestClient (io.helidon.webserver.testsupport.TestClient)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 GenericType (io.helidon.common.GenericType)1 Http (io.helidon.common.http.Http)1 MediaType (io.helidon.common.http.MediaType)1 Handler (io.helidon.webserver.Handler)1 WebServer (io.helidon.webserver.WebServer)1 MediaPublisher (io.helidon.webserver.testsupport.MediaPublisher)1 TestRequest (io.helidon.webserver.testsupport.TestRequest)1 List (java.util.List)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 TimeUnit (java.util.concurrent.TimeUnit)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 CoreMatchers.is (org.hamcrest.CoreMatchers.is)1 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)1