Forum Discussion

rvteja1990's avatar
rvteja1990
Occasional Contributor
9 years ago

Assertion for raw response

I have been trying to create an assertion for raw response. I got a requirement in which whenever i see a response with Invalid URI in headers, it should fail. This is how it is displayed in response...
  • AntonE's avatar
    AntonE
    9 years ago

    You can use startsWith method to check that a string has a required beginning:

     

    def expectedHTTPResponse = 'HTTP/1.1 500 Invalid URI'
    def headers = messageExchange.response.responseHeaders
    def actualHTTPResponse = headers['#status#']
    assert actualHTTPResponse.size() == 1 && actualHTTPResponse[0].startsWith(expectedHTTPResponse), "Invalid URI" 

     

    Note that I removed square brackets in expectedHTTPResponse as we compare just one element now.

  • AntonE's avatar
    AntonE
    9 years ago

    For Groovy assertion to fail its asserted value should be false, so I guess you can just invert the condition:

     

    assert !(actualHTTPResponse.size() == 1 && actualHTTPResponse[0].startsWith(expectedHTTPResponse)), "Invalid URI"

     

    Also, you may want to look at Invalid HTTP Status Code assertion instead. It won't check the string, but if code 500 is a sure sign that the test failed, it will work. You should enter one or more wrong HTTP codes there.

  • nmrao's avatar
    nmrao
    9 years ago

    Not sure why it is closed though your issue is not solved.

     

    Here is the change needed:

     

    def unExpectedHTTPResponse = 'HTTP/1.1 500 Invalid URI'
    def headers = messageExchange.response.responseHeaders
    def actualHTTPResponse = headers['#status#']
    assert !(actualHTTPResponse[0].contains(unExpectedHTTPResponse)), "Found Invalid URI in the response"