【问题标题】:How to retrieve fault information from soap error response using script in SoapUI mock service如何使用 SoapUI 模拟服务中的脚本从肥皂错误响应中检索故障信息
【发布时间】:2019-08-09 19:20:41
【问题描述】:

我在 SoapUI 中创建了一个模拟服务。我在这个模拟服务中使用 Groovy,所以我可以模拟一些请求,以及将其他请求转发到我正在模拟的实际 Web 服务。 当 Web 服务返回三个可能的故障消息之一时,我无法从肥皂响应中检索到该实际故障。

模拟服务 Groovy 脚本仅使用以下响应进行回复(IOException,http 状态 500)。 但是当直接向实际的 Web 服务发送请求时,我得到了我真正想要得到的响应。

转发请求并检索响应的 Groovy 代码:

        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;  

使用 Groovy 脚本模拟服务的响应:

   <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>

直接访问网络服务时的响应(使用相同的请求):

   <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>

使用脚本时,为什么响应与直接检索不一样?

【问题讨论】:

    标签: soap groovy mocking soapui fault


    【解决方案1】:

    好的,我发现我可以以不同的方式使用连接 (URLConnection)。 我根据接受的答案here 做了一些更改。 现在,检索到实际响应(快乐或错误)。因此,在这两种情况下,Web 服务响应都被转发到模拟服务输出。现在我可以在响应中看到故障信息了。

            ...
          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();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多