httplite: added test case for cookie support
git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@1225141 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/httplite/minimum/pom.xml b/httplite/minimum/pom.xml
index e42cc38..f3f3833 100644
--- a/httplite/minimum/pom.xml
+++ b/httplite/minimum/pom.xml
@@ -78,7 +78,7 @@
</includes>
</resource>
</resources>
- <plugins>
+ <plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
diff --git a/httplite/minimum/src/main/java/org/apache/felix/httplite/servlet/HttpServletResponseImpl.java b/httplite/minimum/src/main/java/org/apache/felix/httplite/servlet/HttpServletResponseImpl.java
index 45dd509..6f97b98 100644
--- a/httplite/minimum/src/main/java/org/apache/felix/httplite/servlet/HttpServletResponseImpl.java
+++ b/httplite/minimum/src/main/java/org/apache/felix/httplite/servlet/HttpServletResponseImpl.java
@@ -60,7 +60,8 @@
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 @@
}
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 @@
}
else if (m_getWriterCalled)
{
- m_servletPrintWriter.flush();
+ m_printWriter.flush();
}
if (!m_headersWritten)
@@ -338,13 +353,13 @@
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 @@
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 @@
}
m_buffer.reset();
- m_servletPrintWriter = null;
+ m_printWriter = null;
m_servletOutputStream = null;
m_getOutputStreamCalled = false;
m_getWriterCalled = false;
@@ -450,7 +465,15 @@
*/
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 @@
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
diff --git a/httplite/minimum/src/test/java/org/apache/felix/httplite/osgi/test/cases/TestCookies.java b/httplite/minimum/src/test/java/org/apache/felix/httplite/osgi/test/cases/TestCookies.java
new file mode 100644
index 0000000..418541d
--- /dev/null
+++ b/httplite/minimum/src/test/java/org/apache/felix/httplite/osgi/test/cases/TestCookies.java
@@ -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." );
+ }
+ }
+ }
+}