@Override public String getMatchFailureDescriptionForRequests(RecordedHttpRequest incomingRequest, RecordedHttpRequest expectedRequest) { RecordedHttpBody incomingBody = getBodyFromRequest(incomingRequest); RecordedHttpBody expectedBody = getBodyFromRequest(expectedRequest); if (incomingBody instanceof RecordedStringHttpBody && expectedBody instanceof RecordedStringHttpBody) { return String.format("HTTP Body Mismatch\nIncoming Body: %s\nExpected Body: %s\n", ((RecordedStringHttpBody) incomingBody).getContent(), ((RecordedStringHttpBody) expectedBody).getContent()); } else { return "HTTP Body Mismatch (binary bodies differ)"; } }
@Override public String getMatchFailureDescriptionForRequests(RecordedHttpRequest incomingRequest, RecordedHttpRequest expectedRequest) { RecordedHttpBody incomingBody = getBodyFromRequest(incomingRequest); RecordedHttpBody expectedBody = getBodyFromRequest(expectedRequest); if (incomingBody instanceof RecordedStringHttpBody && expectedBody instanceof RecordedStringHttpBody) { return String.format("HTTP Body Mismatch%nIncoming Body: %s%nExpected Body: %s%n", ((RecordedStringHttpBody) incomingBody).getContent(), ((RecordedStringHttpBody) expectedBody).getContent()); } else { return "HTTP Body Mismatch (binary bodies differ)"; } }
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); }
@Test(expectedExceptions = RuntimeException.class) public void testGetByteArrayUnsupportEncoding() throws Exception { String str = "Hello world"; RecordedStringHttpBody recordedStringHttpBody = new RecordedStringHttpBody(str); recordedStringHttpBody.getContent("UNKNOWN"); } }
@Test public void testGetByteArray() throws Exception { String str = "Hello world"; byte[] content = str.getBytes(); RecordedStringHttpBody recordedStringHttpBody = new RecordedStringHttpBody(str); Assert.assertEquals(content, recordedStringHttpBody.getContent("UTF-8")); }
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 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); } }