private void writeHttpBody(RecordedHttpBody httpBody) throws IOException { if (httpBody instanceof RecordedEncodedHttpBody) { _jsonGenerator.writeObjectFieldStart(SceneSerializationConstant.SCENE_TAG_ENCODED_HTTPBODY); _jsonGenerator.writeStringField(SceneSerializationConstant.SCENE_TAG_HTTPBODY_ENCODING, ((RecordedEncodedHttpBody) httpBody).getEncodingName()); writeHttpBody(((RecordedEncodedHttpBody) httpBody).getDecodedBody()); _jsonGenerator.writeEndObject(); } else if (httpBody instanceof RecordedStringHttpBody) { _jsonGenerator.writeStringField(SceneSerializationConstant.SCENE_TAG_STRING_HTTPBODY, ((RecordedStringHttpBody) httpBody).getContent()); } else { _jsonGenerator.writeBinaryField(SceneSerializationConstant.SCENE_TAG_BINARY_HTTPBODY, ((RecordedByteHttpBody) httpBody).getContent()); } } }
@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 testCreateFromEncodedBytes() throws Exception { String str = "Gaap is awesome"; byte[] content = str.getBytes(); byte[] compressedContent = new GzipCompressor().compress(content); RecordedEncodedHttpBody recordedEncodedHttpBody = new RecordedEncodedHttpBody(compressedContent, "gzip", "UTF-8", "text/html"); Assert.assertEquals(recordedEncodedHttpBody.getContent("UTF-8"), compressedContent); Assert.assertEquals(recordedEncodedHttpBody.getEncodingName(), "gzip"); RecordedHttpBody decodedBody = recordedEncodedHttpBody.getDecodedBody(); Assert.assertTrue(decodedBody instanceof RecordedStringHttpBody); Assert.assertEquals(((RecordedStringHttpBody) decodedBody).getContent(), str); } }
@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); } }