Forum Discussion

cortez's avatar
cortez
Occasional Contributor
12 years ago

[Resolved] REST mock service always returns 200 OK

Hi,

I'm new to SoapUI and although I'm generally finding it a joy to use I am having one small piece of frustration.

I am mocking a REST API and in the Mock Service editor I have a Groovy script for handling POST requests that looks like this:

def responseFolder = "/path/to/docroot"
if ( mockRequest.method == "POST" )
{
mockRequest.httpResponse.setHeader("Location","http://localhost:8080/v1/foo/status/bf248efa-d951-4102-accf-bcd2a8804dc0")
mockRequest.httpResponse.setStatus(202, "Accepted")
mockRunner.returnFile(mockRequest.httpResponse, new File(responseFolder, "foo.json") )
return new com.eviware.soapui.impl.wsdl.mock.WsdlMockResult(mockRequest)
}


So in the response I want the code to be "202 Accepted" and the Location header set to a new 'status update' (my API is asynchronous). Although I can successfully set the header, the request, when fired, always returns "200 OK".

Any ideas?

Thank you,
Christopher

1 Reply

  • cortez's avatar
    cortez
    Occasional Contributor
    OK, I eventually solved it by computing the response from the script, rather than returning the contents of a file, like this:

    if ( mockRequest.method == "POST" ) 
    {
    def mockResponse = mockRequest.getHttpResponse()
    mockResponse.setHeader("Location","/foo/bar/123")
    mockResponse.setStatus(202, "Accepted")
    mockResponse.getWriter().print("{\n \"link\":\"/foo/bar/123\"\n}")
    return new com.eviware.soapui.impl.wsdl.mock.WsdlMockResult(mockRequest)
    }


    The 202 response code is returned successfully using that method.