Added a test case for handling of authentication failures with non-repeatable requests

git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@759942 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Oleg Kalnichevski 2009-03-30 12:33:25 +00:00
parent e2520f8509
commit dccecd2411
2 changed files with 55 additions and 1 deletions

View File

@ -399,7 +399,8 @@ public class DefaultRequestDirector implements RequestDirector {
// Increment exec count for this particular request // Increment exec count for this particular request
wrapper.incrementExecCount(); wrapper.incrementExecCount();
if (wrapper.getExecCount() > 1 && !wrapper.isRepeatable()) { if (wrapper.getExecCount() > 1 && !wrapper.isRepeatable()) {
if(retryReason != null) { this.log.debug("Cannot retry non-repeatable request");
if (retryReason != null) {
throw new NonRepeatableRequestException("Cannot retry request " + throw new NonRepeatableRequestException("Cannot retry request " +
"with a non-repeatable request entity. The cause lists the " + "with a non-repeatable request entity. The cause lists the " +
"reason the original request failed.", retryReason); "reason the original request failed.", retryReason);

View File

@ -28,6 +28,7 @@
package org.apache.http.impl.client; package org.apache.http.impl.client;
import java.io.ByteArrayInputStream;
import java.io.IOException; import java.io.IOException;
import junit.framework.Test; import junit.framework.Test;
@ -41,8 +42,12 @@ import org.apache.http.HttpStatus;
import org.apache.http.auth.AuthScope; import org.apache.http.auth.AuthScope;
import org.apache.http.auth.Credentials; import org.apache.http.auth.Credentials;
import org.apache.http.auth.UsernamePasswordCredentials; import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.CredentialsProvider; import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.NonRepeatableRequestException;
import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.InputStreamEntity;
import org.apache.http.entity.StringEntity; import org.apache.http.entity.StringEntity;
import org.apache.http.localserver.BasicServerTestBase; import org.apache.http.localserver.BasicServerTestBase;
import org.apache.http.localserver.LocalTestServer; import org.apache.http.localserver.LocalTestServer;
@ -198,4 +203,52 @@ public class TestClientAuthentication extends BasicServerTestBase {
assertEquals("test realm", authscope.getRealm()); assertEquals("test realm", authscope.getRealm());
} }
public void testBasicAuthenticationSuccessOnRepeatablePost() throws Exception {
localServer.register("*", new AuthHandler());
localServer.start();
TestCredentialsProvider credsProvider = new TestCredentialsProvider(
new UsernamePasswordCredentials("test", "test"));
DefaultHttpClient httpclient = new DefaultHttpClient();
httpclient.setCredentialsProvider(credsProvider);
HttpPost httppost = new HttpPost("/");
httppost.setEntity(new StringEntity("some important stuff", HTTP.ISO_8859_1));
HttpResponse response = httpclient.execute(getServerHttp(), httppost);
HttpEntity entity = response.getEntity();
assertEquals(HttpStatus.SC_OK, response.getStatusLine().getStatusCode());
assertNotNull(entity);
entity.consumeContent();
AuthScope authscope = credsProvider.getAuthScope();
assertNotNull(authscope);
assertEquals("test realm", authscope.getRealm());
}
public void testBasicAuthenticationFailureOnNonRepeatablePost() throws Exception {
localServer.register("*", new AuthHandler());
localServer.start();
TestCredentialsProvider credsProvider = new TestCredentialsProvider(
new UsernamePasswordCredentials("test", "test"));
DefaultHttpClient httpclient = new DefaultHttpClient();
httpclient.setCredentialsProvider(credsProvider);
HttpPost httppost = new HttpPost("/");
httppost.setEntity(new InputStreamEntity(
new ByteArrayInputStream(
new byte[] { 0,1,2,3,4,5,6,7,8,9 }), -1));
try {
httpclient.execute(getServerHttp(), httppost);
fail("ClientProtocolException should have been thrown");
} catch (ClientProtocolException ex) {
Throwable cause = ex.getCause();
assertNotNull(cause);
assertTrue(cause instanceof NonRepeatableRequestException);
}
}
} }