5.12 Testing Cookies

5.12.1 Problem

You want to create cookies and test for the existence of cookies.

5.12.2 Solution

Use WebConversation's addCookie( ) method to create new cookies, and its getCookieValue( ) method to retrieve cookie values.

5.12.3 Discussion

Cookies are little pieces of information that web applications store on the client browser's machine. Cookies allow web sites to maintain state information as you view different web pages. HttpUnit creates cookies and retrieve cookies using methods on the WebConversation class.

For the sake of an example, let's look at a simple JSP that uses cookies. Example 5-12 shows a JSP that creates a new cookie and then displays the array of cookies from the client.

Example 5-12. A JSP that generates and displays cookies
<%-- generate a cookie --%>
   Cookie cookie = new Cookie("customerId", "12345");

  <head><title>Cookie Demo</title></head>

    <h1>Cookies in this request</h1>
        <th>Cookie Name</th>

    <%-- print all of the cookies for this request --%>
    <% Cookie[] cookies = request.getCookies(  );
      int numCookies = (cookies != null) ? cookies.length : 0;
      for (int i=0; i<numCookies; i++) { %>
          <td><%= cookies[i].getName(  ) %></td>
          <td><%= cookies[i].getValue(  ) %></td>
   <% } %>


The unit test, shown in Example 5-13, works with the JSP shown in Example 5-12. The unit test accomplishes two tasks. First, it shows how you can create new cookies from your own unit tests. In this case, it creates a cookie named "shoppingCartId". Creating a new cookie mimics a real web application in which the shopping cart cookie was created on a prior page.

Example 5-13. A unit test that uses cookies
public void testCookies(  ) throws Exception {
    this.webConversation.addCookie("shoppingCartId", "98765");
    WebRequest req = new GetMethodWebRequest(
    WebResponse cookieDemoPage = this.webConversation.getResponse(req);

    // the JSP should have created the customerId cookie
    assertEquals("customer ID cookie", "12345",

    // make sure the cookie we generated on the client was found
    // somewhere in the HTML page
    String pageSource = cookieDemoPage.getText(  );
    assertTrue("shoppingCardId cookie was not found",
            pageSource.indexOf("shoppingCartId") > -1);

The testCookies( ) method also verifies that the JSP was able to create a new cookie, named "customerId". We use the getCookieValue( ) method to retrieve the cookie. If the JSP failed to create the cookie, its value will be null.

Finally, the unit test verifies that the JSP displays the cookie that was generated from the test. It does this using a very primitive technique, but one that works. It converts the entire page into a string using the getText( ) method. The test then searches for "shoppingCartId" as a substring of the page's text. If the text is found, it means the JSP was able to receive the cookie from the unit test. This is not something you would normally test. Instead, it is used here to show that HttpUnit's cookie functionality works.

5.12.4 See Also

See O'Reilly's Java Servlet Programming by Jason Hunter to learn more about cookies.