Search in sources :

Example 6 with SimpleHttpServerConnection

use of org.apache.commons.httpclient.server.SimpleHttpServerConnection in project ecf by eclipse.

the class TestResponseHeaders method testNoContent.

public void testNoContent() 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 204 NO CONTENT");
            out.println();
            out.flush();
            return true;
        }
    });
    GetMethod method = new GetMethod("/");
    client.executeMethod(method);
    method.getResponseBodyAsString();
    assertTrue(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 204 NO CONTENT");
            out.println("Connection: keep-alive");
            out.println();
            out.flush();
            return true;
        }
    });
    // test with connection header
    method = new GetMethod("/");
    client.executeMethod(method);
    method.getResponseBodyAsString();
    assertTrue(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)

Example 7 with SimpleHttpServerConnection

use of org.apache.commons.httpclient.server.SimpleHttpServerConnection in project ecf by eclipse.

the class URLRetrieveTestCancelConnectJob method testReceiveFile_cancelConnectJob.

// TODO: add test that cancel without connect job, when server does not
// respond
public void testReceiveFile_cancelConnectJob() throws Exception {
    if (!CANCEL_SUPPORTED_ON_CONNECT) {
        trace("WARNING:  Cancel not supported by this provider.  testReceiveFile_cancelConnectJob 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) {

                public void running(IJobChangeEvent jobEvent) {
                    super.running(jobEvent);
                    spawnCancelThread(doCancel, new ICancelable() {

                        public void cancel() {
                            assertNotNull(socketInReadWrapper);
                            assertTrue(socketInReadWrapper.inRead);
                            event.cancel();
                        }
                    });
                }
            });
            event.connectUsingJob(connectJob);
        }
    };
    final SimpleServer server = new SimpleServer(getName());
    SimpleHttpServer simple = server.getSimpleHttpServer();
    simple.setRequestHandler(new HttpRequestHandler() {

        public boolean processRequest(SimpleHttpServerConnection conn, SimpleRequest request) throws IOException {
            trace("Not responding to request " + request.getRequestLine());
            return stalledInRequestHandler(doCancel);
        }
    });
    try {
        // path does not matter as server does not respond.
        testReceive(server.getServerURL() + "/foo", lw);
        assertHasEvent(startConnectEvents, IFileTransferConnectStartEvent.class);
        assertHasNoEvent(startEvents, IIncomingFileTransferReceiveStartEvent.class);
        assertHasNoEvent(dataEvents, IIncomingFileTransferReceiveDataEvent.class);
        IIncomingFileTransferReceiveDoneEvent doneEvent = getDoneEvent();
        assertTrue(doneEvent.getException().toString(), doneEvent.getException() instanceof UserCancelledException);
        assertTrue(doneEvent.getSource().isDone());
        assertSame(doneEvent.getException(), doneEvent.getSource().getException());
        assertNull(tmpFile);
        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) UserCancelledException(org.eclipse.ecf.filetransfer.UserCancelledException) SimpleHttpServerConnection(org.apache.commons.httpclient.server.SimpleHttpServerConnection) IFileTransferListener(org.eclipse.ecf.filetransfer.IFileTransferListener) IJobChangeEvent(org.eclipse.core.runtime.jobs.IJobChangeEvent) SimpleRequest(org.apache.commons.httpclient.server.SimpleRequest) IOException(java.io.IOException) IFileTransferConnectStartEvent(org.eclipse.ecf.filetransfer.events.IFileTransferConnectStartEvent) FileTransferJob(org.eclipse.ecf.filetransfer.FileTransferJob) SimpleHttpServer(org.apache.commons.httpclient.server.SimpleHttpServer) IIncomingFileTransferReceiveDoneEvent(org.eclipse.ecf.filetransfer.events.IIncomingFileTransferReceiveDoneEvent)

Example 8 with SimpleHttpServerConnection

use of org.apache.commons.httpclient.server.SimpleHttpServerConnection in project ecf by eclipse.

the class URLRetrieveTestCancelConnectJob method testReceiveFile_cancelTransferJob.

public void testReceiveFile_cancelTransferJob() throws Exception {
    if (!CANCEL_SUPPORTED_ON_CONNECT) {
        trace("WARNING:  Cancel not supported by this provider.  testReceiveFile_cancelTransferJob 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() {
                    waitForSocketInRead();
                    assertNotNull(socketInReadWrapper);
                    assertTrue(socketInReadWrapper.inRead);
                    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 full body" + request.getRequestLine());
            ResponseWriter w = conn.getWriter();
            writeLines(w, new String[] { "HTTP/1.0 200 OK", "Content-Length: 9", "Content-Type: text/plain; charset=UTF-8", "" });
            w.flush();
            synchronized (doCancel) {
                doCancel[0] = Boolean.TRUE;
            }
            conn.setKeepAlive(true);
            // 
            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);
        assertDoneCancelled();
        assertNotNull(tmpFile);
        assertTrue(tmpFile.exists());
        assertEquals(0, 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 9 with SimpleHttpServerConnection

use of org.apache.commons.httpclient.server.SimpleHttpServerConnection in project ecf by eclipse.

the class TestResponseHeaders method testFoldedHeaders.

public void testFoldedHeaders() throws Exception {
    final String body = "XXX\r\nYYY\r\nZZZ";
    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: close");
            out.println("Content-Length: " + body.length());
            out.println("Content-Type: text/xml; charset=utf-8");
            out.println("\tboundary=XXXX");
            out.println("Date: Wed, 28 Mar 2001");
            out.println(" 05:05:04 GMT");
            out.println("Server: UserLand Frontier/7.0-WinNT");
            out.println();
            out.println(body);
            out.flush();
            return true;
        }
    });
    HttpMethod method = new GetMethod("/");
    client.executeMethod(method);
    assertEquals("close", method.getResponseHeader("Connection").getValue());
    assertEquals(body.length(), Integer.parseInt(method.getResponseHeader("Content-Length").getValue()));
    assertEquals("text/xml; charset=utf-8 boundary=XXXX", method.getResponseHeader("Content-Type").getValue());
    assertEquals("Wed, 28 Mar 2001 05:05:04 GMT", method.getResponseHeader("Date").getValue());
    assertEquals("UserLand Frontier/7.0-WinNT", method.getResponseHeader("Server").getValue());
    assertTrue(method.getResponseHeader("Content-Type").toString().indexOf("boundary") != -1);
}
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)

Example 10 with SimpleHttpServerConnection

use of org.apache.commons.httpclient.server.SimpleHttpServerConnection in project ecf by eclipse.

the class TestResponseHeaders method testProxyNoContentLength.

public void testProxyNoContentLength() throws Exception {
    // test with proxy-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("proxy-connection: keep-alive");
            out.println();
            out.println("12345");
            out.flush();
            return true;
        }
    });
    client.getHostConfiguration().setProxy(server.getLocalAddress(), server.getLocalPort());
    GetMethod method = new GetMethod("/");
    client.executeMethod(method);
    method.getResponseBodyAsString();
    assertFalse(connectionManager.getConection().isOpen());
    // test without proxy-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;
        }
    });
    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