Package org.xlightweb

Source Code of org.xlightweb.FileServiceTest$LogHandler

/*
*  Copyright (c) xlightweb.org, 2008 - 2009. 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.xlightweb.org/
*/
package org.xlightweb;



import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.channels.FileChannel;


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

import org.xlightweb.HttpResponse;
import org.xlightweb.IHttpExchange;
import org.xlightweb.IHttpRequestHandler;
import org.xlightweb.RequestHandlerChain;
import org.xlightweb.client.HttpClient;
import org.xlightweb.client.HttpClientConnection;
import org.xlightweb.server.HttpServer;
import org.xsocket.connection.BlockingConnection;
import org.xsocket.connection.IBlockingConnection;
import org.xsocket.connection.IConnection;
import org.xsocket.connection.IServer;



/**
*
* @author grro@xlightweb.org
*/
public final class FileServiceTest  {

  
  @Test
  public void testFound() throws Exception {

      File file = QAUtil.createTestfile_400k();
      String basepath = file.getParentFile().getAbsolutePath();
   
    IServer server = new HttpServer(new FileServiceRequestHandler(basepath, true));
    server.start();
   
    IBlockingConnection con = new BlockingConnection("localhost", server.getLocalPort());

    con.write("GET /" + file.getName() + " 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";

    if (header.indexOf("200") == -1) {
        String txt = "unexpected response " + header;
        System.out.println("ERROR " + txt);
        Assert.fail(txt);
    }

    int contentLength = QAUtil.readContentLength(header);
    File tempFile = QAUtil.createTempfile();
   
    RandomAccessFile raf = new RandomAccessFile(tempFile, "rw");
    FileChannel fc = raf.getChannel();
   
    con.transferTo(fc, contentLength);
   
    fc.close();
    raf.close();
   
    Assert.assertTrue(QAUtil.isEquals(file, tempFile));

    tempFile.delete();
    file.delete();
    con.close();
    server.close();
  }
 
 
  @Test
  public void testMimeType() throws Exception {

    File file = QAUtil.createTempfile(".txt");
    FileOutputStream fos = new FileOutputStream(file);
    fos.write("test1234".getBytes());
    fos.close();
   
   
    String basePath = file.getParent();
   
    IServer server = new HttpServer(new FileServiceRequestHandler(basePath, true));
    server.start();

    HttpClient httpClient = new HttpClient();
    IHttpResponse response = httpClient.call(new GetRequest("http://localhost:" + server.getLocalPort() + "/" + file.getName()));
   
    Assert.assertEquals(200, response.getStatus());
    if (!response.getContentType().equals("text/plain")) {
      System.out.println("got mime type " + response.getContentType() " instead of text/plain");
      Assert.fail("got mime type " + response.getContentType() " instead of text/plain");
    }
    Assert.assertEquals("test1234", response.getBlockingBody().readString());

    file.delete();
    httpClient.close();
    server.close();
  }
 
  
 
  @Test
  public void testNotFound() throws Exception {
   
      File file = QAUtil.createTestfile_40k();
      String basepath = file.getParentFile().getAbsolutePath();

    IServer server = new HttpServer(new FileServiceRequestHandler(basepath));
    server.start();
   

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

    con.write("GET /doesntExists 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.readStringByLength(contentLength);

    Assert.assertTrue(header.indexOf("404") != -1);
   
    file.delete();
    con.close();   
    server.close();
  }
 
 
  @Test
  public void testLastModified() throws Exception {

      File file = QAUtil.createTestfile_40k();
      String basepath = file.getParentFile().getAbsolutePath();
   
    IServer server = new HttpServer(new FileServiceRequestHandler(basepath));
    server.start();
   
    HttpClientConnection con = new HttpClientConnection("localhost", server.getLocalPort());

    IHttpResponse response = con.call(new GetRequest("/" + file.getName()));
    Assert.assertEquals(200, response.getStatus());
   
    response.getBlockingBody().readString();
   
    String lastModified = response.getHeader("Last-Modified");
    GetRequest secondRequest = new GetRequest("/" + file.getName());
    secondRequest.setHeader("If-Modified-Since", lastModified);
   
    response = con.call(secondRequest);
    Assert.assertEquals("unexpected response " + response, 304, response.getStatus());
   
   
    file.delete();
    con.close();
    server.close();
  }

 
 
  @Test
  public void testChainFound() throws Exception {
   
      File file = QAUtil.createTestfile_400k();
      String basepath = file.getParentFile().getAbsolutePath();

    RequestHandlerChain chain = new RequestHandlerChain();
    chain.addLast(new FileServiceRequestHandler(basepath));
    IServer server = new HttpServer(chain);

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

    con.write("GET /" + file.getName() + " 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.readStringByLength(contentLength);

    Assert.assertTrue(header.indexOf("200") != -1);

    file.delete();
    con.close();
    server.close();
  }
 
 
 
  @Test
  public void testChainNotFound() throws Exception {

      File file = QAUtil.createTestfile_400k();
      String basepath = file.getParentFile().getAbsolutePath();

   
    RequestHandlerChain chain = new RequestHandlerChain();
    chain.addLast(new FileServiceRequestHandler(basepath));
    IServer server = new HttpServer(chain);
    server.start();
   
   
    IBlockingConnection con = new BlockingConnection("localhost", server.getLocalPort());

    con.write("GET /doenstExists 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.readStringByLength(contentLength);

    Assert.assertTrue(header.indexOf("404") != -1);
   
    file.delete();
    con.close();
    server.close();
  }

 
 
  @Test
  public void testChainHandledBySuccessor() throws Exception {

      File file = QAUtil.createTestfile_400k();
      String basepath = file.getParentFile().getAbsolutePath();

    RequestHandlerChain chain = new RequestHandlerChain();
    chain.addLast(new FileServiceRequestHandler(basepath));
    chain.addLast(new BusinessHandler());
   
    IServer server = new HttpServer(chain);
    server.start();
   
   
    IBlockingConnection con = new BlockingConnection("localhost", server.getLocalPort());

    con.write("GET /doesntExists 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);

    file.delete();
    con.close();
    server.close();
  }
 
 
  @Test
  public void testChainHandledByFileServiceNotSuccessor() throws Exception {
   
      File file = QAUtil.createTestfile_400k();
      String basepath = file.getParentFile().getAbsolutePath();

    RequestHandlerChain chain = new RequestHandlerChain();
    chain.addLast(new FileServiceRequestHandler(basepath));
    chain.addLast(new BusinessHandler());
   
    IServer server = new HttpServer(chain);
    server.start();
   
    HttpClientConnection con = new HttpClientConnection("localhost", server.getLocalPort());

    IHttpResponse response = con.call(new GetRequest("/" + file.getName()));
    Assert.assertEquals(200, response.getStatus());

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

    tempFile.delete();
    file.delete();
    con.close();
    server.close();
  }
 
 
  @Test
  public void testChainLogInterceptor() throws Exception {
 
      File file = QAUtil.createTestfile_400k();
      String basepath = file.getParentFile().getAbsolutePath();

    RequestHandlerChain chain = new RequestHandlerChain();
    chain.addLast(new LogHandler());
    FileServiceRequestHandler fileSrv = new FileServiceRequestHandler(basepath);
    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();
   
    BlockingBodyDataSource source = response.getBlockingBody();
    source.transferTo(fc);
   
    fc.close();
    raf.close();
   
    Assert.assertTrue(QAUtil.isEquals(file, tempFile));

    QAUtil.sleep(500);
    Assert.assertTrue(fileSrv.getPendingSendTransactions() == 0);

   
    tempFile.delete();
    file.delete();
    con.close();
    server.close();
  }
 
 
 
  private static final class BusinessHandler implements IHttpRequestHandler  {
   
    public void onRequest(IHttpExchange exchange) throws IOException {
      exchange.send(new HttpResponse(200, "text/plain", "OK"));
    }   
  }
 
 
  private static final class LogHandler implements IHttpRequestHandler {
   
    public void onRequest(final IHttpExchange exchange) throws IOException, BadMessageException {
      IHttpRequest request = exchange.getRequest();
     
      IHttpResponseHandler respHdl = new IHttpResponseHandler() {
       
        public void onResponse(IHttpResponse response) throws IOException {
          exchange.send(response);
        }
       
        public void onException(IOException ioe) throws IOException {
          exchange.sendError(ioe);
        }
      };
     
      exchange.forward(request, respHdl);
    }
   
  }
}
TOP

Related Classes of org.xlightweb.FileServiceTest$LogHandler

TOP
Copyright © 2018 www.massapi.com. 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.