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); }
@Override public void close() { if (_running) { stop(); } }
public FlashbackRunner build() { validate(); return new FlashbackRunner(this); }
private FlashbackRunner(final Builder builder) { _sceneAccessLayer = builder._sceneAccessLayer; if (builder._sceneMode == SceneMode.RECORD || builder._sceneMode == SceneMode.SEQUENTIAL_RECORD) { _proxyServer = createProxyServerInRecordMode(builder); } else { _proxyServer = createProxyServerInReplayMode(builder); } }
@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")); } }
/** * here is the place to be used in test case and within this callable flashback will use the scene specified by sceneConf. * * @param sceneConf the scene config, will be loaded into flashback. * @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(SceneConfiguration sceneConf, Callable<T> callable) throws Exception { try { Scene scene = SceneFactory.create(sceneConf); _flashbackRunner.setScene(scene); return (T) callable.call(); } finally { _flashbackRunner.setScene(_defaultScene); } }
/** * here is the place to be used in test case and within this runnable flashback will use this matchRule. * * @param matchRule the match rule, will be loaded into flashback. * @param runnable to call after the match rule loaded. */ protected void withMatchRule(MatchRule matchRule, Runnable runnable) { try { _flashbackRunner.setMatchRule(matchRule); runnable.run(); } finally { _flashbackRunner.setMatchRule(_defaultMatchRule); } }
/** * Create proxy server in record mode */ private ProxyServer createProxyServerInRecordMode(Builder builder) { ProxyModeControllerFactory proxyModeControllerFactory = new ProxyModeControllerFactory() { @Override public ProxyModeController create(HttpRequest httpRequest) { return new RecordController(_sceneAccessLayer, httpRequest); } }; //Create Http connection flow for record mode List<ConnectionFlowStep> httpConnectionFlow = ConnectionFlowFactory.createFullHttpConnectionFlow(); ProxyServer.Builder proxyServerBuilder = new ProxyServer.Builder().proxyModeControllerFactory(proxyModeControllerFactory) .connectionFlow(Protocol.HTTP, httpConnectionFlow).host(builder._host).port(builder._port); if (requiresHttps(builder)) { //Create Https connection flow for record mode List<ConnectionFlowStep> httpsConnectionFlow = ConnectionFlowFactory .createFullHttpsConnectionFlow(builder._rootCertificateInputStream, builder._rootCertificatePassphrase, builder._certificateAuthority); proxyServerBuilder.connectionFlow(Protocol.HTTPS, httpsConnectionFlow); } return proxyServerBuilder.build(); }
/** * this is the place to close flashback or you can override to do something before close the flashback. */ @AfterTest protected void flashbackGlobalCleanUp() { _flashbackRunner.close(); }
@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")); } }
/** * here is the place to be used in test case and within this runnable flashback will use the scene according to the sceneConf. * * @param sceneConf the scene config, will be loaded into flashback. * @param runnable to call after the scene loaded. */ protected void withScene(SceneConfiguration sceneConf, Runnable runnable) { try { Scene scene = SceneFactory.create(sceneConf); _flashbackRunner.setScene(scene); runnable.run(); } catch (IOException e) { Assert.fail(e.getMessage()); } finally { _flashbackRunner.setScene(_defaultScene); } }
/** * here is the place to be used in test case and within this callable flashback will use this matchRule. * * @param matchRule the match rule will be loaded info flashback. * @param callable to call after the match rule changed in flashback. * @param <T> return type of the callable. * @return return callable result. * @throws Exception */ protected <T> T withMatchRule(MatchRule matchRule, Callable<T> callable) throws Exception { try { _flashbackRunner.setMatchRule(matchRule); return (T) callable.call(); } finally { _flashbackRunner.setMatchRule(_defaultMatchRule); } }
/** * Create proxy server in replay mode */ private ProxyServer createProxyServerInReplayMode(Builder builder) { ProxyModeControllerFactory proxyModeControllerFactory = new ProxyModeControllerFactory() { @Override public ProxyModeController create(HttpRequest httpRequest) { return new ReplayController(_sceneAccessLayer, httpRequest); } }; //Create Http connection flow for replay mode List<ConnectionFlowStep> httpReplayConnectionFlow = ConnectionFlowFactory.createClientOnlyHttpConnectionFlow(); ProxyServer.Builder proxyServerBuilder = new ProxyServer.Builder().proxyModeControllerFactory(proxyModeControllerFactory) .connectionFlow(Protocol.HTTP, httpReplayConnectionFlow).host(builder._host).port(builder._port); if (requiresHttps(builder)) { //Create Https connection flow for replay mode List<ConnectionFlowStep> httpsReplayConnectionFlow = ConnectionFlowFactory .createClientOnlyHttpsConnectionFlow(builder._rootCertificateInputStream, builder._rootCertificatePassphrase, builder._certificateAuthority); proxyServerBuilder.connectionFlow(Protocol.HTTPS, httpsReplayConnectionFlow); } return proxyServerBuilder.build(); }
@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")); } }
@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); } }
@Action(name = "changeMatchRule") public void changeMatchRule(@ActionParam("matchRule") String matchRule) { validate(); MatchRule namedMatchRule = NamedMatchRule.fromString(matchRule); if (namedMatchRule == null) { throw new RestLiServiceException(HttpStatus.S_400_BAD_REQUEST, "Unknown match rule" + matchRule); } _flashbackRunner.setMatchRule(namedMatchRule); }
@Action(name = "shutDownFlashback") public void shutDownFlashback() { validate(); _flashbackRunner.stop(); }
@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")); } }
new SceneAccessLayer(SceneFactory.create(sceneConfiguration), MatchRuleUtils.matchEntireRequest())) .build()) { flashbackRunner.start(); HttpHost host = new HttpHost(PROXY_HOST, PROXY_PORT); String url = "http://www.example.org/";
private void bootstrap() throws InterruptedException { FlashbackRunner.Builder flashbackBuilder = new FlashbackRunner.Builder().sceneAccessLayer(new SceneAccessLayer(_defaultScene, _defaultMatchRule)) .host(_proxyHost).port(_proxyPort).mode(_defaultSceneMode); if (_certificateAuthority != null && _rootCertificateInputStream != null && _rootCertificatePassphrase != null) { flashbackBuilder.certificateAuthority(_certificateAuthority).rootCertificateInputStream(_rootCertificateInputStream) .rootCertificatePassphrase(_rootCertificatePassphrase); } _flashbackRunner = flashbackBuilder.build(); _flashbackRunner.start(); } }