protected Charset determineCharsetToUse(ResponseInfo<?> responseInfo) { // Figure out what charset to use. If responseInfo has one specified via desiredContentWriterEncoding then use // that, otherwise attempt to extract it from the response headers, and use // ResponseInfo.DEFAULT_CONTENT_ENCODING as the "nobody has an opinion" default. return (responseInfo.getDesiredContentWriterEncoding() == null) ? HttpUtils .determineCharsetFromContentType(responseInfo.getHeaders(), ResponseInfo.DEFAULT_CONTENT_ENCODING) : responseInfo.getDesiredContentWriterEncoding(); }
protected Charset determineCharsetToUse(ResponseInfo<?> responseInfo) { // Figure out what charset to use. If responseInfo has one specified via desiredContentWriterEncoding then use // that, otherwise attempt to extract it from the response headers, and use // ResponseInfo.DEFAULT_CONTENT_ENCODING as the "nobody has an opinion" default. return (responseInfo.getDesiredContentWriterEncoding() == null) ? HttpUtils .determineCharsetFromContentType(responseInfo.getHeaders(), ResponseInfo.DEFAULT_CONTENT_ENCODING) : responseInfo.getDesiredContentWriterEncoding(); }
@Test public void determineCharsetFromContentType_returns_default_if_passed_null_headers() { // given Charset defaultCharset = CharsetUtil.US_ASCII; // when Charset actualCharset = HttpUtils.determineCharsetFromContentType(null, defaultCharset); // then assertThat(actualCharset, is(defaultCharset)); }
@Test(expected = InvalidCharsetInContentTypeHeaderException.class) public void determineCharsetFromContentType_throws_InvalidCharsetInContentTypeHeaderException_if_passed_header_with_invalid_charset() { // given HttpHeaders headers = new DefaultHttpHeaders().add(HttpHeaders.Names.CONTENT_TYPE, "text/text charset=garbagio"); // expect HttpUtils.determineCharsetFromContentType(headers, CharsetUtil.UTF_8); fail("Expected an exception but none was thrown"); }
this.cookies = cookies; setPathParams(pathParams); this.contentCharset = HttpUtils.determineCharsetFromContentType(headers, DEFAULT_CONTENT_CHARSET); if (contentChunks != null) { contentChunks.forEach(this::addContentChunk);
@Test @DataProvider(value = { "text/text charset=US-ASCII | UTF-8 | US-ASCII", "text/text charset=us-ascii | UTF-8 | US-ASCII", "text/text | UTF-8 | UTF-8", " | UTF-8 | UTF-8", "null | UTF-8 | UTF-8", }, splitBy = "\\|") public void determineCharsetFromContentType_works(String contentTypeHeader, String defaultCharsetString, String expectedCharsetString) { // given Charset defaultCharset = Charset.forName(defaultCharsetString); Charset expectedCharset = Charset.forName(expectedCharsetString); HttpHeaders headers = new DefaultHttpHeaders().add(HttpHeaders.Names.CONTENT_TYPE, String.valueOf(contentTypeHeader)); // when Charset actualCharset = HttpUtils.determineCharsetFromContentType(headers, defaultCharset); // then assertThat(actualCharset, is(expectedCharset)); }