public static Scene create(SceneConfiguration sceneConfiguration) throws IOException, IllegalStateException { //The reason that pass static variables is that it would be easier to write unit-test return create(sceneConfiguration, SCENE_READER); }
@Test(expectedExceptions = IllegalStateException.class) public void testCreateSceneNotExistInPlaybackMode() throws Exception { runTestGetResult(SceneMode.PLAYBACK, null); }
/** * Build serializable {@Link RecordedHttpBody} using temporary byte buffers. * Based on Charset, we will build concrete Http body either binary or characters. * TODO: throw customized exception if failed to create http body * * */ protected RecordedHttpBody getBody() { try { InputStream byteBufInputStream = new ByteBufInputStream(_bodyByteBuf); return RecordedHttpBodyFactory.create(getContentType(), getContentEncoding(), byteBufInputStream, getCharset()); } catch (IOException e) { throw new RuntimeException("Failed to create Httpbody"); } } }
/** * 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); } }
@Test public void testCreateStringHttpBody() throws IOException { String str = "Hello world"; byte[] content = str.getBytes(); int size = content.length; InputStream is = new ByteArrayInputStream(content); RecordedHttpBody recordedHttpBody = RecordedHttpBodyFactory.create("text/html", "identity", is, "UTF-8"); Assert.assertTrue(recordedHttpBody instanceof RecordedStringHttpBody); Assert.assertFalse(recordedHttpBody instanceof RecordedByteHttpBody); Assert.assertEquals(size, recordedHttpBody.getContent("UTF-8").length); }
@Test public void testCreateSceneNotExistInRecordMode() throws Exception { Scene result = runTestGetResult(SceneMode.RECORD, null); Assert.assertNotNull(result); Assert.assertEquals(result.getName(), NAME); Assert.assertEquals(result.getSceneRoot(), ROOT); Assert.assertFalse(result.isReadable()); }
/** * 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); } }
@Test public void testCreateByteHttpBody() throws IOException { String str = "Hello world"; byte[] content = str.getBytes(); InputStream is = new ByteArrayInputStream(content); RecordedHttpBody recordedHttpBody = RecordedHttpBodyFactory.create("image/gif", "identity", is, "UTF-8"); Assert.assertTrue(recordedHttpBody instanceof RecordedByteHttpBody); Assert.assertFalse(recordedHttpBody instanceof RecordedStringHttpBody); Assert.assertEquals(content, recordedHttpBody.getContent("UTF-8")); }
@Test public void testCreateSceneExistInSequentialRecordMode() throws Exception { Scene expectedScene = EasyMock.createStrictMock(Scene.class); List<RecordedHttpExchange> recordedHttpExchanges = new ArrayList<>(); Date now = new Date(); recordedHttpExchanges.add(new RecordedHttpExchange(null, null, now)); EasyMock.expect(expectedScene.getRecordedHttpExchangeList()).andReturn(recordedHttpExchanges); EasyMock.replay(expectedScene); Scene result = runTestGetResult(SceneMode.SEQUENTIAL_RECORD, expectedScene); Assert.assertNotNull(result); Assert.assertEquals(result.getName(), NAME); Assert.assertEquals(result.getSceneRoot(), ROOT); Assert.assertFalse(result.isReadable()); // Creating a Scene in sequential record mode should clear the list of exchanges to allow for re-recording Assert.assertEquals(result.getRecordedHttpExchangeList().size(), 0); }
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 testCreateGZipEncodedStringHttpBody() throws IOException { String str = "Hello world. This is some extra text to make the string longer so that gzip makes it smaller"; byte[] content = str.getBytes(); byte[] compressedContent = new GzipCompressor().compress(content); InputStream is = new ByteArrayInputStream(compressedContent); RecordedHttpBody recordedHttpBody = RecordedHttpBodyFactory.create("text/html", "gzip", is, "UTF-8"); Assert.assertTrue(recordedHttpBody instanceof RecordedEncodedHttpBody); RecordedEncodedHttpBody encodedBody = (RecordedEncodedHttpBody) recordedHttpBody; Assert.assertEquals(encodedBody.getEncodingName(), "gzip"); Assert.assertTrue(encodedBody.getDecodedBody() instanceof RecordedStringHttpBody); Assert.assertEquals(encodedBody.getDecodedBody().getContent("UTF-8"), content); Assert.assertEquals(encodedBody.getContent("UTF-8"), compressedContent); }
@Test public void testCreateSceneExistInPlaybackMode() throws Exception { Scene expectedScene = EasyMock.createStrictMock(Scene.class); List<RecordedHttpExchange> recordedHttpExchanges = new ArrayList<>(); Date now = new Date(); recordedHttpExchanges.add(new RecordedHttpExchange(null, null, now)); EasyMock.expect(expectedScene.getRecordedHttpExchangeList()).andReturn(recordedHttpExchanges); EasyMock.replay(expectedScene); Scene result = runTestGetResult(SceneMode.PLAYBACK, expectedScene); Assert.assertNotNull(result); Assert.assertEquals(result.getName(), NAME); Assert.assertEquals(result.getSceneRoot(), ROOT); Assert.assertTrue(result.isReadable()); Assert.assertEquals(result.getRecordedHttpExchangeList().get(0).getUpdateTime(), now); }
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 testCreateDeflateEncodedStringHttpBody() throws IOException { String str = "Hello world. This is some extra text to make the string longer so that deflate makes it smaller"; byte[] content = str.getBytes(); byte[] compressedContent = new DeflateCompressor().compress(content); InputStream is = new ByteArrayInputStream(compressedContent); RecordedHttpBody recordedHttpBody = RecordedHttpBodyFactory.create("text/html", "deflate", is, "UTF-8"); Assert.assertTrue(recordedHttpBody instanceof RecordedEncodedHttpBody); RecordedEncodedHttpBody encodedBody = (RecordedEncodedHttpBody) recordedHttpBody; Assert.assertEquals(encodedBody.getEncodingName(), "deflate"); Assert.assertTrue(encodedBody.getDecodedBody() instanceof RecordedStringHttpBody); Assert.assertEquals(encodedBody.getDecodedBody().getContent("UTF-8"), content); Assert.assertEquals(encodedBody.getContent("UTF-8"), compressedContent); } }
@Test public void testCreateSceneExistInRecordMode() throws Exception { Scene expectedScene = EasyMock.createStrictMock(Scene.class); List<RecordedHttpExchange> recordedHttpExchanges = new ArrayList<>(); Date now = new Date(); recordedHttpExchanges.add(new RecordedHttpExchange(null, null, now)); EasyMock.expect(expectedScene.getRecordedHttpExchangeList()).andReturn(recordedHttpExchanges); EasyMock.replay(expectedScene); Scene result = runTestGetResult(SceneMode.RECORD, expectedScene); Assert.assertNotNull(result); Assert.assertEquals(result.getName(), NAME); Assert.assertEquals(result.getSceneRoot(), ROOT); Assert.assertFalse(result.isReadable()); Assert.assertEquals(result.getRecordedHttpExchangeList().get(0).getUpdateTime(), now); }
@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 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")); } }
try (FlashbackRunner flashbackRunner = new FlashbackRunner.Builder().mode(SCENE_MODE) .sceneAccessLayer( new SceneAccessLayer(SceneFactory.create(sceneConfiguration), MatchRuleUtils.matchEntireRequest())) .build()) { flashbackRunner.start();
try { FlashbackRunner.Builder builder = new FlashbackRunner.Builder().mode(_currSceneMode) .sceneAccessLayer(new SceneAccessLayer(SceneFactory.create(sceneConfiguration), namedMatchRule)); if (proxyHost != null) { builder.host(proxyHost);