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

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

Introduction

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

Prototype

public void register(String pattern, HttpRequestHandler handler) 

Source Link

Document

Registers a handler with the local registry.

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   w  ww  . j  av a 2s .  c  om*/
        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:org.apache.camel.component.http4.HttpSOTimeoutTest.java

@Override
protected void registerHandler(LocalTestServer server) {
    server.register("/", new DelayValidationHandler("GET", null, null, getExpectedContent(), 2000));
}

From source file:org.apache.camel.component.http4.HttpBridgeEndpointTest.java

@Override
protected void registerHandler(LocalTestServer server) {
    server.register("/", new BasicValidationHandler("GET", null, null, getExpectedContent()));
}

From source file:org.apache.camel.component.http4.HttpPollingConsumerTest.java

@Override
protected void registerHandler(LocalTestServer server) {
    server.register("/", new DelayValidationHandler("GET", null, null, getExpectedContent(), 1000));
}

From source file:org.apache.camel.component.http4.HttpPathTest.java

@Override
protected void registerHandler(LocalTestServer server) {
    server.register("/search", new BasicValidationHandler("GET", null, null, getExpectedContent()));
    server.register("/test%20/path", new BasicValidationHandler("GET", null, null, getExpectedContent()));
}

From source file:org.apache.camel.component.http4.HttpProducerContentTypeTest.java

@Override
protected void registerHandler(LocalTestServer server) {
    server.register("/content", new HttpRequestHandler() {
        @Override/*from w  w  w.j a  v  a  2s . c  o  m*/
        public void handle(HttpRequest request, HttpResponse response, HttpContext context)
                throws HttpException, IOException {
            String contentType = request.getFirstHeader(Exchange.CONTENT_TYPE).getValue();

            assertEquals(CONTENT_TYPE, contentType);

            response.setEntity(new StringEntity(contentType, "ASCII"));
            response.setStatusCode(HttpStatus.SC_OK);
        }
    });
}

From source file:org.apache.camel.component.http4.HttpProducerTwoParametersWithSameKeyTest.java

@Override
protected void registerHandler(LocalTestServer server) {
    server.register("/myapp", new HttpRequestHandler() {
        @Override/*from  w w  w .j av a 2s.  c om*/
        public void handle(HttpRequest request, HttpResponse response, HttpContext context)
                throws HttpException, IOException {
            String uri = request.getRequestLine().getUri();
            assertEquals("/myapp?from=me&to=foo&to=bar", uri);

            response.setHeader("bar", "yes");
            response.addHeader("foo", "123");
            response.addHeader("foo", "456");
            response.setEntity(new StringEntity("OK", "ASCII"));
            response.setStatusCode(HttpStatus.SC_OK);
        }
    });
}

From source file:org.apache.camel.component.http4.HttpQueryTest.java

@Override
protected void registerHandler(LocalTestServer server) {
    server.register("/", new BasicValidationHandler("GET", "hl=en&q=camel", null, getExpectedContent()));
    server.register("/test/", new BasicValidationHandler("GET", "my=@+camel", null, getExpectedContent()));
    server.register("/user/pass",
            new BasicValidationHandler("GET", "password=baa&username=foo", null, getExpectedContent()));
}

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 w w  .  j ava 2 s .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);
}

From source file:org.apache.camel.component.http4.HttpProducerTwoHeadersWithSameKeyTest.java

@Override
protected void registerHandler(LocalTestServer server) {
    server.register("/myapp", new HttpRequestHandler() {
        @Override//from w  w w  .  ja  va2  s  .co m
        public void handle(HttpRequest request, HttpResponse response, HttpContext context)
                throws HttpException, IOException {
            Header[] from = request.getHeaders("from");
            assertEquals("me", from[0].getValue());
            Header[] to = request.getHeaders("to");
            assertEquals("[foo, bar]", to[0].getValue());

            response.setHeader("bar", "yes");
            response.addHeader("foo", "123");
            response.addHeader("foo", "456");
            response.setEntity(new StringEntity("OK", "ASCII"));
            response.setStatusCode(HttpStatus.SC_OK);
        }
    });
}