cancel
Showing results for 
Search instead for 
Did you mean: 

loop a http request, check response code and break the loop if response code <> 404

Highlighted
Super Contributor

loop a http request, check response code and break the loop if response code <> 404

In ReadyAPI/SOAPUI, I need to go through some sort of "While loop" for a request.

 

here is the loop:

1. send the http request;

2. evaluate response code;

3. re-execute step 1 if response code = 404

stop executing if response code <> 404

 

I have already tried Conditional Go To and Property Wait but both fail (don't work with response code)

 

Anyone can help me??

 

Thanks,
Mathijs

 

2 REPLIES 2
Highlighted
Contributor

Re: loop a http request, check response code and break the loop if response code <> 404

Hi mgroen2,

 

I never really use the conditional goto step, I prefer to use the much more flexible groovy test step.

What you might look for is something like this;

 

 

def response = testRunner.testCase.getTestStepAt(context.getCurrentStepIndex()-1).testRequest.response.getResponseHeaders()
String responseStatus = response["#status#"]
//log.info responseStatus
if (!responseStatus.contains("[HTTP/1.1 2"))
{
testRunner.fail("TESTCASE: " + testRunner.getTestCase().getName() + " TESTSTEP: "+ context.getCurrentStep().getName() + " -- Resultaat van de update van de status was niet succesvol. De testcase faalt en we gaan onmiddellijk naar het einde van de status flow.")
}

 This script you put in a groovy test step and it will go fetch the response status from the previous (rest request) step.

In my case: if it does NOT contain a valid http2* header status then it will fail the test case and stop execution.

 

Hope this helps. If it does, feel free to mark it als "resolved".

De groeten,

Joost

Highlighted
Contributor

Re: loop a http request, check response code and break the loop if response code <> 404

A bit too fast perhaps with my previous reply...
For the loop part of your question you should use the TestRunner goToTestStepByName (or equivalent teststepindex use) to loop it back to the step1.

2 things you might need to take into account:
1) if you have any assertions in step 1, e.g. a valid http200 status header assertion, it might be that the first iteration fails the test step, and (depending on your test case options) this fails your test case
The second iteration no longer hits your 404, so in the end the test case shows your test step1 as passed, but the test case failed. Workaround for this is to create step1 first without assertions and after the Groovy test step again step1 with assertions.

2) As with any looping, pay attention your system does not get stuck in an endless loop. You'll have to kill soapui and lose all your changes! In your case, if your step1 always returns a 404 you might want to put in a limitation to the number of iterations. You can use the context.loopIndex() in an if-else clause to repeat above piece of code x times and then give up.
New Here?
Join us and watch the welcome video:
Announcements
TechCorner Leaderboard
Compete with community members in the TechCorner Challenge and get into the Leaderboard!

Rank Participant Points Earned
1 msiadak 12
2 nmrao 8
3 HimanshuTayal 3
Top Kudoed Authors