static void ensureHttpDataOfString(HttpObject httpObject, String expected) { assertThat(httpObject).isInstanceOf(HttpData.class); assertThat(((HttpData) httpObject).toStringUtf8()).isEqualTo(expected); }
private void validateEchoResponse(AggregatedHttpMessage res) { assertThat(res.status()).isEqualTo(com.linecorp.armeria.common.HttpStatus.OK); assertThat(res.content().toStringUtf8()).isEqualTo(POST_BODY); }
@Nullable @Override public Object convertRequest(ServiceRequestContext ctx, AggregatedHttpMessage request, Class<?> expectedResultType) throws Exception { final MediaType mediaType = request.headers().contentType(); if (mediaType != null && mediaType.is(MediaType.PLAIN_TEXT_UTF_8)) { return new Request(request.content().toStringUtf8()); } return RequestConverterFunction.fallthrough(); } }
@Test public void hello() throws Exception { final AggregatedHttpMessage res = client.get("/hello").aggregate().join(); assertThat(res.status()).isEqualTo(HttpStatus.OK); assertThat(res.content().toStringUtf8()).isEqualTo("Hello, World"); }
@Test public void testPathPatternService() { AggregatedHttpMessage res; res = client.get("/pathPattern/path/armeria").aggregate().join(); assertThat(res.content().toStringUtf8()).isEqualTo("path: armeria"); assertThat(res.status()).isEqualTo(HttpStatus.OK); res = client.get("/pathPattern/regex/armeria").aggregate().join(); assertThat(res.content().toStringUtf8()).isEqualTo("regex: armeria"); assertThat(res.status()).isEqualTo(HttpStatus.OK); res = client.get("/pathPattern/glob/armeria").aggregate().join(); assertThat(res.content().toStringUtf8()).isEqualTo("glob: armeria"); assertThat(res.status()).isEqualTo(HttpStatus.OK); }
@Test public void index() { final AggregatedHttpMessage res = client.get("/").aggregate().join(); assertThat(res.status()).isEqualTo(HttpStatus.OK); assertThat(res.content().toStringUtf8()).isEqualTo("index"); }
@Test public void success() { final AggregatedHttpMessage response = client.get("/hello/Spring").aggregate().join(); assertThat(response.status()).isEqualTo(HttpStatus.OK); assertThat(response.content().toStringUtf8()) .isEqualTo("Hello, Spring! This message is from Armeria annotated service!"); }
@Test public void healthCheck() throws Exception { final AggregatedHttpMessage res = client.get("/internal/healthcheck").aggregate().join(); assertThat(res.status()).isEqualTo(HttpStatus.OK); assertThat(res.content().toStringUtf8()).isEqualTo("ok"); } }
@Test public void shouldRespondAuthnRequest_HttpPost() throws Exception { final AggregatedHttpMessage resp = client.get("/post").aggregate().join(); assertThat(resp.status()).isEqualTo(HttpStatus.OK); assertThat(resp.headers().contentType()).isEqualTo(MediaType.HTML_UTF_8); final Document doc = Jsoup.parse(resp.content().toStringUtf8()); assertThat(doc.body().attr("onLoad")).isEqualTo("document.forms[0].submit()"); // SAMLRequest will be posted to the IdP's SSO URL. final Element form = doc.body().child(0); assertThat(form.attr("method")).isEqualTo("post"); assertThat(form.attr("action")).isEqualTo("http://idp.example.com/saml/sso/post"); assertThat(form.child(0).attr("name")).isEqualTo(SAML_REQUEST); assertThat(form.child(1).attr("name")).isEqualTo(RELAY_STATE); }
@Test public void testAnnotatedServiceRegistrationBean() throws Exception { final HttpClient client = HttpClient.of(newUrl("h1c")); HttpResponse response = client.get("/annotated/get"); AggregatedHttpMessage msg = response.aggregate().get(); assertThat(msg.status()).isEqualTo(HttpStatus.OK); assertThat(msg.content().toStringUtf8()).isEqualTo("annotated"); response = client.get("/annotated/get/2"); msg = response.aggregate().get(); assertThat(msg.status()).isEqualTo(HttpStatus.OK); assertThat(msg.content().toStringUtf8()).isEqualTo("exception"); }
@Test public void shouldGetHelloFromRestController() throws Exception { protocols.forEach(scheme -> { final HttpClient client = HttpClient.of(clientFactory, scheme + "://example.com:" + port); final AggregatedHttpMessage response = client.get("/hello").aggregate().join(); assertThat(response.content().toStringUtf8()).isEqualTo("hello"); }); }
@Test public void shouldGetHelloFromRestController() throws Exception { final HttpClient client = HttpClient.of("http://127.0.0.1:" + port); final AggregatedHttpMessage response = client.get("/proxy?port=" + port).aggregate().join(); assertThat(response.content().toStringUtf8()).isEqualTo("hello"); } }
@Test public void shouldBeAlreadyAuthenticated() throws Exception { final HttpHeaders req = HttpHeaders.of(HttpMethod.GET, "/redirect") .add(HttpHeaderNames.COOKIE, "test=test"); final AggregatedHttpMessage resp = client.execute(req).aggregate().join(); assertThat(resp.status()).isEqualTo(HttpStatus.OK); assertThat(resp.content().toStringUtf8()).isEqualTo("authenticated"); }
@Test public void testHttpServiceRegistrationBean() throws Exception { final HttpClient client = HttpClient.of(newUrl("h1c")); final HttpResponse response = client.get("/ok"); final AggregatedHttpMessage msg = response.aggregate().get(); assertThat(msg.status()).isEqualTo(HttpStatus.OK); assertThat(msg.content().toStringUtf8()).isEqualTo("ok"); }
@Test public void testHttpServiceRegistrationBean() throws Exception { final HttpClient client = HttpClient.of(newUrl("h1c")); final HttpResponse response = client.get("/ok"); final AggregatedHttpMessage msg = response.aggregate().get(); assertThat(msg.status()).isEqualTo(HttpStatus.OK); assertThat(msg.content().toStringUtf8()).isEqualTo("ok"); } }
@Test public void single() { final HttpClient client = HttpClient.of(rule.uri("/single")); AggregatedHttpMessage msg; msg = client.get("/string").aggregate().join(); assertThat(msg.headers().contentType()).isEqualTo(MediaType.PLAIN_TEXT_UTF_8); assertThat(msg.content().toStringUtf8()).isEqualTo("a"); msg = client.get("/json").aggregate().join(); assertThat(msg.headers().contentType()).isEqualTo(MediaType.JSON_UTF_8); assertThatJson(msg.content().toStringUtf8()).isStringEqualTo("a"); msg = client.get("/error").aggregate().join(); assertThat(msg.headers().status()).isEqualTo(HttpStatus.INTERNAL_SERVER_ERROR); }
@Test public void failure() { final AggregatedHttpMessage response = client.get("/hello/a").aggregate().join(); assertThat(response.status()).isEqualTo(HttpStatus.BAD_REQUEST); assertThatJson(response.content().toStringUtf8()) .node("message") .isEqualTo("hello.name: name should have between 3 and 10 characters"); } }
@Test public void testMetrics() throws Exception { final String metricReport = HttpClient.of(newUrl("http")) .get("/internal/metrics") .aggregate().join() .content().toStringUtf8(); assertThat(metricReport).contains("# TYPE jvm_gc_live_data_size_bytes gauge"); } }
@Test public void testThriftServiceRegistrationBean() throws Exception { final HelloService.Iface client = Clients.newClient(newUrl("tbinary+h1c") + "/thrift", HelloService.Iface.class); assertThat(client.hello("world")).isEqualTo("hello world"); final HttpClient httpClient = HttpClient.of(newUrl("h1c")); final HttpResponse response = httpClient.get("/internal/docs/specification.json"); final AggregatedHttpMessage msg = response.aggregate().get(); assertThat(msg.status()).isEqualTo(HttpStatus.OK); assertThatJson(msg.content().toStringUtf8()) .node("services[1].exampleHttpHeaders[0].x-additional-header").isStringEqualTo("headerVal"); }
@Test public void shouldReturnEchoResponse() { final ArmeriaReactiveWebServerFactory factory = factory(); runEchoServer(factory, server -> { final HttpClient client = httpClient(server); validateEchoResponse(sendPostRequest(client)); final AggregatedHttpMessage res = client.get("/hello").aggregate().join(); assertThat(res.status()).isEqualTo(com.linecorp.armeria.common.HttpStatus.OK); assertThat(res.content().toStringUtf8()).isEmpty(); }); }