Examples of HttpServer


Examples of org.xlightweb.server.HttpServer

    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

Examples of org.xlightweb.server.HttpServer

    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

Examples of org.xlightweb.server.HttpServer

 
  @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

Examples of org.xlightweb.server.HttpServer

 
  @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

Examples of org.xlightweb.server.HttpServer

  @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

Examples of org.xlightweb.server.HttpServer

 
    @Ignore
  @Test
  public void testPooledWebServer() throws Exception {
   
    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 {
            HttpClient httpClient = new HttpClient();

            for (int j = 0; j< 1000; j++) {
              IHttpResponse response = httpClient.call(new GetRequest("http://localhost:" + server.getLocalPort() + "/"));
             
              if (response.getStatus() != 200) {
                System.out.println("status 200 expected. Got " + response);
                errors.add("status 200 expected. Got " + response.getStatus());
              }
             
              String body = response.getBlockingBody().readString();
              if (!body.equals("OK")) {
                System.out.println("content OK expected. Got " + body);
              }
            }
           
            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

Examples of org.xlightweb.server.HttpServer

public final class ClientSideCookieTest {
 
 
  @Test
  public void testRewriteCookie() throws Exception {
      HttpServer server = new HttpServer(new RequestHandler());
      server.start();
     
      HttpClient httpClient = new HttpClient();
     
      GetRequest request = new GetRequest("http://localhost:" + server.getLocalPort() + "/test");
      IHttpResponse response = httpClient.call(request);
      Assert.assertEquals(200, response.getStatus());
     
      request = new GetRequest("http://localhost:" + server.getLocalPort() + "/test");
      response = httpClient.call(request);
      Assert.assertEquals(200, response.getStatus());
        Assert.assertEquals("JSESSIONID=1", request.getHeader("Cookie"));
     
        request = new GetRequest("http://localhost:" + server.getLocalPort() + "/test");
        response = httpClient.call(request);
        Assert.assertEquals(200, response.getStatus());
        Assert.assertEquals("JSESSIONID=2", request.getHeader("Cookie"));

        request = new GetRequest("http://localhost:" + server.getLocalPort() + "/test");
        response = httpClient.call(request);
        Assert.assertEquals(200, response.getStatus());
        Assert.assertEquals("JSESSIONID=3", request.getHeader("Cookie"));

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

Examples of org.xlightweb.server.HttpServer

 
   
    @Test
    public void testWebSockets() throws Exception {
       
        HttpServer server = new HttpServer(new DualHandler());
        server.start();
       
        HttpClient httpClient = new HttpClient();
       
        IWebSocketConnection webSocketConnection = httpClient.openWebSocketConnection("ws://localhost:" +  server.getLocalPort());
       
        webSocketConnection.writeMessage(new TextMessage("GetData"));
        WebSocketMessage msg = webSocketConnection.readMessage();
        Assert.assertEquals("id: 556\r\ndata: 566;555\r\n\r\n", msg.toString());
        System.out.println(msg);

        webSocketConnection.writeMessage(new TextMessage("GetData"));
        msg = webSocketConnection.readMessage();
        Assert.assertEquals("id: 557\r\ndata: 567;556\r\n\r\n", msg.toString());
        System.out.println(msg);
       
        webSocketConnection.close();
        httpClient.close();
        server.close();
    }
View Full Code Here

Examples of org.xlightweb.server.HttpServer

  

    @Test
    public void testSSE() throws Exception {
       
        HttpServer server = new HttpServer(new DualHandler());
        server.start();
       
       
        HttpClient httpClient = new HttpClient();
       
        IEventDataSource eventSource = httpClient.openEventDataSource("http://localhost:" +  server.getLocalPort() + "/", false);
       
        Event event = eventSource.readMessage();
        Assert.assertEquals(": keep-alive\r\n\r\n", event.toString());
        System.out.println(event);
       
        event = eventSource.readMessage();
        Assert.assertEquals("id: 556\r\ndata: 566;555\r\n\r\n", event.toString());
        System.out.println(event);
       
        eventSource.close();
       
        httpClient.close();
       
        server.close();
    }
View Full Code Here

Examples of org.xlightweb.server.HttpServer

 
  @Test
  public void testLocal() throws Exception {
   
    IServer server = new HttpServer(new RequestHandler());
    server.start();
   
    HttpClient httpClient = new HttpClient();
    IHttpResponse resp = httpClient.call(new GetRequest("http://localhost:" + server.getLocalPort() + "/"));
    BodyDataSource body = resp.getBody();
   
    body.markReadPosition();
    Assert.assertEquals("line one", body.readStringByDelimiter("\r\n"));
   
    body.resetToReadMark();
    Assert.assertEquals("line one", body.readStringByDelimiter("\r\n"));
    Assert.assertEquals("line two", body.readStringByDelimiter("\r\n"));
   
   
   
    httpClient.close();
    server.close();
  }
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. 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.