Package org.xlightweb.server

Examples of org.xlightweb.server.HttpServer


    OnMessageReceivedHandler hdl = new OnMessageReceivedHandler();

    // start xSocket
    Context ctx = new Context("");
    ctx.addHandler("/ctx1/*", hdl);
    Server server = new HttpServer(ctx);
    ConnectionUtils.start(server);
   
    QAUtil.sleep(200);
    Assert.assertEquals(1, hdl.getCountOnInitCalled());
    Assert.assertEquals(0, hdl.getCountOnDestroyCalled());
     
    server.close();
    QAUtil.sleep(200);
    Assert.assertEquals(1, hdl.getCountOnInitCalled());
    Assert.assertEquals(1, hdl.getCountOnDestroyCalled());

  }
View Full Code Here


    NonthreadedHandler hdl = new NonthreadedHandler();

    // start xSocket
    Context ctx = new Context("");
    ctx.addHandler("/ctx1/*", hdl);
    Server server = new HttpServer(ctx);
    server.start();
   
   
    GetRequest req = new GetRequest("http://localhost:" + server.getLocalPort() + "/ctx1/test/test2");
    req.setHeader("Host", "localhost");
    req.setHeader("User-Agent", "me");

   
    IBlockingConnection con = new BlockingConnection("localhost", server.getLocalPort());

    con.write(req.toString());
   
    String header = con.readStringByDelimiter("\r\n\r\n") + "\r\n";
    int contentLength = QAUtil.readContentLength(header);
     
    String body = con.readStringByLength(contentLength);
    Assert.assertEquals("OK", body);

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


    // start xSocket
    Context ctx = new Context("");
    ctx.addHandler("/ctx1/*", new CompareParametersHandler());
    Server server = new HttpServer(ctx);
    ConnectionUtils.start(server);
   
   
   
    GetRequest reqXSocket = new GetRequest("http://localhost:" + server.getLocalPort() + "/ctx1/test/test2");
    reqXSocket.setHeader("Host", "localhost");
    reqXSocket.setHeader("User-Agent", "me");

    GetRequest reqJetty = new GetRequest("http://localhost:" + servletEngine.getLocalPort() + "/ctx1/test/test2");
    reqJetty.setHeader("Host", "localhost");
    reqJetty.setHeader("User-Agent", "me");

   
    IBlockingConnection conXSocket = new BlockingConnection("localhost", server.getLocalPort());
    IBlockingConnection conJetty = new BlockingConnection("localhost", servletEngine.getLocalPort());

    conXSocket.write(reqXSocket.toString());
   
    String header = conXSocket.readStringByDelimiter("\r\n\r\n");
    int contentLength = QAUtil.readContentLength(header);
     
    String bodyXSocket = conXSocket.readStringByLength(contentLength);
   
   
    conJetty.write(reqJetty.toString());
   
    String headerJetty = conJetty.readStringByDelimiter("\r\n\r\n");
    int contentLengthJetty = QAUtil.readContentLength(headerJetty);
     
    String bodyJetty = conJetty.readStringByLength(contentLengthJetty);
   
    Assert.assertEquals(bodyXSocket, bodyJetty);
   

    conXSocket.close();
    conJetty.close();
    servletEngine.stop();
    server.close();
  }
View Full Code Here


    // start xSocket
    Context ctx = new Context("/ctx0");
    ctx.addHandler("/ctx1/*", new CompareParametersHandler());
    Server server = new HttpServer(ctx);
    ConnectionUtils.start(server);
   
   
   
   
    GetRequest reqXSocket = new GetRequest("http://localhost:" + server.getLocalPort() + "/ctx0/ctx1/test/test2");
    reqXSocket.setHeader("Host", "localhost");
    reqXSocket.setHeader("User-Agent", "me");

   
    GetRequest reqJetty = new GetRequest("http://localhost:" + servletEngine.getLocalPort() + "/ctx0/ctx1/test/test2");
    reqJetty.setHeader("Host", "localhost");
    reqJetty.setHeader("User-Agent", "me");

   
    IBlockingConnection conXSocket = new BlockingConnection("localhost", server.getLocalPort());
    IBlockingConnection conJetty = new BlockingConnection("localhost", servletEngine.getLocalPort());

    conXSocket.write(reqXSocket.toString());
   
    String header = conXSocket.readStringByDelimiter("\r\n\r\n");
    int contentLength = QAUtil.readContentLength(header);
     
    String bodyXSocket = conXSocket.readStringByLength(contentLength);
   
   
    conJetty.write(reqJetty.toString());
   
    String headerJetty = conJetty.readStringByDelimiter("\r\n\r\n");
    int contentLengthJetty = QAUtil.readContentLength(headerJetty);
     
    String bodyJetty = conJetty.readStringByLength(contentLengthJetty);
   
    Assert.assertEquals(bodyXSocket, bodyJetty);

    conJetty.close();
    conXSocket.close();
    servletEngine.stop();
    server.close();
  }
View Full Code Here

    Context ctxSub = new Context(ctxRoot, "/ctx1");
   
    ctxRoot.addHandler("/ctx1/uuu/*", new DoNothingHandler());
   
    ctxSub.addHandler("/test/*", new CompareParametersHandler());
    Server server = new HttpServer(ctxRoot);
    ConnectionUtils.start(server);
   
   

    GetRequest reqXSocket = new GetRequest("http://localhost:" + server.getLocalPort() + "/ctx0/ctx1/test/test2");
    reqXSocket.setHeader("Host", "localhost");
    reqXSocket.setHeader("User-Agent", "me");

   
    GetRequest reqJetty = new GetRequest("http://localhost:" + jettyServer.getConnectors()[0].getLocalPort() + "/ctx0/ctx1/test/test2");
    reqJetty.setHeader("Host", "localhost");
    reqJetty.setHeader("User-Agent", "me");

   
    IBlockingConnection conXSocket = new BlockingConnection("localhost", server.getLocalPort());
    IBlockingConnection conJetty = new BlockingConnection("localhost", jettyServer.getConnectors()[0].getLocalPort());

    conXSocket.write(reqXSocket.toString());
   
    String header = conXSocket.readStringByDelimiter("\r\n\r\n");
    int contentLength = QAUtil.readContentLength(header);
     
    String bodyXSocket = conXSocket.readStringByLength(contentLength);
   
   
    conJetty.write(reqJetty.toString());
   
    String headerJetty = conJetty.readStringByDelimiter("\r\n\r\n");
    int contentLengthJetty = QAUtil.readContentLength(headerJetty);
     
    String bodyJetty = conJetty.readStringByLength(contentLengthJetty);
   
    Assert.assertEquals(bodyXSocket, bodyJetty);

    conJetty.close();
    conXSocket.close();
   
    jettyServer.stop();
    server.close();
  }
View Full Code Here

    MHandler hdl = new MHandler();

    // start xSocket
    Context ctx = new Context("");
    ctx.addHandler("/ctx1/*", hdl);
    HttpServer server = new HttpServer(ctx);
    server.addConnectionHandler(hdl);
    server.start();
   
   
    GetRequest req = new GetRequest("http://localhost:" + server.getLocalPort() + "/ctx1/test/test2");
    req.setHeader("Host", "localhost");
    req.setHeader("User-Agent", "me");

   
    IBlockingConnection con = new BlockingConnection("localhost", server.getLocalPort());

    con.write(req.toString());

    String header = con.readStringByDelimiter("\r\n\r\n") + "\r\n";
    int contentLength = QAUtil.readContentLength(header);
    String body = con.readStringByLength(contentLength);
   
    Assert.assertTrue(header.indexOf("200") != -1);
    Assert.assertEquals("OK", body);
   
   
    Assert.assertNotNull(hdl.getRequest());
    Assert.assertTrue(hdl.getOnRequestThreadname().startsWith("xServerPool"));
   
    Assert.assertEquals(1, hdl.getCountOnConnect());
    Assert.assertTrue(hdl.getOnConnectThreadname().startsWith("xServerPool"));
   
    con.close();
    QAUtil.sleep(200);

    Assert.assertEquals(1, hdl.getCountOnDisconnect());
    Assert.assertTrue(hdl.getOnConnectThreadname().startsWith("xServerPool"));
   
   
    server.close();
  }
View Full Code Here

    MHandler hdl = new MHandler();

    // start xSocket
    Context ctx = new Context("");
    ctx.addHandler("/ctx1/*", hdl);
    HttpServer server = new HttpServer(ctx);
   
    server.setRequestTimeoutMillis(1000);
   
    server.start();
   
   
   
    IBlockingConnection con = new BlockingConnection("localhost", server.getLocalPort());



    QAUtil.sleep(1500);

    Assert.assertEquals(1, hdl.getCountOnRequestTimeout());
    if (!hdl.getOnRequestTimeoutThreadname().startsWith("xServerPool")) {
      System.out.println("handler executed by " + hdl.getOnRequestTimeoutThreadname() + " not by xServerPool...");
      Assert.fail("handler executed by " + hdl.getOnRequestTimeoutThreadname() + " not by xServerPool...");
    }
   
    con.close();
    server.close();
  }
View Full Code Here

 
  @Ignore
  @Test
  public void testApacheClientPooledWebServer() throws Exception {
   
    final HttpServer server = new HttpServer(new RequestHandler());
    server.start();
   
 
    for (int i =0; i < 5; i++) {
      new Thread() {
        @Override
        public void run() {

          running.incrementAndGet();
          try {
            org.apache.commons.httpclient.HttpClient httpClient = new org.apache.commons.httpclient.HttpClient();
            httpClient.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(0, false));
           
            for (int j = 0; j< 1000; j++) {
             
              GetMethod getMeth = new GetMethod("http://localhost:" + server.getLocalPort() + "/");
              httpClient.executeMethod(getMeth);
             
              Assert.assertEquals(200, getMeth.getStatusCode());
              Assert.assertEquals("OK", getMeth.getResponseBodyAsString());
             
              getMeth.releaseConnection();
            }

           
          } catch (Exception e) {
            e.printStackTrace();
            errors.add(e.toString());
           
          } finally {
            running.decrementAndGet();
          }
         
        }
      }.start();
    }

    do {
      QAUtil.sleep(200);
    } while (running.get() > 0);
   
    for (String error : errors) {
      System.out.println(error);
    }
   
    Assert.assertTrue(errors.isEmpty());
   
    server.close();
  }
View Full Code Here

 
  @Ignore
  @Test
  public void testPooledWebServer() throws Exception {
   
    final HttpServer server = new HttpServer(new RequestHandler());
    server.start();
   
 
    for (int i =0; i < 5; i++) {
      new Thread() {
        @Override
        public void run() {

          running.incrementAndGet();
          try {
            HttpClient httpClient = new HttpClient();
            httpClient.setMaxRetries(0);

            for (int j = 0; j< 1000; j++) {
              IHttpResponse response = httpClient.call(new GetRequest("http://localhost:" + server.getLocalPort() + "/"));
             
              Assert.assertEquals(200, response.getStatus());
              Assert.assertEquals("OK", response.getBlockingBody().readString());
            }
           
            httpClient.close();

           
          } catch (Exception e) {
            e.printStackTrace();
            errors.add(e.toString());
           
          } finally {
            running.decrementAndGet();
          }
         
        }
      }.start();
    }

    do {
      QAUtil.sleep(200);
    } while (running.get() > 0);
   
    for (String error : errors) {
      System.out.println(error);
    }
   
    Assert.assertTrue(errors.isEmpty());
   
    server.close();
  }
View Full Code Here

  @Test
  public void testApacheClientPooledWebServer() throws Exception {
   
    System.out.println("testApacheClientPooledWebServer");
   
    final HttpServer server = new HttpServer(new RequestHandler());
    server.start();
   
 
    for (int i =0; i < 3; i++) {
      new Thread() {
        @Override
        public void run() {

          running.incrementAndGet();
          try {
            org.apache.commons.httpclient.HttpClient httpClient = new org.apache.commons.httpclient.HttpClient();
            httpClient.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(0, false));
            for (int j = 0; j< 1000; j++) {
             
              GetMethod getMeth = new GetMethod("http://localhost:" + server.getLocalPort() + "/");
              httpClient.executeMethod(getMeth);
             
              Assert.assertEquals(200, getMeth.getStatusCode());
              Assert.assertEquals("OK", getMeth.getResponseBodyAsString());
             
              getMeth.releaseConnection();
            }

           
          } catch (Exception e) {
            e.printStackTrace();
            errors.add(e.toString());
           
          } finally {
            running.decrementAndGet();
          }
         
        }
      }.start();
    }

    do {
      QAUtil.sleep(200);
    } while (running.get() > 0);
   
    for (String error : errors) {
      System.out.println(error);
    }
   
    Assert.assertTrue(errors.isEmpty());
   
    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.