Search in sources :

Example 1 with SimpleHttpServerConnection

use of org.apache.commons.httpclient.server.SimpleHttpServerConnection 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 2 with SimpleHttpServerConnection

use of org.apache.commons.httpclient.server.SimpleHttpServerConnection 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 3 with SimpleHttpServerConnection

use of org.apache.commons.httpclient.server.SimpleHttpServerConnection 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());
}
Also used : HttpRequestHandler(org.apache.commons.httpclient.server.HttpRequestHandler) ResponseWriter(org.apache.commons.httpclient.server.ResponseWriter) SimpleHttpServerConnection(org.apache.commons.httpclient.server.SimpleHttpServerConnection) SimpleRequest(org.apache.commons.httpclient.server.SimpleRequest) IOException(java.io.IOException) IOException(java.io.IOException)

Example 4 with SimpleHttpServerConnection

use of org.apache.commons.httpclient.server.SimpleHttpServerConnection 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();
}
Also used : HttpRequestHandler(org.apache.commons.httpclient.server.HttpRequestHandler) ResponseWriter(org.apache.commons.httpclient.server.ResponseWriter) SimpleHttpServerConnection(org.apache.commons.httpclient.server.SimpleHttpServerConnection) SimpleRequest(org.apache.commons.httpclient.server.SimpleRequest) IOException(java.io.IOException)

Example 5 with SimpleHttpServerConnection

use of org.apache.commons.httpclient.server.SimpleHttpServerConnection 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());
}
Also used : HttpRequestHandler(org.apache.commons.httpclient.server.HttpRequestHandler) ResponseWriter(org.apache.commons.httpclient.server.ResponseWriter) SimpleHttpServerConnection(org.apache.commons.httpclient.server.SimpleHttpServerConnection) GetMethod(org.apache.commons.httpclient.methods.GetMethod) SimpleRequest(org.apache.commons.httpclient.server.SimpleRequest) IOException(java.io.IOException)

Aggregations

IOException (java.io.IOException)17 HttpRequestHandler (org.apache.commons.httpclient.server.HttpRequestHandler)17 SimpleHttpServerConnection (org.apache.commons.httpclient.server.SimpleHttpServerConnection)17 SimpleRequest (org.apache.commons.httpclient.server.SimpleRequest)17 ResponseWriter (org.apache.commons.httpclient.server.ResponseWriter)15 GetMethod (org.apache.commons.httpclient.methods.GetMethod)4 SimpleHttpServer (org.apache.commons.httpclient.server.SimpleHttpServer)4 SimpleServer (org.eclipse.ecf.internal.tests.filetransfer.httpserver.SimpleServer)4 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 PostMethod (org.apache.commons.httpclient.methods.PostMethod)1 StringRequestEntity (org.apache.commons.httpclient.methods.StringRequestEntity)1 SimpleResponse (org.apache.commons.httpclient.server.SimpleResponse)1 IJobChangeEvent (org.eclipse.core.runtime.jobs.IJobChangeEvent)1 UserCancelledException (org.eclipse.ecf.filetransfer.UserCancelledException)1 IIncomingFileTransferReceiveDoneEvent (org.eclipse.ecf.filetransfer.events.IIncomingFileTransferReceiveDoneEvent)1