StringResponseConverterFunction implements ResponseConverterFunction { @Override public HttpResponse convertResponse(ServiceRequestContext ctx, ResponseHeaders headers, @Nullable Object result, HttpHeaders trailers) throws Exception { final MediaType mediaType = headers.contentType(); if (mediaType != null) { if (mediaType.is(MediaType.ANY_TEXT_TYPE)) { final Charset charset = mediaType.charset(ArmeriaHttpUtil.HTTP_DEFAULT_CONTENT_CHARSET); if (result instanceof Publisher) { return aggregateFrom((Publisher<?>) result, headers, trailers, o -> toHttpData(o, charset)); } if (result instanceof Stream) { return aggregateFrom((Stream<?>) result, headers, trailers, o -> toHttpData(o, charset), ctx.blockingTaskExecutor()); } return HttpResponse.of(headers, toHttpData(result, charset), trailers); } } else if (result instanceof CharSequence) { return HttpResponse.of(headers.toBuilder().contentType(MediaType.PLAIN_TEXT_UTF_8).build(), HttpData.ofUtf8(((CharSequence) result).toString()), trailers); } return ResponseConverterFunction.fallthrough(); }  @Override HttpResponse convertResponse(ServiceRequestContext ctx,
                                        ResponseHeaders headers,
                                        @Nullable Object result,
                                        HttpHeaders trailers);  }
@Test void withoutContentType() throws Exception { StepVerifier.create(function.convertResponse(ctx, ResponseHeaders.of(HttpStatus.OK), "foo", DEFAULT_TRAILERS)) .expectNext(ResponseHeaders.of(HttpStatus.OK, HttpHeaderNames.CONTENT_TYPE, MediaType.PLAIN_TEXT_UTF_8, HttpHeaderNames.CONTENT_LENGTH, 3)) .expectNext(HttpData.ofUtf8("foo")) .expectComplete() .verify(); assertThatThrownBy(() -> function.convertResponse( ctx, ResponseHeaders.of(HttpStatus.OK), ImmutableList.of(), DEFAULT_TRAILERS)) .isInstanceOf(FallthroughException.class); }
@Test void charset() throws Exception { final ResponseHeaders headers = ResponseHeaders.of(HttpStatus.OK, HttpHeaderNames.CONTENT_TYPE, "text/plain; charset=euc-kr"); StepVerifier.create(function.convertResponse(ctx, headers, "한글", DEFAULT_TRAILERS)) .expectNext(headers.toBuilder() .addInt(HttpHeaderNames.CONTENT_LENGTH, 4) .build()) .expectNext(HttpData.of(Charset.forName("euc-kr"), "한글")) .expectComplete() .verify(); }