use of org.apache.commons.httpclient.server.HttpRequestHandler in project ecf by eclipse.
the class URLRetrieveTest method setUp.
/*
* (non-Javadoc)
*
* @see junit.framework.TestCase#setUp()
*/
protected void setUp() throws Exception {
super.setUp();
tmpFile = File.createTempFile("ECFTest", "");
server = new SimpleServer(getName());
SimpleHttpServer simple = server.getSimpleHttpServer();
simple.setRequestHandler(new HttpRequestHandler() {
public boolean processRequest(SimpleHttpServerConnection conn, SimpleRequest request) throws IOException {
trace("Responding to request " + request.getRequestLine());
ResponseWriter w = conn.getWriter();
writeLines(w, new String[] { "HTTP/1.0 200 OK", "Content-Length: 2", "Content-Type: text/plain; charset=UTF-8", "" });
w.flush();
for (int i = 0; i < 2; i++) {
w.write("x");
}
w.flush();
conn.setKeepAlive(true);
return true;
}
});
}
use of org.apache.commons.httpclient.server.HttpRequestHandler in project ecf by eclipse.
the class URLRetrieveTestCancelConnectJob method testReceiveFile_cancelTransferJobInMiddle.
public void testReceiveFile_cancelTransferJobInMiddle(final long len, final boolean expectedSocketInRead) throws Exception {
if (!CANCEL_SUPPORTED_ON_CONNECT) {
trace("WARNING: Cancel not supported by this provider. testReceiveFile_cancelTransferJobInMiddle cannot be used");
return;
}
final Object[] doCancel = new Object[1];
final IFileTransferListener listener = createFileTransferListener();
final FileTransferListenerWrapper lw = new FileTransferListenerWrapper(listener) {
protected void handleStartConnectEvent(final IFileTransferConnectStartEvent event) {
assertNotNull(event.getFileID());
assertNotNull(event.getFileID().getFilename());
FileTransferJob connectJob = event.prepareConnectJob(null);
connectJob.addJobChangeListener(new JobChangeTraceListener(startTime));
event.connectUsingJob(connectJob);
}
protected void handleStartEvent(final IIncomingFileTransferReceiveStartEvent event) {
spawnCancelThread(doCancel, new ICancelable() {
public void cancel() {
if (expectedSocketInRead) {
waitForSocketInRead();
}
event.cancel();
}
});
try {
createTempFile();
event.receive(tmpFile);
} catch (IOException e) {
e.printStackTrace();
fail(e.toString());
}
}
};
final SimpleServer server = new SimpleServer(getName());
SimpleHttpServer simple = server.getSimpleHttpServer();
simple.setRequestHandler(new HttpRequestHandler() {
public boolean processRequest(SimpleHttpServerConnection conn, SimpleRequest request) throws IOException {
trace("Responding to request but never provide only 50% of body" + request.getRequestLine());
ResponseWriter w = conn.getWriter();
writeLines(w, new String[] { "HTTP/1.0 200 OK", "Content-Length: " + len, "Content-Type: text/plain; charset=UTF-8", "" });
w.flush();
for (int i = 0; i < len / 2; i++) {
w.write("x");
}
w.flush();
conn.setKeepAlive(true);
try {
// give it a bit of time to receive the data
Thread.sleep(200);
} catch (InterruptedException e) {
}
return stalledInRequestHandler(doCancel);
}
});
try {
// path does not matter as server does not respond.
testReceive(server.getServerURL() + "/foo", lw);
assertHasEvent(startConnectEvents, IFileTransferConnectStartEvent.class);
assertHasEvent(startEvents, IIncomingFileTransferReceiveStartEvent.class);
assertHasMoreThanEventCount(dataEvents, IIncomingFileTransferReceiveDataEvent.class, 0);
assertDoneCancelled();
assertNotNull(tmpFile);
assertTrue(tmpFile.exists());
assertEquals(len / 2, tmpFile.length());
assertFalse(socketInReadWrapper.inRead);
socketEvents.validateOneSocketCreatedAndClosed();
} finally {
server.shutdown();
}
}
use of org.apache.commons.httpclient.server.HttpRequestHandler in project ecf by eclipse.
the class TestNoncompliant method testNoncompliantStatusLine.
/**
* Tests that a response status line containing \r and \n is handled.
*/
public void testNoncompliantStatusLine() {
this.server.setRequestHandler(new HttpRequestHandler() {
public boolean processRequest(SimpleHttpServerConnection conn, SimpleRequest request) throws IOException {
ResponseWriter out = conn.getWriter();
out.println("HTTP/1.1 444 This status message contains\n" + " a newline and a\r" + " carrage return but that should be OK.");
out.println("Connection: close");
out.println("Content-Length: 0");
out.println();
out.flush();
return true;
}
});
GetMethod method = new GetMethod("/");
try {
client.executeMethod(method);
} catch (Exception e) {
e.printStackTrace();
fail("Unexpected exception: " + e.toString());
}
assertEquals(444, method.getStatusCode());
}
use of org.apache.commons.httpclient.server.HttpRequestHandler in project ecf by eclipse.
the class TestNoncompliant method testMalformed304Response.
/**
* Tests if client is able to handle gracefully malformed responses
* that may not include response body.
*/
public void testMalformed304Response() throws Exception {
this.server.setRequestHandler(new HttpRequestHandler() {
public boolean processRequest(SimpleHttpServerConnection conn, SimpleRequest request) throws IOException {
conn.setSocketTimeout(20000);
ResponseWriter out = conn.getWriter();
out.println("HTTP/1.1 304 OK");
out.println("Connection: keep-alive");
out.println("Content-Length: 100");
out.println();
out.flush();
conn.setKeepAlive(true);
return true;
}
});
GetMethod method = new GetMethod("/");
method.getParams().setSoTimeout(1000);
client.executeMethod(method);
assertEquals(HttpStatus.SC_NOT_MODIFIED, method.getStatusCode());
method.getResponseBody();
}
use of org.apache.commons.httpclient.server.HttpRequestHandler in project ecf by eclipse.
the class TestResponseHeaders method testNoContentLength.
public void testNoContentLength() throws Exception {
// test with connection header
this.server.setRequestHandler(new HttpRequestHandler() {
public boolean processRequest(SimpleHttpServerConnection conn, SimpleRequest request) throws IOException {
ResponseWriter out = conn.getWriter();
out.println("HTTP/1.1 200 OK");
out.println("Connection: keep-alive");
out.println();
out.println("12345");
out.flush();
return true;
}
});
GetMethod method = new GetMethod("/");
client.executeMethod(method);
method.getResponseBodyAsString();
assertFalse(connectionManager.getConection().isOpen());
// test without connection header
this.server.setRequestHandler(new HttpRequestHandler() {
public boolean processRequest(SimpleHttpServerConnection conn, SimpleRequest request) throws IOException {
ResponseWriter out = conn.getWriter();
out.println("HTTP/1.1 200 OK");
out.println();
out.println("12345");
out.flush();
return true;
}
});
// test with connection header
method = new GetMethod("/");
client.executeMethod(method);
method.getResponseBodyAsString();
assertFalse(connectionManager.getConection().isOpen());
}
Aggregations