Package org.xsocket.connection

Examples of org.xsocket.connection.IServer



  @Test
  public void testFragmentedPipeliningRequest() throws Exception {
   
    IServer server = new HttpServer(new RequestHandler());
    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");
   
    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);
    String body = con.readStringByLength(contentLength);
   
    Assert.assertTrue(header.indexOf("200") != -1);
    Assert.assertEquals("OK", body);


    header = con.readStringByDelimiter("\r\n\r\n") + "\r\n";
    contentLength = QAUtil.readContentLength(header);
    body = con.readStringByLength(contentLength);
   
    Assert.assertTrue(header.indexOf("200") != -1);
    Assert.assertEquals("OK", body);

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


 

  @Test
  public void testFragmentedPipeliningRequest2() throws Exception {
   
    IServer server = new HttpServer(new RequestHandler());
    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");

    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);
    String body = con.readStringByLength(contentLength);
   
    Assert.assertTrue(header.indexOf("200") != -1);
    Assert.assertEquals("OK", body);


    header = con.readStringByDelimiter("\r\n\r\n") + "\r\n";
    contentLength = QAUtil.readContentLength(header);
    body = con.readStringByLength(contentLength);
   
    Assert.assertTrue(header.indexOf("200") != -1);
    Assert.assertEquals("OK", body);

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


  @Test
  public void testFragmentedPipeliningRequest3() throws Exception {
   
    IServer server = new HttpServer(new RequestHandler());
    server.start();
   

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

    con.write("GET / HTTP/1.1\r\n" +
          "Host: localhost\r\n" +
          "User-");

    QAUtil.sleep(200);

    con.write("Agent: me\r\n" +
          "\r\n");

    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);
    String body = con.readStringByLength(contentLength);
   
    Assert.assertTrue(header.indexOf("200") != -1);
    Assert.assertEquals("OK", body);


    header = con.readStringByDelimiter("\r\n\r\n") + "\r\n";
    contentLength = QAUtil.readContentLength(header);
    body = con.readStringByLength(contentLength);
   
    Assert.assertTrue(header.indexOf("200") != -1);
    Assert.assertEquals("OK", body);

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

 

  @Test
  public void testFragmentedPipeliningRequest4() throws Exception {
   
    IServer server = new HttpServer(new RequestHandler());
    server.start();
   

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

    con.write("GET / HTTP/1.1\r\n" +
          "Host: localhost\r\n" +
          "User-");

    QAUtil.sleep(200);

    con.write("Agent: me\r\n" +
          "\r\nGET / HT");

   
    QAUtil.sleep(200);

    con.write("TP/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);
    String body = con.readStringByLength(contentLength);
   
    Assert.assertTrue(header.indexOf("200") != -1);
    Assert.assertEquals("OK", body);


    header = con.readStringByDelimiter("\r\n\r\n") + "\r\n";
    contentLength = QAUtil.readContentLength(header);
    body = con.readStringByLength(contentLength);
   
    Assert.assertTrue(header.indexOf("200") != -1);
    Assert.assertEquals("OK", body);

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

 
  @Test
  public void testMissingBlank() throws Exception {
   
    RequestHandler rh = new RequestHandler();
    IServer server = new HttpServer(rh);
    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);
    String body = con.readStringByLength(contentLength);
   
    Assert.assertTrue(header.indexOf("200") != -1);
    Assert.assertEquals("OK", body);
   
    Assert.assertEquals("localhost", rh.getRequest().getHeader("Host"));
   
    Set<String> headerNames = rh.getRequest().getHeaderNameSet();
    Assert.assertTrue(headerNames.remove("User-Agent"));
    Assert.assertTrue(headerNames.remove("Host"));
    Assert.assertTrue(headerNames.isEmpty());
 
    con.close();
    server.close();
  }
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 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

TOP

Related Classes of org.xsocket.connection.IServer

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.