@Test public void testEntireRequestMatchNoBody() throws Exception { Multimap<String, String> headers = LinkedHashMultimap.create(); headers.put("key1", "value1"); headers.put("key2", "value2"); RecordedHttpRequest recordedHttpRequest1 = new RecordedHttpRequest("POST", new URI("http://www.google.com/"), headers, null); RecordedHttpRequest recordedHttpRequest2 = new RecordedHttpRequest("POST", new URI("http://www.google.com/"), headers, null); MatchRule entireRequestRule = MatchRuleUtils.matchEntireRequest(); MatchRule methodUriRule = MatchRuleUtils.matchMethodUri(); Assert.assertTrue(entireRequestRule.test(recordedHttpRequest1, recordedHttpRequest2)); Assert.assertTrue(methodUriRule.test(recordedHttpRequest1, recordedHttpRequest2)); }
public static MatchRule fromString(String predefinedMatchRule) { if (MATCH_ENTIRE_REQUEST._text.equalsIgnoreCase(predefinedMatchRule)) { return MatchRuleUtils.matchEntireRequest(); } if (MATCH_METHOD_URI._text.equalsIgnoreCase(predefinedMatchRule)) { return MatchRuleUtils.matchMethodUri(); } if (MATCH_METHOD_BODY_URI._text.equalsIgnoreCase(predefinedMatchRule)) { return MatchRuleUtils.matchMethodBodyUri(); } if (MATCH_METHOD_URI_BODY_WITHOUT_BOUNDARY._text.equalsIgnoreCase(predefinedMatchRule)) { return MatchRuleUtils.matchMethodUriBodyWithAnyBoundary(); } return null; } }
@Test public void testInsertion() { Set<MatchRule> matchRuleSet = new HashSet<>(); matchRuleSet.add(new MatchBody()); Assert.assertEquals(matchRuleSet.size(), 1); matchRuleSet.add(new MatchBody()); Assert.assertEquals(matchRuleSet.size(), 1); matchRuleSet.add(new MatchHeaders()); matchRuleSet.add(MatchRuleUtils.matchHeadersWithBlacklist(null)); matchRuleSet.add(MatchRuleUtils.matchHeadersWithWhitelist(null)); Assert.assertEquals(matchRuleSet.size(), 4); }
@Test public void testNotMatchUrl() throws IOException, InterruptedException { URL flashbackScene = getClass().getResource(FLASHBACK_SCENE_DIR); String rootPath = flashbackScene.getPath(); SceneConfiguration sceneConfiguration = new SceneConfiguration(rootPath, SCENE_MODE, HTTP_SCENE); try (FlashbackRunner flashbackRunner = new FlashbackRunner.Builder().mode(SCENE_MODE) .sceneAccessLayer( new SceneAccessLayer(SceneFactory.create(sceneConfiguration), MatchRuleUtils.matchEntireRequest())) .build()) { flashbackRunner.start(); HttpHost host = new HttpHost(PROXY_HOST, PROXY_PORT); String url = "http://www.notexist.org/"; HttpClient client = HttpClientBuilder.create().setProxy(host).build(); HttpGet request = new HttpGet(url); HttpResponse httpResponse = client.execute(request); Assert.assertEquals(httpResponse.getStatusLine().getStatusCode(), 400); Assert.assertTrue(EntityUtils.toString(httpResponse.getEntity()) .contains("No Matching Request")); url = "http://www.example.org/"; request = new HttpGet(url); request.addHeader("a", "b"); httpResponse = client.execute(request); Assert.assertEquals(httpResponse.getStatusLine().getStatusCode(), 400); Assert.assertTrue(EntityUtils.toString(httpResponse.getEntity()) .contains("No Matching Request")); } }
@Test public void testMethodUriBodyWithAnyBoundaryMatchForNoBody() throws Exception { Multimap<String, String> headers = LinkedHashMultimap.create(); headers.put("key1", "value1"); headers.put("Content-Type", "multipart/form-data; boundary=wxyz1234abcd5e"); RecordedHttpRequest recordedHttpRequest1 = new RecordedHttpRequest("POST", new URI("http://www.google.com/"), headers, null); RecordedHttpRequest recordedHttpRequest2 = new RecordedHttpRequest("POST", new URI("http://www.google.com/"), headers, null); MatchRule matchRule = MatchRuleUtils.matchMethodUriBodyWithAnyBoundary(); Assert.assertTrue(matchRule.test(recordedHttpRequest1, recordedHttpRequest2)); }
@Test public void testUriBlacklistExactMatch() throws Exception { RecordedHttpRequest recordedHttpRequest1 = new RecordedHttpRequest(null, new URI("http://www.google.com/?a=a"), null, null); RecordedHttpRequest recordedHttpRequest2 = new RecordedHttpRequest(null, new URI("http://www.google.com/?a=a"), null, null); MatchRule matchRule = MatchRuleUtils.matchUriWithQueryBlacklist(new HashSet<>()); Assert.assertTrue(matchRule.test(recordedHttpRequest1, recordedHttpRequest2)); }
@Test public void testHeaderBlacklistMatchNullBL() { Multimap<String, String> headers1 = LinkedHashMultimap.create(); headers1.put("key1", "value1"); headers1.put("key2", "value2"); RecordedHttpRequest recordedHttpRequest1 = new RecordedHttpRequest(null, null, headers1, null); Multimap<String, String> headers2 = LinkedHashMultimap.create(); headers2.put("key1", "value1"); headers2.put("key2", "value2"); RecordedHttpRequest recordedHttpRequest2 = new RecordedHttpRequest(null, null, headers2, null); MatchRule matchRule = MatchRuleUtils.matchHeadersWithBlacklist(null); Assert.assertTrue(matchRule.test(recordedHttpRequest1, recordedHttpRequest2)); }
@Test public void testHeaderWhitelistMatchWithNoneInWL() throws Exception { Multimap<String, String> headers1 = LinkedHashMultimap.create(); headers1.put("key3", "value3"); RecordedHttpRequest recordedHttpRequest1 = new RecordedHttpRequest(null, null, headers1, null); Multimap<String, String> headers2 = LinkedHashMultimap.create(); headers2.put("key2", "value2"); headers2.put("key3", "value3"); RecordedHttpRequest recordedHttpRequest2 = new RecordedHttpRequest(null, null, headers2, null); Set<String> wList = new HashSet<>(); wList.add("key1"); MatchRule matchRule = MatchRuleUtils.matchHeadersWithWhitelist(wList); 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 testPostParameterBlacklistMatch() 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> blackList = new HashSet<>(); blackList.add("b"); blackList.add("d"); RecordedHttpRequest recordedHttpRequest1 = new RecordedHttpRequest(null, null, headers, stringHttpBody1); RecordedHttpRequest recordedHttpRequest2 = new RecordedHttpRequest(null, null, headers, stringHttpBody2); MatchRule matchRule = MatchRuleUtils.matchBodyPostParametersWithBlacklist(blackList); Assert.assertTrue(matchRule.test(recordedHttpRequest1, recordedHttpRequest2)); }
@Test public void testNotMatchMethod() throws IOException, InterruptedException { URL flashbackScene = getClass().getResource(FLASHBACK_SCENE_DIR); String rootPath = flashbackScene.getPath(); SceneConfiguration sceneConfiguration = new SceneConfiguration(rootPath, SCENE_MODE, HTTP_SCENE); try (FlashbackRunner flashbackRunner = new FlashbackRunner.Builder().mode(SCENE_MODE) .sceneAccessLayer( new SceneAccessLayer(SceneFactory.create(sceneConfiguration), MatchRuleUtils.matchEntireRequest())) .build()) { flashbackRunner.start(); HttpHost host = new HttpHost(PROXY_HOST, PROXY_PORT); String url = "http://www.example.org/"; HttpClient client = HttpClientBuilder.create().setProxy(host).build(); HttpPost post = new HttpPost(url); HttpResponse httpResponse = client.execute(post); Assert.assertEquals(httpResponse.getStatusLine().getStatusCode(), 400); Assert.assertTrue(EntityUtils.toString(httpResponse.getEntity()) .contains("No Matching Request")); } }
@Test public void testMethodUriBodyWithAnyBoundaryMatch() throws Exception { Multimap<String, String> headers = LinkedHashMultimap.create(); headers.put("key1", "value1"); headers.put("Content-Type", "multipart/form-data; boundary=wxyz1234abcd5e"); RecordedHttpBody body = new RecordedStringHttpBody("------wxyz1234abcd5e\nContent-Disposition: form-data; name=\"org\" \ngoogle\n------wxyz1234abcd5e"); RecordedHttpRequest recordedHttpRequest1 = new RecordedHttpRequest("POST", new URI("http://www.google.com/"), headers, body); RecordedHttpRequest recordedHttpRequest2 = new RecordedHttpRequest("POST", new URI("http://www.google.com/"), headers, body); MatchRule matchRule = MatchRuleUtils.matchMethodUriBodyWithAnyBoundary(); Assert.assertTrue(matchRule.test(recordedHttpRequest1, recordedHttpRequest2)); }
@Test public void testUriBlacklistMatch() throws Exception { RecordedHttpRequest recordedHttpRequest1 = new RecordedHttpRequest(null, new URI("http://www.google.com/?a=a&b=b&c=c"), null, null); RecordedHttpRequest recordedHttpRequest2 = new RecordedHttpRequest(null, new URI("http://www.google.com/?a=a&b=z&c=c"), null, null); HashSet<String> blacklist = new HashSet<>(); blacklist.add("b"); blacklist.add("c"); blacklist.add("d"); MatchRule matchRule = MatchRuleUtils.matchUriWithQueryBlacklist(blacklist); Assert.assertTrue(matchRule.test(recordedHttpRequest1, recordedHttpRequest2)); }
@Test public void testHeaderBlacklistMatchEmptyBL() throws Exception { Multimap<String, String> headers1 = LinkedHashMultimap.create(); headers1.put("key1", "value1"); headers1.put("key2", "value2"); RecordedHttpRequest recordedHttpRequest1 = new RecordedHttpRequest(null, null, headers1, null); Multimap<String, String> headers2 = LinkedHashMultimap.create(); headers2.put("key1", "value1"); headers2.put("key2", "value2"); RecordedHttpRequest recordedHttpRequest2 = new RecordedHttpRequest(null, null, headers2, null); Set<String> blList = new HashSet<>(); MatchRule matchRule = MatchRuleUtils.matchHeadersWithBlacklist(blList); Assert.assertTrue(matchRule.test(recordedHttpRequest1, recordedHttpRequest2)); }
@Test public void testHeaderWhitelistMatch() throws Exception { Multimap<String, String> headers1 = LinkedHashMultimap.create(); headers1.put("key1", "value1"); headers1.put("key2", "value2"); RecordedHttpRequest recordedHttpRequest1 = new RecordedHttpRequest(null, null, headers1, null); Multimap<String, String> headers2 = LinkedHashMultimap.create(); headers2.put("key1", "value1"); headers2.put("key2", "value2"); headers2.put("key3", "value3"); RecordedHttpRequest recordedHttpRequest2 = new RecordedHttpRequest(null, null, headers2, null); Set<String> wList = new HashSet<>(); wList.add("key1"); wList.add("key2"); MatchRule matchRule = MatchRuleUtils.matchHeadersWithWhitelist(wList); Assert.assertTrue(matchRule.test(recordedHttpRequest1, recordedHttpRequest2)); }
@Test public void testPostParameterWhitelistMatchWithNullParameter() throws Exception { RecordedStringHttpBody stringHttpBody1 = new RecordedStringHttpBody("a=a&b=b&c=c&e="); RecordedStringHttpBody stringHttpBody2 = new RecordedStringHttpBody("a=a&b=x&c=c&d=d&e="); 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"); whitelist.add("e"); 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 testPostParameterBlacklistNotMatch() throws Exception { RecordedStringHttpBody stringHttpBody1 = new RecordedStringHttpBody("a=a&b=b&c=c"); RecordedStringHttpBody stringHttpBody2 = new RecordedStringHttpBody("a=a&b=x&c=c"); Multimap<String, String> headers = LinkedHashMultimap.create(); headers.put("Content-Type", "application/x-www-form-urlencoded"); Set<String> blackList = new HashSet<>(); blackList.add("c"); RecordedHttpRequest recordedHttpRequest1 = new RecordedHttpRequest(null, null, headers, stringHttpBody1); RecordedHttpRequest recordedHttpRequest2 = new RecordedHttpRequest(null, null, headers, stringHttpBody2); MatchRule matchRule = MatchRuleUtils.matchBodyPostParametersWithBlacklist(blackList); Assert.assertFalse(matchRule.test(recordedHttpRequest1, recordedHttpRequest2)); Assert.assertTrue(matchRule.getMatchFailureDescriptionForRequests(recordedHttpRequest1, recordedHttpRequest2).contains("HTTP Body Parameters Mismatch (with Blacklist)")); }
@Test public void testEntireRequestMatch() throws Exception { Multimap<String, String> headers = LinkedHashMultimap.create(); headers.put("key1", "value1"); headers.put("key2", "value2"); RecordedHttpBody body = new RecordedStringHttpBody("body"); RecordedHttpRequest recordedHttpRequest1 = new RecordedHttpRequest("POST", new URI("http://www.google.com/"), headers, body); RecordedHttpRequest recordedHttpRequest2 = new RecordedHttpRequest("POST", new URI("http://www.google.com/"), headers, body); MatchRule entireRequestRule = MatchRuleUtils.matchEntireRequest(); MatchRule methodUriRule = MatchRuleUtils.matchMethodUri(); Assert.assertTrue(entireRequestRule.test(recordedHttpRequest1, recordedHttpRequest2)); Assert.assertTrue(methodUriRule.test(recordedHttpRequest1, recordedHttpRequest2)); }
@Test public void testNotMatchHeaders() throws IOException, InterruptedException { URL flashbackScene = getClass().getResource(FLASHBACK_SCENE_DIR); String rootPath = flashbackScene.getPath(); SceneConfiguration sceneConfiguration = new SceneConfiguration(rootPath, SCENE_MODE, HTTP_SCENE); try (FlashbackRunner flashbackRunner = new FlashbackRunner.Builder().mode(SCENE_MODE) .sceneAccessLayer( new SceneAccessLayer(SceneFactory.create(sceneConfiguration), MatchRuleUtils.matchEntireRequest())) .build()) { flashbackRunner.start(); HttpHost host = new HttpHost(PROXY_HOST, PROXY_PORT); String url = "http://www.example.org/"; HttpClient client = HttpClientBuilder.create().setProxy(host).build(); HttpGet request = new HttpGet(url); request.addHeader("a", "b"); HttpResponse httpResponse = client.execute(request); Assert.assertEquals(httpResponse.getStatusLine().getStatusCode(), 400); Assert.assertTrue(EntityUtils.toString(httpResponse.getEntity()) .contains("No Matching Request")); } }
@Test public void testMethodUriBodyWithAnyBoundaryMatchForDifferentUri() throws Exception { Multimap<String, String> headers = LinkedHashMultimap.create(); headers.put("key1", "value1"); headers.put("Content-Type", "multipart/form-data; boundary=wxyz1234abcd5e"); RecordedHttpBody body = new RecordedStringHttpBody("------wxyz1234abcd5e\nContent-Disposition: form-data; name=\"org\" \ngoogle\n------wxyz1234abcd5e"); RecordedHttpRequest recordedHttpRequest1 = new RecordedHttpRequest("POST", new URI("http://www.linkedin.com/"), headers, body); RecordedHttpRequest recordedHttpRequest2 = new RecordedHttpRequest("POST", new URI("http://www.google.com/"), headers, body); MatchRule matchRule = MatchRuleUtils.matchMethodUriBodyWithAnyBoundary(); Assert.assertFalse(matchRule.test(recordedHttpRequest1, recordedHttpRequest2)); Assert.assertTrue(matchRule.getMatchFailureDescriptionForRequests(recordedHttpRequest1, recordedHttpRequest2).contains("URI Mismatch")); }