Package org.xlightweb.client

Examples of org.xlightweb.client.HttpClientConnection$MessageHandler


    chain.addLast(fileSrv);
   
    IServer server = new HttpServer(chain);
    server.start();
   
    HttpClientConnection con = new HttpClientConnection("localhost", server.getLocalPort());
    con.setOption(IConnection.SO_RCVBUF, 64);
    con.suspendReceiving();

    FutureResponseHandler respHdl = new FutureResponseHandler();
    con.send(new GetRequest("/" + file.getName()), respHdl);

    QAUtil.sleep(1000);
    con.resumeReceiving();

   
    IHttpResponse response = respHdl.getResponse();
    Assert.assertEquals(200, response.getStatus());

    File tempFile = QAUtil.createTempfile();   
    RandomAccessFile raf = new RandomAccessFile(tempFile, "rw");
    FileChannel fc = raf.getChannel();
   
    BodyDataSource source = response.getBody();
    source.transferTo(fc);
   
    fc.close();
    raf.close();
   
    Assert.assertTrue(QAUtil.isEquals(file, tempFile));


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


       
        IServer server = new HttpServer(chain);
        server.start();
       
        // first request
        HttpClientConnection con = new HttpClientConnection("localhost", server.getLocalPort());
        IHttpResponse response = con.call(new GetRequest("http://localhost:" + server.getLocalPort() + "/" + file.getName()));
        Assert.assertEquals(200, response.getStatus());
        Assert.assertTrue(QAUtil.isEquals(file, response.getBody().readByteBuffer()));
        con.close();

       
        QAUtil.sleep(1000);
       
        // repeated request (served by cache!)
        con = new HttpClientConnection("localhost", server.getLocalPort());
        response = con.call(new GetRequest("http://localhost:" + server.getLocalPort() + "/" + file.getName()));
        Assert.assertEquals(200, response.getStatus());
        Assert.assertTrue(response.getHeader("X-Cache").startsWith("HIT - revalidated"));
        Assert.assertTrue(QAUtil.isEquals(file, response.getBody().readByteBuffer()));
        con.close();

       
        server.close();
    }
View Full Code Here

       
        IServer server = new HttpServer(chain);
        server.start();
       
        // first request
        HttpClientConnection con = new HttpClientConnection("localhost", server.getLocalPort());
        IHttpResponse response = con.call(new GetRequest("http://localhost:" + server.getLocalPort() + "/" + file.getName()));
        Assert.assertEquals(200, response.getStatus());
        Assert.assertNotNull(response.getHeader("Expires"));
        Assert.assertTrue(QAUtil.isEquals(file, response.getBody().readByteBuffer()));
        con.close();

       
        QAUtil.sleep(1000);
       
        // repeated request (served by cache!)
        con = new HttpClientConnection("localhost", server.getLocalPort());
        response = con.call(new GetRequest("http://localhost:" + server.getLocalPort() + "/" + file.getName()));
        Assert.assertEquals(200, response.getStatus());
        Assert.assertNotNull(response.getHeader("Expires"));
        Assert.assertTrue(response.getHeader("X-Cache").startsWith("HIT"));
        Assert.assertTrue(QAUtil.isEquals(file, response.getBody().readByteBuffer()));
        con.close();

       
        server.close();
    }
View Full Code Here

        RequestHandler hdl = new RequestHandler();
        HttpServer server = new HttpServer(hdl);
        server.start();

        HttpClientConnection con = new HttpClientConnection("localhost", server.getLocalPort());
       
       
        long start = System.currentTimeMillis();
       
       
        ResponseHandler respHdl = new ResponseHandler();
        con.send(new GetRequest("http://localhost:" + server.getLocalPort() + "/test?delay=2000"), respHdl);
       
        while (respHdl.getResponse() == null) {
            QAUtil.sleep(100);
        }
       
        long elapsed = System.currentTimeMillis() - start;
        System.out.println("elapsed " + elapsed);

        Assert.assertTrue(elapsed < 2000);

        IHttpResponse response = respHdl.getResponse();
        Assert.assertEquals(200, response.getStatus());
        Assert.assertFalse(response.getNonBlockingBody().isCompleteReceived());
       
        con.close();
        server.close();
    }
View Full Code Here

        RequestHandler hdl = new RequestHandler();
        HttpServer server = new HttpServer(hdl);
        server.start();

        HttpClientConnection con = new HttpClientConnection("localhost", server.getLocalPort());
       
       
        long start = System.currentTimeMillis();
       
       
        OnMesageResponseHandler respHdl = new OnMesageResponseHandler();
        con.send(new GetRequest("http://localhost:" + server.getLocalPort() + "/test?delay=2000"), respHdl);
       
        while (respHdl.getResponse() == null) {
            QAUtil.sleep(100);
        }
       
        long elapsed = System.currentTimeMillis() - start;
        System.out.println("elapsed " + elapsed);

        Assert.assertTrue(elapsed >= 2000);

        IHttpResponse response = respHdl.getResponse();
        Assert.assertEquals(200, response.getStatus());
        Assert.assertTrue(response.getNonBlockingBody().isCompleteReceived());
       
        con.close();
        server.close();
    }
View Full Code Here

       
        IServer server = new Server(dh);
        server.start();


        HttpClientConnection con = new HttpClientConnection("localhost", server.getLocalPort());
       
        OnMesageResponseHandler respHdl = new OnMesageResponseHandler();
        con.send(new GetRequest("http://localhost:" + server.getLocalPort() + "/test?delay=2000"), respHdl);
       
        while (respHdl.getException() == null) {
            QAUtil.sleep(100);
        }
       
        con.close();
        server.close();
    }
View Full Code Here

   
    IServer server = new HttpServer(new BadServerHandler());
    server.start();
   
   
    HttpClientConnection con = new HttpClientConnection("localhost", server.getLocalPort());
   
    ResponseHandler respHdl = new ResponseHandler();
    con.send(new GetRequest("/"), respHdl);
   
    QAUtil.sleep(1000);
   
    Assert.assertNotNull(respHdl.getException());
   
    con.close();
    server.close();
  }
View Full Code Here

   
    IServer server = new HttpServer(new BadServerHandler());
    server.start();
   
   
    HttpClientConnection con = new HttpClientConnection("localhost", server.getLocalPort());
   
    ResponseHandler2 respHdl = new ResponseHandler2();
    con.send(new GetRequest("/"), respHdl);
   
    QAUtil.sleep(1000);
   
    Assert.assertNotNull(respHdl.ioeRef.get());
    Assert.assertTrue(respHdl.ioeRef.get() instanceof ProtocolException);
   
    con.close();
    server.close();
  }
View Full Code Here

     
      private HttpClientConnection httpCon;
      private IOException ioe;
     
      public boolean onConnect(INonBlockingConnection connection) throws IOException, BufferUnderflowException, MaxReadSizeExceededException {
          httpCon = new HttpClientConnection(connection);
          return true;
      }
View Full Code Here

  public void testHttpConnection() throws Exception {
      HttpServer server = new HttpServer(new RequestHandler());
      server.start();
     
     
      HttpClientConnection con = new HttpClientConnection("localhost", server.getLocalPort());
      IFutureResponse futureResponse = con.send(new GetRequest("http://localhost:" + server.getLocalPort() + "/?pauseMillis=10"));

      IHttpResponse response = futureResponse.getResponse();
     
      Assert.assertTrue(futureResponse.isDone());
      Assert.assertFalse(futureResponse.isCancelled());
     
      Assert.assertEquals(200, response.getStatus());
     
      Assert.assertFalse(futureResponse.cancel(true));
      Assert.assertFalse(futureResponse.isCancelled());
     
      con.close();
      server.close();
  }
View Full Code Here

TOP

Related Classes of org.xlightweb.client.HttpClientConnection$MessageHandler

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.