hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject svn commit: r1311382 - in /httpcomponents/httpclient/trunk: ./ httpclient/src/main/java/org/apache/http/client/utils/ httpclient/src/main/java/org/apache/http/impl/client/ httpclient/src/test/java/org/apache/http/client/utils/ httpclient/src/test/java/...
Date Mon, 09 Apr 2012 19:18:35 GMT
Author: olegk
Date: Mon Apr  9 19:18:35 2012
New Revision: 1311382

URL: http://svn.apache.org/viewvc?rev=1311382&view=rev
Log:
HTTPCLIENT-1177: always remove fragments from request URIs

Added:
    httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/client/TestDefaultRedirectStrategy.java
  (with props)
Modified:
    httpcomponents/httpclient/trunk/RELEASE_NOTES.txt
    httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/utils/URIUtils.java
    httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/DefaultRedirectStrategy.java
    httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/DefaultRequestDirector.java
    httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/client/utils/TestURIUtils.java

Modified: httpcomponents/httpclient/trunk/RELEASE_NOTES.txt
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/RELEASE_NOTES.txt?rev=1311382&r1=1311381&r2=1311382&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/RELEASE_NOTES.txt (original)
+++ httpcomponents/httpclient/trunk/RELEASE_NOTES.txt Mon Apr  9 19:18:35 2012
@@ -1,3 +1,10 @@
+Changes since 4.2 BETA1
+-------------------
+
+* [HTTPCLIENT-1177] always remove fragments from request URIs
+  Contributed by Oleg Kalnichevski <olegk at apache.org>
+
+
 Release 4.2 BETA1
 -------------------
 

Modified: httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/utils/URIUtils.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/utils/URIUtils.java?rev=1311382&r1=1311381&r2=1311382&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/utils/URIUtils.java
(original)
+++ httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/utils/URIUtils.java
Mon Apr  9 19:18:35 2012
@@ -176,6 +176,33 @@ public class URIUtils {
     }
 
     /**
+     * A convenience method that creates a new {@link URI} whose scheme, host, port ,path,
+     * query are taken from the existing URI if it contains a fragment. The existing URI
+     * is returned unmodified if it has no fragment.
+     *
+     * @param uri
+     *            original URI.
+     * @throws URISyntaxException
+     *             If the resulting URI is invalid.
+     */
+    public static URI rewriteURI(final URI uri) throws URISyntaxException {
+        if (uri == null) {
+            throw new IllegalArgumentException("URI may not be null");
+        }
+        if (uri.getFragment() != null) {
+            return URIUtils.createURI(
+                    uri.getScheme(),
+                    uri.getHost(),
+                    uri.getPort(),
+                    uri.getRawPath(),
+                    uri.getRawQuery(),
+                    null);
+        } else {
+            return uri;
+        }
+    }
+
+    /**
      * Resolves a URI reference against a base URI. Work-around for bug in
      * java.net.URI (<http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4708535>)
      *

Modified: httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/DefaultRedirectStrategy.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/DefaultRedirectStrategy.java?rev=1311382&r1=1311381&r2=1311382&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/DefaultRedirectStrategy.java
(original)
+++ httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/DefaultRedirectStrategy.java
Mon Apr  9 19:18:35 2012
@@ -115,9 +115,15 @@ public class DefaultRedirectStrategy imp
             final HttpRequest request,
             final HttpResponse response,
             final HttpContext context) throws ProtocolException {
+        if (request == null) {
+            throw new IllegalArgumentException("HTTP request may not be null");
+        }
         if (response == null) {
             throw new IllegalArgumentException("HTTP response may not be null");
         }
+        if (context == null) {
+            throw new IllegalArgumentException("HTTP context may not be null");
+        }
         //get the location header to find out where to redirect to
         Header locationHeader = response.getFirstHeader("location");
         if (locationHeader == null) {
@@ -133,63 +139,43 @@ public class DefaultRedirectStrategy imp
 
         URI uri = createLocationURI(location);
 
-        HttpParams params = response.getParams();
+        HttpParams params = request.getParams();
         // rfc2616 demands the location value be a complete URI
         // Location       = "Location" ":" absoluteURI
-        if (!uri.isAbsolute()) {
-            if (params.isParameterTrue(ClientPNames.REJECT_RELATIVE_REDIRECT)) {
-                throw new ProtocolException("Relative redirect location '"
-                        + uri + "' not allowed");
-            }
-            // Adjust location URI
-            HttpHost target = (HttpHost) context.getAttribute(
-                    ExecutionContext.HTTP_TARGET_HOST);
-            if (target == null) {
-                throw new IllegalStateException("Target host not available " +
-                        "in the HTTP context");
-            }
-            try {
+        try {
+            // Drop fragment
+            uri = URIUtils.rewriteURI(uri);
+            if (!uri.isAbsolute()) {
+                if (params.isParameterTrue(ClientPNames.REJECT_RELATIVE_REDIRECT)) {
+                    throw new ProtocolException("Relative redirect location '"
+                            + uri + "' not allowed");
+                }
+                // Adjust location URI
+                HttpHost target = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
+                if (target == null) {
+                    throw new IllegalStateException("Target host not available " +
+                            "in the HTTP context");
+                }
                 URI requestURI = new URI(request.getRequestLine().getUri());
                 URI absoluteRequestURI = URIUtils.rewriteURI(requestURI, target, true);
                 uri = URIUtils.resolve(absoluteRequestURI, uri);
-            } catch (URISyntaxException ex) {
-                throw new ProtocolException(ex.getMessage(), ex);
             }
+        } catch (URISyntaxException ex) {
+            throw new ProtocolException(ex.getMessage(), ex);
         }
 
+        RedirectLocations redirectLocations = (RedirectLocations) context.getAttribute(
+                REDIRECT_LOCATIONS);
+        if (redirectLocations == null) {
+            redirectLocations = new RedirectLocations();
+            context.setAttribute(REDIRECT_LOCATIONS, redirectLocations);
+        }
         if (params.isParameterFalse(ClientPNames.ALLOW_CIRCULAR_REDIRECTS)) {
-
-            RedirectLocations redirectLocations = (RedirectLocations) context.getAttribute(
-                    REDIRECT_LOCATIONS);
-
-            if (redirectLocations == null) {
-                redirectLocations = new RedirectLocations();
-                context.setAttribute(REDIRECT_LOCATIONS, redirectLocations);
-            }
-
-            URI redirectURI;
-            if (uri.getFragment() != null) {
-                try {
-                    HttpHost target = new HttpHost(
-                            uri.getHost(),
-                            uri.getPort(),
-                            uri.getScheme());
-                    redirectURI = URIUtils.rewriteURI(uri, target, true);
-                } catch (URISyntaxException ex) {
-                    throw new ProtocolException(ex.getMessage(), ex);
-                }
-            } else {
-                redirectURI = uri;
-            }
-
-            if (redirectLocations.contains(redirectURI)) {
-                throw new CircularRedirectException("Circular redirect to '" +
-                        redirectURI + "'");
-            } else {
-                redirectLocations.add(redirectURI);
+            if (redirectLocations.contains(uri)) {
+                throw new CircularRedirectException("Circular redirect to '" + uri + "'");
             }
         }
-
+        redirectLocations.add(uri);
         return uri;
     }
 

Modified: httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/DefaultRequestDirector.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/DefaultRequestDirector.java?rev=1311382&r1=1311381&r2=1311382&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/DefaultRequestDirector.java
(original)
+++ httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/DefaultRequestDirector.java
Mon Apr  9 19:18:35 2012
@@ -379,14 +379,18 @@ public class DefaultRequestDirector impl
                 // Make sure the request URI is absolute
                 if (!uri.isAbsolute()) {
                     HttpHost target = route.getTargetHost();
-                    uri = URIUtils.rewriteURI(uri, target);
+                    uri = URIUtils.rewriteURI(uri, target, true);
                     request.setURI(uri);
+                } else {
+                    uri = URIUtils.rewriteURI(uri);
                 }
             } else {
                 // Make sure the request URI is relative
                 if (uri.isAbsolute()) {
                     uri = URIUtils.rewriteURI(uri, null);
                     request.setURI(uri);
+                } else {
+                    uri = URIUtils.rewriteURI(uri);
                 }
             }
 

Modified: httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/client/utils/TestURIUtils.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/client/utils/TestURIUtils.java?rev=1311382&r1=1311381&r2=1311382&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/client/utils/TestURIUtils.java
(original)
+++ httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/client/utils/TestURIUtils.java
Mon Apr  9 19:18:35 2012
@@ -40,285 +40,87 @@ public class TestURIUtils {
     private URI baseURI = URI.create("http://a/b/c/d;p?q");
 
     @Test
-    public void testRewite00() throws Exception {
-        URI uri = URI.create("http://thishost/stuff");
+    public void testRewite() throws Exception {
         HttpHost target = new HttpHost("thathost", -1);
-        Assert.assertEquals("http://thathost/stuff", URIUtils.rewriteURI(uri, target).toString());
+        Assert.assertEquals("http://thathost/stuff", URIUtils.rewriteURI(
+                URI.create("http://thishost/stuff"), target).toString());
+        Assert.assertEquals("/stuff", URIUtils.rewriteURI(
+                URI.create("http://thishost/stuff"), null).toString());
+        Assert.assertEquals("/", URIUtils.rewriteURI(
+                URI.create("http://thishost//"), null).toString());
+        Assert.assertEquals("/stuff///morestuff", URIUtils.rewriteURI(
+                URI.create("http://thishost//stuff///morestuff"), null).toString());
+        Assert.assertEquals("http://thathost/stuff", URIUtils.rewriteURI(
+                URI.create("http://thishost/stuff#crap"), target, true).toString());
+        Assert.assertEquals("http://thathost/stuff#crap", URIUtils.rewriteURI(
+                URI.create("http://thishost/stuff#crap"), target, false).toString());
+        Assert.assertEquals("/stuff/", URIUtils.rewriteURI(
+                URI.create("http://thishost//////////////stuff/"), null).toString());
+        Assert.assertEquals("http://thathost/stuff", URIUtils.rewriteURI(
+                URI.create("http://thathost/stuff")).toString());
+        Assert.assertEquals("http://thathost/stuff", URIUtils.rewriteURI(
+                URI.create("http://thathost/stuff#fragment")).toString());
     }
 
     @Test
-    public void testRewite01() throws Exception {
-        URI uri = URI.create("http://thishost/stuff");
-        Assert.assertEquals("/stuff", URIUtils.rewriteURI(uri, null).toString());
-    }
-
-    @Test
-    public void testRewite02() throws Exception {
-        URI uri = URI.create("http://thishost//");
-        Assert.assertEquals("/", URIUtils.rewriteURI(uri, null).toString());
-    }
-
-    @Test
-    public void testRewite03() throws Exception {
-        URI uri = URI.create("http://thishost//stuff///morestuff");
-        Assert.assertEquals("/stuff///morestuff", URIUtils.rewriteURI(uri, null).toString());
-    }
-
-    @Test
-    public void testRewite04() throws Exception {
-        URI uri = URI.create("http://thishost/stuff#crap");
-        HttpHost target = new HttpHost("thathost", -1);
-        Assert.assertEquals("http://thathost/stuff", URIUtils.rewriteURI(uri, target, true).toString());
-    }
-
-    @Test
-    public void testRewite05() throws Exception {
-        URI uri = URI.create("http://thishost/stuff#crap");
-        HttpHost target = new HttpHost("thathost", -1);
-        Assert.assertEquals("http://thathost/stuff#crap", URIUtils.rewriteURI(uri, target,
false).toString());
-    }
-
-    @Test
-    public void testRewite06() throws Exception {
-        URI uri = URI.create("http://thishost//////////////stuff/");
-        Assert.assertEquals("/stuff/", URIUtils.rewriteURI(uri, null).toString());
-    }
-
-    @Test
-    public void testResolve00() {
+    public void testResolve() {
         Assert.assertEquals("g:h", URIUtils.resolve(this.baseURI, "g:h").toString());
-    }
-
-    @Test
-    public void testResolve01() {
         Assert.assertEquals("http://a/b/c/g", URIUtils.resolve(this.baseURI, "g").toString());
-    }
-
-    @Test
-    public void testResolve02() {
         Assert.assertEquals("http://a/b/c/g", URIUtils.resolve(this.baseURI, "./g").toString());
-    }
-
-    @Test
-    public void testResolve03() {
         Assert.assertEquals("http://a/b/c/g/", URIUtils.resolve(this.baseURI, "g/").toString());
-    }
-
-    @Test
-    public void testResolve04() {
         Assert.assertEquals("http://a/g", URIUtils.resolve(this.baseURI, "/g").toString());
-    }
-
-    @Test
-    public void testResolve05() {
         Assert.assertEquals("http://g", URIUtils.resolve(this.baseURI, "//g").toString());
-    }
-
-    @Test
-    public void testResolve06() {
         Assert.assertEquals("http://a/b/c/d;p?y", URIUtils.resolve(this.baseURI, "?y").toString());
-    }
-
-    @Test
-    public void testResolve06_() {
         Assert.assertEquals("http://a/b/c/d;p?y#f", URIUtils.resolve(this.baseURI, "?y#f")
                 .toString());
-    }
-
-    @Test
-    public void testResolve07() {
         Assert.assertEquals("http://a/b/c/g?y", URIUtils.resolve(this.baseURI, "g?y").toString());
-    }
-
-    @Test
-    public void testResolve08() {
         Assert.assertEquals("http://a/b/c/d;p?q#s", URIUtils.resolve(this.baseURI, "#s")
                 .toString());
-    }
-
-    @Test
-    public void testResolve09() {
         Assert.assertEquals("http://a/b/c/g#s", URIUtils.resolve(this.baseURI, "g#s").toString());
-    }
-
-    @Test
-    public void testResolve10() {
         Assert.assertEquals("http://a/b/c/g?y#s", URIUtils.resolve(this.baseURI, "g?y#s")
                 .toString());
-    }
-
-    @Test
-    public void testResolve11() {
         Assert.assertEquals("http://a/b/c/;x", URIUtils.resolve(this.baseURI, ";x").toString());
-    }
-
-    @Test
-    public void testResolve12() {
         Assert.assertEquals("http://a/b/c/g;x", URIUtils.resolve(this.baseURI, "g;x").toString());
-    }
-
-    @Test
-    public void testResolve13() {
         Assert.assertEquals("http://a/b/c/g;x?y#s", URIUtils.resolve(this.baseURI, "g;x?y#s")
                 .toString());
-    }
-
-    @Test
-    public void testResolve14() {
         Assert.assertEquals("http://a/b/c/d;p?q", URIUtils.resolve(this.baseURI, "").toString());
-    }
-
-    @Test
-    public void testResolve15() {
         Assert.assertEquals("http://a/b/c/", URIUtils.resolve(this.baseURI, ".").toString());
-    }
-
-    @Test
-    public void testResolve16() {
         Assert.assertEquals("http://a/b/c/", URIUtils.resolve(this.baseURI, "./").toString());
-    }
-
-    @Test
-    public void testResolve17() {
         Assert.assertEquals("http://a/b/", URIUtils.resolve(this.baseURI, "..").toString());
-    }
-
-    @Test
-    public void testResolve18() {
         Assert.assertEquals("http://a/b/", URIUtils.resolve(this.baseURI, "../").toString());
-    }
-
-    @Test
-    public void testResolve19() {
         Assert.assertEquals("http://a/b/g", URIUtils.resolve(this.baseURI, "../g").toString());
-    }
-
-    @Test
-    public void testResolve20() {
         Assert.assertEquals("http://a/", URIUtils.resolve(this.baseURI, "../..").toString());
-    }
-
-    @Test
-    public void testResolve21() {
         Assert.assertEquals("http://a/", URIUtils.resolve(this.baseURI, "../../").toString());
-    }
-
-    @Test
-    public void testResolve22() {
         Assert.assertEquals("http://a/g", URIUtils.resolve(this.baseURI, "../../g").toString());
-    }
-
-    @Test
-    public void testResolveAbnormal23() {
         Assert.assertEquals("http://a/g", URIUtils.resolve(this.baseURI, "../../../g").toString());
-    }
-
-    @Test
-    public void testResolveAbnormal24() {
         Assert.assertEquals("http://a/g", URIUtils.resolve(this.baseURI, "../../../../g")
                 .toString());
-    }
-
-    @Test
-    public void testResolve25() {
         Assert.assertEquals("http://a/g", URIUtils.resolve(this.baseURI, "/./g").toString());
-    }
-
-    @Test
-    public void testResolve26() {
         Assert.assertEquals("http://a/g", URIUtils.resolve(this.baseURI, "/../g").toString());
-    }
-
-    @Test
-    public void testResolve27() {
         Assert.assertEquals("http://a/b/c/g.", URIUtils.resolve(this.baseURI, "g.").toString());
-    }
-
-    @Test
-    public void testResolve28() {
         Assert.assertEquals("http://a/b/c/.g", URIUtils.resolve(this.baseURI, ".g").toString());
-    }
-
-    @Test
-    public void testResolve29() {
         Assert.assertEquals("http://a/b/c/g..", URIUtils.resolve(this.baseURI, "g..").toString());
-    }
-
-    @Test
-    public void testResolve30() {
         Assert.assertEquals("http://a/b/c/..g", URIUtils.resolve(this.baseURI, "..g").toString());
-    }
-
-    @Test
-    public void testResolve31() {
         Assert.assertEquals("http://a/b/g", URIUtils.resolve(this.baseURI, "./../g").toString());
-    }
-
-    @Test
-    public void testResolve32() {
         Assert.assertEquals("http://a/b/c/g/", URIUtils.resolve(this.baseURI, "./g/.").toString());
-    }
-
-    @Test
-    public void testResolve33() {
         Assert.assertEquals("http://a/b/c/g/h", URIUtils.resolve(this.baseURI, "g/./h").toString());
-    }
-
-    @Test
-    public void testResolve34() {
         Assert.assertEquals("http://a/b/c/h", URIUtils.resolve(this.baseURI, "g/../h").toString());
-    }
-
-    @Test
-    public void testResolve35() {
         Assert.assertEquals("http://a/b/c/g;x=1/y", URIUtils.resolve(this.baseURI, "g;x=1/./y")
                 .toString());
-    }
-
-    @Test
-    public void testResolve36() {
         Assert.assertEquals("http://a/b/c/y", URIUtils.resolve(this.baseURI, "g;x=1/../y")
                 .toString());
-    }
-
-    @Test
-    public void testResolve37() {
         Assert.assertEquals("http://a/b/c/g?y/./x", URIUtils.resolve(this.baseURI, "g?y/./x")
                 .toString());
-    }
-
-    @Test
-    public void testResolve38() {
         Assert.assertEquals("http://a/b/c/g?y/../x", URIUtils.resolve(this.baseURI, "g?y/../x")
                 .toString());
-    }
-
-    @Test
-    public void testResolve39() {
         Assert.assertEquals("http://a/b/c/g#s/./x", URIUtils.resolve(this.baseURI, "g#s/./x")
                 .toString());
-    }
-
-    @Test
-    public void testResolve40() {
         Assert.assertEquals("http://a/b/c/g#s/../x", URIUtils.resolve(this.baseURI, "g#s/../x")
                 .toString());
-    }
-
-    @Test
-    public void testResolve41() {
         Assert.assertEquals("http:g", URIUtils.resolve(this.baseURI, "http:g").toString());
-    }
-
-    // examples from section 5.2.4
-    @Test
-    public void testResolve42() {
+        // examples from section 5.2.4
         Assert.assertEquals("http://s/a/g", URIUtils.resolve(this.baseURI,
                 "http://s/a/b/c/./../../g").toString());
-    }
-
-    @Test
-    public void testResolve43() {
         Assert.assertEquals("http://s/mid/6", URIUtils.resolve(this.baseURI,
                 "http://s/mid/content=5/../6").toString());
     }

Added: httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/client/TestDefaultRedirectStrategy.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/client/TestDefaultRedirectStrategy.java?rev=1311382&view=auto
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/client/TestDefaultRedirectStrategy.java
(added)
+++ httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/client/TestDefaultRedirectStrategy.java
Mon Apr  9 19:18:35 2012
@@ -0,0 +1,335 @@
+/*
+ * ====================================================================
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Apache Software Foundation.  For more
+ * information on the Apache Software Foundation, please see
+ * <http://www.apache.org/>.
+ */
+package org.apache.http.impl.client;
+
+import java.net.URI;
+import java.util.List;
+
+import org.apache.http.HttpHost;
+import org.apache.http.HttpResponse;
+import org.apache.http.HttpStatus;
+import org.apache.http.HttpVersion;
+import org.apache.http.ProtocolException;
+import org.apache.http.client.methods.HttpDelete;
+import org.apache.http.client.methods.HttpGet;
+import org.apache.http.client.methods.HttpHead;
+import org.apache.http.client.methods.HttpPost;
+import org.apache.http.client.methods.HttpPut;
+import org.apache.http.client.methods.HttpUriRequest;
+import org.apache.http.client.params.ClientPNames;
+import org.apache.http.message.BasicHttpResponse;
+import org.apache.http.protocol.BasicHttpContext;
+import org.apache.http.protocol.ExecutionContext;
+import org.apache.http.protocol.HttpContext;
+import org.junit.Assert;
+import org.junit.Test;
+
+public class TestDefaultRedirectStrategy {
+    
+    @Test
+    public void testIsRedirectable() {
+        DefaultRedirectStrategy redirectStrategy = new DefaultRedirectStrategy(); 
+        Assert.assertTrue(redirectStrategy.isRedirectable(HttpGet.METHOD_NAME));
+        Assert.assertTrue(redirectStrategy.isRedirectable(HttpHead.METHOD_NAME));
+        Assert.assertFalse(redirectStrategy.isRedirectable(HttpPut.METHOD_NAME));
+        Assert.assertFalse(redirectStrategy.isRedirectable(HttpPost.METHOD_NAME));
+        Assert.assertFalse(redirectStrategy.isRedirectable(HttpDelete.METHOD_NAME));
+    }
+
+    @Test
+    public void testIsRedirectedMovedTemporary() throws Exception {
+        DefaultRedirectStrategy redirectStrategy = new DefaultRedirectStrategy(); 
+        HttpContext context = new BasicHttpContext();
+        HttpGet httpget = new HttpGet("http://localhost/");
+        HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 
+                HttpStatus.SC_MOVED_TEMPORARILY, "Redirect");
+        response.addHeader("Location", "http://localhost/stuff");
+        Assert.assertTrue(redirectStrategy.isRedirected(httpget, response, context));
+        HttpPost httppost = new HttpPost("http://localhost/");
+        Assert.assertFalse(redirectStrategy.isRedirected(httppost, response, context));
+    }
+
+    @Test
+    public void testIsRedirectedMovedTemporaryNoLocation() throws Exception {
+        DefaultRedirectStrategy redirectStrategy = new DefaultRedirectStrategy(); 
+        HttpContext context = new BasicHttpContext();
+        HttpGet httpget = new HttpGet("http://localhost/");
+        HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 
+                HttpStatus.SC_MOVED_TEMPORARILY, "Redirect");
+        Assert.assertFalse(redirectStrategy.isRedirected(httpget, response, context));
+    }
+
+    @Test
+    public void testIsRedirectedMovedPermanently() throws Exception {
+        DefaultRedirectStrategy redirectStrategy = new DefaultRedirectStrategy(); 
+        HttpContext context = new BasicHttpContext();
+        HttpGet httpget = new HttpGet("http://localhost/");
+        HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 
+                HttpStatus.SC_MOVED_PERMANENTLY, "Redirect");
+        Assert.assertTrue(redirectStrategy.isRedirected(httpget, response, context));
+        HttpPost httppost = new HttpPost("http://localhost/");
+        Assert.assertFalse(redirectStrategy.isRedirected(httppost, response, context));
+    }
+
+    @Test
+    public void testIsRedirectedTemporaryRedirect() throws Exception {
+        DefaultRedirectStrategy redirectStrategy = new DefaultRedirectStrategy(); 
+        HttpContext context = new BasicHttpContext();
+        HttpGet httpget = new HttpGet("http://localhost/");
+        HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 
+                HttpStatus.SC_TEMPORARY_REDIRECT, "Redirect");
+        Assert.assertTrue(redirectStrategy.isRedirected(httpget, response, context));
+        HttpPost httppost = new HttpPost("http://localhost/");
+        Assert.assertFalse(redirectStrategy.isRedirected(httppost, response, context));
+    }
+
+    @Test
+    public void testIsRedirectedSeeOther() throws Exception {
+        DefaultRedirectStrategy redirectStrategy = new DefaultRedirectStrategy(); 
+        HttpContext context = new BasicHttpContext();
+        HttpGet httpget = new HttpGet("http://localhost/");
+        HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 
+                HttpStatus.SC_SEE_OTHER, "Redirect");
+        Assert.assertTrue(redirectStrategy.isRedirected(httpget, response, context));
+        HttpPost httppost = new HttpPost("http://localhost/");
+        Assert.assertTrue(redirectStrategy.isRedirected(httppost, response, context));
+    }
+
+    @Test
+    public void testIsRedirectedUnknownStatus() throws Exception {
+        DefaultRedirectStrategy redirectStrategy = new DefaultRedirectStrategy(); 
+        HttpContext context = new BasicHttpContext();
+        HttpGet httpget = new HttpGet("http://localhost/");
+        HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 333, "Redirect");
+        Assert.assertFalse(redirectStrategy.isRedirected(httpget, response, context));
+        HttpPost httppost = new HttpPost("http://localhost/");
+        Assert.assertFalse(redirectStrategy.isRedirected(httppost, response, context));
+    }
+
+    @Test
+    public void testIsRedirectedInvalidInput() throws Exception {
+        DefaultRedirectStrategy redirectStrategy = new DefaultRedirectStrategy(); 
+        HttpContext context = new BasicHttpContext();
+        HttpGet httpget = new HttpGet("http://localhost/");
+        HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 
+                HttpStatus.SC_SEE_OTHER, "Redirect");
+        try {
+            redirectStrategy.isRedirected(null, response, context);
+            Assert.fail("IllegalArgumentException expected");
+        } catch (IllegalArgumentException expected) {
+        }
+        try {
+            redirectStrategy.isRedirected(httpget, null, context);
+            Assert.fail("IllegalArgumentException expected");
+        } catch (IllegalArgumentException expected) {
+        }
+    }
+
+    @Test
+    public void testGetLocationUri() throws Exception {
+        DefaultRedirectStrategy redirectStrategy = new DefaultRedirectStrategy(); 
+        HttpContext context = new BasicHttpContext();
+        HttpGet httpget = new HttpGet("http://localhost/");
+        HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 
+                HttpStatus.SC_MOVED_TEMPORARILY, "Redirect");
+        response.addHeader("Location", "http://localhost/stuff");
+        URI uri = redirectStrategy.getLocationURI(httpget, response, context);
+        Assert.assertEquals(URI.create("http://localhost/stuff"), uri);
+    }
+
+    @Test(expected=ProtocolException.class)
+    public void testGetLocationUriMissingHeader() throws Exception {
+        DefaultRedirectStrategy redirectStrategy = new DefaultRedirectStrategy(); 
+        HttpContext context = new BasicHttpContext();
+        HttpGet httpget = new HttpGet("http://localhost/");
+        HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 
+                HttpStatus.SC_MOVED_TEMPORARILY, "Redirect");
+        redirectStrategy.getLocationURI(httpget, response, context);
+    }
+
+    @Test(expected=ProtocolException.class)
+    public void testGetLocationUriInvalidLocation() throws Exception {
+        DefaultRedirectStrategy redirectStrategy = new DefaultRedirectStrategy(); 
+        HttpContext context = new BasicHttpContext();
+        HttpGet httpget = new HttpGet("http://localhost/");
+        HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 
+                HttpStatus.SC_MOVED_TEMPORARILY, "Redirect");
+        response.addHeader("Location", "http://localhost/not valid");
+        redirectStrategy.getLocationURI(httpget, response, context);
+    }
+
+    @Test
+    public void testGetLocationUriRelative() throws Exception {
+        DefaultRedirectStrategy redirectStrategy = new DefaultRedirectStrategy(); 
+        HttpContext context = new BasicHttpContext();
+        context.setAttribute(ExecutionContext.HTTP_TARGET_HOST, new HttpHost("localhost"));
+        HttpGet httpget = new HttpGet("http://localhost/");
+        HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 
+                HttpStatus.SC_MOVED_TEMPORARILY, "Redirect");
+        response.addHeader("Location", "/stuff");
+        URI uri = redirectStrategy.getLocationURI(httpget, response, context);
+        Assert.assertEquals(URI.create("http://localhost/stuff"), uri);
+    }
+
+    @Test(expected=IllegalStateException.class)
+    public void testGetLocationUriRelativeMissingTargetHost() throws Exception {
+        DefaultRedirectStrategy redirectStrategy = new DefaultRedirectStrategy(); 
+        HttpContext context = new BasicHttpContext();
+        HttpGet httpget = new HttpGet("http://localhost/");
+        HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 
+                HttpStatus.SC_MOVED_TEMPORARILY, "Redirect");
+        response.addHeader("Location", "/stuff");
+        URI uri = redirectStrategy.getLocationURI(httpget, response, context);
+        Assert.assertEquals(URI.create("http://localhost/stuff"), uri);
+    }
+
+    @Test
+    public void testGetLocationUriRelativeWithFragment() throws Exception {
+        DefaultRedirectStrategy redirectStrategy = new DefaultRedirectStrategy(); 
+        HttpContext context = new BasicHttpContext();
+        context.setAttribute(ExecutionContext.HTTP_TARGET_HOST, new HttpHost("localhost"));
+        HttpGet httpget = new HttpGet("http://localhost/");
+        HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 
+                HttpStatus.SC_MOVED_TEMPORARILY, "Redirect");
+        response.addHeader("Location", "/stuff#fragment");
+        URI uri = redirectStrategy.getLocationURI(httpget, response, context);
+        Assert.assertEquals(URI.create("http://localhost/stuff"), uri);
+    }
+
+    @Test
+    public void testGetLocationUriAbsoluteWithFragment() throws Exception {
+        DefaultRedirectStrategy redirectStrategy = new DefaultRedirectStrategy(); 
+        HttpContext context = new BasicHttpContext();
+        context.setAttribute(ExecutionContext.HTTP_TARGET_HOST, new HttpHost("localhost"));
+        HttpGet httpget = new HttpGet("http://localhost/");
+        HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 
+                HttpStatus.SC_MOVED_TEMPORARILY, "Redirect");
+        response.addHeader("Location", "http://localhost/stuff#fragment");
+        URI uri = redirectStrategy.getLocationURI(httpget, response, context);
+        Assert.assertEquals(URI.create("http://localhost/stuff"), uri);
+    }
+
+    @Test(expected=ProtocolException.class)
+    public void testGetLocationUriRelativeLocationNotAllowed() throws Exception {
+        DefaultRedirectStrategy redirectStrategy = new DefaultRedirectStrategy(); 
+        HttpContext context = new BasicHttpContext();
+        context.setAttribute(ExecutionContext.HTTP_TARGET_HOST, new HttpHost("localhost"));
+        HttpGet httpget = new HttpGet("http://localhost/");
+        httpget.getParams().setParameter(ClientPNames.REJECT_RELATIVE_REDIRECT, Boolean.TRUE);
+        HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 
+                HttpStatus.SC_MOVED_TEMPORARILY, "Redirect");
+        response.addHeader("Location", "/stuff");
+        redirectStrategy.getLocationURI(httpget, response, context);
+    }
+
+    @Test
+    public void testGetLocationUriAllowCircularRedirects() throws Exception {
+        DefaultRedirectStrategy redirectStrategy = new DefaultRedirectStrategy(); 
+        HttpContext context = new BasicHttpContext();
+        context.setAttribute(ExecutionContext.HTTP_TARGET_HOST, new HttpHost("localhost"));
+        HttpGet httpget = new HttpGet("http://localhost/");
+        httpget.getParams().setParameter(ClientPNames.ALLOW_CIRCULAR_REDIRECTS, Boolean.TRUE);
+        HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 
+                HttpStatus.SC_MOVED_TEMPORARILY, "Redirect");
+        response.addHeader("Location", "http://localhost/stuff");
+        URI uri = URI.create("http://localhost/stuff");
+        Assert.assertEquals(uri, redirectStrategy.getLocationURI(httpget, response, context));
+        Assert.assertEquals(uri, redirectStrategy.getLocationURI(httpget, response, context));
+        Assert.assertEquals(uri, redirectStrategy.getLocationURI(httpget, response, context));
+        
+        RedirectLocations redirectLocations = (RedirectLocations) context.getAttribute(
+                DefaultRedirectStrategy.REDIRECT_LOCATIONS);
+        Assert.assertNotNull(redirectLocations);
+        Assert.assertTrue(redirectLocations.contains(uri));
+        List<URI> uris = redirectLocations.getAll();
+        Assert.assertNotNull(uris);
+        Assert.assertEquals(3, uris.size());
+        Assert.assertEquals(uri, uris.get(0));
+        Assert.assertEquals(uri, uris.get(1));
+        Assert.assertEquals(uri, uris.get(2));
+    }
+
+    @Test(expected=ProtocolException.class)
+    public void testGetLocationUriDisallowCircularRedirects() throws Exception {
+        DefaultRedirectStrategy redirectStrategy = new DefaultRedirectStrategy(); 
+        HttpContext context = new BasicHttpContext();
+        context.setAttribute(ExecutionContext.HTTP_TARGET_HOST, new HttpHost("localhost"));
+        HttpGet httpget = new HttpGet("http://localhost/");
+        httpget.getParams().setParameter(ClientPNames.ALLOW_CIRCULAR_REDIRECTS, Boolean.FALSE);
+        HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 
+                HttpStatus.SC_MOVED_TEMPORARILY, "Redirect");
+        response.addHeader("Location", "http://localhost/stuff");
+        URI uri = URI.create("http://localhost/stuff");
+        Assert.assertEquals(uri, redirectStrategy.getLocationURI(httpget, response, context));
+        redirectStrategy.getLocationURI(httpget, response, context);
+    }
+
+    @Test
+    public void testGetLocationUriInvalidInput() throws Exception {
+        DefaultRedirectStrategy redirectStrategy = new DefaultRedirectStrategy(); 
+        HttpContext context = new BasicHttpContext();
+        HttpGet httpget = new HttpGet("http://localhost/");
+        HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 
+                HttpStatus.SC_MOVED_TEMPORARILY, "Redirect");
+        response.addHeader("Location", "http://localhost/stuff");
+        try {
+            redirectStrategy.getLocationURI(null, response, context);
+            Assert.fail("IllegalArgumentException expected");
+        } catch (IllegalArgumentException expected) {
+        }
+        try {
+            redirectStrategy.getLocationURI(httpget, null, context);
+            Assert.fail("IllegalArgumentException expected");
+        } catch (IllegalArgumentException expected) {
+        }
+        try {
+            redirectStrategy.getLocationURI(httpget, response, null);
+            Assert.fail("IllegalArgumentException expected");
+        } catch (IllegalArgumentException expected) {
+        }
+    }
+
+    @Test
+    public void testGetRedirectRequest() throws Exception {
+        DefaultRedirectStrategy redirectStrategy = new DefaultRedirectStrategy(); 
+        HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 
+                HttpStatus.SC_SEE_OTHER, "Redirect");
+        response.addHeader("Location", "http://localhost/stuff");
+        HttpContext context1 = new BasicHttpContext();
+        HttpUriRequest redirect1 = redirectStrategy.getRedirect(
+                new HttpGet("http://localhost/"), response, context1);
+        Assert.assertEquals("GET", redirect1.getMethod());
+        HttpContext context2 = new BasicHttpContext();
+        HttpUriRequest redirect2 = redirectStrategy.getRedirect(
+                new HttpPost("http://localhost/"), response, context2);
+        Assert.assertEquals("GET", redirect2.getMethod());
+        HttpContext context3 = new BasicHttpContext();
+        HttpUriRequest redirect3 = redirectStrategy.getRedirect(
+                new HttpHead("http://localhost/"), response, context3);
+        Assert.assertEquals("HEAD", redirect3.getMethod());
+    }
+
+}

Propchange: httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/client/TestDefaultRedirectStrategy.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/client/TestDefaultRedirectStrategy.java
------------------------------------------------------------------------------
    svn:keywords = Date Revision

Propchange: httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/client/TestDefaultRedirectStrategy.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain



Mime
View raw message