Search in sources :

Example 11 with SimpleHttpServer

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

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

the class TestDigestAuth method testDigestAuthenticationWithStaleNonce.

public void testDigestAuthenticationWithStaleNonce() throws Exception {
    // configure the server
    // use arbitrary port
    SimpleHttpServer server = new SimpleHttpServer();
    server.setTestname(getName());
    server.setHttpService(new StaleNonceService());
    // configure the client
    HttpClient client = new HttpClient();
    client.getHostConfiguration().setHost(server.getLocalAddress(), server.getLocalPort(), Protocol.getProtocol("http"));
    client.getState().setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("username", "password"));
    FakeHttpMethod httpget = new FakeHttpMethod("/");
    try {
        client.executeMethod(httpget);
    } finally {
        httpget.releaseConnection();
    }
    assertNotNull(httpget.getStatusLine());
    assertEquals(HttpStatus.SC_OK, httpget.getStatusLine().getStatusCode());
    Map table = AuthChallengeParser.extractParams(httpget.getRequestHeader("Authorization").getValue());
    assertEquals("username", table.get("username"));
    assertEquals("realm1", table.get("realm"));
    assertEquals("/", table.get("uri"));
    assertEquals("321CBA", table.get("nonce"));
    assertEquals("7f5948eefa115296e9279225041527b3", table.get("response"));
    server.destroy();
}
Also used : FakeHttpMethod(org.apache.commons.httpclient.FakeHttpMethod) SimpleHttpServer(org.apache.commons.httpclient.server.SimpleHttpServer) HttpClient(org.apache.commons.httpclient.HttpClient) Map(java.util.Map) UsernamePasswordCredentials(org.apache.commons.httpclient.UsernamePasswordCredentials)

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