Package org.xlightweb.server

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


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

 
   
    @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

  

    @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

 
  @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

               
                ds.write(new Event("second event", "1").toString());
            }
        };
       
        HttpServer server = new HttpServer(hdl);
        server.start();
       
       
        HttpClient client = new HttpClient();
       
        GetRequest request = new GetRequest("http://localhost:" + server.getLocalPort() + "/Events");
        request.setHeader("Accept", "text/event-stream");
       
        IHttpResponse response = client.call(request);
        BodyDataSource body = response.getBody();
       
View Full Code Here

                resp.setHeader("Cache-Control", "public, max-age=1000");
                exchange.send(resp);
            }
        };
       
        HttpServer server = new HttpServer(reqHdl);
        server.start();
       
        HttpClient httpClient = new HttpClient();
        httpClient.setCacheMaxSizeKB(100);
       
        IHttpResponse resp = httpClient.call(new GetRequest("http://localhost:" + server.getLocalPort() + "/"));
        Assert.assertEquals(200, resp.getStatus());
        Assert.assertEquals("test", resp.getBody().readString());
        Assert.assertNull(resp.getHeader(CacheHandler.XHEADER_NAME));
       
        QAUtil.sleep(1000);
       
        resp = httpClient.call(new GetRequest("http://localhost:" + server.getLocalPort() + "/"));
        Assert.assertEquals(200, resp.getStatus());
        Assert.assertTrue(resp.getHeader(CacheHandler.XHEADER_NAME).startsWith("HIT"));
        Assert.assertEquals("test", resp.getBody().readString());
       
       
        Assert.assertEquals(1, httpClient.getNumCacheHit());
        Assert.assertEquals(1, httpClient.getNumCacheMiss());
       
        httpClient.close();
        server.close();
  }  
View Full Code Here

                resp.setHeader("Cache-Control", "public, max-age=1000, must-revalidate");
                exchange.send(resp);
            }
        };
       
        HttpServer server = new HttpServer(reqHdl);
        server.start();
       
        HttpClient httpClient = new HttpClient();
        httpClient.setCacheMaxSizeKB(100);
       
        IHttpResponse resp = httpClient.call(new GetRequest("http://localhost:" + server.getLocalPort() + "/"));
        Assert.assertEquals(200, resp.getStatus());
        Assert.assertEquals("test", resp.getBody().readString());
        Assert.assertNull(resp.getHeader(CacheHandler.XHEADER_NAME));
       
        QAUtil.sleep(1000);
       
        resp = httpClient.call(new GetRequest("http://localhost:" + server.getLocalPort() + "/"));
        Assert.assertEquals(200, resp.getStatus());
        Assert.assertNull(resp.getHeader(CacheHandler.XHEADER_NAME));
        Assert.assertEquals("test", resp.getBody().readString());
       
       
        Assert.assertEquals(0, httpClient.getNumCacheHit());
        Assert.assertEquals(2, httpClient.getNumCacheMiss());
       
        httpClient.close();
        server.close();
    }  
View Full Code Here

                resp.setHeader("Cache-Control", "public");
                exchange.send(resp);
            }
        };
       
        HttpServer server = new HttpServer(reqHdl);
        server.start();
       
        HttpClient httpClient = new HttpClient();
        httpClient.setCacheMaxSizeKB(100);
       
        IHttpResponse resp = httpClient.call(new GetRequest("http://localhost:" + server.getLocalPort() + "/"));
        Assert.assertEquals(200, resp.getStatus());
        Assert.assertEquals("test", resp.getBody().readString());
        Assert.assertNull(resp.getHeader(CacheHandler.XHEADER_NAME));
       
        QAUtil.sleep(1000);
       
        resp = httpClient.call(new GetRequest("http://localhost:" + server.getLocalPort() + "/"));
        Assert.assertEquals(200, resp.getStatus());
        Assert.assertNull(resp.getHeader(CacheHandler.XHEADER_NAME));
        Assert.assertEquals("test", resp.getBody().readString());
       
       
        Assert.assertEquals(0, httpClient.getNumCacheHit());
        Assert.assertEquals(2, httpClient.getNumCacheMiss());
       
        httpClient.close();
        server.close();
    }    
View Full Code Here

 
    @Test
    public void testSimple() throws Exception {
       
        RequestHandler reqHdl = new RequestHandler();
        HttpServer server = new HttpServer(reqHdl);
        server.start();
       
        HttpClientConnection con = new HttpClientConnection("localhost", server.getLocalPort());
       
        FutureResponseHandler respHdl = new FutureResponseHandler();
        BodyDataSink dataSink = con.send(new HttpRequestHeader("POST", "http://localhost:" + server.getLocalPort() + "/"), respHdl);
       
        dataSink.write("test");
        QAUtil.sleep(200);
       
        dataSink.write("OneTwo");
        QAUtil.sleep(200);
       
        dataSink.write("End");
        dataSink.close();
       
        IHttpResponse response = respHdl.getResponse();
       
        Assert.assertEquals(200, response.getStatus());
        Assert.assertEquals("testOneTwoEnd", DataConverter.toString(reqHdl.getData()));
       
        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.