Package org.xlightweb.server

Examples of org.xlightweb.server.HttpServer


            public void onMessage(IWebSocketConnection con) throws IOException {
                con.writeMessage(new TextMessage("should not be sent"));
            }
        };
       
        HttpServer server = new HttpServer(hdl);
        server.start();
       
        HttpClient httpClient = new HttpClient();
       
        try {
            httpClient.openWebSocketConnection("ws://localhost:" + server.getLocalPort());
            Assert.fail("UnsupportedProtocolException expected");
        } catch (UnsupportedProtocolException expected) { }
               
        httpClient.close();
        server.close();
    }
View Full Code Here


   
   
   
    @Test
    public void testClientAndServerSSL() throws Exception {
        HttpServer server = new HttpServer(0, new MixedRequestHandler(), SSLTestContextFactory.getSSLContext(), true);
        server.start();
       
        HttpClient httpClient = new HttpClient(SSLTestContextFactory.getSSLContext());
       
        IWebSocketConnection webSocketConnection = httpClient.openWebSocketConnection("wss://localhost:" + server.getLocalPort());
       
        WebSocketMessage msg = webSocketConnection.readMessage();
        Assert.assertTrue(msg.isTextMessage());
        Assert.assertEquals("Hello you", msg.toString());
       
        webSocketConnection.writeMessage(new TextMessage("01234567890"));
       
        msg = webSocketConnection.readMessage();
        Assert.assertTrue(msg.isTextMessage());
        Assert.assertEquals("01234567890", msg.toString());
       
        httpClient.close();
        server.close();
    }
View Full Code Here

    rootCtx.addHandler("/chat/service/*", new ChatServerHandler());
   
    String basePath = ChatExample.class.getResource("").getFile();
    rootCtx.addHandler("/chat/*", new FileServiceRequestHandler(basePath, true));
   
    HttpServer server = new HttpServer(9090, rootCtx);
    server.run();
  }
View Full Code Here

  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

    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

  @Test
  public void testMaxTransactions() throws Exception {
      System.out.println("testMaxTransactions");
   
    IServer server = new HttpServer(new TransactionServerHandler());
    ConnectionUtils.start(server);

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

    for (int i = 5; i > 0; --i) {
   
      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);

      if (i == 1) {
        Assert.assertTrue(header.indexOf("Connection: close") != -1);
       
        QAUtil.sleep(400);
       
        try {
          con.readByte();
          Assert.fail("ClosedChannelException expected");
        } catch (ClosedChannelException expected) { }

       
      } else {
        int start = header.indexOf("Keep-Alive: max=");
        int end = header.indexOf("\r\n", start + 1);
        int count = Integer.parseInt(header.substring(start + "Keep-Alive: max=".length(), end));
        Assert.assertEquals(i - 1, count);
        Assert.assertTrue(con.isOpen());
      }
    }

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

 
  @Test
  public void testMaxTransactionsContainerManager() throws Exception {
      System.out.println("testMaxTransactionsContainerManager");
     
    HttpServer server = new HttpServer(new ServerHandler());
    server.setMaxTransactions(5);
    ConnectionUtils.start(server);
   
    IBlockingConnection con = new BlockingConnection("localhost", server.getLocalPort());

    for (int i = 5; i > 0; --i) {
   
      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);

      if (i == 1) {
        Assert.assertTrue(header.indexOf("Connection: close") != -1);
       
        QAUtil.sleep(400);
       
        try {
          con.readByte();
          Assert.fail("ClosedChannelException expected");
        } catch (ClosedChannelException expected) { }

       
      } else {
        int start = header.indexOf("Keep-Alive: max=");
        int end = header.indexOf("\r\n", start + 1);
        int count = Integer.parseInt(header.substring(start + "Keep-Alive: max=".length(), end));
        Assert.assertEquals(i - 1, count);
        Assert.assertTrue(con.isOpen());
      }
    }

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

  @Test
  public void testClientSideCloseHeader() throws Exception {
      System.out.println("testClientSideCloseHeader");
   
   
    IServer server = new HttpServer(new TransactionServerHandler());
    ConnectionUtils.start(server);

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

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


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

  @Test
  public void testRequestTimeout() throws Exception {
      System.out.println("testRequestTimeout");
     
    IServer server = new HttpServer(new RequestTimeoutServerHandler());
    server.start();
   
    IBlockingConnection con = new BlockingConnection("localhost", server.getLocalPort());

    for (int i = 0; i < 3; i++) {
      QAUtil.sleep(200);
      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);

      Assert.assertTrue(header.indexOf("200") != -1);
    }
   
    QAUtil.sleep(1500);

    try {
      con.readByte();
      Assert.fail("ClosedChannelException expected");
    } catch (ClosedChannelException expected) { }

    server.close()
  }
View Full Code Here

 
  @Test
  public void testHttp1_0_Keep_Alive() throws Exception {
      System.out.println("testHttp1_0_Keep_Alive");
     
    IServer server = new HttpServer(new ServerHandler());
    ConnectionUtils.start(server);
   
    IBlockingConnection con = new BlockingConnection("localhost", server.getLocalPort());

    QAUtil.sleep(200);
    con.write("GET / HTTP/1.0\r\n" +
          "Host: localhost\r\n" +
          "User-Agent: me\r\n" +
          "Connection: Keep-Alive\r\n" +
          "\r\n");
     
    String header = con.readStringByDelimiter("\r\n\r\n") + "\r\n";
    int contentLength = QAUtil.readContentLength(header);
    con.readStringByLength(contentLength);
   
    Assert.assertTrue(header.indexOf("HTTP/1.0") != -1);
    Assert.assertTrue(header.indexOf("Connection: close") != -1)// xSocket will ignore keep-alive if HTTP/1.0

    Assert.assertTrue(header.indexOf("200") != -1);

    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.