Forum Discussion

richie's avatar
richie
Community Hero
6 years ago

How To Assert On an Empty JSON Response?

Hey,

 

I have 2 REST requests - a POST and a GET.  The POST updates the record (either create/update/delete) and I use the GET to verify the results of the POST.

 

I am trying to prove a record has been deleted from my CRM - I can't use JDBC (which would be nice and easy) - I can only use the GET method to prove the record no longer exists.

 

I am struggling!

 

Once the record is deleted, there is just nothing to assert on in the 'Outline' tab.  If I check the RAW response - I can see the headers and an empty response for the body - e.g. []  (response is .json.)

 

I found the following https://stackoverflow.com/questions/18198482/how-to-check-that-a-soap-response-is-empty

 

and it indicated I could add in the script assertion type wiht the code as follows:

 

assert messageExchange.hasResponse() == false

But this actually fails when I run the GET for some reason.

 

Would anyone know why the above script assertion wouldn't work?  The only thing I can think of is that its not working because there is some sort of response (even if the response just has headers and no body to the response). The original poster on stackoverflow said this worked for a null/empty response, but this isnt working for me.

 

 

 

I've run out of ideas and googling hasn't helped me.  I could force the response to be xml (by using an Accept header on the GET request which would generate a <results> tag to assert on in the response - however other project requirements have forced me to get the response in .json which means theres nothing generated in the outline tab in the response.

 

Many thanks!

 

richie

 

3 Replies

  • aaronpliu's avatar
    aaronpliu
    Frequent Contributor

    Hi

     

    Most of time response of POST / PUT / DELETE is empty or just return a [] / {}. But the response status should be 200 if success. So firstly you need to determine if response is good. and then like what you said. Sending a GET request to check if update takes effect or not.

    For example, if you delete a record, you may know its ID or name, then you needs to check if it really deleted in GET.

    Adding "Valid HTTP Status Code" in  assertion alongwith code=200, 202.

    If have response, then simply check it thru script assertion if need

    def response = messageExchange.response?.contentAsString
    if(response != null){
        // example
        assert response.size() > 0, "No any content returned in Response"
    }

     

    Regards,

    /Aaron

    • Olga_T's avatar
      Olga_T
      SmartBear Alumni (Retired)

      Hi all,

       

      richie, have you had a chance to try the suggestion that aaronpliu provided? Maybe, do you have any updates on the situation?

       

    • richie's avatar
      richie
      Community Hero

      Hi aaronpliu

       

      thanks for the help - but I must be missing something important but obvious here - cos it didn't work as I expected.

       

      I added the script assertion you provided on the GET request that I'm using to prove the record was deleted correctly. (just as an aside - I'm getting valid responses back from the application server so I'm not worried about checking for http status codes).

       

      The script assertion you provided is below

       

      def response = messageExchange.response?.contentAsString
      if(response != null){
      // example
      assert response.size() > 0, "No any content returned in Response"
      }


      When I forced the GET's response to include a body (e.g.by using the 'Accept' header with value of application/xml on the request) - I get an empty <results /> element in the outline tab and the script assertion 'passed'.

       

      When I forced the response to be completely empty (e.g.by using the 'Accept' header with value of application/json on the request) - I get nothing in the response payload (outline tab is empty) and the script assertion 'passed'. Admittedly if I look at the raw I do get [] - and I'm unsure if this is just SoapUI representing a blank response body or not.

       

      You'll have to forgive me, my groovy is rubbish - but reading the above it appears that if there is a payload in response, then assert the response size is >0, (which I get) and return a message saying there isn't any content.??? I appreciate it appears you specified the assertion as possible example.

       

      I played around with operands - or tried to - to assert when there is no payload in response, so the script is as follows:

       

      def response = messageExchange.response?.contentAsString
      if(response == null){
      // example
      assert response.size() == 0, "No content returned in Response"
      }


      but again - whether there was a <results /> tag in the payload (forcing response to me XML) or whether the was nothing in the response (forcing response to be json) the script always passes!

       

      Am I missing something? essentially the script passes whether the response includes a payload AND when it doesn't!


      I am very grateful for your help though!

       

      richie