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

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

Introduction

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

Prototype

public LocalTestServer(BasicHttpProcessor proc, HttpParams params) 

Source Link

Document

Creates a new test server.

Usage

From source file:org.apromore.plugin.deployment.yawl.YAWLDeploymentPluginUnitTest.java

@Before
public void setUp() throws Exception {
    server = new LocalTestServer(null, null);
    server.start();/*  w  w w. ja v a  2  s  . c o  m*/
    deploymentPlugin = new YAWLDeploymentPlugin();
}

From source file:nl.surfnet.coin.api.client.OpenConextOAuthClientImplTest.java

@Before
public void initialize() throws Exception {

    requestHandler = new ResourceRequestHandler();

    testServer = new LocalTestServer(null, null);
    testServer.register("/whatever/*", requestHandler);
    //    testServer.g
    testServer.start();//from w  w  w  .  j a  va2  s. c o  m
    client = new OpenConextOAuthClientImpl();
    client.setEndpointBaseUrl("http://" + testServer.getServiceAddress().getHostString() + ":"
            + testServer.getServiceAddress().getPort() + "/whatever/");
    client.setConsumerKey("key");
    client.setConsumerSecret("secret");
    OAuthRepository repository = new InMemoryOAuthRepositoryImpl();
    repository.storeToken("key", USER_ID);
    repository.storeToken("key", null); // for client creds
    client.setRepository(repository);

}

From source file:io.joynr.messaging.bounceproxy.monitoring.PerformanceReporterTest.java

@Before
public void setUp() throws Exception {

    // use local test server to intercept http requests sent by the reporter
    server = new LocalTestServer(null, null);
    server.register("*", mockHandler);
    server.start();/*from   w ww.  j a  va2  s.  co  m*/

    final String serverUrl = "http://localhost:" + server.getServiceAddress().getPort();

    // set test properties manually for a better overview of
    // what is tested
    Properties properties = new Properties();

    // have to set the BPC base url manually as this is the
    // mock server which gets port settings dynamically
    properties.put(BounceProxyPropertyKeys.PROPERTY_BOUNCE_PROXY_CONTROLLER_BASE_URL, serverUrl);
    properties.put(BounceProxyPropertyKeys.PROPERTY_BOUNCE_PROXY_ID, "X.Y");
    properties.put(BounceProxyPropertyKeys.PROPERTY_BOUNCEPROXY_URL_FOR_BPC, "http://joyn-bpX.muc/bp");
    properties.put(BounceProxyPropertyKeys.PROPERTY_BOUNCEPROXY_URL_FOR_CC, "http://joyn-bpX.de/bp");
    properties.put(BounceProxyPropertyKeys.PROPERTY_BOUNCE_PROXY_MONITORING_FREQUENCY_MS, "100");
    properties.put(BounceProxyPropertyKeys.PROPERTY_BOUNCE_PROXY_SEND_LIFECYCLE_REPORT_RETRY_INTERVAL_MS,
            "500");
    properties.put(BounceProxyPropertyKeys.PROPERTY_BOUNCE_PROXY_MAX_SEND_SHUTDOWN_TIME_SECS, "2");

    Injector injector = Guice.createInjector(new PropertyLoadingModule(properties), new AbstractModule() {

        @Override
        protected void configure() {

            bind(ScheduledExecutorService.class).toInstance(mockExecutorService);
            bind(BounceProxyPerformanceMonitor.class).toInstance(mockPerformanceMonitor);
            bind(BounceProxyLifecycleMonitor.class).toInstance(mockLifecycleMonitor);
            bind(CloseableHttpClient.class).toInstance(HttpClients.createDefault());
        }

    });

    reporter = injector.getInstance(BounceProxyPerformanceReporter.class);

    // fake the clock for a more robust testing
    clock = new Clock();

    Answer<Void> mockScheduledExecutor = new Answer<Void>() {

        @Override
        public Void answer(final InvocationOnMock invocation) throws Throwable {

            long frequencyMs = (Long) invocation.getArguments()[2];
            Runnable runnable = (Runnable) invocation.getArguments()[0];

            clock.setFrequencyMs(frequencyMs);
            clock.setRunnable(runnable);

            return null;
        }
    };
    Mockito.doAnswer(mockScheduledExecutor).when(mockExecutorService).scheduleWithFixedDelay(
            Mockito.any(Runnable.class), Mockito.anyLong(), Mockito.anyLong(), Mockito.any(TimeUnit.class));
}

From source file:io.joynr.messaging.MessageSchedulerTest.java

@Before
public void setUp() throws Exception {

    String messagePath = CHANNELPATH + channelId + "/message/";

    server = new LocalTestServer(null, null);
    server.register(messagePath, new HttpRequestHandler() {

        @Override/*from  www  .j av  a 2s.c  o  m*/
        public void handle(HttpRequest request, HttpResponse response, HttpContext context)
                throws HttpException, IOException {
            response.setStatusCode(sendMessageResponseCode);
            response.setHeader("msgId", sendMessageId);
            serverResponded = true;
        }
    });
    server.start();
    serviceAddress = "http://" + server.getServiceAddress().getHostName() + ":"
            + server.getServiceAddress().getPort();
    bounceProxyUrl = serviceAddress + BOUNCEPROXYPATH;

    Properties properties = new Properties();
    properties.put(MessagingPropertyKeys.CHANNELID, channelId);
    properties.put(MessagingPropertyKeys.BOUNCE_PROXY_URL, bounceProxyUrl);

    AbstractModule mockModule = new AbstractModule() {

        @Override
        protected void configure() {
            bind(LocalChannelUrlDirectoryClient.class).toInstance(mockChannelUrlDir);
        }

    };

    Injector injector = Guice.createInjector(new JoynrPropertiesModule(properties),
            Modules.override(new MessagingTestModule()).with(mockModule), new AbstractModule() {
                @Override
                protected void configure() {
                    bind(RequestConfig.class).toProvider(HttpDefaultRequestConfigProvider.class)
                            .in(Singleton.class);
                }
            }, new AtmosphereMessagingModule());
    messageScheduler = injector.getInstance(MessageScheduler.class);
}

From source file:brooklyn.util.ResourceUtilsHttpTest.java

@BeforeClass(alwaysRun = true)
public void setUp() throws Exception {
    utils = ResourceUtils.create(this, "mycontext");

    BasicHttpProcessor httpProcessor = new BasicHttpProcessor();
    httpProcessor.addInterceptor(new ResponseServer());
    httpProcessor.addInterceptor(new ResponseContent());
    httpProcessor.addInterceptor(new ResponseConnControl());
    httpProcessor.addInterceptor(new RequestBasicAuth());
    httpProcessor.addInterceptor(new ResponseBasicUnauthorized());

    server = new LocalTestServer(httpProcessor, null);
    server.register("/simple", new SimpleResponseHandler("OK"));
    server.register("/empty", new SimpleResponseHandler(HttpStatus.SC_NO_CONTENT, ""));
    server.register("/missing", new SimpleResponseHandler(HttpStatus.SC_NOT_FOUND, "Missing"));
    server.register("/redirect", new SimpleResponseHandler(HttpStatus.SC_MOVED_TEMPORARILY, "Redirect",
            new BasicHeader("Location", "/simple")));
    server.register("/cycle", new SimpleResponseHandler(HttpStatus.SC_MOVED_TEMPORARILY, "Redirect",
            new BasicHeader("Location", "/cycle")));
    server.register("/secure", new SimpleResponseHandler(HttpStatus.SC_MOVED_TEMPORARILY, "Redirect",
            new BasicHeader("Location", "https://0.0.0.0/")));
    server.register("/auth", new AuthHandler("test", "test", "OK"));
    server.register("/auth_escape", new AuthHandler("test@me:/", "test", "OK"));
    server.register("/auth_escape2", new AuthHandler("test@me:test", "", "OK"));
    server.register("/no_credentials", new CheckNoCredentials());
    server.start();/*from  w  w  w . ja v a2  s . co  m*/

    InetSocketAddress addr = server.getServiceAddress();
    baseUrl = "http://" + addr.getHostName() + ":" + addr.getPort();
}

From source file:io.joynr.messaging.bounceproxy.monitoring.MonitoringServiceClientTest.java

@Before
public void setUp() throws Exception {

    // use local test server to intercept http requests sent by the reporter
    server = new LocalTestServer(null, null);
    server.register("*", handler);
    server.start();//from   ww w.  j a v a2  s  .  c o m

    final String serverUrl = "http://localhost:" + server.getServiceAddress().getPort();

    // set test properties manually for a better overview of
    // what is tested
    Properties properties = new Properties();

    // have to set the BPC base url manually as this is the
    // mock server which gets port settings dynamically
    properties.put(BounceProxyPropertyKeys.PROPERTY_BOUNCE_PROXY_CONTROLLER_BASE_URL, serverUrl);
    properties.put(BounceProxyPropertyKeys.PROPERTY_BOUNCE_PROXY_ID, "X.Y");
    properties.put(BounceProxyPropertyKeys.PROPERTY_BOUNCEPROXY_URL_FOR_BPC, "http://joyn-bpX.muc/bp");
    properties.put(BounceProxyPropertyKeys.PROPERTY_BOUNCEPROXY_URL_FOR_CC, "http://joyn-bpX.de/bp");
    properties.put(BounceProxyPropertyKeys.PROPERTY_BOUNCE_PROXY_MONITORING_FREQUENCY_MS, "100");

    properties.put(BounceProxyPropertyKeys.PROPERTY_BOUNCE_PROXY_SEND_LIFECYCLE_REPORT_RETRY_INTERVAL_MS,
            "100");
    properties.put(BounceProxyPropertyKeys.PROPERTY_BOUNCE_PROXY_MAX_SEND_SHUTDOWN_TIME_SECS, "1");

    Injector injector = Guice.createInjector(new PropertyLoadingModule(properties),
            new ControlledBounceProxyModule() {
                @Override
                protected void configure() {
                    bind(ChannelService.class).to(DefaultBounceProxyChannelServiceImpl.class);
                    bind(BounceProxyLifecycleMonitor.class).to(MonitoringServiceClient.class);
                    bind(TimestampProvider.class).toInstance(mockTimestampProvider);
                    bind(BounceProxyInformation.class).toProvider(BounceProxyInformationProvider.class);
                }
            });

    reporter = injector.getInstance(MonitoringServiceClient.class);
}

From source file:com.asakusafw.yaess.jobqueue.client.HttpJobClientTest.java

/**
 * Initializes the test./*from  w  w  w.j  a v a 2s. c  o m*/
 * @throws Exception if some errors were occurred
 */
@Before
public void setUp() throws Exception {
    BasicHttpProcessor proc = new BasicHttpProcessor();
    proc.addInterceptor(new ResponseDate());
    proc.addInterceptor(new ResponseServer());
    proc.addInterceptor(new ResponseContent());
    proc.addInterceptor(new ResponseConnControl());
    proc.addInterceptor(new RequestBasicAuth());
    proc.addInterceptor(new ResponseBasicUnauthorized());
    server = new LocalTestServer(proc, null);
    server.start();
    InetSocketAddress address = server.getServiceAddress();
    baseUrl = new URL("http", address.getHostName(), address.getPort(), "/").toExternalForm();
}

From source file:org.mitre.dsmiley.httpproxy.ProxyServletTest.java

@Before
public void setUp() throws Exception {
    localTestServer = new LocalTestServer(null, null);
    localTestServer.start();//w ww . ja v a2 s.  c o  m
    localTestServer.register("/targetPath*", new RequestInfoHandler());//matches /targetPath and /targetPath/blahblah

    servletRunner = new ServletRunner();

    Properties servletProps = new Properties();
    servletProps.setProperty("http.protocol.handle-redirects", "false");
    servletProps.setProperty(ProxyServlet.P_LOG, "true");
    servletProps.setProperty(ProxyServlet.P_FORWARDEDFOR, "true");
    setUpServlet(servletProps);

    sc = servletRunner.newClient();
    sc.getClientProperties().setAutoRedirect(false);//don't want httpunit itself to redirect

}

From source file:org.sakaiproject.nakamura.docproxy.url.UrlRepositoryProcessorTest.java

@Before
public void setUp() throws Exception {
    when(node.getSession().getUserID()).thenReturn("ch1411");

    HashMap<String, Object> docProps = new HashMap<String, Object>();
    docProps.put("key1", "value1");
    docResult1 = new UrlDocumentResult(docPath1, "text/plain", 1000, docProps);
    docResult2 = new UrlDocumentResult(docPath2, "text/plain", 2000, null);
    docResult3 = new UrlDocumentResult(docPath3, "text/plain", 3000, null);
    docResult4 = new UrlDocumentResult(docPath4, "text/plain", 4000, null);
    docResult5 = new UrlDocumentResult(docPath5, "text/plain", 5000, null);

    docHandler = new DocumentRequestHandler(docResult1);
    metadataHandler = new MetadataRequestHandler(docResult1);
    removeHandler = new RemoveRequestHandler();
    searchHandler = new SearchRequestHandler(docResult1, docResult2, docResult3, docResult4, docResult5);
    updateHandler = new UpdateRequestHandler();

    // setup the local test server
    server = new LocalTestServer(null, null);
    server.register("/document", docHandler);
    server.register("/metadata", metadataHandler);
    server.register("/remove", removeHandler);
    server.register("/search", searchHandler);
    server.register("/update", updateHandler);
    server.start();/*from w ww.  j ava2 s .c o  m*/

    // setup & activate the url doc processor
    String serverUrl = "http://" + server.getServiceHostName() + ":" + server.getServicePort();

    Properties props = new Properties();
    props.put(UrlRepositoryProcessor.DOCUMENT_URL, serverUrl + "/document?p=");
    props.put(UrlRepositoryProcessor.METADATA_URL, serverUrl + "/metadata?p=");
    props.put(UrlRepositoryProcessor.REMOVE_URL, serverUrl + "/remove?p=");
    props.put(UrlRepositoryProcessor.SEARCH_URL, serverUrl + "/search");
    props.put(UrlRepositoryProcessor.UPDATE_URL, serverUrl + "/update?p=");
    props.put(UrlRepositoryProcessor.HMAC_HEADER, "X-HMAC");
    props.put(UrlRepositoryProcessor.SHARED_KEY, "superSecretSharedKey");

    ComponentContext context = mock(ComponentContext.class);
    when(context.getProperties()).thenReturn(props);

    processor = new UrlRepositoryProcessor();
    processor.activate(context);
}