/** * Converts the {@link AggregatedHttpMessage} into a new {@link HttpRequest} and closes the stream. */ static HttpRequest of(AggregatedHttpMessage message) { return of(message.headers(), message.content(), message.trailingHeaders()); }
@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(); } }
protected HttpResponse echo(AggregatedHttpMessage aReq) { return HttpResponse.of( HttpHeaders.of(HttpStatus.OK), aReq.content()); } }
@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 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 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 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 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(); }); }
@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"); }
private static void fillRequest( ServiceRequestContext ctx, AggregatedHttpMessage aReq, Request jReq) { jReq.setDispatcherType(DispatcherType.REQUEST); jReq.setAsyncSupported(false, "armeria"); jReq.setSecure(ctx.sessionProtocol().isTls()); jReq.setMetaData(toRequestMetadata(ctx, aReq)); final HttpData content = aReq.content(); if (!content.isEmpty()) { jReq.getHttpInput().addContent(new Content(ByteBuffer.wrap( content.array(), content.offset(), content.length()))); } jReq.getHttpInput().eof(); }