Package org.xlightweb.client

Examples of org.xlightweb.client.HttpClient


       
       
        HttpServer server = new HttpServer(reqHdl);
        server.start();
       
        HttpClient httpClient = new HttpClient();
        httpClient.setCacheMaxSizeKB(100);
       
        IHttpResponse resp = httpClient.call(new GetRequest("http://localhost:" + server.getLocalPort() + "/"));
        Assert.assertEquals(200, resp.getStatus());
        Assert.assertEquals("test", resp.getBody().readString());
        Assert.assertNull(resp.getHeader(CacheHandler.XHEADER_NAME));
        String lastModified = resp.getHeader("Last-Modified");
       
        QAUtil.sleep(1000);
       
        IHttpRequest request = new GetRequest("http://localhost:" + server.getLocalPort() + "/");
        request.setHeader("If-Modified-Since", lastModified);
        resp = httpClient.call(request);
        Assert.assertEquals(304, resp.getStatus());
        Assert.assertNull(resp.getHeader(CacheHandler.XHEADER_NAME));
       
        QAUtil.sleep(1000);
       
        Assert.assertEquals(0, httpClient.getNumCacheHit());
        Assert.assertEquals(1, httpClient.getNumCacheMiss());
       
        httpClient.close();
        server.close();
    }  
View Full Code Here


  @Test
  public void testPoolCall() throws Exception {
    System.setProperty("org.xlightweb.showDetailedError", "true");
 
    final HttpClient httpClient = new HttpClient();

    final IServer server = new HttpServer(new EchoHandler());
    server.start();

    for (int i = 0; i < 3; i++) {
      Thread t = new Thread() {

        @Override
        public void run() {

          running.incrementAndGet();
          try {
            for (int j = 0; j < 50; j++) {
              IHttpResponse response = httpClient.call(new PostRequest("http://localhost:" + server.getLocalPort() + "/", "text/plain", "test"));
              Assert.assertEquals(200, response.getStatus());
              Assert.assertEquals("test", response.getBody().readString());
            }
          } catch (Exception e) {
            errors.add(e.toString());
           
          } finally {
            running.decrementAndGet();
          }
        }
      };
      t.start();
    }

    do {
      QAUtil.sleep(200);
    } while (running.get() > 0);

    for (String error : errors) {
      System.out.println(error);
    }

    Assert.assertTrue(errors.isEmpty());
    Assert.assertTrue(httpClient.getNumCreated() <= 10);
    Assert.assertEquals(0, httpClient.getNumDestroyed());

    httpClient.close();
    server.close();
  }
View Full Code Here

    }



    public void onInit() {
      httpClient = new HttpClient();
      httpClient.setResponseTimeoutMillis(responseTimeoutSec * 1000L);
      httpClient.setBodyDataReceiveTimeoutMillis(responseTimeoutSec * 1000L);
    }
 
View Full Code Here

  public void testPoolSend() throws Exception {
    System.out.println("testPoolSend");
    System.setProperty("org.xlightweb.showDetailedError", "true");

   
    final HttpClient httpClient = new HttpClient();

    final IServer server = new HttpServer(new EchoHandler());
    ConnectionUtils.start(server);


    for (int i = 0; i < 5; i++) {
      Thread t = new Thread() {

        @Override
        public void run() {

          running.incrementAndGet();
          try {
            for (int j = 0; j < 30; j++) {
              IHttpResponseHandler hdl = new IHttpResponseHandler() {
                public void onResponse(IHttpResponse response) throws IOException {
                  openResponses.incrementAndGet();
                }
               
                public void onException(IOException ioe) {
                }
              };
             
              openResponses.decrementAndGet();
              httpClient.send(new PostRequest("http://localhost:" + server.getLocalPort() + "/", "text/plian", "test"), hdl);
              QAUtil.sleep(10);
            }
          } catch (Exception e) {
            errors.add(e.toString());
          }

          running.decrementAndGet();
        }
      };
      t.start();
    }

    do {
      QAUtil.sleep(200);
    } while ((running.get() > 0) || (openResponses.get() > 0));

   
    for (String error : errors) {
      System.out.println(error);
    }

    Assert.assertTrue(errors.isEmpty());

    Assert.assertTrue(httpClient.getNumDestroyed() == 0);


    server.close();
    httpClient.close();
  }
View Full Code Here

    server.setTransactionLogMaxSize(1000);
    server.start();

    ConnectionUtils.registerMBean(server);
   
    HttpClient httpClient = new HttpClient();

    IHttpResponse response = httpClient.call(new GetRequest("http://localhost:" + server.getLocalPort() + "/s/"));
    Assert.assertEquals(200, response.getStatus());

    QAUtil.sleep(1000);

   
        Assert.assertEquals(0, (int) server.getTransactionsPending());
    Assert.assertEquals(1, server.getTransactionInfos().size());
    for (String info : server.getTransactionInfos()) {
        System.out.println(info);
        Assert.assertTrue(info.indexOf("(NO BODY) -> 200 OK ") != -1);
    }

 
   
    httpClient.close();
    server.close();
  }
View Full Code Here

          server.setTransactionLogMaxSize(1000);
          server.start();

          ConnectionUtils.registerMBean(server);
         
          HttpClient httpClient = new HttpClient();

          IHttpResponse response = httpClient.call(new PostRequest("http://localhost:" + server.getLocalPort() + "/s/", new NameValuePair("param", "value"), new NameValuePair("param2", "value2")));
          Assert.assertEquals(200, response.getStatus());

          QAUtil.sleep(1000);

         
          Assert.assertEquals(0, (int) server.getTransactionsPending());
          Assert.assertEquals(1, server.getTransactionInfos().size());
          for (String info : server.getTransactionInfos()) {
              System.out.println(info);
              Assert.assertTrue(info.indexOf("-> 200 OK ") != -1);
          }

     
         
          httpClient.close();
          server.close();
      }
View Full Code Here

        RequestHandler hdl = new RequestHandler();
        HttpServer server = new HttpServer(hdl);
        server.start();

        HttpClient httpClient = new HttpClient();
       
        long start = System.currentTimeMillis();
       
       
        OnMesageResponseHandler respHdl = new OnMesageResponseHandler();
        httpClient.send(new GetRequest("http://localhost:" + server.getLocalPort() + "/test?delay=2000"), respHdl);
       
        while (respHdl.getResponse() == null) {
            QAUtil.sleep(100);
        }
       
        long elapsed = System.currentTimeMillis() - start;
        System.out.println("elapsed " + elapsed);

        Assert.assertTrue(elapsed >= 2000);

        IHttpResponse response = respHdl.getResponse();
        Assert.assertEquals(200, response.getStatus());
        Assert.assertTrue(response.getNonBlockingBody().isCompleteReceived());
       
        httpClient.close();
        server.close();
    }
View Full Code Here

       
        IServer server = new Server(dh);
        server.start();


        HttpClient httpClient = new HttpClient();
       
        OnMesageResponseHandler respHdl = new OnMesageResponseHandler();
        httpClient.send(new GetRequest("http://localhost:" + server.getLocalPort() + "/test?delay=2000"), respHdl);
       
        while (respHdl.getException() == null) {
            QAUtil.sleep(100);
        }
       
        httpClient.close();
        server.close();
    }
View Full Code Here

       
        IServer server = new Server(dh);
        server.start();


        HttpClient httpClient = new HttpClient();
        httpClient.setAutoHandleCookies(false);
        httpClient.setMaxRetries(0);
       
        OnMesageResponseHandler respHdl = new OnMesageResponseHandler();
        httpClient.send(new GetRequest("http://localhost:" + server.getLocalPort() + "/test?delay=2000"), respHdl);
       
        while (respHdl.getException() == null) {
            QAUtil.sleep(100);
        }
       
        httpClient.close();
        server.close();
    }
View Full Code Here

       
        IServer server = new Server(dh);
        server.start();


        HttpClient httpClient = new HttpClient();
       
        OnMesageResponseHandler respHdl = new OnMesageResponseHandler();
        httpClient.send(new GetRequest("http://localhost:" + server.getLocalPort() + "/test?delay=2000"), respHdl);
       
        while (respHdl.getException() == null) {
            QAUtil.sleep(100);
        }
       
        httpClient.close();
        server.close();
    }
View Full Code Here

TOP

Related Classes of org.xlightweb.client.HttpClient

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.