[jboss-remoting-commits] JBoss Remoting SVN: r5412 - remoting2/branches/2.x/src/tests/org/jboss/test/remoting/transport/http/contenttype.

jboss-remoting-commits at lists.jboss.org jboss-remoting-commits at lists.jboss.org
Mon Aug 31 22:54:01 EDT 2009


Author: ron.sigal at jboss.com
Date: 2009-08-31 22:54:00 -0400 (Mon, 31 Aug 2009)
New Revision: 5412

Modified:
   remoting2/branches/2.x/src/tests/org/jboss/test/remoting/transport/http/contenttype/ContentTypeTestCase.java
Log:
JBREM-1101: Added new test methods.

Modified: remoting2/branches/2.x/src/tests/org/jboss/test/remoting/transport/http/contenttype/ContentTypeTestCase.java
===================================================================
--- remoting2/branches/2.x/src/tests/org/jboss/test/remoting/transport/http/contenttype/ContentTypeTestCase.java	2009-09-01 02:52:51 UTC (rev 5411)
+++ remoting2/branches/2.x/src/tests/org/jboss/test/remoting/transport/http/contenttype/ContentTypeTestCase.java	2009-09-01 02:54:00 UTC (rev 5412)
@@ -28,6 +28,7 @@
 import java.net.HttpURLConnection;
 import java.net.InetAddress;
 import java.net.URL;
+import java.util.HashMap;
 import java.util.Map;
 
 import javax.management.MBeanServer;
@@ -42,15 +43,14 @@
 import org.jboss.remoting.InvokerLocator;
 import org.jboss.remoting.ServerInvocationHandler;
 import org.jboss.remoting.ServerInvoker;
-import org.jboss.remoting.callback.Callback;
-import org.jboss.remoting.callback.HandleCallbackException;
 import org.jboss.remoting.callback.InvokerCallbackHandler;
 import org.jboss.remoting.transport.Connector;
 import org.jboss.remoting.transport.PortUtil;
+import org.jboss.remoting.transport.web.WebUtil;
 
 
 /**
- * Unit test for JBREM-653.
+ * Unit test for JBREM-653 and JBREM-1101.
  * 
  * @author <a href="ron.sigal at jboss.com">Ron Sigal</a>
  * @version $Revision$
@@ -64,13 +64,17 @@
    
    private static boolean firstTime = true;
    private static String CONTENT_TYPE = "test/testContentType";
+   private static String INVALID_CONTENT_TYPE_CR = "test/x" + '\r' + "y";
+   private static String INVALID_CONTENT_TYPE_LF = "test/x" + '\n' + "y";
    private static String REQUEST = "testRequest";
    private static String RESPONSE = "testResponse";
    
-   // remoting server connector
-   private Connector connector;
-   private InvokerLocator serverLocator;
-   private TestInvocationHandler invocationHandler;
+   protected String host;
+   protected int port;
+   protected String locatorURI;
+   protected InvokerLocator serverLocator;
+   protected Connector connector;
+   protected TestInvocationHandler invocationHandler;
 
    
    public void setUp() throws Exception
@@ -101,16 +105,7 @@
       log.info("entering " + getName());
       
       // Start server.
-      String host = InetAddress.getLocalHost().getHostAddress();
-      int port = PortUtil.findFreePort(host);
-      String locatorURI = "http://" + host + ":" + port; 
-      serverLocator = new InvokerLocator(locatorURI);
-      log.info("Starting remoting server with locator uri of: " + locatorURI);
-      connector = new Connector(serverLocator);
-      connector.create();
-      invocationHandler = new TestInvocationHandler();
-      connector.addInvocationHandler("sample", invocationHandler);
-      connector.start();
+      setupServer(CONTENT_TYPE);
       
       // Send a message and receive a response using an HttpURLConnection.
       URL url = new URL(locatorURI);
@@ -135,27 +130,129 @@
       log.info(getName() + " PASSES");
    }
    
+   
+   /**
+    * Verifies that content-type with CR supplied by ServerInvocationHandler is discarded.
+    */
+   public void testInvalidContentTypeServerCR() throws Throwable
+   {
+      log.info("entering " + getName());
 
+      // Start server.
+      setupServer(INVALID_CONTENT_TYPE_CR);
+
+      // Send a message and receive a response using an HttpURLConnection.
+      URL url = new URL(locatorURI);
+      HttpURLConnection conn = (HttpURLConnection) url.openConnection();
+      conn.setDoOutput(true);
+      conn.setDoInput(true);
+      byte[] requestBytes = REQUEST.getBytes();
+      OutputStream os = conn.getOutputStream();
+      os.write(requestBytes);
+      String contentType = conn.getContentType();
+      log.info("content-type: " + contentType);
+      InputStream is = conn.getInputStream();
+      BufferedReader reader = new BufferedReader(new InputStreamReader(is));
+      String response = reader.readLine();
+      log.info("response: " + response);
+
+      // Verify that content-type is the default value.
+      log.info("content-type: " + contentType);
+      assertEquals(WebUtil.HTML, contentType);
+      assertEquals(RESPONSE, response);
+
+      connector.stop();
+      log.info(getName() + " PASSES");
+   }
+
+
+   /**
+    * Verifies that content-type with LF supplied by ServerInvocationHandler is discarded.
+    */
+   public void testInvalidContentTypeServerLF() throws Throwable
+   {
+      log.info("entering " + getName());
+
+      // Start server.
+      setupServer(INVALID_CONTENT_TYPE_LF);
+
+      // Send a message and receive a response using an HttpURLConnection.
+      URL url = new URL(locatorURI);
+      HttpURLConnection conn = (HttpURLConnection) url.openConnection();
+      conn.setDoOutput(true);
+      conn.setDoInput(true);
+      byte[] requestBytes = REQUEST.getBytes();
+      OutputStream os = conn.getOutputStream();
+      os.write(requestBytes);
+      String contentType = conn.getContentType();
+      log.info("content-type: " + contentType);
+      InputStream is = conn.getInputStream();
+      BufferedReader reader = new BufferedReader(new InputStreamReader(is));
+      String response = reader.readLine();
+      log.info("response: " + response);
+
+      // Verify that content-type is the default value.
+      log.info("content-type: " + contentType);
+      assertEquals(WebUtil.HTML, contentType);
+      assertEquals(RESPONSE, response);
+
+      connector.stop();
+      log.info(getName() + " PASSES");
+   }
+   
+   
+   protected String getTransport()
+   {
+      return "http";
+   }
+   
+   
+   protected void addExtraClientConfig(Map config) {}
+   protected void addExtraServerConfig(Map config) {}
+   
+   
+   protected void setupServer(String contentType) throws Exception
+   {
+      host = InetAddress.getLocalHost().getHostAddress();
+      port = PortUtil.findFreePort(host);
+      locatorURI = getTransport() + "://" + host + ":" + port;
+      String metadata = System.getProperty("remoting.metadata");
+      if (metadata != null)
+      {
+         locatorURI += "/?" + metadata;
+      }
+      serverLocator = new InvokerLocator(locatorURI);
+      log.info("Starting remoting server with locator uri of: " + locatorURI);
+      HashMap config = new HashMap();
+      config.put(InvokerLocator.FORCE_REMOTE, "true");
+      addExtraServerConfig(config);
+      connector = new Connector(serverLocator, config);
+      connector.create();
+      invocationHandler = new TestInvocationHandler(contentType);
+      connector.addInvocationHandler("test", invocationHandler);
+      connector.start();
+   }
+   
+   
    static class TestInvocationHandler implements ServerInvocationHandler
    {
+      String contentType;
+      public TestInvocationHandler(String contentType)
+      {
+         this.contentType = contentType;
+      }
       public void addListener(InvokerCallbackHandler callbackHandler) {}
       public Object invoke(final InvocationRequest invocation) throws Throwable
       {
          Map response = invocation.getReturnPayload();
-         response.put("Content-Type", CONTENT_TYPE);
+         if (response != null)
+         {
+            response.put("Content-Type", contentType);
+         }
          return RESPONSE;
       }
       public void removeListener(InvokerCallbackHandler callbackHandler) {}
       public void setMBeanServer(MBeanServer server) {}
       public void setInvoker(ServerInvoker invoker) {}
    }
-   
-   
-   static class TestCallbackHandler implements InvokerCallbackHandler
-   {
-      public void handleCallback(Callback callback) throws HandleCallbackException
-      {
-         log.info("received callback");
-      }  
-   }
 }
\ No newline at end of file



More information about the jboss-remoting-commits mailing list