Search in sources :

Example 11 with FeedbackService

use of org.apache.commons.httpclient.FeedbackService 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)

Example 12 with FeedbackService

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

the class TestBasicAuth method testPreemptiveAuthorizationTrueWithoutCreds.

public void testPreemptiveAuthorizationTrueWithoutCreds() 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();
    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_UNAUTHORIZED, httpget.getStatusLine().getStatusCode());
    Header auth = httpget.getRequestHeader("Authorization");
    assertNull(auth);
    AuthState authstate = httpget.getHostAuthState();
    assertNotNull(authstate.getAuthScheme());
    assertTrue(authstate.getAuthScheme() instanceof BasicScheme);
    assertNotNull(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 13 with FeedbackService

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

the class TestBasicAuth method testCustomAuthorizationHeader.

public void testCustomAuthorizationHeader() throws Exception {
    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/");
    String authResponse = "Basic " + EncodingUtil.getAsciiString(Base64.encodeBase64(EncodingUtil.getAsciiBytes("testuser:testpass")));
    httpget.addRequestHeader(new Header("Authorization", authResponse));
    try {
        this.client.executeMethod(httpget);
    } finally {
        httpget.releaseConnection();
    }
    assertNotNull(httpget.getStatusLine());
    assertEquals(HttpStatus.SC_OK, httpget.getStatusLine().getStatusCode());
}
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) GetMethod(org.apache.commons.httpclient.methods.GetMethod) AuthRequestHandler(org.apache.commons.httpclient.server.AuthRequestHandler) UsernamePasswordCredentials(org.apache.commons.httpclient.UsernamePasswordCredentials)

Example 14 with FeedbackService

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

the class TestBasicAuth method testPreemptiveAuthorizationFailure.

public void testPreemptiveAuthorizationFailure() throws Exception {
    UsernamePasswordCredentials creds = new UsernamePasswordCredentials("testuser", "testpass");
    UsernamePasswordCredentials wrongcreds = new UsernamePasswordCredentials("testuser", "garbage");
    HttpRequestHandlerChain handlerchain = new HttpRequestHandlerChain();
    handlerchain.appendHandler(new AuthRequestHandler(creds));
    handlerchain.appendHandler(new HttpServiceHandler(new FeedbackService()));
    HttpState state = new HttpState();
    state.setCredentials(AuthScope.ANY, wrongcreds);
    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_UNAUTHORIZED, httpget.getStatusLine().getStatusCode());
    AuthState authstate = httpget.getHostAuthState();
    assertNotNull(authstate.getAuthScheme());
    assertTrue(authstate.getAuthScheme() instanceof BasicScheme);
    assertEquals("test", authstate.getRealm());
    assertTrue(authstate.isPreemptive());
}
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)

Example 15 with FeedbackService

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

the class TestSSLTunnelParams method testTunnellingParamsHostLevel.

/**
 * Tests correct proparation of HTTP params from the host config to
 * CONNECT method.
 */
public void testTunnellingParamsHostLevel() throws IOException {
    this.proxy.addHandler(new HttpVersionHandler());
    this.server.setHttpService(new FeedbackService());
    this.client.getHostConfiguration().getParams().setParameter(HttpMethodParams.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
    GetMethod httpget = new GetMethod("/test/");
    try {
        this.client.executeMethod(httpget);
        assertNotNull(httpget.getStatusLine());
        assertEquals(HttpStatus.SC_HTTP_VERSION_NOT_SUPPORTED, httpget.getStatusLine().getStatusCode());
    } finally {
        httpget.releaseConnection();
    }
    this.client.getHostConfiguration().getParams().setParameter(HttpMethodParams.PROTOCOL_VERSION, HttpVersion.HTTP_1_0);
    httpget = new GetMethod("/test/");
    try {
        this.client.executeMethod(httpget);
        assertNotNull(httpget.getStatusLine());
        assertEquals(HttpStatus.SC_OK, httpget.getStatusLine().getStatusCode());
    } finally {
        httpget.releaseConnection();
    }
}
Also used : FeedbackService(org.apache.commons.httpclient.FeedbackService) GetMethod(org.apache.commons.httpclient.methods.GetMethod)

Aggregations

FeedbackService (org.apache.commons.httpclient.FeedbackService)15 GetMethod (org.apache.commons.httpclient.methods.GetMethod)14 UsernamePasswordCredentials (org.apache.commons.httpclient.UsernamePasswordCredentials)12 AuthRequestHandler (org.apache.commons.httpclient.server.AuthRequestHandler)12 HttpRequestHandlerChain (org.apache.commons.httpclient.server.HttpRequestHandlerChain)12 HttpServiceHandler (org.apache.commons.httpclient.server.HttpServiceHandler)12 HttpState (org.apache.commons.httpclient.HttpState)9 Header (org.apache.commons.httpclient.Header)8 HeadMethod (org.apache.commons.httpclient.methods.HeadMethod)1