HttpResponseUtils { public static <T> void charset(HttpResponse response, String charset) { String type = getHeader(response, "Content-Type"); if (type == null) { type = "text/plain"; } String newType = type.replaceAll(";\\s*charset=[^;]*", "") + "; charset=" + charset; response.getHeaders().remove("Content-Type"); header(response, "Content-Type", newType); }  static HttpResponse redirect(String url, RedirectStatusCode code); static HttpResponse response(String body); static T getHeader(HttpResponse response, String name); static void header(HttpResponse response, String name, String value); static void charset(HttpResponse response, String charset); static HttpResponse contentType(HttpResponse response, String type); static HttpResponse contentLength(HttpResponse response, Long len); static HttpResponse lastModified(HttpResponse response, Date lastModified); static boolean isJarDirectory(JarURLConnection conn); static Long connectionContentLength(URLConnection conn); static Date connectionLastModified(URLConnection conn); static ContentData resourceData(URL url); static HttpResponse urlResponse(URL url); static HttpResponse resourceResponse(String path, OptionMap options); static boolean isEmptyBody(HttpResponse response);  }
@Test void charset() { HttpResponse response = builder(HttpResponse.of("for testing")) .set(HttpResponse::setHeaders, Headers.of("Content-Type", "text/html")) .build(); HttpResponseUtils.charset(response, "ISO_8859_1"); assertThat(response.getHeaders().get("Content-Type")) .isEqualTo("text/html; charset=ISO_8859_1"); }
@Test void charsetWithoutContentType() { HttpResponse response = HttpResponse.of("for testing"); response.getHeaders().remove("Content-Type"); HttpResponseUtils.charset(response, "ISO_8859_1"); assertThat(response.getHeaders().get("Content-Type")) .isEqualTo("text/plain; charset=ISO_8859_1"); }