/** * Given content type, charset, construct concrete {@link com.linkedin.flashback.serializable.RecordedByteHttpBody} * @param contentType http body content type * @param contentEncoding http body content encoding * @param inputStream input stream from http request/response * @param charset charset * @return concrete {@link com.linkedin.flashback.serializable.RecordedHttpBody} * * */ public static RecordedHttpBody create(String contentType, String contentEncoding, final InputStream inputStream, final String charset) throws IOException { if (HttpUtilities.isCompressedContentEncoding(contentEncoding)) { return new RecordedEncodedHttpBody(IOUtils.toByteArray(inputStream), contentEncoding, charset, contentType); } else if (HttpUtilities.isTextContentType(contentType)) { return new RecordedStringHttpBody(IOUtils.toString(inputStream, charset)); } else { return new RecordedByteHttpBody(IOUtils.toByteArray(inputStream)); } } }
@Test(expectedExceptions = RuntimeException.class) public void testGetByteArrayUnsupportEncoding() throws Exception { String str = "Hello world"; RecordedStringHttpBody recordedStringHttpBody = new RecordedStringHttpBody(str); recordedStringHttpBody.getContent("UNKNOWN"); } }
@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)"; } }
/** * Constructor used to create a RecordedEncodedHttpBody instance from the encoded (wire) bytes * * @param encodedContent the already encoded content, representing the bytes transferred over the wire * @param encodingName name of the encoding used to encode the content * @param charset charset * @param contentType http Content-Type for the content (once decoded) * @throws java.io.IOException */ public RecordedEncodedHttpBody(byte[] encodedContent, String encodingName, String charset, String contentType) throws IOException { _encodedContent = encodedContent; _encodingName = encodingName; byte[] decodedContent = getDecompressor().decompress(encodedContent); if (HttpUtilities.isTextContentType(contentType)) { _decodedBody = new RecordedStringHttpBody(new String(decodedContent, charset)); } else { _decodedBody = new RecordedByteHttpBody(decodedContent); } }
@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")); }
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 public void testDifferentParameters() throws Exception { RecordedStringHttpBody stringHttpBody1 = new RecordedStringHttpBody("a=a&b=b&c=c"); RecordedStringHttpBody stringHttpBody2 = new RecordedStringHttpBody("a=a&b=b&c=ccc"); Multimap<String, String> headers = LinkedHashMultimap.create(); headers.put("Content-Type", "application/x-www-form-urlencoded"); RecordedHttpRequest recordedHttpRequest1 = new RecordedHttpRequest(null, null, headers, stringHttpBody1); RecordedHttpRequest recordedHttpRequest2 = new RecordedHttpRequest(null, null, headers, stringHttpBody2); MatchRule matchRule = new MatchBodyPostParameters(); Assert.assertFalse(matchRule.test(recordedHttpRequest1, recordedHttpRequest2)); }
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 testBodyMatchForMultipartDataWithDifferentBoundary() { RecordedHttpBody stringHttpBody1 = new RecordedStringHttpBody("------wxyz1234abcd5e\nContent-Disposition: form-data; name=\"org\" \nMMM\n------wxyz1234abcd5e"); RecordedHttpBody stringHttpBody2 = new RecordedStringHttpBody("------abcd5678wxyz4v\nContent-Disposition: form-data; name=\"org\" \nMMM\n------abcd5678wxyz4v"); Multimap<String, String> headers1 = LinkedHashMultimap.create(); headers1.put(HttpHeaders.CONTENT_TYPE, "multipart/form-data; boundary=wxyz1234abcd5e"); Multimap<String, String> headers2 = LinkedHashMultimap.create(); headers2.put(HttpHeaders.CONTENT_TYPE, "multipart/form-data; boundary=abcd5678wxyz4v"); RecordedHttpRequest recordedHttpRequest1 = new RecordedHttpRequest(null, null, headers1, stringHttpBody1); RecordedHttpRequest recordedHttpRequest2 = new RecordedHttpRequest(null, null, headers2, stringHttpBody2); MatchRule matchRule = new MatchBodyWithAnyBoundary(); Assert.assertTrue(matchRule.test(recordedHttpRequest1, recordedHttpRequest2)); }
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 testMatchWithSomeNullParameters() throws Exception { RecordedStringHttpBody stringHttpBody1 = new RecordedStringHttpBody("a=a&b=&c=c&d="); RecordedStringHttpBody stringHttpBody2 = new RecordedStringHttpBody("a=a&b=&c=c&d="); Multimap<String, String> headers = LinkedHashMultimap.create(); headers.put("Content-Type", "application/x-www-form-urlencoded"); RecordedHttpRequest recordedHttpRequest1 = new RecordedHttpRequest(null, null, headers, stringHttpBody1); RecordedHttpRequest recordedHttpRequest2 = new RecordedHttpRequest(null, null, headers, stringHttpBody2); MatchRule matchRule = new MatchBodyPostParameters(); Assert.assertTrue(matchRule.test(recordedHttpRequest1, recordedHttpRequest2)); } }
@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 testIsStringBodyNotMatchWithDiffCharset() { RecordedStringHttpBody stringHttpBody1 = new RecordedStringHttpBody("造字"); RecordedStringHttpBody stringHttpBody2 = new RecordedStringHttpBody("造字"); Multimap<String, String> headers1 = LinkedHashMultimap.create(); headers1.put(HttpHeaders.CONTENT_TYPE, "text/html; charset=euc-kr"); Multimap<String, String> headers2 = LinkedHashMultimap.create(); headers2.put(HttpHeaders.CONTENT_TYPE, "text/html; charset=big5"); RecordedHttpRequest recordedHttpRequest1 = new RecordedHttpRequest(null, null, headers1, stringHttpBody1); RecordedHttpRequest recordedHttpRequest2 = new RecordedHttpRequest(null, null, headers2, stringHttpBody2); MatchRule matchRule = new MatchBodyWithAnyBoundary(); Assert.assertFalse(matchRule.test(recordedHttpRequest1, recordedHttpRequest2)); }
@Test public void testPostParameterWhitelistMatch() throws Exception { RecordedStringHttpBody stringHttpBody1 = new RecordedStringHttpBody("a=a&b=b&c=c"); RecordedStringHttpBody stringHttpBody2 = new RecordedStringHttpBody("a=a&b=x&c=c&d=d"); Multimap<String, String> headers = LinkedHashMultimap.create(); headers.put("Content-Type", "application/x-www-form-urlencoded"); Set<String> whitelist = new HashSet<>(); whitelist.add("a"); whitelist.add("c"); RecordedHttpRequest recordedHttpRequest1 = new RecordedHttpRequest(null, null, headers, stringHttpBody1); RecordedHttpRequest recordedHttpRequest2 = new RecordedHttpRequest(null, null, headers, stringHttpBody2); MatchRule matchRule = MatchRuleUtils.matchBodyPostParametersWithWhitelist(whitelist); Assert.assertTrue(matchRule.test(recordedHttpRequest1, recordedHttpRequest2)); }
@Test public void testIsStringBodyMatch() { RecordedStringHttpBody stringHttpBody1 = new RecordedStringHttpBody("abc"); RecordedStringHttpBody stringHttpBody2 = new RecordedStringHttpBody("abc"); RecordedHttpRequest recordedHttpRequest1 = new RecordedHttpRequest(null, null, null, stringHttpBody1); RecordedHttpRequest recordedHttpRequest2 = new RecordedHttpRequest(null, null, null, stringHttpBody2); MatchRule matchRule = new MatchBodyWithAnyBoundary(); Assert.assertTrue(matchRule.test(recordedHttpRequest1, recordedHttpRequest2)); }
@Test public void testBodyMatchForMultipartDataWithSameBoundary() { RecordedHttpBody incomingHttpBody = new RecordedStringHttpBody("------wxyz1234abcd5e\nContent-Disposition: form-data; name=\"org\" \nMMM\n------wxyz1234abcd5e"); RecordedHttpBody expectedHttpBody = new RecordedStringHttpBody("------wxyz1234abcd5e\nContent-Disposition: form-data; name=\"org\" \nMMM\n------wxyz1234abcd5e"); Multimap<String, String> headers1 = LinkedHashMultimap.create(); headers1.put(HttpHeaders.CONTENT_TYPE, "multipart/form-data; boundary=wxyz1234abcd5e"); Multimap<String, String> headers2 = LinkedHashMultimap.create(); headers2.put(HttpHeaders.CONTENT_TYPE, "multipart/form-data; boundary=wxyz1234abcd5e"); RecordedHttpRequest recordedHttpRequest1 = new RecordedHttpRequest(null, null, headers1, incomingHttpBody); RecordedHttpRequest recordedHttpRequest2 = new RecordedHttpRequest(null, null, headers2, expectedHttpBody); MatchRule matchRule = new MatchBodyWithAnyBoundary(); Assert.assertTrue(matchRule.test(recordedHttpRequest1, recordedHttpRequest2)); }
@Test public void testExactMatch() throws Exception { RecordedStringHttpBody stringHttpBody1 = new RecordedStringHttpBody("a=a&b=b&c=c"); RecordedStringHttpBody stringHttpBody2 = new RecordedStringHttpBody("a=a&b=b&c=c"); Multimap<String, String> headers = LinkedHashMultimap.create(); headers.put("Content-Type", "application/x-www-form-urlencoded"); RecordedHttpRequest recordedHttpRequest1 = new RecordedHttpRequest(null, null, headers, stringHttpBody1); RecordedHttpRequest recordedHttpRequest2 = new RecordedHttpRequest(null, null, headers, stringHttpBody2); MatchRule matchRule = new MatchBodyPostParameters(); Assert.assertTrue(matchRule.test(recordedHttpRequest1, recordedHttpRequest2)); }
@Test public void testNotMatchDifferentOrder() throws Exception { RecordedStringHttpBody stringHttpBody1 = new RecordedStringHttpBody("a=a&b=b&c=c"); RecordedStringHttpBody stringHttpBody2 = new RecordedStringHttpBody("a=a&c=c&b=b"); Multimap<String, String> headers = LinkedHashMultimap.create(); headers.put("Content-Type", "application/x-www-form-urlencoded"); RecordedHttpRequest recordedHttpRequest1 = new RecordedHttpRequest(null, null, headers, stringHttpBody1); RecordedHttpRequest recordedHttpRequest2 = new RecordedHttpRequest(null, null, headers, stringHttpBody2); MatchRule matchRule = new MatchBodyPostParameters(); Assert.assertFalse(matchRule.test(recordedHttpRequest1, recordedHttpRequest2)); }
@Test public void testCookieHeader() throws URISyntaxException, IOException { Multimap<String, String> headers = LinkedHashMultimap.create(); headers.put("key1", "value1"); headers.put("Set-Cookie", "YSxiLGM=, ZCxlLGY="); int status = 200; String str = "Hello world"; RecordedStringHttpBody recordedStringHttpBody = new RecordedStringHttpBody(str); RecordedHttpResponse recordedHttpResponse = new RecordedHttpResponse(status, headers, recordedStringHttpBody); FullHttpResponse fullHttpResponse = NettyHttpResponseMapper.from(recordedHttpResponse); Assert.assertEquals(fullHttpResponse.getStatus().code(), status); Assert.assertEquals(fullHttpResponse.headers().get("key1"), "value1"); List<String> headrValues = fullHttpResponse.headers().getAll("Set-Cookie"); Assert.assertEquals(headrValues.size(), 1); Assert.assertTrue(headrValues.contains("YSxiLGM=, ZCxlLGY=")); }