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.