0

I created a mock service in SoapUI. I am using Groovy in this mock service so I can mock some requests, as well as forward other requests to the actual web service I am mocking. When the web service returns one of three possible fault messages, I am unable to retrieve that actual fault from the soap response.

The mock service Groovy script just replies with the response herebelow (IOException, http status 500). But when sending a request to the actual web service directly, I get the response I actually would like to get.

Groovy code which forwards the request and retrieve a response:

        def soapUrl = new URL("[actual web service]");
        def connection = soapUrl.openConnection();
        connection.setRequestMethod("POST");
        connection.setRequestProperty("Content-Type" ,"text/html");
        connection.setRequestProperty("SOAPAction", "");
        connection.doOutput = true;

        Writer writer = new OutputStreamWriter(connection.outputStream);
        writer.write(soapRequest);
        writer.flush();
        writer.close();
        connection.connect();

        def soapResponse = connection.content.text;
        // alert.showInfoMessage(soapResponse);

        requestContext.responseMessage = soapResponse;  

Response using the Groovy scripted mock service:

   <soapenv:Body>
      <soapenv:Fault>
         <faultcode>Server</faultcode>
         <faultstring>Failed to dispatch using script; java.io.IOException: Server returned HTTP response code: 500 for URL: [the endpoint url]</faultstring>
      </soapenv:Fault>
   </soapenv:Body>
</soapenv:Envelope>

Response when accessing the web service directly (with the same request):

   <soapenv:Body>
      <soapenv:Fault>
         <faultcode>soapenv:Server</faultcode>
         <faultstring> [actual fault message] </faultstring>
         <detail> [useful details about the fault] </detail>
      </soapenv:Fault>
   </soapenv:Body>
</soapenv:Envelope>

When using the script, why is the response not the same as if I would retrieve it directly?

Kjeld
  • 444
  • 4
  • 14

1 Answers1

0

Ok I found out I can use the connection (URLConnection) in a different way. I made some changes based on the accepted answer here. Now, the actual response, happy or error, is retrieved. So in both cases the web service response is being forwarded to the mock service output. And now I can see the fault info in the response.

        ...
      connection.connect();

    // Get the response
    HttpURLConnection httpConnection = (HttpURLConnection) connection;
    InputStream is;
    if (httpConnection.getResponseCode() < HttpURLConnection.HTTP_BAD_REQUEST) {
            is = httpConnection.getInputStream();
    } else {
        // Http error
            is = httpConnection.getErrorStream();
    }

    // Read from input stream
    StringBuilder builder = new StringBuilder();
    BufferedReader buffer = new BufferedReader(new InputStreamReader(is));
      String line;
    while ((line = buffer.readLine()) != null) {
        builder.append(line);
    }
    buffer.close();

    // Forward the response to mock service output
    requestContext.responseMessage = builder.toString();
Kjeld
  • 444
  • 4
  • 14