Search in sources :

Example 1 with EchoService

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

Example 2 with EchoService

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

Aggregations

EchoService (org.apache.commons.httpclient.EchoService)2 Header (org.apache.commons.httpclient.Header)2 HttpState (org.apache.commons.httpclient.HttpState)2 UsernamePasswordCredentials (org.apache.commons.httpclient.UsernamePasswordCredentials)2 StringRequestEntity (org.apache.commons.httpclient.methods.StringRequestEntity)2 AuthRequestHandler (org.apache.commons.httpclient.server.AuthRequestHandler)2 HttpRequestHandlerChain (org.apache.commons.httpclient.server.HttpRequestHandlerChain)2 HttpServiceHandler (org.apache.commons.httpclient.server.HttpServiceHandler)2 PostMethod (org.apache.commons.httpclient.methods.PostMethod)1 PutMethod (org.apache.commons.httpclient.methods.PutMethod)1