use of org.apache.commons.httpclient.server.AuthRequestHandler 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());
}
use of org.apache.commons.httpclient.server.AuthRequestHandler 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());
}
use of org.apache.commons.httpclient.server.AuthRequestHandler 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());
}
use of org.apache.commons.httpclient.server.AuthRequestHandler in project ecf by eclipse.
the class TestEntityEnclosingMethod method testEnclosedEntityRepeatable.
public void testEnclosedEntityRepeatable() throws Exception {
String inputstr = "This is a test message";
byte[] input = inputstr.getBytes("US-ASCII");
InputStream instream = new ByteArrayInputStream(input);
RequestEntity requestentity = new InputStreamRequestEntity(instream, InputStreamRequestEntity.CONTENT_LENGTH_AUTO);
PostMethod method = new PostMethod("/");
method.setRequestEntity(requestentity);
UsernamePasswordCredentials creds = new UsernamePasswordCredentials("testuser", "testpass");
HttpRequestHandlerChain handlerchain = new HttpRequestHandlerChain();
handlerchain.appendHandler(new AuthRequestHandler(creds));
handlerchain.appendHandler(new HttpServiceHandler(new EchoService()));
this.server.setRequestHandler(handlerchain);
this.client.getState().setCredentials(AuthScope.ANY, creds);
try {
this.client.executeMethod(method);
assertEquals(200, method.getStatusCode());
String body = method.getResponseBodyAsString();
assertEquals(inputstr, body);
assertNull(method.getRequestHeader("Transfer-Encoding"));
assertNotNull(method.getRequestHeader("Content-Length"));
assertEquals(input.length, Integer.parseInt(method.getRequestHeader("Content-Length").getValue()));
} finally {
method.releaseConnection();
}
}
use of org.apache.commons.httpclient.server.AuthRequestHandler in project ecf by eclipse.
the class TestEntityEnclosingMethod method testEnclosedEntityNonRepeatable.
public void testEnclosedEntityNonRepeatable() throws Exception {
String inputstr = "This is a test message";
byte[] input = inputstr.getBytes("US-ASCII");
InputStream instream = new ByteArrayInputStream(input);
RequestEntity requestentity = new InputStreamRequestEntity(instream, InputStreamRequestEntity.CONTENT_LENGTH_AUTO);
PostMethod method = new PostMethod("/");
method.setRequestEntity(requestentity);
method.setContentChunked(true);
UsernamePasswordCredentials creds = new UsernamePasswordCredentials("testuser", "testpass");
HttpRequestHandlerChain handlerchain = new HttpRequestHandlerChain();
handlerchain.appendHandler(new AuthRequestHandler(creds));
handlerchain.appendHandler(new HttpServiceHandler(new EchoService()));
this.server.setRequestHandler(handlerchain);
this.client.getState().setCredentials(AuthScope.ANY, creds);
try {
this.client.executeMethod(method);
fail("ProtocolException should have been thrown");
} catch (ProtocolException ex) {
// expected
} finally {
method.releaseConnection();
}
}
Aggregations