public static String convertContentChunksToRawString(Charset contentCharset, Collection<HttpContent> contentChunks) { byte[] rawBytes = convertContentChunksToRawBytes(contentChunks); if (rawBytes == null) return null; return convertRawBytesToString(contentCharset, rawBytes); }
/** * {@inheritDoc} */ @Override public synchronized String getRawContent() { if (!isCompleteRequestWithAllChunks) return null; if (rawContent != null) return rawContent; // The raw content string has not been loaded/cached yet. Do that now. rawContent = HttpUtils.convertRawBytesToString(getContentCharset(), getRawContentBytes()); return rawContent; }
@Test public void convertRawBytesToString_removes_byte_order_marks() { // given String uuid1String = UUID.randomUUID().toString(); String uuid2String = UUID.randomUUID().toString(); String contentWithBOM = uuid1String + "\uFEFF" + uuid2String + "\uFEFF"; // when String result = HttpUtils.convertRawBytesToString(CharsetUtil.UTF_8, contentWithBOM.getBytes(CharsetUtil.UTF_8)); // then assertThat(result, is(uuid1String + uuid2String)); }
@Test public void convertRawBytesToString_returns_null_when_byte_array_is_empty() { // expect assertThat(HttpUtils.convertRawBytesToString(CharsetUtil.UTF_8, new byte[0]), is("")); }
@Test(expected = IllegalArgumentException.class) public void convertRawBytesToString_throws_IllegalArgumentException_if_contentCharset_is_null() { // expect HttpUtils.convertRawBytesToString(null, new byte[0]); fail("Expected IllegalArgumentException but no exception was thrown"); }
@Test public void convertRawBytesToString_returns_null_when_byte_array_is_null() { // expect assertThat(HttpUtils.convertRawBytesToString(CharsetUtil.UTF_8, null), nullValue()); }
@Test @DataProvider(value = { "UTF-8", "UTF-16", "ISO-8859-1" }, splitBy = "\\|") public void convertRawBytesToString_works(String charsetString) { // given Charset contentCharset = Charset.forName(charsetString); String uuidString = UUID.randomUUID().toString(); byte[] rawBytes = uuidString.getBytes(contentCharset); // when String result = HttpUtils.convertRawBytesToString(contentCharset, rawBytes); // then assertThat(result, is(uuidString)); }