private RecordedHttpBody getBodyFromRequest(RecordedHttpRequest request) { RecordedHttpBody body = request.getHttpBody(); if (body instanceof RecordedEncodedHttpBody) { return ((RecordedEncodedHttpBody) body).getDecodedBody(); } else { return body; } } }
private RecordedHttpBody getBodyFromRequest(RecordedHttpRequest request) { RecordedHttpBody body = request.getHttpBody(); if (body instanceof RecordedEncodedHttpBody) { return ((RecordedEncodedHttpBody) body).getDecodedBody(); } else { return body; } } }
public static FullHttpResponse generateNoMatchResponse(RecordedHttpRequest recordedHttpRequest) { StringBuilder bodyTextBuilder = new StringBuilder(); bodyTextBuilder.append("No Matching Request\n").append("Incoming Request Method: ") .append(recordedHttpRequest.getMethod()).append("\n").append("Incoming Request URI: ") .append(recordedHttpRequest.getUri()).append("\n").append("Incoming Request Headers: ") .append(recordedHttpRequest.getHeaders()).append("\n"); RecordedHttpBody incomingBody = recordedHttpRequest.getHttpBody(); if (incomingBody != null) { if (incomingBody instanceof RecordedEncodedHttpBody) { incomingBody = ((RecordedEncodedHttpBody) incomingBody).getDecodedBody(); } if (incomingBody instanceof RecordedStringHttpBody) { bodyTextBuilder.append("Incoming Request Body: ").append(((RecordedStringHttpBody) incomingBody).getContent()); } else { bodyTextBuilder.append("Incoming Request Body: (binary content)"); } } ByteBuf badRequestBody = Unpooled.wrappedBuffer(bodyTextBuilder.toString().getBytes(Charset.forName("UTF-8"))); return new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.BAD_REQUEST, badRequestBody); }
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()); } } }
private Map<String, String> getPostParametersFromRequest(RecordedHttpRequest request) throws UnsupportedEncodingException { RecordedHttpBody body = request.getHttpBody(); if (body instanceof RecordedEncodedHttpBody) { body = ((RecordedEncodedHttpBody) body).getDecodedBody(); } assert (body instanceof RecordedStringHttpBody); String content = ((RecordedStringHttpBody) body).getContent(); return HttpUtilities.stringToUrlParams(content, request.getCharset()); }
@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 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); } }