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 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)); }
@Test public void testDifferentHeaders() throws Exception { Multimap<String, String> headers1 = LinkedHashMultimap.create(); headers1.put("key1", "value1"); headers1.put("key2", "value2"); Multimap<String, String> headers2 = LinkedHashMultimap.create(); headers2.put("key1", "value1"); headers2.put("key3", "value3"); RecordedHttpRequest recordedHttpRequest1 = new RecordedHttpRequest("GET", new URI("http://www.google.com/"), headers1, null); RecordedHttpRequest recordedHttpRequest2 = new RecordedHttpRequest("GET", new URI("http://www.google.com/"), headers2, null); MatchRule entireRequestRule = MatchRuleUtils.matchEntireRequest(); MatchRule methodUriRule = MatchRuleUtils.matchMethodUri(); Assert.assertFalse(entireRequestRule.test(recordedHttpRequest1, recordedHttpRequest2)); Assert.assertTrue(methodUriRule.test(recordedHttpRequest1, recordedHttpRequest2)); Assert.assertTrue(entireRequestRule.getMatchFailureDescriptionForRequests(recordedHttpRequest1, recordedHttpRequest2).contains("HTTP Headers Mismatch")); }
@Test public void testDifferentURIs() throws Exception { Multimap<String, String> headers = LinkedHashMultimap.create(); headers.put("key1", "value1"); headers.put("key2", "value2"); RecordedHttpRequest recordedHttpRequest1 = new RecordedHttpRequest("GET", new URI("http://www.google.com/"), headers, null); RecordedHttpRequest recordedHttpRequest2 = new RecordedHttpRequest("GET", new URI("http://www.google.com/foo/"), headers, null); MatchRule entireRequestRule = MatchRuleUtils.matchEntireRequest(); MatchRule methodUriRule = MatchRuleUtils.matchMethodUri(); Assert.assertFalse(entireRequestRule.test(recordedHttpRequest1, recordedHttpRequest2)); Assert.assertFalse(methodUriRule.test(recordedHttpRequest1, recordedHttpRequest2)); Assert.assertTrue(entireRequestRule.getMatchFailureDescriptionForRequests(recordedHttpRequest1, recordedHttpRequest2).contains("URI Mismatch")); Assert.assertTrue(entireRequestRule.getMatchFailureDescriptionForRequests(recordedHttpRequest1, recordedHttpRequest2).contains("URI Mismatch")); }
@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 testDifferentMethods() 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("PUT", new URI("http://www.google.com/"), headers, body); MatchRule entireRequestRule = MatchRuleUtils.matchEntireRequest(); MatchRule methodUriRule = MatchRuleUtils.matchMethodUri(); Assert.assertFalse(entireRequestRule.test(recordedHttpRequest1, recordedHttpRequest2)); Assert.assertFalse(methodUriRule.test(recordedHttpRequest1, recordedHttpRequest2)); Assert.assertTrue(entireRequestRule.getMatchFailureDescriptionForRequests(recordedHttpRequest1, recordedHttpRequest2).contains("HTTP Method Mismatch")); Assert.assertTrue(methodUriRule.getMatchFailureDescriptionForRequests(recordedHttpRequest1, recordedHttpRequest2).contains("HTTP Method Mismatch")); }
@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 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 testDifferentBody() throws Exception { Multimap<String, String> headers = LinkedHashMultimap.create(); headers.put("key1", "value1"); headers.put("key2", "value2"); RecordedHttpBody body1 = new RecordedStringHttpBody("body1"); RecordedHttpBody body2 = new RecordedStringHttpBody("body2"); RecordedHttpRequest recordedHttpRequest1 = new RecordedHttpRequest("POST", new URI("http://www.google.com/"), headers, body1); RecordedHttpRequest recordedHttpRequest2 = new RecordedHttpRequest("POST", new URI("http://www.google.com/"), headers, body2); MatchRule entireRequestRule = MatchRuleUtils.matchEntireRequest(); MatchRule methodUriRule = MatchRuleUtils.matchMethodUri(); Assert.assertFalse(entireRequestRule.test(recordedHttpRequest1, recordedHttpRequest2)); Assert.assertTrue(methodUriRule.test(recordedHttpRequest1, recordedHttpRequest2)); Assert.assertTrue(entireRequestRule.getMatchFailureDescriptionForRequests(recordedHttpRequest1, recordedHttpRequest2).contains("HTTP Body Mismatch")); }
@Test() public void testReplayHttp() throws InterruptedException, IOException { 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); HttpResponse httpResponse0 = client.execute(request); Assert.assertTrue(EntityUtils.toString(httpResponse0.getEntity()) .contains("I am from Flashback scene, not http://example.org")); url = "http://www.nba.com/"; request = new HttpGet(url); HttpResponse httpResponse1 = client.execute(request); Assert.assertTrue(EntityUtils.toString(httpResponse1.getEntity()) .contains("I am from Flashback scene, not http://www.nba.com")); url = "http://www.notexist.org/"; request = new HttpGet(url); HttpResponse httpResponse2 = client.execute(request); Assert.assertEquals(httpResponse2.getStatusLine().getStatusCode(), 400); Assert.assertTrue(EntityUtils.toString(httpResponse2.getEntity()) .contains("No Matching Request")); } }
try (FlashbackRunner flashbackRunner = new FlashbackRunner.Builder().mode(SCENE_MODE) .sceneAccessLayer( new SceneAccessLayer(SceneFactory.create(sceneConfiguration), MatchRuleUtils.matchEntireRequest())) .build()) { flashbackRunner.start();