微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何处理具有 QAF Webservice 主体的 REST API 请求的 GET 请求

如何解决如何处理具有 QAF Webservice 主体的 REST API 请求的 GET 请求

我将 QAF Webservice support 用于 API 自动化。我有一个 GET 请求有正文的情况。如果我将请求作为使用属性文件或 xml 文件传递​​,则在执行时我会收到 404 not found 响应。如果 GET 请求没有正文,则在该场景中它可以正常工作,不会出现任何问题。但不是 GET 请求具有正文。调试后发现,如果一个GET请求有body,最后的jersey client API将请求从POST改为GET。请让我知道如何使用 QAF WebService 处理这种情况。

谢谢,

解决方法

您可以使用 apache HttpClient 来获取请求体。为了使用 apache HttpClient,您需要提供 RestClientFactory 的实现并使用属性 rest.client.impl 进行注册。

这是来自 qaf 用户组的 sample code

package qaf.example.tests;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;

import com.qmetry.qaf.automation.ws.rest.RestClientFactory;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientHandler;
import com.sun.jersey.api.client.config.ClientConfig;
import com.sun.jersey.client.apache.ApacheHttpClient;
import com.sun.jersey.client.apache.ApacheHttpClientHandler;
import com.sun.jersey.client.apache.config.DefaultApacheHttpClientConfig;

/**
 * @author chirag
 *
 */
public class ApacheClientProvider extends RestClientFactory {

    @Override
    protected Client createClient() {
        MultiThreadedHttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager();
        connectionManager.getParams().setConnectionTimeout(5000);
        connectionManager.getParams().setSoTimeout(1000);
        connectionManager.getParams().setDefaultMaxConnectionsPerHost(10);
        HttpClient httpClient = new HttpClient(connectionManager);
        ApacheHttpClientHandler clientHandler = new ApacheHttpClientHandler(httpClient);
        ClientHandler root = new ApacheHttpClient(clientHandler );
        ClientConfig config = new DefaultApacheHttpClientConfig();
        Client client = new Client(root,config);
        return client;
    }

}

为了使用它,请使用 rest.client.impl 属性注册您的类,在上述情况下:

rest.client.impl=qaf.example.tests.ApacheClientProvider

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。