From dccecd2411e32776f47f379a1a59933de21aee3f Mon Sep 17 00:00:00 2001 From: Oleg Kalnichevski Date: Mon, 30 Mar 2009 12:33:25 +0000 Subject: [PATCH] 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 --- .../impl/client/DefaultRequestDirector.java | 3 +- .../impl/client/TestClientAuthentication.java | 53 +++++++++++++++++++ 2 files changed, 55 insertions(+), 1 deletion(-) diff --git a/httpclient/src/main/java/org/apache/http/impl/client/DefaultRequestDirector.java b/httpclient/src/main/java/org/apache/http/impl/client/DefaultRequestDirector.java index 3e25eb325..a2bdd3adf 100644 --- a/httpclient/src/main/java/org/apache/http/impl/client/DefaultRequestDirector.java +++ b/httpclient/src/main/java/org/apache/http/impl/client/DefaultRequestDirector.java @@ -399,7 +399,8 @@ public class DefaultRequestDirector implements RequestDirector { // Increment exec count for this particular request wrapper.incrementExecCount(); 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 " + "with a non-repeatable request entity. The cause lists the " + "reason the original request failed.", retryReason); diff --git a/httpclient/src/test/java/org/apache/http/impl/client/TestClientAuthentication.java b/httpclient/src/test/java/org/apache/http/impl/client/TestClientAuthentication.java index 25451978e..79bcf37cb 100644 --- a/httpclient/src/test/java/org/apache/http/impl/client/TestClientAuthentication.java +++ b/httpclient/src/test/java/org/apache/http/impl/client/TestClientAuthentication.java @@ -28,6 +28,7 @@ package org.apache.http.impl.client; +import java.io.ByteArrayInputStream; import java.io.IOException; import junit.framework.Test; @@ -41,8 +42,12 @@ import org.apache.http.HttpStatus; import org.apache.http.auth.AuthScope; import org.apache.http.auth.Credentials; import org.apache.http.auth.UsernamePasswordCredentials; +import org.apache.http.client.ClientProtocolException; 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.HttpPost; +import org.apache.http.entity.InputStreamEntity; import org.apache.http.entity.StringEntity; import org.apache.http.localserver.BasicServerTestBase; import org.apache.http.localserver.LocalTestServer; @@ -198,4 +203,52 @@ public class TestClientAuthentication extends BasicServerTestBase { 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); + } + } + }