How to use doIf properly


I have a scenarios where I have to upload an X file to the Y endpoint. The upload process usually takes more than 10mins roughly 13-17 minutes. I do verify the file uploaded successfully by calling the polling endpoint (every 10 mins).

The api security is implemented by Keycloak and after 5 mins my auth-token is expiring resulting the test to fail.

I need to add a condition to my API call chain that: “In case the endpoint returns 401 - Unauthorized do an extra call Z to generate a new auth token”.

Up until now I wasn’t able to do this. Below I will list some code I wrote and wait for some help.

val uploadFile: ChainBuilder = exec(securedPost(EndPoint.FileUpload)
.exec {
_.set(“uploadStatus”, “PENDING”)
.asLongAs(session => !session(“uploadStatus”).as[String].equals(“SUCCESS”) && !session(“uploadStatus”).as[String].equals(“WARNING”)) {
exec(securedGet(“Poll file status”, “${pollingUrl}”) // here is where the 401 is thrown after 5 minutes
.check(uploadStatus, submissionId, batchId)
.doIf(session => session(“statusCode”).as[Int] == 401) {