@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")); }
@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)); }
@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)); }
@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 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 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 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 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 testIsStringBodyNotMatch() { RecordedStringHttpBody stringHttpBody1 = new RecordedStringHttpBody("abc"); RecordedStringHttpBody stringHttpBody2 = new RecordedStringHttpBody("abcd"); RecordedHttpRequest recordedHttpRequest1 = new RecordedHttpRequest(null, null, null, stringHttpBody1); RecordedHttpRequest recordedHttpRequest2 = new RecordedHttpRequest(null, null, null, stringHttpBody2); MatchRule matchRule = new MatchBodyWithAnyBoundary(); Assert.assertFalse(matchRule.test(recordedHttpRequest1, recordedHttpRequest2)); }
@Test public void testIsStringBodyNotMatch() throws Exception { RecordedStringHttpBody stringHttpBody1 = new RecordedStringHttpBody("abc"); RecordedStringHttpBody stringHttpBody2 = new RecordedStringHttpBody("abcd"); RecordedHttpRequest recordedHttpRequest1 = new RecordedHttpRequest(null, null, null, stringHttpBody1); RecordedHttpRequest recordedHttpRequest2 = new RecordedHttpRequest(null, null, null, stringHttpBody2); MatchRule matchRule = new MatchBody(); Assert.assertFalse(matchRule.test(recordedHttpRequest1, recordedHttpRequest2)); }
@Test public void testIsStringBodyMatch() throws Exception { 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 MatchBody(); Assert.assertTrue(matchRule.test(recordedHttpRequest1, recordedHttpRequest2)); }
@Test public void testBodyMatchForDifferentRequestTypes() { RecordedHttpBody incomingHttpBody = 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(); RecordedHttpRequest recordedHttpRequest1 = new RecordedHttpRequest("POST", null, headers1, incomingHttpBody); RecordedHttpRequest recordedHttpRequest2 = new RecordedHttpRequest("GET", null, headers2, null); MatchRule matchRule = new MatchBodyWithAnyBoundary(); Assert.assertFalse(matchRule.test(recordedHttpRequest1, recordedHttpRequest2)); } }
@Test public void testCreateFromDecodedStringBody() throws Exception { String str = "Gaap is awesome"; byte[] content = str.getBytes(); byte[] compressedContent = new GzipCompressor().compress(content); RecordedStringHttpBody recordedStringHttpBody = new RecordedStringHttpBody(str); RecordedEncodedHttpBody recordedEncodedHttpBody = new RecordedEncodedHttpBody(recordedStringHttpBody, "gzip"); Assert.assertEquals(recordedEncodedHttpBody.getContent("UTF-8"), compressedContent); }
@Test public void testNullAndEmptyBodyMatch() { RecordedStringHttpBody stringHttpBody2 = new RecordedStringHttpBody(""); RecordedHttpRequest recordedHttpRequest1 = new RecordedHttpRequest(null, null, null, null); RecordedHttpRequest recordedHttpRequest2 = new RecordedHttpRequest(null, null, null, stringHttpBody2); MatchRule matchRule = new MatchBodyWithAnyBoundary(); Assert.assertTrue(matchRule.test(recordedHttpRequest1, recordedHttpRequest2)); }
@Test public void testNullAndEmptyBodyMatch() { RecordedStringHttpBody stringHttpBody2 = new RecordedStringHttpBody(""); RecordedHttpRequest recordedHttpRequest1 = new RecordedHttpRequest(null, null, null, null); RecordedHttpRequest recordedHttpRequest2 = new RecordedHttpRequest(null, null, null, stringHttpBody2); MatchRule matchRule = new MatchBody(); Assert.assertTrue(matchRule.test(recordedHttpRequest1, recordedHttpRequest2)); }
@Test public void testNullAndNonEmptyBodyNotMatch() { RecordedStringHttpBody stringHttpBody2 = new RecordedStringHttpBody("abc"); RecordedHttpRequest recordedHttpRequest1 = new RecordedHttpRequest(null, null, null, null); RecordedHttpRequest recordedHttpRequest2 = new RecordedHttpRequest(null, null, null, stringHttpBody2); MatchRule matchRule = new MatchBodyWithAnyBoundary(); Assert.assertFalse(matchRule.test(recordedHttpRequest1, recordedHttpRequest2)); }
@Test public void testNullAndNonEmptyBodyNotMatch() { RecordedStringHttpBody stringHttpBody2 = new RecordedStringHttpBody("abc"); RecordedHttpRequest recordedHttpRequest1 = new RecordedHttpRequest(null, null, null, null); RecordedHttpRequest recordedHttpRequest2 = new RecordedHttpRequest(null, null, null, stringHttpBody2); MatchRule matchRule = new MatchBody(); Assert.assertFalse(matchRule.test(recordedHttpRequest1, recordedHttpRequest2)); }