To where was HttpClient's LocalTestServer moved?
Asked Answered
J

2

6

I've read about how HttpClient's LocalTestServer can be used for automated testing, however I can't seem to find where it's been moved. I tried defining dependency to httpclient with tests classifier:

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

but there doesn't seem to be a LocalTestServer class defined in there. Has this been discontinued?

Jingoism answered 24/4, 2016 at 16:31 Comment(0)
B
6

Your test should now extend org.apache.http.localserver.LocalServerTestBase.

This is available in the httpclient module with classifier tests.

Your pom could look like:

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

Related issue:

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

Related changeset:

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

Baler answered 26/4, 2016 at 13:3 Comment(0)
A
1

Can use simple implementation of the 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();
    }

}

Example usage

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
}

}

Andro answered 23/8, 2019 at 13:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.