public class UserScenarioEndpointsProvider implements Iterable {
public static final String SCENARIO_ID = "my-user-scenario";
public static final String STEP_1_ID = "step321";
public static final String STEP_2_ID = "step2";
public static final String STEP_3_ID = "step3";
private static Logger log = LoggerFactory.getLogger(UserScenarioEndpointsProvider.class);
private List<JHttpUserScenario> userScenarios = new ArrayList<>();
public UserScenarioEndpointsProvider() {
JHttpUserScenario userScenario = new JHttpUserScenario(SCENARIO_ID, "My User Scenario");
userScenario
.withScenarioGlobalContext(new JHttpScenarioGlobalContext()
.withGlobalEndpoint(new JHttpEndpoint("https://httpbin.org/")))
.addStep(JHttpUserScenarioStep.builder(STEP_1_ID)
.withDisplayName("Step #321")
.withWaitAfterExecutionInSeconds(3)
.build())
.addStep(JHttpUserScenarioStep.builder(STEP_2_ID)
.withDisplayName("Step #2")
.withWaitAfterExecutionInSeconds(3)
.withQuery(new JHttpQuery().get().path("/get"))
.withPreProcessGlobalContextFunction((prevStep, context) -> {
context.withGlobalHeaders(prevStep.getResponse().getHeaders());
})
.withPostProcessFunction(response -> {
if (response.getStatus().is2xxSuccessful()) {
log.info("Step 2 is successful!");
return true;
}
return false;
})
.build())
.addStep(JHttpUserScenarioStep.builder(STEP_3_ID)
.withDisplayName("Step #3")
.withWaitAfterExecutionInSeconds(3)
.withQuery(new JHttpQuery().get().path("/response-headers?key=val"))
.withPreProcessFunction((prevStep, currentStep) -> {
currentStep.setEndpoint(new JHttpEndpoint("http://www.scala-lang.org"));
currentStep.setQuery(new JHttpQuery().get().path("/"));
})
.build());
JHttpUserScenario userScenarioBasicAuthAuto = new JHttpUserScenario("my-user-scenario-basic-auth",
"Basic Auth User Scenario");
userScenarioBasicAuthAuto
.withScenarioGlobalContext(new JHttpScenarioGlobalContext()
.withGlobalEndpoint(new JHttpEndpoint("https://httpbin.org/"))
.withBasicAuth("userName", "userPassword")
)
.addStep(JHttpUserScenarioStep.builder("basic_auto_1")
.withQuery(new JHttpQuery().get().path("/basic-auth/userName/userPassword"))
.withDisplayName("Auth pass")
.withWaitAfterExecutionInSeconds(3)
.build())
.addStep(JHttpUserScenarioStep.builder("basic_auto_2")
.withQuery(new JHttpQuery().get().path("/basic-auth/userName/userPassword"))
.withDisplayName("Auth pass (validation)")
.withWaitAfterExecutionInSeconds(3)
.withPostProcessFunction(response -> {
if (response.getStatus().value() != 200) {
log.error("Unexpected status returned. Expected: 200. Returned: {}", response.getStatus().value());
return false;
}
return true;
})
.build())
.addStep(JHttpUserScenarioStep.builder("basic_auto_3")
.withQuery(new JHttpQuery().get().path("/basic-auth/userName/userPassword"))
.withDisplayName("Auth fail (validation)")
.withWaitAfterExecutionInSeconds(3)
.withPreProcessFunction((prevStep, currentStep) -> {
currentStep.getQuery().headers(null);
})
.withPostProcessFunction(response -> {
if (response.getStatus().value() != 401) {
log.error("Unexpected status returned. Expected: 401. Returned: {}", response.getStatus().value());
return false;
}
return true;
})
.build());
userScenarios.add(userScenario);
userScenarios.add(userScenarioBasicAuthAuto);
}
@Override
public Iterator<JHttpUserScenario> iterator() {
return userScenarios.iterator();
}
}