Example usage for org.apache.http.localserver LocalTestServer getServiceAddress

List of usage examples for org.apache.http.localserver LocalTestServer getServiceAddress

Introduction

In this page you can find the example usage for org.apache.http.localserver LocalTestServer getServiceAddress.

Prototype

public InetSocketAddress getServiceAddress() 

Source Link

Document

Obtains the local address the server is listening on

Usage

From source file:com.uber.jenkins.phabricator.utils.TestUtils.java

public static String getTestServerAddress(LocalTestServer server) {
    return String.format("http://%s:%s", server.getServiceAddress().getHostName(),
            server.getServiceAddress().getPort());
}

From source file:com.flipkart.foxtrot.client.LocalServerTestRule.java

@Override
public Statement apply(final Statement base, Description description) {
    return new Statement() {
        @Override/* w w w.j a  v  a  2s .  co m*/
        public void evaluate() throws Throwable {
            LocalTestServer localTestServer = new LocalTestServer(null, null);
            for (Map.Entry<String, HttpRequestHandler> handler : handlers.entrySet()) {
                localTestServer.register(handler.getKey(), handler.getValue());
            }
            localTestServer.start();
            logger.info("Started test server");
            try {
                hostPort.setHostName(localTestServer.getServiceAddress().getHostName());
                hostPort.setPort(localTestServer.getServiceAddress().getPort());
                base.evaluate();
            } finally {
                localTestServer.stop();
                logger.info("Stopped test server");
            }
        }
    };
}

From source file:org.ojbc.util.TestHttpUtils.java

@Test
public void test() throws Exception {
    final String responseString = "yeppers";
    LocalTestServer server = new LocalTestServer(null, null);
    server.register("/test", new HttpRequestHandler() {
        @Override// w w  w .  j av a2s.c o m
        public void handle(HttpRequest request, HttpResponse response, HttpContext context)
                throws HttpException, IOException {
            response.setEntity(new StringEntity(responseString));
        }
    });
    server.start();
    String host = server.getServiceAddress().getHostName();
    int port = server.getServiceAddress().getPort();
    String response = HttpUtils.post("ok?", "http://" + host + ":" + port + "/test");
    assertEquals(responseString, response);
}