5

I have created a RESTful service and I am testing it in SOAPUI project. In a test case I want to verify the HTTP response status codes in groovy script.

I have tried using the following:

def value = messageExchange.responseHeaders["#status#"]

assert value==200

But it always gives the error and doesn't validates.(The test I am using does returns 200 status code, but I don't know how to put the groovy correctly)

Can anyone plz guide me. I dont want to perform GET operation in the groovy. I am performing GET separately using soapui and I just want to test the status codes.

Amrit
  • 2,295
  • 4
  • 25
  • 42
  • Similar questions that have been asked earlier: http://stackoverflow.com/questions/6330449/how-to-add-assertion-for-http-204-content-in-rest http://stackoverflow.com/questions/16662959/how-to-test-the-response-content-type-using-soap-ui – kdabir Feb 18 '14 at 07:37
  • @kunal Thanks Kunal! It solved my purpose :) – Amrit Feb 18 '14 at 08:02

2 Answers2

11

This is going to be very useful for future use. I did some searching and found a post regarding this at the SoapUI forum.

I tested it with an HTTP step (sorry, I don't have any rest services to use) and it works well. Luckily, SoapUI handles most all requests the same way so it should work for you as well.

And the raw code:

def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context )
def httpResponseHeaders = context.testCase.testSteps["testName"].testRequest.response.responseHeaders
def httpStatus = httpResponseHeaders["#status#"]
def httpStatusCode = (httpStatus =~ "[1-5]\\d\\d")[0]

log.info("HTTP status code: " + httpStatusCode)
Paul Muir
  • 304
  • 4
  • 9
1

For anyone working in ReadyAPI, the solution has changed a bit since 2014.

def httpResponseHeaders = testRunner.testCase.getTestStepByName("testName").testRequest.response.responseHeaders
def httpStatus = httpResponseHeaders["#status#"]
def httpStatusCode = (httpStatus =~ "[1-5]\\d\\d")[0]

log.info("HTTP status code: " + httpStatusCode)

This swaps the context variable for the testRunner variable, both of which are exposed by ReadyAPI.

ReadyAPI Groovy docs

Dharman
  • 30,962
  • 25
  • 85
  • 135
Daly
  • 787
  • 2
  • 12
  • 23