Skip to content

Supporting dynamic changes

Shangshang Feng edited this page Mar 9, 2017 · 7 revisions

Change scene

In order to allow for flexibility in testing, Flashback lets you dynamically change scenes and match rules. Changing scenes dynamically allows testing the same requests with different responses (such as success, time_out, rate_limit, etc.). Scene changes only apply to scenarios where we have POSTed data to update the external resource. See the diagram below as an example.

Use-case-need-dynamic-changes.jpg

SceneConfiguration sceneConfiguration = new SceneConfiguration(rootPath1, sceneMode, sceneName1);
try (FlashbackRunner flashbackRunner = new FlashbackRunner.Builder().mode(sceneMode).host(host).port(port).sceneAccessLayer(
    new SceneAccessLayer(SceneFactory.create(sceneConfiguration), matchRule)).build()) {
  flashbackRunner.start();
 
  //make http request call
  sceneConfiguration = new SceneConfiguration(rootPath2, sceneMode, sceneName2);
  flashbackRunner.setScene(SceneFactory.create(sceneConfiguration));
  //make http request call again.
  sceneConfiguration = new SceneConfiguration(rootPath3, sceneMode, sceneName3);
  flashbackRunner.setScene(SceneFactory.create(sceneConfiguration));
}
//In record mode, after recording is done, the same Http transaction will be stored in multiple files(in our case, we will get 3 files):
//rootPath1/scene1, rootPath2/scene2 and rootPath3/scene3
//In replay mode, the response will be coming from those multiple different files(3 files in our case).

Change match rules

Being able to change the match rule dynamically allows us to test complicated scenarios. For example, we have a use case that requires us to test HTTP calls to both public and private resources of Twitter. For public resources, the HTTP requests are constant, so we can use the “MatchAll” rule. However, for private resources, we need to sign requests with an OAuth consumer secret and an OAuth access token. These requests contain a lot of parameters that have unpredictable values, so the static “MatchAll” rule wouldn’t work.

SceneConfiguration sceneConfiguration = new SceneConfiguration(rootPath, sceneMode, sceneName);
try (FlashbackRunner flashbackRunner = new FlashbackRunner.Builder().mode(sceneMode).host(host).port(port).sceneAccessLayer(
    new SceneAccessLayer(SceneFactory.create(sceneConfiguration), matchRule1)).build()) {
  flashbackRunner.start();
 
  //make http request call
  flashbackRunner.setMatchRule(matchRule2);
  //make http request call again.
  flashbackRunner.setMatchRule(matchRule3);
}