cancel
Showing results for 
Search instead for 
Did you mean: 

Assertion for raw response

SOLVED
Highlighted
Occasional Contributor

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 "HTTP/1.1 500 Invalid URI - /xyz/abc/123". 

 

In the above header whenever i see "Invalid URI" assertion should fail.

 

When i checked forums i found a script assertion, with which i have modified it like this. However its failing since the URI part is not mentioned in expectedHTTPResponse. I would like to make it generic, such a way that whatever may be the URI, whenever i encounter Invalid URI it should fail the assertion.

 

Below is the piece of code:

 

def expectedHTTPResponse = ['HTTP/1.1 500 Invalid URI']
def headers = messageExchange.response.responseHeaders
def actualHTTPResponse = headers['#status#']
assert expectedHTTPResponse == actualHTTPResponse, "Invalid URI" 

 

Can anyone help me with this? Any other possible solutions to achieve this?

 

 

3 ACCEPTED SOLUTIONS

Accepted Solutions
Highlighted
Staff

Re: Assertion for raw response

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.


Anton Egorov
ReadyAPI developer

View solution in original post

Highlighted
Staff

Re: Assertion for raw response

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.


Anton Egorov
ReadyAPI developer

View solution in original post

Highlighted
Community Hero

Re: Assertion for raw response

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"


Regards,
Rao.

View solution in original post

8 REPLIES 8
Highlighted
Community Hero

Re: Assertion for raw response

Please post raw response. Do you want to check if the status code is 500, then fail?


Regards,
Rao.
Occasional Contributor

Re: Assertion for raw response

This is the raw response i'm getting:

 

HTTP/1.1 500 Invalid URI - /xyz/abc/123"

Content-Type: text/xml

X-Backside-Transport: FAIL FAIL

Connection: close

 

I want to check whether the raw response has "Invalid URI". If it has Invalid URI in response assertion should fail.

 

Thank you.

Highlighted
Staff

Re: Assertion for raw response

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.


Anton Egorov
ReadyAPI developer

View solution in original post

Highlighted
Occasional Contributor

Re: Assertion for raw response

Thanks for your response. I really appreciate your help Anton. Your piece of code works as expected. 

I need a small change in this,

Is there any way that we can fail this assertion when it matches with expected HTTP Response (InvalidURI)?

I mean, since its matching with expected HTTPResponse, assertion is valid and its displaying as 'Green'. I would like to see 'Red' color whenever InvalidURI encounters in response. For the rest of all the other responses it should show as 'Green'

 

 

 

 

Highlighted
Staff

Re: Assertion for raw response

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.


Anton Egorov
ReadyAPI developer

View solution in original post

Highlighted
Occasional Contributor

Re: Assertion for raw response

Thanks again. Its working as expected. Yes i have already looked into invalid HTTP status code but here i would like to check  for string irrespective of HTTP codes. Hope your code will work in all the scenarios. Thank you.

Highlighted
Community Hero

Re: Assertion for raw response

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"


Regards,
Rao.

View solution in original post

Highlighted
Occasional Contributor

Re: Assertion for raw response

Thank you so much Rao. Your piece of code is working as expected. It seems both scripts are working as expected.I have a question, what is different from your piece of code and Anton code. 

 

New Here?
Join us and watch the welcome video:
Announcements
Top Kudoed Authors