use of org.apache.commons.httpclient.server.SimpleHttpServer in project ecf by eclipse.
the class RestServiceTest method testStart.
public void testStart() {
SimpleHttpServer server = service.getServer();
assertNotNull(server);
service.run();
assertTrue(server.isRunning());
}
use of org.apache.commons.httpclient.server.SimpleHttpServer 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.SimpleHttpServer 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.SimpleHttpServer in project ecf by eclipse.
the class TestRedirects method testCrossSiteRedirect.
public void testCrossSiteRedirect() throws IOException {
String host = this.server.getLocalAddress();
int port = this.server.getLocalPort();
SimpleHttpServer thatserver = new SimpleHttpServer();
this.server.setHttpService(new BasicRedirectService(host, port));
thatserver.setHttpService(new BasicRedirectService(host, port));
thatserver.setTestname(getName());
HostConfiguration hostconfig = new HostConfiguration();
hostconfig.setHost(thatserver.getLocalAddress(), thatserver.getLocalPort(), Protocol.getProtocol("http"));
GetMethod httpget = new GetMethod("/oldlocation/");
httpget.setFollowRedirects(true);
try {
this.client.executeMethod(hostconfig, httpget);
assertEquals(HttpStatus.SC_OK, httpget.getStatusCode());
assertEquals("/newlocation/", httpget.getPath());
assertEquals(host, httpget.getURI().getHost());
assertEquals(port, httpget.getURI().getPort());
assertEquals(new URI("http://" + host + ":" + port + "/newlocation/", false), httpget.getURI());
} finally {
httpget.releaseConnection();
}
thatserver.destroy();
}
use of org.apache.commons.httpclient.server.SimpleHttpServer in project ecf by eclipse.
the class HttpClientTestBase method setUp.
// ------------------------------------------------- TestCase setup/shutdown
public void setUp() throws IOException {
// Configure socket factories
SimpleSocketFactory serversocketfactory = null;
Protocol testhttp = null;
/* XXX removed because ssl test code not included
if (this.useSSL) {
serversocketfactory = new SimpleSSLSocketFactory();
testhttp = new Protocol("https",
(ProtocolSocketFactory)new SimpleSSLTestProtocolSocketFactory(), 443);
} else {
serversocketfactory = new SimplePlainSocketFactory();
testhttp = Protocol.getProtocol("http");
}
*/
// use arbitrary port
this.server = new SimpleHttpServer(serversocketfactory, 0);
this.server.setTestname(getName());
this.client = new HttpClient();
this.client.getHostConfiguration().setHost(this.server.getLocalAddress(), this.server.getLocalPort(), testhttp);
if (this.useProxy) {
this.proxy = new SimpleProxy();
client.getHostConfiguration().setProxy(proxy.getLocalAddress(), proxy.getLocalPort());
}
}
Aggregations