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