/** * here is the place to be used in test case and within this runnable flashback will use the scene according to the sceneName. * * @param sceneName the scene name, will be loaded into flashback using PLAYBACK mode and default scene path. * @param runnable to call after the scene loaded. */ protected void withScene(String sceneName, Runnable runnable) { SceneConfiguration sceneConfiguration = new SceneConfiguration(_defaultScenePath, _defaultSceneMode, sceneName); withScene(sceneConfiguration, runnable); }
/** * here is the place to be used in test case and within this callable flashback will use the scene specified by sceneName. * * @param sceneName the scene name, will be loaded into flashback using PLAYBACK mode and default scene path. * @param callable to call after the scene changed in flashback. * @param <T> return type of the callable. * @return return callable result. * @throws Exception */ protected <T> T withScene(String sceneName, Callable<T> callable) throws Exception { SceneConfiguration sceneConfiguration = new SceneConfiguration(_defaultScenePath, _defaultSceneMode, sceneName); return withScene(sceneConfiguration, callable); }
private void determineDefaultSettings() throws IOException { // if default scenePath hasn't been set, need to figure it out // from either your java default resource folder or home dir. if (_defaultScenePath == null) { _defaultScenePath = HOME_DIR; URL flashbackScene = getClass().getResource(FLASHBACK_SCENE_DIR); if (flashbackScene != null) { _defaultScenePath = flashbackScene.getPath(); } } // if default sceneName is set, it must exist (because by default is PLAYBACK mode) // and we will try to load it. if (_defaultSceneName != null) { SceneConfiguration sceneConfiguration = new SceneConfiguration(_defaultScenePath, _defaultSceneMode, _defaultSceneName); _defaultScene = SceneFactory.create(sceneConfiguration); } // after determined the default Scene&MatchRule for this test class, set them. _flashbackRunner.setScene(_defaultScene); _flashbackRunner.setMatchRule(_defaultMatchRule); }
@Test public void testCanReplayScene() { SceneConfiguration sceneConfiguration1 = new SceneConfiguration(ROOT_PATH, SceneMode.PLAYBACK, SCENE_NAME); Scene scene1 = new Scene(sceneConfiguration1); SceneAccessLayer sceneAccessLayer = new SceneAccessLayer(scene1, EasyMock.createStrictMock(SceneWriter.class), EasyMock.createStrictMock(MatchRule.class)); Assert.assertTrue(sceneAccessLayer.canPlayback()); Scene scene2 = new Scene(new SceneConfiguration(ROOT_PATH, SceneMode.RECORD, SCENE_NAME)); sceneAccessLayer.setScene(scene2); Assert.assertFalse(sceneAccessLayer.canPlayback()); Scene scene3 = new Scene(new SceneConfiguration(ROOT_PATH, SceneMode.SEQUENTIAL_PLAYBACK, SCENE_NAME)); sceneAccessLayer.setScene(scene3); Assert.assertTrue(sceneAccessLayer.canPlayback()); Scene scene4 = new Scene(new SceneConfiguration(ROOT_PATH, SceneMode.SEQUENTIAL_RECORD, SCENE_NAME)); sceneAccessLayer.setScene(scene4); Assert.assertFalse(sceneAccessLayer.canPlayback()); }
@Action(name = "changeScene") public void changeScene(@ActionParam("sceneName") String sceneName) { validate(); SceneConfiguration sceneConfiguration = new SceneConfiguration(_scenePath, _currSceneMode, sceneName); try { _flashbackRunner.setScene(SceneFactory.create(sceneConfiguration)); } catch (IOException e) { throw new RestLiServiceException(HttpStatus.S_500_INTERNAL_SERVER_ERROR, e); } }
@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")); } }
private Scene runTestGetResult(SceneMode scenMode, Scene expectedScene) throws IOException { _sceneReader = EasyMock.createStrictMock(SceneReader.class); SceneConfiguration sceneConfiguration = new SceneConfiguration(ROOT, scenMode, NAME); EasyMock.expect(_sceneReader.readScene(EasyMock.anyString(), EasyMock.anyString())).andReturn(expectedScene); EasyMock.replay(_sceneReader); Scene result = SceneFactory.create(sceneConfiguration, _sceneReader); return result; }
@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 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 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")); } }
URL flashbackScene = getClass().getResource(FLASHBACK_SCENE_DIR); String rootPath = flashbackScene.getPath(); SceneConfiguration sceneConfiguration = new SceneConfiguration(rootPath, SCENE_MODE, "setCookie"); try (FlashbackRunner flashbackRunner = new FlashbackRunner.Builder().mode(SCENE_MODE) .sceneAccessLayer(
SceneConfiguration sceneConfiguration = new SceneConfiguration(_scenePath, _currSceneMode, sceneName); try { FlashbackRunner.Builder builder = new FlashbackRunner.Builder().mode(_currSceneMode)