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

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

Introduction

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

Prototype

public void start() throws Exception 

Source Link

Document

Starts this test server.

Usage

From source file:se.vgregion.pubsub.inttest.SubscriberRunner.java

public static void main(String[] args) throws Exception {

    LocalTestServer server = new LocalTestServer(null, null);

    server.register("/*", new HttpRequestHandler() {
        @Override/*from  www  .  j  ava2  s  .  co  m*/
        public void handle(HttpRequest request, HttpResponse response, HttpContext context)
                throws HttpException, IOException {
            String challenge = getQueryParamValue(request.getRequestLine().getUri(), "hub.challenge");

            if (challenge != null) {
                // subscription verification, confirm
                System.out.println("Respond to challenge");
                response.setEntity(new StringEntity(challenge));
            } else if (request instanceof HttpEntityEnclosingRequest) {
                HttpEntity entity = ((HttpEntityEnclosingRequest) request).getEntity();
                //                    System.out.println(HttpUtil.readContent(entity));
            } else {
                System.err.println("Unknown request");
            }
        }
    });
    server.start();

    HttpPost post = new HttpPost("http://localhost:8080/");

    List<NameValuePair> parameters = new ArrayList<NameValuePair>();
    parameters.add(new BasicNameValuePair("hub.callback", buildTestUrl(server, "/").toString()));
    parameters.add(new BasicNameValuePair("hub.mode", "subscribe"));
    parameters.add(new BasicNameValuePair("hub.topic", "http://feeds.feedburner.com/protocol7/main"));
    parameters.add(new BasicNameValuePair("hub.verify", "sync"));

    post.setEntity(new UrlEncodedFormEntity(parameters));

    DefaultHttpClient client = new DefaultHttpClient();
    client.execute(post);
}

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

@Override
public Statement apply(final Statement base, Description description) {
    return new Statement() {
        @Override//  ww w.j  ava 2s  . c  o 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/*from w  ww. j  a v a  2s.  co  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);
}

From source file:org.neo4j.ext.udc.impl.UdcExtensionImplTest.java

@Test
public void shouldRecordSuccessesWhenThereIsAServer() throws Exception {
    // first, set up the test server
    LocalTestServer server = new LocalTestServer(null, null);
    PingerHandler handler = new PingerHandler();
    server.register("/*", handler);
    server.start();

    final String hostname = server.getServiceHostName();
    final String serverAddress = hostname + ":" + server.getServicePort();

    Map<String, String> config = new HashMap<String, String>();
    config.put(UdcExtensionImpl.FIRST_DELAY_CONFIG_KEY, "100");
    config.put(UdcExtensionImpl.UDC_HOST_ADDRESS_KEY, serverAddress);

    EmbeddedGraphDatabase graphdb = createTempDatabase(config);
    assertGotSuccessWithRetry(IS_GREATER_THAN_ZERO);
    assertGotFailureWithRetry(IS_ZERO);//  w w  w. j a  va  2 s .c o m
    destroy(graphdb);
}

From source file:org.neo4j.ext.udc.impl.UdcExtensionImplTest.java

@Test
public void shouldBeAbleToSpecifySourceWithConfig() throws Exception {
    // first, set up the test server
    LocalTestServer server = new LocalTestServer(null, null);
    PingerHandler handler = new PingerHandler();
    server.register("/*", handler);
    server.start();

    final String hostname = server.getServiceHostName();
    final String serverAddress = hostname + ":" + server.getServicePort();

    Map<String, String> config = new HashMap<String, String>();
    config.put(UdcExtensionImpl.FIRST_DELAY_CONFIG_KEY, "100");
    config.put(UdcExtensionImpl.UDC_HOST_ADDRESS_KEY, serverAddress);
    config.put(UdcExtensionImpl.UDC_SOURCE_KEY, "test");

    EmbeddedGraphDatabase graphdb = createTempDatabase(config);
    assertGotSuccessWithRetry(IS_GREATER_THAN_ZERO);
    assertEquals("test", handler.getQueryMap().get("source"));

    destroy(graphdb);// w  w w . j ava 2 s.  c o  m
}