Forum Discussion

timjohnarm's avatar
timjohnarm
Occasional Contributor
8 years ago
Solved

How to use Groovy to set Status, Content-Length, Content-Type in the HTTP Response for a MockService

I promise I have genuinely tried and failed to find an answer both here and over on StackOverflow. I've seen a number of variants on my question but nothing where the answer is as simple as mockResponse.setHeader(Content-Length, 0)

 

So I want to set various HTTP Response header fields in the Script of my MockService Response script.

 

The dialog tells me that I've got a number of variables available to me including log, mockRequest, mockResponse.

 

I've worked out the simple HelloWorld equivalent

log.info "Hello from my script"

I made a guess that for log that the methods would be info, warn etc.

 

But how do I use mockResponse to set some HTTP Header's in particular as mentioned in the question Content-Length and Status. I tried to find a reference for the methods but failed.

 

 

  • You can set an HTTP header by the code:

     

    mockRequest.httpResponse.setHeader('MyHeader', "HiThere;")

    Similarly you can set the Content-Length:

     

    mockRequest.httpResponse.setContentLength(456)

     

    BUT: further processing overrides your settings, e.g. the Content-Length with be the actual length of the response content.

     

    I assume you need to break standard mock processing and return the response by your own, e.g. as with this code in the OnRequest script:

     

    mockRequest.httpResponse.sendError(404)

    There is a conflict between our code and the standard mock processing (which normally has to do the settings). I am not aware yet, hot to tell SoapUI that it shall not override my values.

     

10 Replies

  • KarelHusa's avatar
    KarelHusa
    Champion Level 3

    You can set an HTTP header by the code:

     

    mockRequest.httpResponse.setHeader('MyHeader', "HiThere;")

    Similarly you can set the Content-Length:

     

    mockRequest.httpResponse.setContentLength(456)

     

    BUT: further processing overrides your settings, e.g. the Content-Length with be the actual length of the response content.

     

    I assume you need to break standard mock processing and return the response by your own, e.g. as with this code in the OnRequest script:

     

    mockRequest.httpResponse.sendError(404)

    There is a conflict between our code and the standard mock processing (which normally has to do the settings). I am not aware yet, hot to tell SoapUI that it shall not override my values.

     

    • nmrao's avatar
      nmrao
      Champion Level 3
      That makes sense and user neither have to calculate the content length nor set it, and it is done implicitly. Not really sure why OP is interested in setting it.
      • KarelHusa's avatar
        KarelHusa
        Champion Level 3

        I believe it makes sense to build mocks with non-standard behavior. 

         

        Especially in heavily heterogenous environments, where are dozens of backend systems and different implementations of services, Many of my projects include ESB implementation, where we need to be prepared for different kinds of responses from backend services.

         

        Therefore the mocks do scenarios e.g.:

        - not well-formed XML

        - invalid XML

        - unexpected HTTP code

        - long response time

        - unavailability of the service

        - pretending the service does not exist

        - missing required HTTP headers

        - giving SOAP Faults

         

        The ESB service needs to handle all these responses and provide a response to the consumer which is appropriate and understandable.

         

        This is why I also develop special processing in mocks.

         

    • timjohnarm's avatar
      timjohnarm
      Occasional Contributor

      Hello Karel thanks for your response.

       

      The missing piece was the httpResponse in the middle.

       

      The MockResponse class has its own methods

      mockResponse.responseHttpStatus
      mockResponse.setResponseHeaders

       

      But using httpResponse is easier and at least for me clearer.

  • nmrao's avatar
    nmrao
    Champion Level 3

    Question is confusing by below contradicting statements:

     

    "the answer is as simple as mockResponse.setHeader(Content-Length, 0)"

     

    " how do I use mockResponse to set some HTTP Header's in particular as mentioned in the question Content-Length and Status. I tried to find a reference for the methods but failed."