Search in sources :

Example 16 with HttpServiceHandler

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

the class TestBasicAuth method testBasicAuthenticationWithNoCredsRetry.

public void testBasicAuthenticationWithNoCredsRetry() throws IOException {
    UsernamePasswordCredentials creds = new UsernamePasswordCredentials("testuser", "testpass");
    HttpRequestHandlerChain handlerchain = new HttpRequestHandlerChain();
    handlerchain.appendHandler(new AuthRequestHandler(creds));
    handlerchain.appendHandler(new HttpServiceHandler(new FeedbackService()));
    this.server.setRequestHandler(handlerchain);
    GetMethod httpget = new GetMethod("/test/");
    try {
        this.client.executeMethod(httpget);
        assertNotNull(httpget.getStatusLine());
        assertEquals(HttpStatus.SC_UNAUTHORIZED, httpget.getStatusLine().getStatusCode());
        AuthState authstate = httpget.getHostAuthState();
        assertNotNull(authstate.getAuthScheme());
        assertTrue(authstate.getAuthScheme() instanceof BasicScheme);
        assertEquals("test", authstate.getRealm());
    } finally {
        httpget.releaseConnection();
    }
    // now try with credentials
    httpget = new GetMethod("/test/");
    try {
        this.client.getState().setCredentials(AuthScope.ANY, creds);
        this.client.executeMethod(httpget);
        assertNotNull(httpget.getStatusLine());
        assertEquals(HttpStatus.SC_OK, httpget.getStatusLine().getStatusCode());
    } finally {
        httpget.releaseConnection();
    }
}
Also used : HttpServiceHandler(org.apache.commons.httpclient.server.HttpServiceHandler) FeedbackService(org.apache.commons.httpclient.FeedbackService) HttpRequestHandlerChain(org.apache.commons.httpclient.server.HttpRequestHandlerChain) GetMethod(org.apache.commons.httpclient.methods.GetMethod) AuthRequestHandler(org.apache.commons.httpclient.server.AuthRequestHandler) UsernamePasswordCredentials(org.apache.commons.httpclient.UsernamePasswordCredentials)

Example 17 with HttpServiceHandler

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

the class TestBasicAuth method testBasicAuthentication.

public void testBasicAuthentication() throws Exception {
    UsernamePasswordCredentials creds = new UsernamePasswordCredentials("testuser", "testpass");
    HttpRequestHandlerChain handlerchain = new HttpRequestHandlerChain();
    handlerchain.appendHandler(new AuthRequestHandler(creds));
    handlerchain.appendHandler(new HttpServiceHandler(new FeedbackService()));
    HttpState state = new HttpState();
    AuthScope authscope = new AuthScope(this.server.getLocalAddress(), this.server.getLocalPort(), "test");
    state.setCredentials(authscope, creds);
    this.client.setState(state);
    this.server.setRequestHandler(handlerchain);
    GetMethod httpget = new GetMethod("/test/");
    try {
        this.client.executeMethod(httpget);
    } finally {
        httpget.releaseConnection();
    }
    assertNotNull(httpget.getStatusLine());
    assertEquals(HttpStatus.SC_OK, httpget.getStatusLine().getStatusCode());
    Header auth = httpget.getRequestHeader("Authorization");
    assertNotNull(auth);
    String expected = "Basic " + EncodingUtil.getAsciiString(Base64.encodeBase64(EncodingUtil.getAsciiBytes("testuser:testpass")));
    assertEquals(expected, auth.getValue());
    AuthState authstate = httpget.getHostAuthState();
    assertNotNull(authstate.getAuthScheme());
    assertTrue(authstate.getAuthScheme() instanceof BasicScheme);
    assertEquals("test", authstate.getRealm());
}
Also used : Header(org.apache.commons.httpclient.Header) HttpServiceHandler(org.apache.commons.httpclient.server.HttpServiceHandler) FeedbackService(org.apache.commons.httpclient.FeedbackService) HttpRequestHandlerChain(org.apache.commons.httpclient.server.HttpRequestHandlerChain) HttpState(org.apache.commons.httpclient.HttpState) GetMethod(org.apache.commons.httpclient.methods.GetMethod) AuthRequestHandler(org.apache.commons.httpclient.server.AuthRequestHandler) UsernamePasswordCredentials(org.apache.commons.httpclient.UsernamePasswordCredentials)

Example 18 with HttpServiceHandler

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

the class TestBasicAuth method testPutBasicAuthentication.

public void testPutBasicAuthentication() throws Exception {
    UsernamePasswordCredentials creds = new UsernamePasswordCredentials("testuser", "testpass");
    HttpRequestHandlerChain handlerchain = new HttpRequestHandlerChain();
    handlerchain.appendHandler(new AuthRequestHandler(creds));
    handlerchain.appendHandler(new HttpServiceHandler(new EchoService()));
    HttpState state = new HttpState();
    AuthScope authscope = new AuthScope(this.server.getLocalAddress(), this.server.getLocalPort(), "test");
    state.setCredentials(authscope, creds);
    this.client.setState(state);
    this.server.setRequestHandler(handlerchain);
    PutMethod put = new PutMethod("/test/");
    put.setRequestEntity(new StringRequestEntity("Test body"));
    try {
        this.client.executeMethod(put);
        assertEquals("Test body", put.getResponseBodyAsString());
    } finally {
        put.releaseConnection();
    }
    assertNotNull(put.getStatusLine());
    assertEquals(HttpStatus.SC_OK, put.getStatusLine().getStatusCode());
    Header auth = put.getRequestHeader("Authorization");
    assertNotNull(auth);
    String expected = "Basic " + EncodingUtil.getAsciiString(Base64.encodeBase64(EncodingUtil.getAsciiBytes("testuser:testpass")));
    assertEquals(expected, auth.getValue());
    AuthState authstate = put.getHostAuthState();
    assertNotNull(authstate.getAuthScheme());
    assertTrue(authstate.getAuthScheme() instanceof BasicScheme);
    assertEquals("test", authstate.getRealm());
}
Also used : StringRequestEntity(org.apache.commons.httpclient.methods.StringRequestEntity) EchoService(org.apache.commons.httpclient.EchoService) HttpState(org.apache.commons.httpclient.HttpState) UsernamePasswordCredentials(org.apache.commons.httpclient.UsernamePasswordCredentials) Header(org.apache.commons.httpclient.Header) HttpServiceHandler(org.apache.commons.httpclient.server.HttpServiceHandler) HttpRequestHandlerChain(org.apache.commons.httpclient.server.HttpRequestHandlerChain) PutMethod(org.apache.commons.httpclient.methods.PutMethod) AuthRequestHandler(org.apache.commons.httpclient.server.AuthRequestHandler)

Example 19 with HttpServiceHandler

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

the class TestBasicAuth method testPreemptiveAuthorizationTrueWithCreds.

public void testPreemptiveAuthorizationTrueWithCreds() throws Exception {
    UsernamePasswordCredentials creds = new UsernamePasswordCredentials("testuser", "testpass");
    HttpRequestHandlerChain handlerchain = new HttpRequestHandlerChain();
    handlerchain.appendHandler(new AuthRequestHandler(creds));
    handlerchain.appendHandler(new HttpServiceHandler(new FeedbackService()));
    HttpState state = new HttpState();
    state.setCredentials(AuthScope.ANY, creds);
    this.client.setState(state);
    this.client.getParams().setAuthenticationPreemptive(true);
    this.server.setRequestHandler(handlerchain);
    GetMethod httpget = new GetMethod("/test/");
    try {
        this.client.executeMethod(httpget);
    } finally {
        httpget.releaseConnection();
    }
    assertNotNull(httpget.getStatusLine());
    assertEquals(HttpStatus.SC_OK, httpget.getStatusLine().getStatusCode());
    Header auth = httpget.getRequestHeader("Authorization");
    assertNotNull(auth);
    String expected = "Basic " + EncodingUtil.getAsciiString(Base64.encodeBase64(EncodingUtil.getAsciiBytes("testuser:testpass")));
    assertEquals(expected, auth.getValue());
    AuthState authstate = httpget.getHostAuthState();
    assertNotNull(authstate.getAuthScheme());
    assertTrue(authstate.getAuthScheme() instanceof BasicScheme);
    assertNull(authstate.getRealm());
    assertTrue(authstate.isPreemptive());
}
Also used : Header(org.apache.commons.httpclient.Header) HttpServiceHandler(org.apache.commons.httpclient.server.HttpServiceHandler) FeedbackService(org.apache.commons.httpclient.FeedbackService) HttpRequestHandlerChain(org.apache.commons.httpclient.server.HttpRequestHandlerChain) HttpState(org.apache.commons.httpclient.HttpState) GetMethod(org.apache.commons.httpclient.methods.GetMethod) AuthRequestHandler(org.apache.commons.httpclient.server.AuthRequestHandler) UsernamePasswordCredentials(org.apache.commons.httpclient.UsernamePasswordCredentials)

Example 20 with HttpServiceHandler

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

the class TestBasicAuth method testBasicAuthenticationWithInvalidCredentials.

public void testBasicAuthenticationWithInvalidCredentials() throws Exception {
    UsernamePasswordCredentials creds = new UsernamePasswordCredentials("testuser", "testpass");
    HttpRequestHandlerChain handlerchain = new HttpRequestHandlerChain();
    handlerchain.appendHandler(new AuthRequestHandler(creds));
    handlerchain.appendHandler(new HttpServiceHandler(new FeedbackService()));
    HttpState state = new HttpState();
    AuthScope authscope = new AuthScope(this.server.getLocalAddress(), this.server.getLocalPort(), "test");
    state.setCredentials(authscope, new UsernamePasswordCredentials("test", "stuff"));
    this.client.setState(state);
    this.server.setRequestHandler(handlerchain);
    GetMethod httpget = new GetMethod("/test/");
    try {
        this.client.executeMethod(httpget);
    } finally {
        httpget.releaseConnection();
    }
    assertNotNull(httpget.getStatusLine());
    assertEquals(HttpStatus.SC_UNAUTHORIZED, httpget.getStatusLine().getStatusCode());
    AuthState authstate = httpget.getHostAuthState();
    assertNotNull(authstate.getAuthScheme());
    assertTrue(authstate.getAuthScheme() instanceof BasicScheme);
    assertEquals("test", authstate.getRealm());
}
Also used : HttpServiceHandler(org.apache.commons.httpclient.server.HttpServiceHandler) FeedbackService(org.apache.commons.httpclient.FeedbackService) HttpRequestHandlerChain(org.apache.commons.httpclient.server.HttpRequestHandlerChain) HttpState(org.apache.commons.httpclient.HttpState) GetMethod(org.apache.commons.httpclient.methods.GetMethod) AuthRequestHandler(org.apache.commons.httpclient.server.AuthRequestHandler) UsernamePasswordCredentials(org.apache.commons.httpclient.UsernamePasswordCredentials)

Aggregations

AuthRequestHandler (org.apache.commons.httpclient.server.AuthRequestHandler)37 HttpRequestHandlerChain (org.apache.commons.httpclient.server.HttpRequestHandlerChain)37 HttpServiceHandler (org.apache.commons.httpclient.server.HttpServiceHandler)37 GetMethod (org.apache.commons.httpclient.methods.GetMethod)22 UsernamePasswordCredentials (org.apache.commons.httpclient.UsernamePasswordCredentials)14 StringRequestEntity (org.apache.commons.httpclient.methods.StringRequestEntity)14 PostMethod (org.apache.commons.httpclient.methods.PostMethod)13 FeedbackService (org.apache.commons.httpclient.FeedbackService)12 HttpState (org.apache.commons.httpclient.HttpState)11 Header (org.apache.commons.httpclient.Header)10 ByteArrayInputStream (java.io.ByteArrayInputStream)2 InputStream (java.io.InputStream)2 EchoService (org.apache.commons.httpclient.EchoService)2 InputStreamRequestEntity (org.apache.commons.httpclient.methods.InputStreamRequestEntity)2 RequestEntity (org.apache.commons.httpclient.methods.RequestEntity)2 HeadMethod (org.apache.commons.httpclient.methods.HeadMethod)1 PutMethod (org.apache.commons.httpclient.methods.PutMethod)1