ServerSideKeepAliveTest.java :  » Web-Server » xsocket » org » xsocket » connection » http » Java Open Source

Java Open Source » Web Server » xsocket 
xsocket » org » xsocket » connection » http » ServerSideKeepAliveTest.java
/*
 *  Copyright (c) xsocket.org, 2006 - 2008. All rights reserved.
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2.1 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * Please refer to the LGPL license at: http://www.gnu.org/copyleft/lesser.txt
 * The latest copy of this software may be found on http://www.xsocket.org/
 */
package org.xsocket.connection.http;



import java.io.IOException;


import org.junit.Assert;
import org.junit.Test;

import org.xsocket.connection.IServer;
import org.xsocket.connection.ConnectionUtils;
import org.xsocket.connection.http.HttpResponse;
import org.xsocket.connection.http.client.GetRequest;
import org.xsocket.connection.http.client.HttpClientConnection;
import org.xsocket.connection.http.server.HttpServer;
import org.xsocket.connection.http.server.IHttpResponseContext;
import org.xsocket.connection.http.server.IHttpRequestHandler;




/**
*
* @author grro@xsocket.org
*/
public final class ServerSideKeepAliveTest  {



  @Test
  public void testMaxTransactions() throws Exception {
    IServer server = new HttpServer(new TransactionServerHandler());
    ConnectionUtils.start(server);
    
    HttpClientConnection con = new HttpClientConnection("localhost", server.getLocalPort());
    for (int i = 5; i > 0; --i) {
      HttpResponse response = con.call(new GetRequest("/"));
      if (i == 1) {
        String connection = response.getHeader("Connection");
        Assert.assertEquals("close", connection);
        
        QAUtil.sleep(400);
        Assert.assertFalse(con.isOpen());
        
      } else {
        String keepAlive = response.getHeader("Keep-alive");
        Assert.assertTrue(keepAlive.indexOf(Integer.toString(i - 1)) != -1);
        Assert.assertTrue(con.isOpen());
      }
    }

    server.close();  
  }
  
  
  @Test
  public void testRequestTimeout() throws Exception {
    IServer server = new HttpServer(new RequstTimeoutServerHandler());
    ConnectionUtils.start(server);
    
    HttpClientConnection con = new HttpClientConnection("localhost", server.getLocalPort());
    for (int i = 0; i < 3; i++) {
      QAUtil.sleep(200);
      HttpResponse response = con.call(new GetRequest("/"));
      Assert.assertEquals(200, response.getStatus());
      Assert.assertTrue(con.isOpen());
    }
    
    QAUtil.sleep(1000);
    Assert.assertFalse(con.isOpen());

    server.close();  
  }
  

  
  @Test
  public void testContainerManager() throws Exception {
    HttpServer server = new HttpServer(new ServerHandler());
    server.setMaxTransactions(5);
    ConnectionUtils.start(server);
    
    
    HttpClientConnection con = new HttpClientConnection("localhost", server.getLocalPort());
    for (int i = 5; i > 0; --i) {
      HttpResponse response = con.call(new GetRequest("/"));
      if (i == 1) {
        String connection = response.getHeader("Connection");
        Assert.assertEquals("close", connection);
        
        QAUtil.sleep(400);
        Assert.assertFalse(con.isOpen());
        
      } else {
        String keepAlive = response.getHeader("Keep-alive");
        Assert.assertTrue(keepAlive.indexOf(Integer.toString(i - 1)) != -1);
        Assert.assertTrue(con.isOpen());
      }
    }

    server.close();  
  }
  

  
  
  
  private static final class TransactionServerHandler implements IHttpRequestHandler {
    
    public void onRequest(HttpRequest request, IHttpResponseContext responseContext) throws IOException {
      responseContext.setMaxTransactions(5);
      responseContext.send(200);
    }
  }

  private static final class RequstTimeoutServerHandler implements IHttpRequestHandler {
    
    public void onRequest(HttpRequest request, IHttpResponseContext responseContext) throws IOException {
      responseContext.setRequestTimeoutMillis(500);
      responseContext.send(200);
    }
  }


  private static final class ServerHandler implements IHttpRequestHandler {
    
    public void onRequest(HttpRequest request, IHttpResponseContext responseContext) throws IOException {
      responseContext.send(200);
    }
  }

}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.