felix-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From kgil...@apache.org
Subject svn commit: r1225141 - in /felix/trunk/httplite/minimum: pom.xml src/main/java/org/apache/felix/httplite/servlet/HttpServletResponseImpl.java src/test/java/org/apache/felix/httplite/osgi/test/cases/TestCookies.java
Date Wed, 28 Dec 2011 08:16:39 GMT
Author: kgilmer
Date: Wed Dec 28 08:16:38 2011
New Revision: 1225141

URL: http://svn.apache.org/viewvc?rev=1225141&view=rev
Log:
httplite: added test case for cookie support

Added:
    felix/trunk/httplite/minimum/src/test/java/org/apache/felix/httplite/osgi/test/cases/TestCookies.java
Modified:
    felix/trunk/httplite/minimum/pom.xml
    felix/trunk/httplite/minimum/src/main/java/org/apache/felix/httplite/servlet/HttpServletResponseImpl.java

Modified: felix/trunk/httplite/minimum/pom.xml
URL: http://svn.apache.org/viewvc/felix/trunk/httplite/minimum/pom.xml?rev=1225141&r1=1225140&r2=1225141&view=diff
==============================================================================
--- felix/trunk/httplite/minimum/pom.xml (original)
+++ felix/trunk/httplite/minimum/pom.xml Wed Dec 28 08:16:38 2011
@@ -78,7 +78,7 @@
 				</includes>
 			</resource>
 		</resources>
-		<plugins>
+		<plugins>			
 			<plugin>
 				<groupId>org.apache.maven.plugins</groupId>
 				<artifactId>maven-compiler-plugin</artifactId>

Modified: felix/trunk/httplite/minimum/src/main/java/org/apache/felix/httplite/servlet/HttpServletResponseImpl.java
URL: http://svn.apache.org/viewvc/felix/trunk/httplite/minimum/src/main/java/org/apache/felix/httplite/servlet/HttpServletResponseImpl.java?rev=1225141&r1=1225140&r2=1225141&view=diff
==============================================================================
--- felix/trunk/httplite/minimum/src/main/java/org/apache/felix/httplite/servlet/HttpServletResponseImpl.java
(original)
+++ felix/trunk/httplite/minimum/src/main/java/org/apache/felix/httplite/servlet/HttpServletResponseImpl.java
Wed Dec 28 08:16:38 2011
@@ -60,7 +60,8 @@ public class HttpServletResponseImpl imp
     private boolean m_getOutputStreamCalled = false;
     private boolean m_getWriterCalled = false;
     private ServletOutputStreamImpl m_servletOutputStream;
-    private PrintWriter m_servletPrintWriter;
+    private PrintWriter m_printWriter;
+    private List m_cookies = null;
 
     private int m_statusCode = HttpURLConnection.HTTP_OK;
     private String m_customStatusMessage = null;
@@ -97,6 +98,20 @@ public class HttpServletResponseImpl imp
         }
 
         m_out.write(buildResponse(m_statusCode, m_headers, m_customStatusMessage, null));
+        
+        if (m_cookies != null)
+        {
+            m_out.write( "Set-Cookie: ".getBytes() );
+            for (Iterator i = m_cookies.iterator(); i.hasNext();)
+            {
+                Cookie cookie = (Cookie) i.next();
+                m_out.write( cookieToHeader(cookie) );
+                
+                if (i.hasNext()) {
+                    m_out.write( ';' );
+                }
+            }
+        }
         m_out.write(HttpConstants.HEADER_DELEMITER.getBytes());
         m_out.flush();
 
@@ -248,7 +263,7 @@ public class HttpServletResponseImpl imp
         }
         else if (m_getWriterCalled)
         {
-            m_servletPrintWriter.flush();
+            m_printWriter.flush();
         }
 
         if (!m_headersWritten)
@@ -338,13 +353,13 @@ public class HttpServletResponseImpl imp
             throw new IllegalStateException(
                 "getOutputStream method has already been called for this response object.");
 
-        if (m_servletPrintWriter == null)
+        if (m_printWriter == null)
         {
             m_buffer = new ByteArrayOutputStream(m_bufferSize);
-            m_servletPrintWriter = new PrintWriter(m_buffer);
+            m_printWriter = new PrintWriter(m_buffer);
         }
 
-        return m_servletPrintWriter;
+        return m_printWriter;
     }
 
     /* (non-Javadoc)
@@ -365,7 +380,7 @@ public class HttpServletResponseImpl imp
             throw new IllegalStateException("Response has already been committed.");
         }
         m_buffer.reset();
-        m_servletPrintWriter = null;
+        m_printWriter = null;
         m_servletOutputStream = null;
         m_getOutputStreamCalled = false;
         m_getWriterCalled = false;
@@ -385,7 +400,7 @@ public class HttpServletResponseImpl imp
         }
 
         m_buffer.reset();
-        m_servletPrintWriter = null;
+        m_printWriter = null;
         m_servletOutputStream = null;
         m_getOutputStreamCalled = false;
         m_getWriterCalled = false;
@@ -450,7 +465,15 @@ public class HttpServletResponseImpl imp
      */
     public void addCookie(final Cookie cookie)
     {
-        throw new UnimplementedAPIException();
+        if (m_cookies == null)
+        {
+            m_cookies = new ArrayList();
+        }
+        
+        if (!m_cookies.contains( cookie ))
+        {
+            m_cookies.add( cookie );
+        }        
     }
 
     /* (non-Javadoc)
@@ -729,6 +752,30 @@ public class HttpServletResponseImpl imp
 
         return buffer.toString().getBytes();
     }
+    
+    /**
+     * Convert a cookie into the HTTP header in response.
+     * 
+     * @param cookie Cookie
+     * @return String as byte array of cookie as header
+     */
+    private byte[] cookieToHeader( Cookie cookie )
+    {
+        if (cookie == null || cookie.getName() == null || cookie.getValue() == null) 
+        {
+            throw new IllegalArgumentException( "Invalid cookie" );
+        }
+        
+        StringBuffer sb = new StringBuffer();
+                
+        sb.append( cookie.getName() );
+        sb.append( '=' );
+        sb.append( cookie.getValue() );
+        
+        //TODO: Implement all Cookie fields
+            
+        return sb.toString().getBytes();
+    }
 
     /**
      * Append name and value as an HTTP header to a StringBuffer

Added: felix/trunk/httplite/minimum/src/test/java/org/apache/felix/httplite/osgi/test/cases/TestCookies.java
URL: http://svn.apache.org/viewvc/felix/trunk/httplite/minimum/src/test/java/org/apache/felix/httplite/osgi/test/cases/TestCookies.java?rev=1225141&view=auto
==============================================================================
--- felix/trunk/httplite/minimum/src/test/java/org/apache/felix/httplite/osgi/test/cases/TestCookies.java
(added)
+++ felix/trunk/httplite/minimum/src/test/java/org/apache/felix/httplite/osgi/test/cases/TestCookies.java
Wed Dec 28 08:16:38 2011
@@ -0,0 +1,208 @@
+package org.apache.felix.httplite.osgi.test.cases;
+
+
+import java.io.IOException;
+import java.net.HttpURLConnection;
+import java.util.List;
+
+import javax.servlet.Servlet;
+import javax.servlet.ServletException;
+import javax.servlet.http.Cookie;
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import org.apache.felix.httplite.osgi.test.AbstractHttpliteTestCase;
+import org.osgi.service.http.HttpService;
+import org.osgi.service.http.NamespaceException;
+
+
+/**
+ * Test that servlet container handles cookies correctly.
+ *
+ */
+public class TestCookies extends AbstractHttpliteTestCase
+{
+    private static final int MODE_NONE_SET = 1;
+    private static final int MODE_SIMPLE = 2;
+    private static final int MODE_MULTI = 0;
+
+
+    //TODO: Test cookie expiration
+
+    /**
+     * Test that no cookies are sent by default.
+     * 
+     * @throws ServletException
+     * @throws NamespaceException
+     * @throws IOException
+     */
+    public void testNoCookiesSentByDefault() throws ServletException, NamespaceException,
IOException
+    {
+        HttpService httpService = getHTTPService( registry.getBundleContext() );
+        CookieServlet servlet = new CookieServlet( MODE_NONE_SET );
+        httpService.registerServlet( "/test", servlet, null, null );
+
+        //Test that no cookies are currently set.
+        HttpURLConnection client = getConnection( DEFAULT_BASE_URL + "/test", "GET" );
+
+        client.connect();
+        assertTrue( client.getResponseCode() == 200 );
+
+    }
+
+
+    /**
+     * Test creating and sending one cookie.
+     * 
+     * @throws ServletException
+     * @throws NamespaceException
+     * @throws IOException
+     */
+    public void testSimpleCookie() throws ServletException, NamespaceException, IOException
+    {
+        HttpService httpService = getHTTPService( registry.getBundleContext() );
+        CookieServlet servlet = new CookieServlet( MODE_SIMPLE );
+        httpService.registerServlet( "/test", servlet, null, null );
+
+        //Test that no cookies are currently set.
+        HttpURLConnection client = getConnection( DEFAULT_BASE_URL + "/test", "GET" );
+
+        //Set the cookie in the response
+        client.connect();
+        assertTrue( client.getResponseCode() == 200 );
+        assertNotNull( client.getHeaderFields().get( "Set-Cookie" ) );
+        List l = ( List ) client.getHeaderFields().get( "Set-Cookie" );
+        assertTrue( l.contains( "testcookie=testvalue" ) );
+
+        for ( int i = 0; i < 10; ++i )
+        {
+            //Confirm the cookie in the request
+            client = getConnection( DEFAULT_BASE_URL + "/test", "GET" );
+            client.addRequestProperty( "Cookie", "testcookie=testvalue" );
+            client.connect();
+            assertTrue( client.getResponseCode() == 200 );
+        }
+
+    }
+
+
+    /**
+     * Test sending and recieving multiple cookies.
+     * 
+     * @throws ServletException
+     * @throws NamespaceException
+     * @throws IOException
+     */
+    public void testMultipleCookies() throws ServletException, NamespaceException, IOException
+    {
+        HttpService httpService = getHTTPService( registry.getBundleContext() );
+        CookieServlet servlet = new CookieServlet( MODE_MULTI );
+        httpService.registerServlet( "/test", servlet, null, null );
+
+        //Test that no cookies are currently set.
+        HttpURLConnection client = getConnection( DEFAULT_BASE_URL + "/test", "GET" );
+
+        //Set the cookie in the response
+        client.connect();
+        assertTrue( client.getResponseCode() == 200 );
+        assertNotNull( client.getHeaderFields().get( "Set-Cookie" ) );
+        List l = ( List ) client.getHeaderFields().get( "Set-Cookie" );
+        assertTrue( l.contains( "testcookie=testvalue;testcookie2=testvalue2" ) );
+
+        for ( int i = 0; i < 10; ++i )
+        {
+            //Confirm the cookie in the request
+            client = getConnection( DEFAULT_BASE_URL + "/test", "GET" );
+            client.addRequestProperty( "Cookie", "testcookie=testvalue;testcookie2=testvalue2"
);
+            client.connect();
+            assertTrue( client.getResponseCode() == 200 );
+        }
+
+    }
+
+    /**
+     * Servlet to test cookie support.
+     *
+     */
+    private class CookieServlet extends HttpServlet implements Servlet
+    {
+
+        private final int m_mode;
+        private int requestCount = 0;
+        private Cookie[] m_cookies;
+
+
+        public CookieServlet( int mode )
+        {
+            this.m_mode = mode;
+        }
+
+
+        public Cookie[] getCookies()
+        {
+            return m_cookies;
+        }
+
+
+        protected void doGet( HttpServletRequest req, HttpServletResponse resp ) throws ServletException,
IOException
+        {
+            m_cookies = req.getCookies();
+
+            switch ( m_mode )
+            {
+                case MODE_NONE_SET:
+                    Cookie[] cookies = req.getCookies();
+
+                    assertNull( cookies );
+
+                    break;
+                case MODE_SIMPLE:
+                    requestCount++;
+                    if ( requestCount == 1 )
+                    {
+                        assertNull( req.getCookies() );
+                        Cookie c = new Cookie( "testcookie", "testvalue" );
+                        resp.addCookie( c );
+
+                    }
+                    else if ( requestCount > 1 )
+                    {
+                        Cookie[] c = req.getCookies();
+
+                        assertNotNull( c );
+                        assertTrue( c.length == 1 );
+                        assertTrue( c[0].getName().equals( "testcookie" ) );
+                        assertTrue( c[0].getValue().equals( "testvalue" ) );
+                    }
+
+                    break;
+                case MODE_MULTI:
+                    requestCount++;
+                    if ( requestCount == 1 )
+                    {
+                        assertNull( req.getCookies() );
+                        Cookie c = new Cookie( "testcookie", "testvalue" );
+                        resp.addCookie( c );
+                        c = new Cookie( "testcookie2", "testvalue2" );
+                        resp.addCookie( c );
+                    }
+                    else if ( requestCount > 1 )
+                    {
+                        Cookie[] c = req.getCookies();
+
+                        assertNotNull( c );
+                        assertTrue( c.length == 2 );
+                        assertTrue( c[0].getName().equals( "testcookie" ) );
+                        assertTrue( c[0].getValue().equals( "testvalue" ) );
+                        assertTrue( c[1].getName().equals( "testcookie2" ) );
+                        assertTrue( c[1].getValue().equals( "testvalue2" ) );
+                    }
+
+                    break;
+                default:
+                    throw new ServletException( "Invalid test mode." );
+            }
+        }
+    }
+}



Mime
View raw message