如何处理具有 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