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

Java Open Source » Web Server » xsocket 
xsocket » org » xsocket » connection » http » BodyHandlerTest.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.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.BufferUnderflowException;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;
import java.util.logging.Level;

import javax.net.ssl.SSLContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


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

import org.xsocket.Execution;
import org.xsocket.MaxReadSizeExceededException;
import org.xsocket.connection.ConnectionUtils;
import org.xsocket.connection.IDataHandler;
import org.xsocket.connection.INonBlockingConnection;
import org.xsocket.connection.IServer;
import org.xsocket.connection.Server;
import org.xsocket.connection.IConnection.FlushMode;
import org.xsocket.connection.http.BlockingBodyDataSource;
import org.xsocket.connection.http.BodyDataSink;
import org.xsocket.connection.http.HttpRequestHeader;
import org.xsocket.connection.http.HttpResponse;
import org.xsocket.connection.http.client.GetRequest;
import org.xsocket.connection.http.client.HttpClient;
import org.xsocket.connection.http.client.HttpClientConnection;
import org.xsocket.connection.http.client.IHttpClientEndpoint;
import org.xsocket.connection.http.client.IHttpResponseHandler;
import org.xsocket.connection.http.client.PostRequest;
import org.xsocket.connection.http.server.HttpServer;
import org.xsocket.connection.http.server.HttpServerConnection;
import org.xsocket.connection.http.server.IHttpResponseContext;
import org.xsocket.connection.http.server.IHttpRequestHandler;





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


  @Test
  public void testClientMutlithreaded() throws Exception {

    Thread.currentThread().setName("testThread");
    
    IServer server = new HttpServer(new HeaderInfoServerHandler());
    ConnectionUtils.start(server);

    HttpClientConnection con = new HttpClientConnection("localhost", server.getLocalPort());
    
    HttpResponse response = con.call(new GetRequest("/"));

    MultithreadedBodyHandler bh = new MultithreadedBodyHandler();
    response.getNonBlockingBody().setDataHandler(bh);
    
    QAUtil.sleep(300);
    
    Assert.assertTrue(bh.getCountCalled() > 0);
    Assert.assertFalse(bh.getThreadname().startsWith("xDispatcher") || bh.getThreadname().startsWith("testThread"));
    

    con.close();
    server.close();
  }

  
  

  @Test
  public void testClientSinglethreaded() throws Exception {

    Thread.currentThread().setName("testThread");
    
    IServer server = new HttpServer(new HeaderInfoServerHandler());
    ConnectionUtils.start(server);

    HttpClientConnection con = new HttpClientConnection("localhost", server.getLocalPort());
    
    HttpResponse response = con.call(new GetRequest("/"));

    NonthreadedBodyHandler bh = new NonthreadedBodyHandler();
    response.getNonBlockingBody().setDataHandler(bh);
    
    QAUtil.sleep(300);
    
    Assert.assertTrue(bh.getCountCalled() > 0);
    Assert.assertTrue(bh.getThreadname().startsWith("xDispatcher") || bh.getThreadname().startsWith("testThread"));
    

    con.close();
    server.close();
  }
  
  
  
  
  @Test
  public void testClientReadError() throws Exception {

    IDataHandler errorneousServer = new IDataHandler() {
      
      public boolean onData(INonBlockingConnection connection) throws IOException, BufferUnderflowException, MaxReadSizeExceededException {

        connection.setAutoflush(false);
        
        // read request
        connection.readStringByDelimiter("\r\n\r\n");
        
        // write response
        connection.write("HTTP/1.1 200 OK\r\n");
        connection.write("Transfer-Encoding: chunked\r\n");
        connection.write("\r\n");
        connection.write("2\r\n");
        connection.write("12\r\n");
        connection.write("P\r\n");  // write invalid char
        connection.flush();
        
        return true;
      }
    };
    
    IServer server = new Server(errorneousServer);
    ConnectionUtils.start(server);

    HttpClientConnection con = new HttpClientConnection("localhost", server.getLocalPort());
    
    HttpResponse response = con.call(new GetRequest("/"));

    BodyHandler bh = new BodyHandler();
    response.getNonBlockingBody().setDataHandler(bh);
  
    QAUtil.sleep(1000);
    
    
    try {
      bh.getNonBlockingBodyDataSource().available();  
      Assert.fail("IOException expected");
    } catch (IOException expected) { }
    

    con.close();
    server.close();
  }

  
  private static final class BodyHandler implements IBodyDataHandler {
    
    private NonBlockingBodyDataSource bodyDataSource = null;
    
    public boolean onData(NonBlockingBodyDataSource bodyDataSource) {
      this.bodyDataSource = bodyDataSource;
      return true;
    }
    
    public NonBlockingBodyDataSource getNonBlockingBodyDataSource() {
      return bodyDataSource;
    }
  }
  
  
  
  
  private static final class MultithreadedBodyHandler implements IBodyDataHandler {

    private int countCalled = 0;
    private String threadName = null;
    
    public boolean onData(NonBlockingBodyDataSource bodyDataSource) {
      threadName = Thread.currentThread().getName();
      countCalled++;
      return true;
    }
    
    
    public int getCountCalled() {
      return countCalled;
    }
    
    public String getThreadname() {
      return threadName; 
    }
  }

  
  private static final class NonthreadedBodyHandler implements IBodyDataHandler {

    private int countCalled = 0;
    private String threadName = null;
    
    @Execution(Execution.NONTHREADED)
    public boolean onData(NonBlockingBodyDataSource bodyDataSource) {
      threadName = Thread.currentThread().getName();
      countCalled++;
      return true;
    }
    
    
    public int getCountCalled() {
      return countCalled;
    }
    
    public String getThreadname() {
      return threadName; 
    }
  }

  
  
  private static final class BlackholeHandler implements IHttpRequestHandler  {
    
    public void onRequest(HttpRequest request, IHttpResponseContext httpServerEndpoint) throws IOException {
    
      BodyDataSink ds = httpServerEndpoint.send(new HttpResponseHeader(200));
      // don't send the body -> response stays open
    }
  }
 
}
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.