HttpClient的LocalTestServer移动到哪里了?

时间:2016-04-24 16:31:04

标签: java apache-commons-httpclient

我已经了解了HttpClient LocalTestServer如何用于自动化测试,但我似乎无法找到它被移动的位置。我尝试使用httpclient分类器定义与tests的依赖关系:

'org.apache.httpcomponents:httpclient:4.5.2:tests'

但似乎并没有在那里定义LocalTestServer类。这已经停止了吗?

2 个答案:

答案 0 :(得分:6)

您的测试现在应该延长org.apache.http.localserver.LocalServerTestBase

httpclient模块中提供了分类器tests

你的pom看起来像:

<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpclient</artifactId>
    <version>4.5.2</version>
    <scope>test</scope>
    <classifier>tests</classifier>
</dependency>

相关问题:

https://issues.apache.org/jira/browse/HTTPCLIENT-1172

相关变更集:

https://github.com/apache/httpclient/commit/2ebd8202849c1f4a17d4320543e315a46cbfdc10

答案 1 :(得分:1)

可以使用LocalServer的简单实现

public class LocalHttpServer extends ExternalResource {
private static final Logger log = LoggerFactory.getLogger(LocalHttpServer.class);
private final int port;
private MockServer server;

public LocalHttpServer(int port) {
    this.port = port;
}

@Override
protected void before() throws Throwable {
    server = new MockServer();
    server.setUp();
}

public void start() throws Exception {
    server.start(port);
    log.info("LocalHttpServer started on {}", port);
}

/**
 * Need to be setup before starting server
 */
public LocalHttpServer registerSimpleHandler(String path, String resp) {
    server.registerSimpleHandler(path, resp);
    return this;
}

@Override
protected void after() {
    try {
        server.shutDown();
    } catch (Exception e) {
        e.printStackTrace();
    }
    log.info("LocalHttpServer shutdown on {}", port);
}

static class MockServer extends LocalServerTestBase {
    @Override
    public void setUp() throws Exception {
        super.setUp();
        HttpRequestFactory requestFactory = new DefaultHttpRequestFactory() {
            @Override
            public HttpRequest newHttpRequest(final RequestLine requestline) throws MethodNotSupportedException {
                return super.newHttpRequest(requestline);
            }
        };
        HttpMessageParserFactory<HttpRequest> requestParserFactory = new DefaultHttpRequestParserFactory(
                BasicLineParser.INSTANCE, requestFactory);
        DefaultBHttpServerConnectionFactory connectionFactory = new DefaultBHttpServerConnectionFactory(
                ConnectionConfig.DEFAULT, requestParserFactory, DefaultHttpResponseWriterFactory.INSTANCE);
        this.serverBootstrap.setConnectionFactory(connectionFactory);
    }

    public void registerSimpleHandler(String path, String resp) {
        this.serverBootstrap.registerHandler(path, (request, response, context) ->
                response.setEntity(new StringEntity(resp, ContentType.TEXT_PLAIN)));
    }

    public void start(int port) throws Exception {
        this.serverBootstrap.setListenerPort(port);
        start();
    }

}

示例用法

public class HttpTest {
private static final LocalHttpServer LOCAL_HTTP = new LocalHttpServer(8080);

@ClassRule
public static final RuleChain RULE_CHAIN = RuleChain.outerRule(LOCAL_HTTP);

@Before
public void setUp() throws Exception {
    LOCAL_HTTP.registerSimpleHandler("/path", "response")
            .start();
}

@Test
public void someTest() {
       //request here
}

}

相关问题