Apache HttpClient中的单元测试超时 [英] Unit test timeouts in Apache HttpClient

查看:159
本文介绍了Apache HttpClient中的单元测试超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试测试此方法,以检查超时设置是否正确.

I'm trying to test this method checking that the timeouts are correctly set.

public HttpClientBuilder getClientBuilderWithTimeouts(final int connT, final int reqT, final int socketT){
    RequestConfig.Builder requestBuilder = RequestConfig.custom();
    requestBuilder = requestBuilder.setConnectTimeout(connT);
    requestBuilder = requestBuilder.setConnectionRequestTimeout(reqT);
    requestBuilder = requestBuilder.setSocketTimeout(socketT);
    HttpClientBuilder clientBuilder = HttpClientBuilder.create();     
    clientBuilder.setDefaultRequestConfig(requestBuilder.build());
    return clientBuilder;
}

在我的单元测试中,尽管getParams()会抛出UnsupportedOperationException.在不使用集成测试的情况下如何解决这个问题?

In my unit test though getParams() throws UnsupportedOperationException. How can I solve this without using an integration test?

import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.client.config.RequestConfig;
...

@Test
public void testGetClientBuilderWithTimeouts(){

    HttpClientBuilder clBuilder = utils.getClientBuilderWithTimeouts(10, 20, 30);
    CloseableHttpClient testclient = clBuilder.build();

    System.out.println(testclient.getParams().getParameter("http.socket.timeout"));
    System.out.println(testclient.getParams().getParameter("http.connection.timeout"));
    // asserts ..
}

推荐答案

从utils类(要测试)中获取客户端之后,可以尝试在构建实际客户端之前添加HttpRequestInterceptor.

You can try to add a HttpRequestInterceptor before building the actual client, after getting the client from your utils class (which you want to test).

...

import org.apache.http.HttpRequestInterceptor;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.protocol.HttpContext;
import org.apache.http.HttpRequest;
import org.apache.http.HttpException;

... 

builder.addInterceptorFirst(new HttpRequestInterceptor() {

      @Override
      public void process(HttpRequest request, HttpContext context) throws HttpException, IOException {
        // Get hold of the client context, which holds the request config
        RequestConfig requestConfig = HttpClientContext.adapt(context).getRequestConfig();

        assertEquals(10, requestConfig.getConnectTimeout());
        assertEquals(20, requestConfig.getConnectionRequestTimeout());
        assertEquals(30, requestConfig.getSocketTimeout());
      }
    });     

现在只需对任何URL进行调用,并忽略客户端对URL的请求失败时将抛出的异常.由于拦截器是首先添加的,并且是请求拦截器,因此应在尝试请求之前调用该拦截器,从而为请求配置提供断言.

Now just do a call to any URL and ignore the exception the client is going to throw in case the request to the URL fails. Since the interceptor is added as first and is a request interceptor, it should be called before the request is attempted, giving you the request config to assert.

这篇关于Apache HttpClient中的单元测试超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆