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)));
}
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)));
}
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)));
}
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)));
}
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));
}
Aggregations