public RecordedHttpMessage(Multimap<String, String> headers, RecordedHttpBody httpBody) { if (headers != null) { _headers = headers; } _httpBody = httpBody; // Update the Content-Length header if appropriate if (_headers.containsKey(HttpHeaders.CONTENT_LENGTH)) { try { int contentLength = _httpBody.getContent(getCharset()).length; _headers = LinkedHashMultimap.create(_headers); _headers.put(HttpHeaders.CONTENT_LENGTH, Integer.toString(contentLength)); } catch (IOException e) { logger.error("Caught exception " + e + " while updating Content-Length header"); } } }
private static byte[] createHttpBodyBytes(RecordedHttpResponse recordedHttpResponse) throws IOException { return recordedHttpResponse.getHttpBody().getContent(recordedHttpResponse.getCharset()); } }
/** * Returns the bytes meant to be sent over the wire. If necessary, this method * will encode the underlying http body content and cache the result so that * we don't have to do this more than once. * * @param charSet charset * @return the (encoded) content for the wire * @throws java.io.IOException */ @Override public byte[] getContent(String charSet) throws IOException { if (_encodedContent == null) { // Only compress the content once and cache the result _encodedContent = getCompressor().compress(_decodedBody.getContent(charSet)); } return _encodedContent; }
@Test public void testCreateStringHttpBody() throws IOException { String str = "Hello world"; byte[] content = str.getBytes(); int size = content.length; InputStream is = new ByteArrayInputStream(content); RecordedHttpBody recordedHttpBody = RecordedHttpBodyFactory.create("text/html", "identity", is, "UTF-8"); Assert.assertTrue(recordedHttpBody instanceof RecordedStringHttpBody); Assert.assertFalse(recordedHttpBody instanceof RecordedByteHttpBody); Assert.assertEquals(size, recordedHttpBody.getContent("UTF-8").length); }
@Test public void testCreateByteHttpBody() throws IOException { String str = "Hello world"; byte[] content = str.getBytes(); InputStream is = new ByteArrayInputStream(content); RecordedHttpBody recordedHttpBody = RecordedHttpBodyFactory.create("image/gif", "identity", is, "UTF-8"); Assert.assertTrue(recordedHttpBody instanceof RecordedByteHttpBody); Assert.assertFalse(recordedHttpBody instanceof RecordedStringHttpBody); Assert.assertEquals(content, recordedHttpBody.getContent("UTF-8")); }
String recordedBody = new String(expectedBody.getContent(charSet2)).replaceAll(recordedRequestBoundaryValue, currentRequestBoundaryValue); expectedBody = new RecordedByteHttpBody(recordedBody.getBytes(charSet2)); return Arrays.equals(incomingBody.getContent(charSet1), expectedBody.getContent(charSet2)); } catch (IOException e) { throw new RuntimeException("Failed to convert to byte arrays", e);
@Override public boolean test(RecordedHttpRequest incomingRequest, RecordedHttpRequest expectedRequest) { String charSet1 = incomingRequest.getCharset(); String charSet2 = expectedRequest.getCharset(); RecordedHttpBody incomingBody = incomingRequest.getHttpBody(); if (incomingBody == null) { incomingBody = new RecordedByteHttpBody(new byte[0]); } RecordedHttpBody expectedBody = expectedRequest.getHttpBody(); if (expectedBody == null) { expectedBody = new RecordedByteHttpBody(new byte[0]); } try { return Arrays.equals(incomingBody.getContent(charSet1), expectedBody.getContent(charSet2)); } catch (IOException e) { //TODO: PLACEHOLDER, error handling will be in separate RB. throw new RuntimeException("Failed to convert to byte arrays", e); } }
@Test public void testCreateGZipEncodedStringHttpBody() throws IOException { String str = "Hello world. This is some extra text to make the string longer so that gzip makes it smaller"; byte[] content = str.getBytes(); byte[] compressedContent = new GzipCompressor().compress(content); InputStream is = new ByteArrayInputStream(compressedContent); RecordedHttpBody recordedHttpBody = RecordedHttpBodyFactory.create("text/html", "gzip", is, "UTF-8"); Assert.assertTrue(recordedHttpBody instanceof RecordedEncodedHttpBody); RecordedEncodedHttpBody encodedBody = (RecordedEncodedHttpBody) recordedHttpBody; Assert.assertEquals(encodedBody.getEncodingName(), "gzip"); Assert.assertTrue(encodedBody.getDecodedBody() instanceof RecordedStringHttpBody); Assert.assertEquals(encodedBody.getDecodedBody().getContent("UTF-8"), content); Assert.assertEquals(encodedBody.getContent("UTF-8"), compressedContent); }
@Test public void testCreateDeflateEncodedStringHttpBody() throws IOException { String str = "Hello world. This is some extra text to make the string longer so that deflate makes it smaller"; byte[] content = str.getBytes(); byte[] compressedContent = new DeflateCompressor().compress(content); InputStream is = new ByteArrayInputStream(compressedContent); RecordedHttpBody recordedHttpBody = RecordedHttpBodyFactory.create("text/html", "deflate", is, "UTF-8"); Assert.assertTrue(recordedHttpBody instanceof RecordedEncodedHttpBody); RecordedEncodedHttpBody encodedBody = (RecordedEncodedHttpBody) recordedHttpBody; Assert.assertEquals(encodedBody.getEncodingName(), "deflate"); Assert.assertTrue(encodedBody.getDecodedBody() instanceof RecordedStringHttpBody); Assert.assertEquals(encodedBody.getDecodedBody().getContent("UTF-8"), content); Assert.assertEquals(encodedBody.getContent("UTF-8"), compressedContent); } }
@Test public void testBuild() throws IOException { HttpResponse httpResponse = new DefaultFullHttpResponse(HttpVersion.HTTP_1_0, HttpResponseStatus.GATEWAY_TIMEOUT); RecordedHttpResponseBuilder recordedHttpResponseBuilder = new RecordedHttpResponseBuilder(httpResponse); String charset = "UTF-8"; String str1 = "Hello world"; HttpContent httpContent1 = new DefaultHttpContent(Unpooled.copiedBuffer(str1.getBytes(charset))); recordedHttpResponseBuilder.appendHttpContent(httpContent1); String str2 = "second content"; HttpContent httpContent2 = new DefaultHttpContent(Unpooled.copiedBuffer(str2.getBytes(charset))); recordedHttpResponseBuilder.appendHttpContent(httpContent2); String lastStr = "Last chunk"; HttpContent lastContent = new DefaultLastHttpContent(Unpooled.copiedBuffer(lastStr.getBytes(charset))); recordedHttpResponseBuilder.appendHttpContent(lastContent); RecordedHttpResponse recordedHttpResponse = recordedHttpResponseBuilder.build(); Assert.assertEquals(recordedHttpResponse.getStatus(), HttpResponseStatus.GATEWAY_TIMEOUT.code()); Assert.assertEquals((str1 + str2 + lastStr).getBytes(charset), recordedHttpResponse.getHttpBody().getContent(charset)); } }
@Test public void testBuildContent() throws Exception { HttpRequest nettyRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_0, HttpMethod.GET, "www.google.com"); RecordedHttpRequestBuilder recordedHttpRequestBuilder = new RecordedHttpRequestBuilder(nettyRequest); String charset = "UTF-8"; String str1 = "first content"; HttpContent httpContent1 = new DefaultHttpContent(Unpooled.copiedBuffer(str1.getBytes(charset))); recordedHttpRequestBuilder.appendHttpContent(httpContent1); String str2 = "second content"; HttpContent httpContent2 = new DefaultHttpContent(Unpooled.copiedBuffer(str2.getBytes(charset))); recordedHttpRequestBuilder.appendHttpContent(httpContent2); String lastStr = "Last chunk"; HttpContent lastContent = new DefaultLastHttpContent(Unpooled.copiedBuffer(lastStr.getBytes(charset))); recordedHttpRequestBuilder.appendHttpContent(lastContent); RecordedHttpRequest recordedHttpRequest = recordedHttpRequestBuilder.build(); Assert .assertEquals((str1 + str2 + lastStr).getBytes(charset), recordedHttpRequest.getHttpBody().getContent(charset)); } }