View Javadoc

1   /*
2    * $Id$
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.transport.http.functional;
12  
13  import org.mule.tck.DynamicPortTestCase;
14  
15  import java.io.IOException;
16  import java.util.concurrent.CountDownLatch;
17  import java.util.concurrent.TimeUnit;
18  import java.util.concurrent.atomic.AtomicBoolean;
19  
20  import javax.servlet.ServletException;
21  import javax.servlet.http.HttpServlet;
22  import javax.servlet.http.HttpServletRequest;
23  import javax.servlet.http.HttpServletResponse;
24  
25  import org.apache.commons.httpclient.Cookie;
26  import org.apache.commons.httpclient.HttpClient;
27  import org.apache.commons.httpclient.HttpState;
28  import org.apache.commons.httpclient.cookie.CookiePolicy;
29  import org.apache.commons.httpclient.methods.PostMethod;
30  import org.apache.commons.logging.Log;
31  import org.apache.commons.logging.LogFactory;
32  import org.mortbay.jetty.Connector;
33  import org.mortbay.jetty.Server;
34  import org.mortbay.jetty.bio.SocketConnector;
35  import org.mortbay.jetty.servlet.ServletHandler;
36  
37  public class HttpMultipleCookiesTestCase extends DynamicPortTestCase
38  {
39      //private static final int LOCAL_JETTY_SERVER_PORT = 4020;
40      protected static String TEST_MESSAGE = "Test Http Request ";
41      protected static final Log logger = LogFactory.getLog(HttpMultipleCookiesTestCase.class);
42  
43      private CountDownLatch simpleServerLatch = new CountDownLatch(1);
44      private CountDownLatch simpleServerShutdownLatch = new CountDownLatch(1);
45      private static AtomicBoolean cookiesRecieved = new AtomicBoolean(false);
46  
47      private Server server = null;
48  
49      public HttpMultipleCookiesTestCase()
50      {
51          super();
52          setStartContext(false);
53      }
54  
55      @Override
56      protected void doSetUp() throws Exception
57      {
58          super.doSetUp();
59          startServer();
60          assertTrue(simpleServerLatch.await(RECEIVE_TIMEOUT, TimeUnit.MILLISECONDS));
61      }
62  
63      @Override
64      protected void doTearDown() throws Exception
65      {
66          // TODO Auto-generated method stub
67          super.doTearDown();
68          muleContext.stop();
69          stopServer();
70          assertTrue(simpleServerShutdownLatch.await(RECEIVE_TIMEOUT, TimeUnit.MILLISECONDS));
71      }
72              
73      @Override
74      protected String getConfigResources()
75      {
76          return "http-multiple-cookies-test.xml";
77      }
78  
79      public void testSendDirectly() throws Exception
80      {
81          muleContext.start();
82          sendMessage(getPorts().get(1));
83      }
84  
85      public void testSendviaMule() throws Exception
86      {
87          muleContext.start();
88          sendMessage(getPorts().get(0));
89      }
90  
91      protected void sendMessage(int port) throws Exception
92      {
93          HttpClient client2 = new HttpClient();
94          client2.getParams().setCookiePolicy(CookiePolicy.RFC_2109);
95          HttpState state = new HttpState();
96          Cookie cookie1 = new Cookie("localhost", "TheFirst", "First", "/", null, false);
97          state.addCookie(cookie1);
98          Cookie cookie2 = new Cookie("localhost", "TheSecond", "Value2", "/", null, false);
99          state.addCookie(cookie2);
100         Cookie cookie3 = new Cookie("localhost", "TheThird", "Value3", "/", null, false);
101         state.addCookie(cookie3);
102 
103         client2.setState(state);
104         PostMethod method = new PostMethod("http://localhost:" + port);
105         Thread.sleep(5000);
106         client2.executeMethod(method);
107         assertEquals(TEST_MESSAGE, method.getResponseBodyAsString());
108         assertTrue("Cookies were not recieved", cookiesRecieved.get());
109 
110         for (Cookie cookie : client2.getState().getCookies())
111         {
112             logger.debug(cookie.getName() + " " + cookie.getValue());
113         }
114         assertEquals(6, client2.getState().getCookies().length);
115 
116     }
117 
118     protected void startServer() throws Exception
119     {
120         logger.debug("server starting");
121         Server server = new Server();
122         Connector connector = new SocketConnector();
123         connector.setPort(getPorts().get(1));
124         server.setConnectors(new Connector[]{connector});
125 
126         ServletHandler handler = new ServletHandler();
127         server.setHandler(handler);
128 
129         handler.addServletWithMapping(HelloServlet.class.getName(), "/");
130 
131         server.start();
132         // server.join();
133         simpleServerLatch.countDown();
134         logger.debug("Server started");
135     }
136     
137     protected void stopServer() throws Exception
138     {
139         logger.debug("server stopping");
140         
141         if(server != null && server.isRunning())
142         {
143             assertEquals(1, server.getConnectors());            
144             // this test only uses one connector
145             server.getConnectors()[0].stop();
146         }
147         
148         simpleServerShutdownLatch.countDown();
149         logger.debug("Server stopped");
150     }
151 
152     public static class HelloServlet extends HttpServlet
153     {
154         @Override
155         protected void doGet(HttpServletRequest request, HttpServletResponse response)
156             throws ServletException, IOException
157         {
158             try
159             {
160                 response.setContentType("text/xml");
161                 response.setContentLength(TEST_MESSAGE.length());
162                 for (int i = 0; i < 3; i++)
163                 {
164                     javax.servlet.http.Cookie cookie1 = new javax.servlet.http.Cookie("OutputCookieName" + i,
165                         "OutputCookieValue" + i);
166                     response.addCookie(cookie1);
167                 }
168                 cookiesRecieved.set(false);
169                 javax.servlet.http.Cookie[] cookies = request.getCookies();
170                 if (cookies != null)
171                 {
172                     for (javax.servlet.http.Cookie cookie : cookies)
173                     {
174                         logger.debug(cookie.getName() + " " + cookie.getValue());
175                         cookiesRecieved.set(true);
176                     }
177                 }
178                 response.setStatus(HttpServletResponse.SC_OK);
179                 response.getWriter().println(TEST_MESSAGE);
180 
181             }
182             catch (Exception e)
183             {
184                 logger.error("Servlet error", e);
185                 throw new ServletException(e);
186             }
187         }
188 
189         @Override
190         protected void doPost(HttpServletRequest request, HttpServletResponse response)
191             throws ServletException, IOException
192         {
193             doGet(request, response);
194         }
195 
196     }
197 
198     @Override
199     protected int getNumPortsToFind()
200     {
201         return 2;
202     }
203 }