Search in sources :

Example 1 with SimpleHttpServer

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());
}
Also used : SimpleHttpServer(org.apache.commons.httpclient.server.SimpleHttpServer)

Example 2 with SimpleHttpServer

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;
        }
    });
}
Also used : SimpleServer(org.eclipse.ecf.internal.tests.filetransfer.httpserver.SimpleServer) HttpRequestHandler(org.apache.commons.httpclient.server.HttpRequestHandler) ResponseWriter(org.apache.commons.httpclient.server.ResponseWriter) SimpleHttpServer(org.apache.commons.httpclient.server.SimpleHttpServer) SimpleHttpServerConnection(org.apache.commons.httpclient.server.SimpleHttpServerConnection) SimpleRequest(org.apache.commons.httpclient.server.SimpleRequest) IOException(java.io.IOException)

Example 3 with SimpleHttpServer

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();
    }
}
Also used : SimpleServer(org.eclipse.ecf.internal.tests.filetransfer.httpserver.SimpleServer) HttpRequestHandler(org.apache.commons.httpclient.server.HttpRequestHandler) IIncomingFileTransferReceiveStartEvent(org.eclipse.ecf.filetransfer.events.IIncomingFileTransferReceiveStartEvent) SimpleHttpServerConnection(org.apache.commons.httpclient.server.SimpleHttpServerConnection) IFileTransferListener(org.eclipse.ecf.filetransfer.IFileTransferListener) IOException(java.io.IOException) SimpleRequest(org.apache.commons.httpclient.server.SimpleRequest) IFileTransferConnectStartEvent(org.eclipse.ecf.filetransfer.events.IFileTransferConnectStartEvent) ResponseWriter(org.apache.commons.httpclient.server.ResponseWriter) FileTransferJob(org.eclipse.ecf.filetransfer.FileTransferJob) SimpleHttpServer(org.apache.commons.httpclient.server.SimpleHttpServer)

Example 4 with SimpleHttpServer

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();
}
Also used : SimpleHttpServer(org.apache.commons.httpclient.server.SimpleHttpServer) GetMethod(org.apache.commons.httpclient.methods.GetMethod)

Example 5 with SimpleHttpServer

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());
    }
}
Also used : SimpleHttpServer(org.apache.commons.httpclient.server.SimpleHttpServer) Protocol(org.apache.commons.httpclient.protocol.Protocol) SimpleSocketFactory(org.apache.commons.httpclient.server.SimpleSocketFactory) SimpleProxy(org.apache.commons.httpclient.server.SimpleProxy)

Aggregations

SimpleHttpServer (org.apache.commons.httpclient.server.SimpleHttpServer)12 IOException (java.io.IOException)5 HttpRequestHandler (org.apache.commons.httpclient.server.HttpRequestHandler)4 SimpleHttpServerConnection (org.apache.commons.httpclient.server.SimpleHttpServerConnection)4 SimpleRequest (org.apache.commons.httpclient.server.SimpleRequest)4 SimpleServer (org.eclipse.ecf.internal.tests.filetransfer.httpserver.SimpleServer)4 ResponseWriter (org.apache.commons.httpclient.server.ResponseWriter)3 FileTransferJob (org.eclipse.ecf.filetransfer.FileTransferJob)3 IFileTransferListener (org.eclipse.ecf.filetransfer.IFileTransferListener)3 IFileTransferConnectStartEvent (org.eclipse.ecf.filetransfer.events.IFileTransferConnectStartEvent)3 IIncomingFileTransferReceiveStartEvent (org.eclipse.ecf.filetransfer.events.IIncomingFileTransferReceiveStartEvent)2 Map (java.util.Map)1 FakeHttpMethod (org.apache.commons.httpclient.FakeHttpMethod)1 HttpClient (org.apache.commons.httpclient.HttpClient)1 UsernamePasswordCredentials (org.apache.commons.httpclient.UsernamePasswordCredentials)1 GetMethod (org.apache.commons.httpclient.methods.GetMethod)1 Protocol (org.apache.commons.httpclient.protocol.Protocol)1 SimpleProxy (org.apache.commons.httpclient.server.SimpleProxy)1 SimpleSocketFactory (org.apache.commons.httpclient.server.SimpleSocketFactory)1 IJobChangeEvent (org.eclipse.core.runtime.jobs.IJobChangeEvent)1