/*
* 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.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.ClosedChannelException;
import org.junit.Assert;
import org.junit.Test;
import org.xlightweb.AbstractBodyParser;
import org.xlightweb.AbstractHttpConnection;
import org.xlightweb.BodyDataSink;
import org.xlightweb.ComposedByteBuffer;
import org.xlightweb.HttpResponse;
import org.xlightweb.HttpResponseHeader;
import org.xlightweb.IHttpExchange;
import org.xlightweb.IHttpRequestHandler;
import org.xlightweb.SimpleMessageBodyParser;
import org.xlightweb.server.HttpServer;
import org.xsocket.connection.BlockingConnection;
import org.xsocket.connection.IBlockingConnection;
import org.xsocket.connection.IServer;
import org.xsocket.connection.ConnectionUtils;
import org.xsocket.connection.IConnection.FlushMode;
/**
*
* @author grro@xlightweb.org
*/
public final class DowngradeResponseTest {
@Test
public void testSimpleHttp_0_9_Response() throws Exception {
IServer server = new HttpServer(new SimpleResponseServerHandler());
ConnectionUtils.start(server);
IBlockingConnection con = new BlockingConnection("localhost", server.getLocalPort());
con.write("GET /test HTTP/1.1\r\n" +
"Host: localhost\r\n" +
"User-Agent: test\r\n" +
"\r\n");
String header = con.readStringByDelimiter("\r\n\r\n") + "\r\n";
Assert.assertTrue(header.indexOf("200") != -1);
Assert.assertTrue(header.indexOf("Connection: close") != -1);
Assert.assertEquals("test123456", con.readStringByLength(10));
try {
con.readByte();
Assert.fail("ClosedChannelException expected");
} catch (ClosedChannelException expected) { }
con.close();
server.close();
}
@Test
public void testHttp_1_0_Response() throws Exception {
IServer server = new HttpServer(new ResponseServerHandler());
ConnectionUtils.start(server);
IBlockingConnection con = new BlockingConnection("localhost", server.getLocalPort());
con.write("GET /test?chunked=false HTTP/1.0\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);
Assert.assertTrue(header.indexOf("HTTP/1.0") != -1);
Assert.assertTrue(header.indexOf("Connection: close") != -1);
con.close();
server.close();
}
@Test
public void testHttp_1_0_ResponseKeepAlive() throws Exception {
IServer server = new HttpServer(new ResponseServerHandler());
ConnectionUtils.start(server);
IBlockingConnection con = new BlockingConnection("localhost", server.getLocalPort());
con.write("GET /test?chunked=false HTTP/1.0\r\n" +
"Connection: keep-alive\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);
Assert.assertTrue(header.indexOf("HTTP/1.0") != -1);
Assert.assertTrue(header.indexOf("Connection: close") != -1);
con.close();
server.close();
}
@Test
public void testHttp_1_0_ResponseChunked() throws Exception {
IServer server = new HttpServer(new ResponseServerHandler());
server.start();
IBlockingConnection con = new BlockingConnection("localhost", server.getLocalPort());
con.write("GET /test?chunked=true HTTP/1.0\r\n" +
"\r\n");
String header = con.readStringByDelimiter("\r\n\r\n") + "\r\n";
Assert.assertTrue(header.indexOf("200") != -1);
Assert.assertTrue(header.indexOf("HTTP/1.0") != -1);
Assert.assertTrue(header.indexOf("Connection: close") != -1);
Assert.assertEquals("test123456", con.readStringByLength(10));
try {
con.readByte();
Assert.fail("ClosedChannelException expected");
} catch (ClosedChannelException expected) { }
con.close();
server.close();
}
private static final class SimpleResponseServerHandler implements IHttpRequestHandler {
public void onRequest(IHttpExchange exchange) throws IOException {
HttpResponseHeader responseHeader = new HttpResponseHeader(200);
responseHeader.setProtocolVersionSilence("0.9");
responseHeader.setSimpleResponse(true);
AbstractBodyParser bodyParser = new SimpleMessageBodyParser((AbstractHttpConnection) exchange.getConnection(), responseHeader);
HttpResponse response = new HttpResponse(responseHeader);
response.setBodyDataSourceSilence(bodyParser.getDataSource());
ComposedByteBuffer rawData = new ComposedByteBuffer();
rawData.append(new ByteBuffer[] { ByteBuffer.wrap("test123456".getBytes()) });
bodyParser.parse(rawData);
exchange.send(response);
bodyParser.onDisconnect();
}
}
private static final class ResponseServerHandler implements IHttpRequestHandler {
public void onRequest(IHttpExchange exchange) throws IOException {
boolean chunkedTest = exchange.getRequest().getBooleanParameter("chunked");
if (chunkedTest) {
BodyDataSink bodyDataSink = exchange.send(new HttpResponseHeader(200, "text/plain"));
bodyDataSink.setFlushmode(FlushMode.ASYNC);
bodyDataSink.write("test123456");
bodyDataSink.close();
} else {
BodyDataSink bodyDataSink = exchange.send(new HttpResponseHeader(200, "text/plain"), 10);
bodyDataSink.setFlushmode(FlushMode.ASYNC);
bodyDataSink.write("test123456");
bodyDataSink.close();
}
}
}
}