Search in sources :

Example 6 with AuthRequestHandler

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

the class TestProxy method testGetHostAuthConnKeepAlive.

/**
 * Tests GET via non-authenticating proxy + host auth + connection keep-alive
 */
public void testGetHostAuthConnKeepAlive() throws Exception {
    UsernamePasswordCredentials creds = new UsernamePasswordCredentials("testuser", "testpass");
    this.client.getState().setCredentials(AuthScope.ANY, creds);
    HttpRequestHandlerChain handlerchain = new HttpRequestHandlerChain();
    handlerchain.appendHandler(new AuthRequestHandler(creds, "test", true));
    handlerchain.appendHandler(new HttpServiceHandler(new FeedbackService()));
    this.server.setRequestHandler(handlerchain);
    GetMethod get = new GetMethod("/");
    try {
        this.client.executeMethod(get);
        assertEquals(HttpStatus.SC_OK, get.getStatusCode());
    } finally {
        get.releaseConnection();
    }
}
Also used : HttpServiceHandler(org.apache.commons.httpclient.server.HttpServiceHandler) HttpRequestHandlerChain(org.apache.commons.httpclient.server.HttpRequestHandlerChain) GetMethod(org.apache.commons.httpclient.methods.GetMethod) AuthRequestHandler(org.apache.commons.httpclient.server.AuthRequestHandler)

Example 7 with AuthRequestHandler

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

the class TestProxy method testPostProxyAuthHostInvalidAuth.

/**
 * Tests POST via non-authenticating proxy + invalid host auth
 */
public void testPostProxyAuthHostInvalidAuth() throws Exception {
    UsernamePasswordCredentials creds = new UsernamePasswordCredentials("testuser", "testpass");
    this.client.getState().setProxyCredentials(AuthScope.ANY, creds);
    HttpRequestHandlerChain handlerchain = new HttpRequestHandlerChain();
    handlerchain.appendHandler(new AuthRequestHandler(creds));
    handlerchain.appendHandler(new HttpServiceHandler(new FeedbackService()));
    this.client.getState().setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("testuser", "wrongstuff"));
    this.server.setRequestHandler(handlerchain);
    this.proxy.requireAuthentication(creds, "test", true);
    PostMethod post = new PostMethod("/");
    post.setRequestEntity(new StringRequestEntity("Like tons of stuff"));
    try {
        this.client.executeMethod(post);
        assertEquals(HttpStatus.SC_UNAUTHORIZED, post.getStatusCode());
    } finally {
        post.releaseConnection();
    }
}
Also used : StringRequestEntity(org.apache.commons.httpclient.methods.StringRequestEntity) HttpServiceHandler(org.apache.commons.httpclient.server.HttpServiceHandler) PostMethod(org.apache.commons.httpclient.methods.PostMethod) HttpRequestHandlerChain(org.apache.commons.httpclient.server.HttpRequestHandlerChain) AuthRequestHandler(org.apache.commons.httpclient.server.AuthRequestHandler)

Example 8 with AuthRequestHandler

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

the class TestProxy method testPostProxyAuthHostAuthConnClose.

/**
 * Tests POST via authenticating proxy + host auth + connection close
 */
public void testPostProxyAuthHostAuthConnClose() throws Exception {
    UsernamePasswordCredentials creds = new UsernamePasswordCredentials("testuser", "testpass");
    this.client.getState().setCredentials(AuthScope.ANY, creds);
    this.client.getState().setProxyCredentials(AuthScope.ANY, creds);
    HttpRequestHandlerChain handlerchain = new HttpRequestHandlerChain();
    handlerchain.appendHandler(new AuthRequestHandler(creds, "test", false));
    handlerchain.appendHandler(new HttpServiceHandler(new FeedbackService()));
    this.server.setRequestHandler(handlerchain);
    this.proxy.requireAuthentication(creds, "test", true);
    PostMethod post = new PostMethod("/");
    post.setRequestEntity(new StringRequestEntity("Like tons of stuff"));
    try {
        this.client.executeMethod(post);
        assertEquals(HttpStatus.SC_OK, post.getStatusCode());
        assertNotNull(post.getResponseBodyAsString());
    } finally {
        post.releaseConnection();
    }
}
Also used : StringRequestEntity(org.apache.commons.httpclient.methods.StringRequestEntity) HttpServiceHandler(org.apache.commons.httpclient.server.HttpServiceHandler) PostMethod(org.apache.commons.httpclient.methods.PostMethod) HttpRequestHandlerChain(org.apache.commons.httpclient.server.HttpRequestHandlerChain) AuthRequestHandler(org.apache.commons.httpclient.server.AuthRequestHandler)

Example 9 with AuthRequestHandler

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

the class TestProxy method testGetHostInvalidAuth.

/**
 * Tests GET via non-authenticating proxy + invalid host auth
 */
public void testGetHostInvalidAuth() throws Exception {
    UsernamePasswordCredentials creds = new UsernamePasswordCredentials("testuser", "testpass");
    this.client.getState().setCredentials(AuthScope.ANY, creds);
    HttpRequestHandlerChain handlerchain = new HttpRequestHandlerChain();
    handlerchain.appendHandler(new AuthRequestHandler(creds));
    handlerchain.appendHandler(new HttpServiceHandler(new FeedbackService()));
    this.client.getState().setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("testuser", "wrongstuff"));
    this.server.setRequestHandler(handlerchain);
    GetMethod get = new GetMethod("/");
    try {
        this.client.executeMethod(get);
        assertEquals(HttpStatus.SC_UNAUTHORIZED, get.getStatusCode());
    } finally {
        get.releaseConnection();
    }
}
Also used : HttpServiceHandler(org.apache.commons.httpclient.server.HttpServiceHandler) HttpRequestHandlerChain(org.apache.commons.httpclient.server.HttpRequestHandlerChain) GetMethod(org.apache.commons.httpclient.methods.GetMethod) AuthRequestHandler(org.apache.commons.httpclient.server.AuthRequestHandler)

Example 10 with AuthRequestHandler

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

the class TestBasicAuth method testPostBasicAuthentication.

public void testPostBasicAuthentication() 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);
    PostMethod post = new PostMethod("/test/");
    post.setRequestEntity(new StringRequestEntity("Test body"));
    try {
        this.client.executeMethod(post);
        assertEquals("Test body", post.getResponseBodyAsString());
    } finally {
        post.releaseConnection();
    }
    assertNotNull(post.getStatusLine());
    assertEquals(HttpStatus.SC_OK, post.getStatusLine().getStatusCode());
    Header auth = post.getRequestHeader("Authorization");
    assertNotNull(auth);
    String expected = "Basic " + EncodingUtil.getAsciiString(Base64.encodeBase64(EncodingUtil.getAsciiBytes("testuser:testpass")));
    assertEquals(expected, auth.getValue());
    AuthState authstate = post.getHostAuthState();
    assertNotNull(authstate.getAuthScheme());
    assertTrue(authstate.getAuthScheme() instanceof BasicScheme);
    assertEquals("test", authstate.getRealm());
}
Also used : StringRequestEntity(org.apache.commons.httpclient.methods.StringRequestEntity) PostMethod(org.apache.commons.httpclient.methods.PostMethod) 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) AuthRequestHandler(org.apache.commons.httpclient.server.AuthRequestHandler)

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