incubator-sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bdelacre...@apache.org
Subject svn commit: r922264 - in /sling/trunk/bundles/commons/testing/src/main/java/org/apache/sling/commons/testing/integration: HttpStatusCodeException.java HttpTestBase.java SlingIntegrationTestClient.java
Date Fri, 12 Mar 2010 14:08:53 GMT
Author: bdelacretaz
Date: Fri Mar 12 14:08:52 2010
New Revision: 922264

URL: http://svn.apache.org/viewvc?rev=922264&view=rev
Log:
SlingIntegrationTestClient now includes content when throwing HttpStatusCodeException, to
make troubleshooting easier

Modified:
    sling/trunk/bundles/commons/testing/src/main/java/org/apache/sling/commons/testing/integration/HttpStatusCodeException.java
    sling/trunk/bundles/commons/testing/src/main/java/org/apache/sling/commons/testing/integration/HttpTestBase.java
    sling/trunk/bundles/commons/testing/src/main/java/org/apache/sling/commons/testing/integration/SlingIntegrationTestClient.java

Modified: sling/trunk/bundles/commons/testing/src/main/java/org/apache/sling/commons/testing/integration/HttpStatusCodeException.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/commons/testing/src/main/java/org/apache/sling/commons/testing/integration/HttpStatusCodeException.java?rev=922264&r1=922263&r2=922264&view=diff
==============================================================================
--- sling/trunk/bundles/commons/testing/src/main/java/org/apache/sling/commons/testing/integration/HttpStatusCodeException.java
(original)
+++ sling/trunk/bundles/commons/testing/src/main/java/org/apache/sling/commons/testing/integration/HttpStatusCodeException.java
Fri Mar 12 14:08:52 2010
@@ -29,10 +29,15 @@ public class HttpStatusCodeException ext
     
     public HttpStatusCodeException(int expectedStatus, int actualStatus,
             String method, String url) {
+        this(expectedStatus, actualStatus, method, url, null);
+    }
 
+    public HttpStatusCodeException(int expectedStatus, int actualStatus,
+            String method, String url, String content) {
         super("Expected status code " + expectedStatus + " for " + method
-            + ", got " + actualStatus + ", URL=" + url);
-
+                + ", got " + actualStatus + ", URL=" + url 
+                + (content != null ? ", Content=[" + content + "]" : "")
+                );
         this.expectedStatus = expectedStatus;
         this.actualStatus = actualStatus;
     }

Modified: sling/trunk/bundles/commons/testing/src/main/java/org/apache/sling/commons/testing/integration/HttpTestBase.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/commons/testing/src/main/java/org/apache/sling/commons/testing/integration/HttpTestBase.java?rev=922264&r1=922263&r2=922264&view=diff
==============================================================================
--- sling/trunk/bundles/commons/testing/src/main/java/org/apache/sling/commons/testing/integration/HttpTestBase.java
(original)
+++ sling/trunk/bundles/commons/testing/src/main/java/org/apache/sling/commons/testing/integration/HttpTestBase.java
Fri Mar 12 14:08:52 2010
@@ -36,6 +36,7 @@ import org.apache.commons.httpclient.Hea
 import org.apache.commons.httpclient.HttpClient;
 import org.apache.commons.httpclient.HttpException;
 import org.apache.commons.httpclient.HttpMethod;
+import org.apache.commons.httpclient.HttpMethodBase;
 import org.apache.commons.httpclient.NameValuePair;
 import org.apache.commons.httpclient.UsernamePasswordCredentials;
 import org.apache.commons.httpclient.auth.AuthScope;
@@ -310,14 +311,7 @@ public class HttpTestBase extends TestCa
             get.setQueryString(params.toArray(nvp));
         }
         final int status = httpClient.executeMethod(get);
-        final InputStream is = get.getResponseBodyAsStream();
-        final StringBuffer content = new StringBuffer();
-        final String charset = get.getResponseCharSet();
-        final byte [] buffer = new byte[16384];
-        int n = 0;
-        while( (n = is.read(buffer, 0, buffer.length)) > 0) {
-            content.append(new String(buffer, 0, n, charset));
-        }
+        final String content = getResponseBodyAsStream(get, 0);
         assertEquals("Expected status " + expectedStatusCode + " for " + url + " (content="
+ content + ")",
                 expectedStatusCode,status);
         final Header h = get.getResponseHeader("Content-Type");
@@ -432,4 +426,22 @@ public class HttpTestBase extends TestCa
             );
         }
     }
+
+    /** Return m's response body as a string, optionally limiting the length that we read
+     * @param maxLength if 0, no limit
+     */
+    public static String getResponseBodyAsStream(HttpMethodBase m, int maxLength) throws
IOException {
+        final InputStream is = m.getResponseBodyAsStream();
+        final StringBuilder content = new StringBuilder();
+        final String charset = m.getResponseCharSet();
+        final byte [] buffer = new byte[16384];
+        int n = 0;
+        while( (n = is.read(buffer, 0, buffer.length)) > 0) {
+            content.append(new String(buffer, 0, n, charset));
+            if(maxLength != 0 && content.length() > maxLength) {
+                throw new IllegalArgumentException("Response body size is over maxLength
(" + maxLength + ")");
+            }
+        }
+        return content.toString();
+    }
 }

Modified: sling/trunk/bundles/commons/testing/src/main/java/org/apache/sling/commons/testing/integration/SlingIntegrationTestClient.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/commons/testing/src/main/java/org/apache/sling/commons/testing/integration/SlingIntegrationTestClient.java?rev=922264&r1=922263&r2=922264&view=diff
==============================================================================
--- sling/trunk/bundles/commons/testing/src/main/java/org/apache/sling/commons/testing/integration/SlingIntegrationTestClient.java
(original)
+++ sling/trunk/bundles/commons/testing/src/main/java/org/apache/sling/commons/testing/integration/SlingIntegrationTestClient.java
Fri Mar 12 14:08:52 2010
@@ -20,7 +20,6 @@ import java.io.File;
 import java.io.IOException;
 import java.io.InputStream;
 import java.util.ArrayList;
-import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 
@@ -161,9 +160,10 @@ public class SlingIntegrationTestClient 
             }
         }
 
+        final int expected = 302;
         final int status = httpClient.executeMethod(post);
-        if(status!=302) {
-            throw new HttpStatusCodeException(302, status, "POST", url);
+        if(status!=expected) {
+            throw new HttpStatusCodeException(expected, status, "POST", url, HttpTestBase.getResponseBodyAsStream(post,
0));
         }
         String location = post.getResponseHeader("Location").getValue();
         post.releaseConnection();
@@ -193,8 +193,9 @@ public class SlingIntegrationTestClient 
         post.setRequestEntity(new MultipartRequestEntity(parts, post.getParams()));
 
         final int status = httpClient.executeMethod(post);
-        if(status!=200) { // fmeschbe: The default sling status is 200, not 302
-            throw new HttpStatusCodeException(200, status, "POST", url);
+        final int expected = 200;
+        if(status!=expected) {
+            throw new HttpStatusCodeException(expected, status, "POST", HttpTestBase.getResponseBodyAsStream(post,
0));
         }
     }
 
@@ -217,9 +218,10 @@ public class SlingIntegrationTestClient 
         post.setFollowRedirects(false);
         post.setRequestEntity(new MultipartRequestEntity(parts, post.getParams()));
 
+        final int expected = 200;
         final int status = httpClient.executeMethod(post);
-        if(status!=200) { // fmeschbe: The default sling status is 200, not 302
-            throw new HttpStatusCodeException(200, status, "POST", url);
+        if(status!=expected) {
+            throw new HttpStatusCodeException(expected, status, "POST", HttpTestBase.getResponseBodyAsStream(post,
0));
         }
     }
 }



Mime
View raw message