/*
* 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.util.concurrent.atomic.AtomicBoolean;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.PostMethod;
import org.junit.Assert;
import org.junit.Test;
import org.xlightweb.HttpResponse;
import org.xlightweb.IHttpExchange;
import org.xlightweb.IHttpRequestHandler;
import org.xlightweb.server.HttpServer;
import org.xsocket.connection.BlockingConnection;
import org.xsocket.connection.IBlockingConnection;
/**
*
* @author grro@xlightweb.org
*/
public final class Server100ContinueTest {
@Test
public void testAutoContinue() throws Exception {
HttpServer server = new HttpServer(new ItWorksHandler());
server.start();
IBlockingConnection con = new BlockingConnection("localhost", server.getLocalPort());
con.write("POST / HTTP/1.1\r\n" +
"Host: localhost\r\n" +
"User-Agent: me\r\n" +
"Expect: 100-Continue\r\n" +
"Content-Length: 2000\r\n" +
"\r\n");
String header = con.readStringByDelimiter("\r\n\r\n") + "\r\n";
Assert.assertTrue(header.indexOf("100") != -1);
con.write(QAUtil.generateByteArray(2000));
header = con.readStringByDelimiter("\r\n\r\n") + "\r\n";
Assert.assertTrue(header.indexOf("200") != -1);
con.close();
server.close();
}
@Test
public void testManualContinue() throws Exception {
ContinueHandler hdl = new ContinueHandler();
HttpServer server = new HttpServer(hdl);
server.start();
IBlockingConnection con = new BlockingConnection("localhost", server.getLocalPort());
con.write("POST / HTTP/1.1\r\n" +
"Host: localhost\r\n" +
"User-Agent: me\r\n" +
"Expect: 100-Continue\r\n" +
"Content-Length: 2000\r\n" +
"\r\n");
String header = con.readStringByDelimiter("\r\n\r\n") + "\r\n";
Assert.assertTrue(header.indexOf("100") != -1);
QAUtil.sleep(300);
Assert.assertTrue(hdl.is100ContinueSent());
con.write(QAUtil.generateByteArray(2000));
header = con.readStringByDelimiter("\r\n\r\n") + "\r\n";
Assert.assertTrue(header.indexOf("200") != -1);
int length = QAUtil.readContentLength(header);
String txt = con.readStringByLength(length);
Assert.assertEquals("OK", txt);
con.close();
server.close();
}
@Test
public void testManualContinue2() throws Exception {
ContinueHandler2 hdl = new ContinueHandler2();
HttpServer server = new HttpServer(hdl);
server.start();
IBlockingConnection con = new BlockingConnection("localhost", server.getLocalPort());
con.write("POST / HTTP/1.1\r\n" +
"Host: localhost\r\n" +
"User-Agent: me\r\n" +
"Expect: 100-Continue\r\n" +
"Content-Length: 2000\r\n" +
"\r\n");
String header = con.readStringByDelimiter("\r\n\r\n") + "\r\n";
Assert.assertTrue(header.indexOf("100") != -1);
QAUtil.sleep(300);
Assert.assertTrue(hdl.is100ContinueSent());
con.write(QAUtil.generateByteArray(2000));
header = con.readStringByDelimiter("\r\n\r\n") + "\r\n";
Assert.assertTrue(header.indexOf("200") != -1);
int length = QAUtil.readContentLength(header);
String txt = con.readStringByLength(length);
Assert.assertEquals("OK", txt);
con.close();
server.close();
}
@Test
public void testManualContinue3() throws Exception {
IHttpRequestHandler hdl = new IHttpRequestHandler() {
@Supports100Continue
public void onRequest(IHttpExchange exchange) throws IOException {
exchange.send(new HttpResponse(100)); // send continue
exchange.send(new HttpResponse(200, "text/plain", "OK"));
}
};
HttpServer server = new HttpServer(hdl);
server.start();
IBlockingConnection con = new BlockingConnection("localhost", server.getLocalPort());
con.write("POST / HTTP/1.1\r\n" +
"Host: localhost\r\n" +
"User-Agent: me\r\n" +
"Expect: 100-Continue\r\n" +
"Content-Length: 2000\r\n" +
"\r\n");
String header = con.readStringByDelimiter("\r\n\r\n") + "\r\n";
Assert.assertTrue(header.indexOf("100") != -1);
QAUtil.sleep(300);
con.write(QAUtil.generateByteArray(2000));
header = con.readStringByDelimiter("\r\n\r\n") + "\r\n";
Assert.assertTrue(header.indexOf("200") != -1);
int length = QAUtil.readContentLength(header);
String txt = con.readStringByLength(length);
Assert.assertEquals("OK", txt);
con.close();
server.close();
}
@Test
public void testManualContinueTwice() throws Exception {
IHttpRequestHandler hdl = new IHttpRequestHandler() {
@Supports100Continue
public void onRequest(IHttpExchange exchange) throws IOException {
exchange.sendContinue();
exchange.sendContinue();
exchange.send(new HttpResponse(200, "text/plain", "OK"));
}
};
HttpServer server = new HttpServer(hdl);
server.start();
IBlockingConnection con = new BlockingConnection("localhost", server.getLocalPort());
con.write("POST / HTTP/1.1\r\n" +
"Host: localhost\r\n" +
"User-Agent: me\r\n" +
"Expect: 100-Continue\r\n" +
"Content-Length: 2000\r\n" +
"\r\n");
String header = con.readStringByDelimiter("\r\n\r\n") + "\r\n";
Assert.assertTrue(header.indexOf("100") != -1);
QAUtil.sleep(300);
con.write(QAUtil.generateByteArray(2000));
header = con.readStringByDelimiter("\r\n\r\n") + "\r\n";
Assert.assertTrue(header.indexOf("200") != -1);
int length = QAUtil.readContentLength(header);
String txt = con.readStringByLength(length);
Assert.assertEquals("OK", txt);
con.close();
server.close();
}
@Test
public void testApacheClient() throws Exception {
ContinueHandler hdl = new ContinueHandler();
HttpServer server = new HttpServer(hdl);
server.start();
HttpClient httpClient = new HttpClient();
httpClient.getParams().setParameter("http.protocol.expect-continue", true);
PostMethod meth = new PostMethod("http://localhost:" + server.getLocalPort() + "/test");
meth.setRequestBody("OK");
httpClient.executeMethod(meth);
Assert.assertEquals(200, meth.getStatusCode());
Assert.assertEquals("OK", meth.getResponseBodyAsString());
meth.releaseConnection();
server.close();
}
@Test
public void testManualContinueWithMissingAnnotation() throws Exception {
MissingAnnotationContinueHandler hdl = new MissingAnnotationContinueHandler();
HttpServer server = new HttpServer(hdl);
server.start();
IBlockingConnection con = new BlockingConnection("localhost", server.getLocalPort());
con.write("POST / HTTP/1.1\r\n" +
"Host: localhost\r\n" +
"User-Agent: me\r\n" +
"Expect: 100-Continue\r\n" +
"Content-Length: 2000\r\n" +
"\r\n");
String header = con.readStringByDelimiter("\r\n\r\n") + "\r\n";
Assert.assertTrue(header.indexOf("100") != -1);
Assert.assertFalse(hdl.is100ContinueSent());
con.write(QAUtil.generateByteArray(2000));
header = con.readStringByDelimiter("\r\n\r\n") + "\r\n";
Assert.assertTrue(header.indexOf("200") != -1);
int length = QAUtil.readContentLength(header);
String txt = con.readStringByLength(length);
Assert.assertEquals("OK", txt);
con.close();
server.close();
}
@Test
public void testAutoContinueWithInterceptor() throws Exception {
IHttpRequestHandler interceptor = new IHttpRequestHandler() {
public void onRequest(final IHttpExchange exchange) throws IOException, BadMessageException {
IHttpRequest request = exchange.getRequest();
request.addHeader("X-Intercepted", "true");
IHttpResponseHandler hdl = new IHttpResponseHandler() {
public void onResponse(IHttpResponse response) throws IOException {
response.addHeader("X-Intercepted", "true");
exchange.send(response);
}
public void onException(IOException ioe) throws IOException {
exchange.sendError(ioe);
}
};
exchange.forward(request, hdl);
}
};
RequestHandlerChain chain = new RequestHandlerChain();
chain.addLast(interceptor);
chain.addLast(new ItWorksHandler());
HttpServer server = new HttpServer(chain);
server.start();
IBlockingConnection con = new BlockingConnection("localhost", server.getLocalPort());
con.write("POST / HTTP/1.1\r\n" +
"Host: localhost\r\n" +
"User-Agent: me\r\n" +
"Expect: 100-Continue\r\n" +
"Content-Length: 2000\r\n" +
"\r\n");
String header = con.readStringByDelimiter("\r\n\r\n") + "\r\n";
Assert.assertTrue(header.indexOf("100") != -1);
con.write(QAUtil.generateByteArray(2000));
header = con.readStringByDelimiter("\r\n\r\n") + "\r\n";
Assert.assertTrue(header.indexOf("200") != -1);
Assert.assertTrue(header.indexOf("X-Intercepted") != -1);
int length = QAUtil.readContentLength(header);
String txt = con.readStringByLength(length);
Assert.assertEquals("OK", txt);
con.close();
server.close();
}
@Test
public void testAutoContinueWithNestedInterceptor() throws Exception {
IHttpRequestHandler interceptor = new IHttpRequestHandler() {
public void onRequest(final IHttpExchange exchange) throws IOException, BadMessageException {
IHttpRequest request = exchange.getRequest();
request.addHeader("X-Intercepted1", "true");
IHttpResponseHandler hdl = new IHttpResponseHandler() {
public void onResponse(IHttpResponse response) throws IOException {
response.addHeader("X-Intercepted1", "true");
exchange.send(response);
}
public void onException(IOException ioe) throws IOException {
exchange.sendError(ioe);
}
};
exchange.forward(request, hdl);
}
};
IHttpRequestHandler interceptor2 = new IHttpRequestHandler() {
public void onRequest(final IHttpExchange exchange) throws IOException, BadMessageException {
IHttpRequest request = exchange.getRequest();
request.addHeader("X-Intercepted2", "true");
IHttpResponseHandler hdl = new IHttpResponseHandler() {
public void onResponse(IHttpResponse response) throws IOException {
response.addHeader("X-Intercepted2", "true");
exchange.send(response);
}
public void onException(IOException ioe) throws IOException {
exchange.sendError(ioe);
}
};
exchange.forward(request, hdl);
}
};
RequestHandlerChain chain = new RequestHandlerChain();
chain.addLast(interceptor);
chain.addLast(new ItWorksHandler());
RequestHandlerChain outerChain = new RequestHandlerChain();
outerChain.addLast(interceptor2);
outerChain.addLast(chain);
HttpServer server = new HttpServer(outerChain);
server.start();
IBlockingConnection con = new BlockingConnection("localhost", server.getLocalPort());
con.write("POST / HTTP/1.1\r\n" +
"Host: localhost\r\n" +
"User-Agent: me\r\n" +
"Expect: 100-Continue\r\n" +
"Content-Length: 2000\r\n" +
"\r\n");
String header = con.readStringByDelimiter("\r\n\r\n") + "\r\n";
Assert.assertTrue(header.indexOf("100") != -1);
con.write(QAUtil.generateByteArray(2000));
header = con.readStringByDelimiter("\r\n\r\n") + "\r\n";
Assert.assertTrue(header.indexOf("200") != -1);
Assert.assertTrue(header.indexOf("X-Intercepted1") != -1);
Assert.assertTrue(header.indexOf("X-Intercepted2") != -1);
int length = QAUtil.readContentLength(header);
String txt = con.readStringByLength(length);
Assert.assertEquals("OK", txt);
con.close();
server.close();
}
@Test
public void testAutoContinueWithNestedInterceptor2() throws Exception {
IHttpRequestHandler interceptor = new IHttpRequestHandler() {
@Supports100Continue
public void onRequest(final IHttpExchange exchange) throws IOException, BadMessageException {
IHttpRequest request = exchange.getRequest();
request.addHeader("X-Intercepted1", "true");
IHttpResponseHandler hdl = new IHttpResponseHandler() {
public void onResponse(IHttpResponse response) throws IOException {
response.addHeader("X-Intercepted1", "true");
exchange.send(response);
}
public void onException(IOException ioe) throws IOException {
exchange.sendError(ioe);
}
};
exchange.forward(request, hdl);
}
};
IHttpRequestHandler interceptor2 = new IHttpRequestHandler() {
public void onRequest(final IHttpExchange exchange) throws IOException, BadMessageException {
IHttpRequest request = exchange.getRequest();
request.addHeader("X-Intercepted2", "true");
IHttpResponseHandler hdl = new IHttpResponseHandler() {
public void onResponse(IHttpResponse response) throws IOException {
response.addHeader("X-Intercepted2", "true");
exchange.send(response);
}
public void onException(IOException ioe) throws IOException {
exchange.sendError(ioe);
}
};
exchange.forward(request, hdl);
}
};
RequestHandlerChain chain = new RequestHandlerChain();
chain.addLast(interceptor);
chain.addLast(new ItWorksHandler());
RequestHandlerChain outerChain = new RequestHandlerChain();
outerChain.addLast(interceptor2);
outerChain.addLast(chain);
HttpServer server = new HttpServer(outerChain);
server.start();
IBlockingConnection con = new BlockingConnection("localhost", server.getLocalPort());
con.write("POST / HTTP/1.1\r\n" +
"Host: localhost\r\n" +
"User-Agent: me\r\n" +
"Expect: 100-Continue\r\n" +
"Content-Length: 2000\r\n" +
"\r\n");
String header = con.readStringByDelimiter("\r\n\r\n") + "\r\n";
Assert.assertTrue(header.indexOf("100") != -1);
con.write(QAUtil.generateByteArray(2000));
header = con.readStringByDelimiter("\r\n\r\n") + "\r\n";
Assert.assertTrue(header.indexOf("200") != -1);
Assert.assertTrue(header.indexOf("X-Intercepted1") != -1);
Assert.assertTrue(header.indexOf("X-Intercepted2") != -1);
int length = QAUtil.readContentLength(header);
String txt = con.readStringByLength(length);
Assert.assertEquals("OK", txt);
con.close();
server.close();
}
@Test
public void testManualContinueWithNestedInterceptor() throws Exception {
IHttpRequestHandler interceptor = new IHttpRequestHandler() {
@Supports100Continue
public void onRequest(final IHttpExchange exchange) throws IOException, BadMessageException {
IHttpRequest request = exchange.getRequest();
request.addHeader("X-Intercepted1", "true");
IHttpResponseHandler hdl = new IHttpResponseHandler() {
public void onResponse(IHttpResponse response) throws IOException {
response.addHeader("X-Intercepted1", "true");
exchange.send(response);
}
public void onException(IOException ioe) throws IOException {
exchange.sendError(ioe);
}
};
exchange.forward(request, hdl);
}
};
IHttpRequestHandler interceptor2 = new IHttpRequestHandler() {
@Supports100Continue
public void onRequest(final IHttpExchange exchange) throws IOException, BadMessageException {
IHttpRequest request = exchange.getRequest();
request.addHeader("X-Intercepted2", "true");
IHttpResponseHandler hdl = new IHttpResponseHandler() {
public void onResponse(IHttpResponse response) throws IOException {
response.addHeader("X-Intercepted2", "true");
exchange.send(response);
}
public void onException(IOException ioe) throws IOException {
exchange.sendError(ioe);
}
};
exchange.forward(request, hdl);
}
};
RequestHandlerChain chain = new RequestHandlerChain();
chain.addLast(interceptor);
ContinueHandler hdl = new ContinueHandler();
chain.addLast(hdl);
RequestHandlerChain outerChain = new RequestHandlerChain();
outerChain.addLast(interceptor2);
outerChain.addLast(chain);
HttpServer server = new HttpServer(outerChain);
server.start();
IBlockingConnection con = new BlockingConnection("localhost", server.getLocalPort());
con.write("POST / HTTP/1.1\r\n" +
"Host: localhost\r\n" +
"User-Agent: me\r\n" +
"Expect: 100-Continue\r\n" +
"Content-Length: 2000\r\n" +
"\r\n");
String header = con.readStringByDelimiter("\r\n\r\n") + "\r\n";
Assert.assertTrue(header.indexOf("100") != -1);
Assert.assertTrue(hdl.is100ContinueSent());
con.write(QAUtil.generateByteArray(2000));
header = con.readStringByDelimiter("\r\n\r\n") + "\r\n";
Assert.assertTrue(header.indexOf("200") != -1);
Assert.assertTrue(header.indexOf("X-Intercepted1") != -1);
Assert.assertTrue(header.indexOf("X-Intercepted2") != -1);
int length = QAUtil.readContentLength(header);
String txt = con.readStringByLength(length);
Assert.assertEquals("OK", txt);
con.close();
server.close();
}
@Test
public void testManualContinueWithNonAnnotatedInterceptor() throws Exception {
IHttpRequestHandler interceptor = new IHttpRequestHandler() {
public void onRequest(final IHttpExchange exchange) throws IOException, BadMessageException {
IHttpRequest request = exchange.getRequest();
request.addHeader("X-Intercepted", "true");
IHttpResponseHandler hdl = new IHttpResponseHandler() {
public void onResponse(IHttpResponse response) throws IOException {
response.addHeader("X-Intercepted", "true");
exchange.send(response);
}
public void onException(IOException ioe) throws IOException {
exchange.sendError(ioe);
}
};
exchange.forward(request, hdl);
}
};
RequestHandlerChain chain = new RequestHandlerChain();
chain.addLast(interceptor);
ContinueHandler hdl = new ContinueHandler();
chain.addLast(hdl);
HttpServer server = new HttpServer(chain);
server.start();
IBlockingConnection con = new BlockingConnection("localhost", server.getLocalPort());
con.write("POST / HTTP/1.1\r\n" +
"Host: localhost\r\n" +
"User-Agent: me\r\n" +
"Expect: 100-Continue\r\n" +
"Content-Length: 2000\r\n" +
"\r\n");
String header = con.readStringByDelimiter("\r\n\r\n") + "\r\n";
Assert.assertTrue(header.indexOf("100") != -1);
Assert.assertFalse(hdl.is100ContinueSent());
con.write(QAUtil.generateByteArray(2000));
header = con.readStringByDelimiter("\r\n\r\n") + "\r\n";
if (header.indexOf("200") == -1) {
Assert.fail();
}
Assert.assertTrue(header.indexOf("X-Intercepted") != -1);
int length = QAUtil.readContentLength(header);
String txt = con.readStringByLength(length);
Assert.assertEquals("OK", txt);
con.close();
server.close();
}
@Test
public void testManualContinueWithAnnotatedInterceptor() throws Exception {
IHttpRequestHandler interceptor = new IHttpRequestHandler() {
@Supports100Continue
public void onRequest(final IHttpExchange exchange) throws IOException, BadMessageException {
IHttpRequest request = exchange.getRequest();
request.addHeader("X-Intercepted", "true");
IHttpResponseHandler hdl = new IHttpResponseHandler() {
public void onResponse(IHttpResponse response) throws IOException {
response.addHeader("X-Intercepted", "true");
exchange.send(response);
}
public void onException(IOException ioe) throws IOException {
exchange.sendError(ioe);
}
};
exchange.forward(request, hdl);
}
};
RequestHandlerChain chain = new RequestHandlerChain();
chain.addLast(interceptor);
ContinueHandler hdl = new ContinueHandler();
chain.addLast(hdl);
HttpServer server = new HttpServer(chain);
server.start();
IBlockingConnection con = new BlockingConnection("localhost", server.getLocalPort());
con.write("POST / HTTP/1.1\r\n" +
"Host: localhost\r\n" +
"User-Agent: me\r\n" +
"Expect: 100-Continue\r\n" +
"Content-Length: 2000\r\n" +
"\r\n");
String header = con.readStringByDelimiter("\r\n\r\n") + "\r\n";
Assert.assertTrue(header.indexOf("100") != -1);
Assert.assertTrue(hdl.is100ContinueSent());
Assert.assertTrue(header.indexOf("X-Intercepted: true") != -1);
con.write(QAUtil.generateByteArray(2000));
header = con.readStringByDelimiter("\r\n\r\n") + "\r\n";
Assert.assertTrue(header.indexOf("200") != -1);
Assert.assertTrue(header.indexOf("X-Intercepted") != -1);
int length = QAUtil.readContentLength(header);
String txt = con.readStringByLength(length);
Assert.assertEquals("OK", txt);
con.close();
server.close();
}
private static final class ItWorksHandler implements IHttpRequestHandler {
public void onRequest(IHttpExchange exchange) throws IOException {
exchange.send(new HttpResponse(200, "text/plain", "OK"));
}
}
@Supports100Continue
private static final class ContinueHandler implements IHttpRequestHandler {
private AtomicBoolean is100ContinueSent = new AtomicBoolean(false);
public void onRequest(IHttpExchange exchange) throws IOException {
is100ContinueSent.set(exchange.sendContinue());
exchange.send(new HttpResponse(200, "text/plain", "OK"));
}
public boolean is100ContinueSent() {
return is100ContinueSent.get();
}
}
private static final class ContinueHandler2 implements IHttpRequestHandler {
private AtomicBoolean is100ContinueSent = new AtomicBoolean(false);
@Supports100Continue
public void onRequest(IHttpExchange exchange) throws IOException {
is100ContinueSent.set(exchange.sendContinue());
exchange.send(new HttpResponse(200, "text/plain", "OK"));
}
public boolean is100ContinueSent() {
return is100ContinueSent.get();
}
}
private static final class MissingAnnotationContinueHandler implements IHttpRequestHandler {
private AtomicBoolean is100ContinueSent = new AtomicBoolean(false);
public void onRequest(IHttpExchange exchange) throws IOException {
is100ContinueSent.set(exchange.sendContinue());
exchange.send(new HttpResponse(200, "text/plain", "OK"));
}
public boolean is100ContinueSent() {
return is100ContinueSent.get();
}
}
}