You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@knox.apache.org by km...@apache.org on 2013/06/19 21:28:59 UTC

git commit: Add form param support to MockServer. Enhance Templeton test to check form param.

Updated Branches:
  refs/heads/master 3a6737681 -> ab97f9a04


Add form param support to MockServer.  Enhance Templeton test to check form param.


Project: http://git-wip-us.apache.org/repos/asf/incubator-knox/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-knox/commit/ab97f9a0
Tree: http://git-wip-us.apache.org/repos/asf/incubator-knox/tree/ab97f9a0
Diff: http://git-wip-us.apache.org/repos/asf/incubator-knox/diff/ab97f9a0

Branch: refs/heads/master
Commit: ab97f9a04174382e2f12a5763d6b6dc2faac572a
Parents: 3a67376
Author: Kevin Minder <ke...@hortonworks.com>
Authored: Wed Jun 19 15:28:54 2013 -0400
Committer: Kevin Minder <ke...@hortonworks.com>
Committed: Wed Jun 19 15:28:54 2013 -0400

----------------------------------------------------------------------
 gateway-test-utils/pom.xml                      |  5 ++
 .../hadoop/test/mock/MockRequestMatcher.java    | 72 +++++++++++++++-----
 .../hadoop/gateway/GatewayFuncTestDriver.java   |  4 ++
 3 files changed, 65 insertions(+), 16 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-knox/blob/ab97f9a0/gateway-test-utils/pom.xml
----------------------------------------------------------------------
diff --git a/gateway-test-utils/pom.xml b/gateway-test-utils/pom.xml
index 392a7a8..d2ccd19 100644
--- a/gateway-test-utils/pom.xml
+++ b/gateway-test-utils/pom.xml
@@ -89,6 +89,11 @@
             <scope>provided</scope>
         </dependency>
 
+        <dependency>
+            <groupId>org.apache.commons</groupId>
+            <artifactId>commons-lang3</artifactId>
+        </dependency>
+
     </dependencies>
 
 </project>
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/incubator-knox/blob/ab97f9a0/gateway-test-utils/src/main/java/org/apache/hadoop/test/mock/MockRequestMatcher.java
----------------------------------------------------------------------
diff --git a/gateway-test-utils/src/main/java/org/apache/hadoop/test/mock/MockRequestMatcher.java b/gateway-test-utils/src/main/java/org/apache/hadoop/test/mock/MockRequestMatcher.java
index f456b7d..2206dd4 100644
--- a/gateway-test-utils/src/main/java/org/apache/hadoop/test/mock/MockRequestMatcher.java
+++ b/gateway-test-utils/src/main/java/org/apache/hadoop/test/mock/MockRequestMatcher.java
@@ -18,6 +18,7 @@
 package org.apache.hadoop.test.mock;
 
 import org.apache.commons.io.IOUtils;
+import org.apache.commons.lang3.ArrayUtils;
 
 import javax.servlet.http.Cookie;
 import javax.servlet.http.HttpServletRequest;
@@ -51,6 +52,7 @@ public class MockRequestMatcher {
   private String characterEncoding = null;
   private Integer contentLength = null;
   private byte[] entity = null;
+  private Map<String,String[]> formParams = null;
 
   public MockRequestMatcher( MockResponseProvider response ) {
     this.response = response;
@@ -114,6 +116,23 @@ public class MockRequestMatcher {
     return this;
   }
 
+  public MockRequestMatcher formParam( String name, String... values ) {
+    if( entity != null ) {
+      throw new IllegalStateException( "Entity already specified." );
+    }
+    if( formParams == null ) {
+      formParams = new HashMap<String, String[]>();
+    }
+    String[] currentValues = formParams.get( name );
+    if( currentValues == null ) {
+      currentValues = values;
+    } else if ( values != null ) {
+      currentValues = ArrayUtils.addAll( currentValues, values );
+    }
+    formParams.put( name, currentValues );
+    return this;
+  }
+
   public MockRequestMatcher content( String string, Charset charset ) {
     characterEncoding( charset.name() );
     content( string.getBytes( charset ) );
@@ -121,6 +140,9 @@ public class MockRequestMatcher {
   }
 
   public MockRequestMatcher content( byte[] entity ) {
+    if( formParams != null ) {
+      throw new IllegalStateException( "Form params already specified." );
+    }
     this.entity = entity;
     return this;
   }
@@ -205,22 +227,6 @@ public class MockRequestMatcher {
               " does not have the required content length",
           request.getContentLength(), is( contentLength ) );
     }
-    if( entity != null ) {
-      if( characterEncoding == null || request.getCharacterEncoding() == null ) {
-        byte[] bytes = IOUtils.toByteArray( request.getInputStream() );
-        assertThat(
-            "Request " + request.getMethod() + " " + request.getRequestURL() +
-                " content does not match the required content",
-            bytes, is( entity ) );
-      } else {
-        String expect = new String( entity, characterEncoding );
-        String actual = IOUtils.toString( request.getInputStream(), request.getCharacterEncoding() );
-        assertThat(
-            "Request " + request.getMethod() + " " + request.getRequestURL() +
-                " content does not match the required content",
-            actual, is( expect ) );
-      }
-    }
     if( attributes != null ) {
       for( String name: attributes.keySet() ) {
         assertThat(
@@ -250,6 +256,40 @@ public class MockRequestMatcher {
             Arrays.asList( values ), hasItem( queryParams.get( name ) ) );
       }
     }
+    if( formParams != null ) {
+      String paramString = IOUtils.toString( request.getInputStream(), request.getCharacterEncoding() );
+      Map<String,String[]> requestParams = parseQueryString( paramString == null ? "" : paramString );
+      for( String name: formParams.keySet() ) {
+        String[] actualValues = requestParams.get( name );
+        assertThat(
+            "Request " + request.getMethod() + " " + request.getRequestURL() +
+                " form params " + paramString + " is missing parameter '" + name + "'",
+            actualValues, notNullValue() );
+        String[] expectedValues = formParams.get( name );
+        for( String expectedValue : expectedValues ) {
+          assertThat(
+              "Request " + request.getMethod() + " " + request.getRequestURL() +
+                  " form params " + paramString + " is missing a value " + expectedValue + " for parameter '" + name + "'",
+              Arrays.asList( actualValues ), hasItem( expectedValue ) );
+        }
+      }
+    }
+    if( entity != null ) {
+      if( characterEncoding == null || request.getCharacterEncoding() == null ) {
+        byte[] bytes = IOUtils.toByteArray( request.getInputStream() );
+        assertThat(
+            "Request " + request.getMethod() + " " + request.getRequestURL() +
+                " content does not match the required content",
+            bytes, is( entity ) );
+      } else {
+        String expect = new String( entity, characterEncoding );
+        String actual = IOUtils.toString( request.getInputStream(), request.getCharacterEncoding() );
+        assertThat(
+            "Request " + request.getMethod() + " " + request.getRequestURL() +
+                " content does not match the required content",
+            actual, is( expect ) );
+      }
+    }
   }
 
   // Separate method to minimally scope the depreciation suppression.

http://git-wip-us.apache.org/repos/asf/incubator-knox/blob/ab97f9a0/gateway-test/src/test/java/org/apache/hadoop/gateway/GatewayFuncTestDriver.java
----------------------------------------------------------------------
diff --git a/gateway-test/src/test/java/org/apache/hadoop/gateway/GatewayFuncTestDriver.java b/gateway-test/src/test/java/org/apache/hadoop/gateway/GatewayFuncTestDriver.java
index 2987078..209dd17 100644
--- a/gateway-test/src/test/java/org/apache/hadoop/gateway/GatewayFuncTestDriver.java
+++ b/gateway-test/src/test/java/org/apache/hadoop/gateway/GatewayFuncTestDriver.java
@@ -618,6 +618,10 @@ public class GatewayFuncTestDriver {
         .expect()
         .method( "POST" )
         .pathInfo( "/mapreduce/jar" )
+        .formParam( "user.name", user )
+        .formParam( "jar", jar )
+        .formParam( "class", main )
+        .formParam( "arg", input, output )
         .respond()
         .status( status )
         .contentType( "application/json" )