Package org.xlightweb.server

Examples of org.xlightweb.server.HttpServer


   
    @Test
    public void testFile() throws Exception {
       
        RequestHandler reqHdl = new RequestHandler();
        HttpServer server = new HttpServer(reqHdl);
        server.start();
       
        HttpClientConnection con = new HttpClientConnection("localhost", server.getLocalPort());
       
        File file = QAUtil.createTestfile_4k();
       
        FutureResponseHandler respHdl = new FutureResponseHandler();
        BodyDataSink dataSink = con.send(new HttpRequestHeader("POST", "http://localhost:" + server.getLocalPort() + "/"), respHdl);
       
       
        RandomAccessFile raf = new RandomAccessFile(file, "r");
        FileChannel fc = raf.getChannel();
        dataSink.transferFrom(fc);
        fc.close();
        raf.close();
        dataSink.close();
       
        IHttpResponse response = respHdl.getResponse();
       
        Assert.assertEquals(200, response.getStatus());
        Assert.assertTrue(QAUtil.isEquals(file, reqHdl.getData()));
       
        file.delete();
        con.close();
        server.close();
    }
View Full Code Here


   
    @Test
    public void testFile2() throws Exception {
       
        RequestHandler2 reqHdl = new RequestHandler2();
        HttpServer server = new HttpServer(reqHdl);
        server.start();
       
        HttpClientConnection con = new HttpClientConnection("localhost", server.getLocalPort());
       
        File file = QAUtil.createTestfile_4k();
       
        FutureResponseHandler respHdl = new FutureResponseHandler();
        BodyDataSink dataSink = con.send(new HttpRequestHeader("POST", "http://localhost:" + server.getLocalPort() + "/"), respHdl);
       
       
        RandomAccessFile raf = new RandomAccessFile(file, "r");
        FileChannel fc = raf.getChannel();
        dataSink.transferFrom(fc);
        fc.close();
        raf.close();
        dataSink.close();
       
        IHttpResponse response = respHdl.getResponse();
       
        Assert.assertEquals(200, response.getStatus());
        Assert.assertTrue(QAUtil.isEquals(file, "ISO-8859-1", reqHdl.getData()));
       
        file.delete();
        con.close();
        server.close();
    }
View Full Code Here

public final class HttpClientCookieTest  {

  @Test
  public void testMultipleCookies() throws Exception {
   
    HttpServer server = new HttpServer(new RequestHandler());
    server.start();
   
    HttpClient httpClient = new HttpClient();
    httpClient.setAutoHandleCookies(true);
   
    IHttpRequest req = new GetRequest("http://localhost:" + server.getLocalPort() + "/");
    httpClient.call(req);
   
    req = new GetRequest("http://localhost:" + server.getLocalPort() + "/");
    httpClient.call(req);
   
    Assert.assertEquals("name1=1; name2=2", req.getHeader("Cookie"));
   
    httpClient.close();
    server.close();
  }
View Full Code Here

  public void testClientInMemory() throws Exception {
   
    WebContainer servletEngine = new WebContainer(new HeaderInfoServlet());
    servletEngine.start();
   
    IServer server = new HttpServer(new HeaderInfoServerHandler());
    ConnectionUtils.start(server);
    int xSocketPort = server.getLocalPort();

   
    // warm up
    System.out.println("warm up jetty");
    perform(3, 100, servletEngine.getLocalPort(), null);
    QAUtil.sleep(200);
   
    System.out.println("warm up xSocket");
    perform(3, 100, xSocketPort, null);
    QAUtil.sleep(200);
   
   
    System.out.println("run jetty");
    int elapsedJetty = perform(4, 300, servletEngine.getLocalPort(), null);
    QAUtil.sleep(200);
   
    System.out.println("run xSocket");
    int elapsedXSocket = perform(4, 300, xSocketPort, null);
    QAUtil.sleep(200);
   
   
    server.close();
    servletEngine.stop();
   
    printResult("in memory", elapsedJetty, elapsedXSocket, 30);
  }
View Full Code Here

  public void testClientFile() throws Exception {
   
    WebContainer servletEngine = new WebContainer(new DocServlet());
    servletEngine.start();
   
    IServer server = new HttpServer(new DocServerHandler());
    ConnectionUtils.start(server);
    int xSocketPort = server.getLocalPort();

   
    // warm up
    System.out.println("warm up jetty");
    perform(5, 200, servletEngine.getLocalPort(), 3232);
    QAUtil.sleep(200);
   
    System.out.println("warm up xSocket");
    perform(5, 20, xSocketPort, 3232);
    QAUtil.sleep(200);
   
   
    System.out.println("run jetty");
    int elapsedJetty = perform(5, 100, servletEngine.getLocalPort(), 3232);
    QAUtil.sleep(200);
   
    System.out.println("run xSocket");
    int elapsedXSocket = perform(5, 100, xSocketPort, 3232);
    QAUtil.sleep(200);
   
   
    server.close();
    servletEngine.stop();
   
    printResult("file test", elapsedJetty, elapsedXSocket, 30);
  }
View Full Code Here

 
    @Test
    public void testReturnOnHeader() throws Exception {

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

        HttpClient httpClient = new HttpClient();
       
        long start = System.currentTimeMillis();
        IHttpResponse response = httpClient.call(new GetRequest("http://localhost:" + server.getLocalPort() + "/test?delay=2000"));
       
        long elapsed = System.currentTimeMillis() - start;
        System.out.println("elapsed " + elapsed);
       
        Assert.assertTrue(elapsed < 2000);
        Assert.assertEquals(200, response.getStatus());
        Assert.assertFalse(response.getNonBlockingBody().isCompleteReceived());
       
        httpClient.close();
        server.close();
    }
View Full Code Here

   
    @Test
    public void testReturnOnMessage() throws Exception {

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

        HttpClient httpClient = new HttpClient();
        httpClient.setCallReturnOnMessage(true);
       
        long start = System.currentTimeMillis();
        IHttpResponse response = httpClient.call(new GetRequest("http://localhost:" + server.getLocalPort() + "/test?delay=2000"));
       
        long elapsed = System.currentTimeMillis() - start;
        System.out.println("elapsed " + elapsed);
       
        Assert.assertTrue(elapsed >= 2000);
        Assert.assertEquals(200, response.getStatus());
        Assert.assertTrue(response.getNonBlockingBody().isCompleteReceived());
       
        httpClient.close();
        server.close();
    }
View Full Code Here

    root.addLast(h1);
   
    RequestHandler h2 = new RequestHandler();
    root.addLast(h2);
   
    IServer server = new HttpServer(root);
    ConnectionUtils.start(server);
   
    Assert.assertEquals(1, h1.getCountOnInitCalled());
    Assert.assertEquals(1, h2.getCountOnInitCalled());
   
    server.close();
   
    Assert.assertEquals(1, h1.getCountOnDestroyCalled());
    Assert.assertEquals(1, h2.getCountOnDestroyCalled());
  }
View Full Code Here

    root.addLast(h1);
   
    RequestHandler h2 = new RequestHandler();
    root.addLast(h2);
   
    IServer server = new HttpServer(root);
    server.start();
   
   
    IBlockingConnection con = new BlockingConnection("localhost", server.getLocalPort());

    con.write("GET / HTTP/1.1\r\n" +
          "Host: localhost\r\n" +
          "User-Agent: me\r\n" +
          "\r\n");
     
    String header = con.readStringByDelimiter("\r\n\r\n") + "\r\n";
    int contentLength = QAUtil.readContentLength(header);
     
    con.readByteBufferByLength(contentLength);

   
    QAUtil.sleep(400);
   
    if (h1.countOnRequestCalled() != 1) {
      String msg = "RequestFilter should haven been called once not " + h1.countOnRequestCalled();
      System.out.println(msg);
      Assert.fail(msg)
    }
   
   
    if (h2.countOnRequestCalled() != 1) {
      String msg = "RequestHandler should haven been called once not " + h2.countOnRequestCalled();
      System.out.println(msg);
      Assert.fail(msg)
    }

   
    if (!h2.getOnRequestThreadname().startsWith("xServerPool")) {
      String msg = "RequestHandler should be executed by xServerPool not by " + h2.getOnRequestThreadname();
      System.out.println(msg);
      Assert.fail(msg);
    }
   
    con.close();
    server.close();
  }
View Full Code Here

    root.addLast(h1);
   
    NonThreadedRequestHandler h2 = new NonThreadedRequestHandler();
    root.addLast(h2);
   
    IServer server = new HttpServer(root);
    server.start();
   
    IBlockingConnection con = new BlockingConnection("localhost", server.getLocalPort());

    con.write("GET / HTTP/1.1\r\n" +
          "Host: localhost\r\n" +
          "User-Agent: me\r\n" +
          "\r\n");
     
    String header = con.readStringByDelimiter("\r\n\r\n") + "\r\n";
    int contentLength = QAUtil.readContentLength(header);
     
    con.readByteBufferByLength(contentLength);
   
   
    QAUtil.sleep(200);
   
    Assert.assertEquals(1, h1.countOnRequestCalled());
    Assert.assertFalse(h1.onRequestThreadname.startsWith("xServerPool"));
    Assert.assertEquals(1, h2.countOnRequestCalled());
    Assert.assertFalse(h2.onRequestThreadname.startsWith("xServerPool"));
   
    con.close();
    server.close();
  }
View Full Code Here

TOP

Related Classes of org.xlightweb.server.HttpServer

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.