1
2
3
4
5
6
7
8
9
10
11 package org.mule.transport.http.functional;
12
13 import org.mule.module.client.MuleClient;
14
15 import java.io.BufferedReader;
16 import java.util.ArrayList;
17 import java.util.HashMap;
18 import java.util.List;
19 import java.util.Map;
20
21 import edu.emory.mathcs.backport.java.util.concurrent.CountDownLatch;
22 import edu.emory.mathcs.backport.java.util.concurrent.TimeUnit;
23
24 public class HttpCookieTestCase extends AbstractMockHttpServerTestCase
25 {
26
27 private static final String COOKIE_HEADER = "Cookie:";
28
29 private CountDownLatch latch = new CountDownLatch(1);
30 private boolean cookieFound = false;
31 private List<String> cookieHeaders = new ArrayList<String>();
32
33 @Override
34 protected String getConfigResources()
35 {
36 return "http-cookie-test.xml";
37 }
38
39 @Override
40 protected MockHttpServer getHttpServer(CountDownLatch serverStartLatch)
41 {
42 return new SimpleHttpServer(getPorts().get(0), serverStartLatch, latch);
43 }
44
45 public void testCookies() throws Exception
46 {
47 Map<String, String> properties = new HashMap<String, String>();
48 properties.put("COOKIE_HEADER","MYCOOKIE");
49
50 MuleClient client = new MuleClient(muleContext);
51 client.dispatch("vm://vm-in", "foobar", properties);
52
53 assertTrue(latch.await(RECEIVE_TIMEOUT, TimeUnit.MILLISECONDS));
54 assertTrue(cookieFound);
55
56 assertEquals(3, cookieHeaders.size());
57 assertThereIsCookieWithThisContent("Cookie: $Version=0; customCookie=yes", cookieHeaders);
58 assertThereIsCookieWithThisContent("Cookie: $Version=0; expressionCookie=MYCOOKIE", cookieHeaders);
59 assertThereIsCookieWithThisContent("MULE_SESSION=", cookieHeaders);
60 }
61
62 private void assertThereIsCookieWithThisContent(String content, List<String> listOfRawCookies)
63 {
64 for (String rawCookie : listOfRawCookies)
65 {
66 if (rawCookie != null && rawCookie.contains(content))
67 {
68 return;
69 }
70 }
71 fail("There should be a cookie with content '" + content + "': " + listOfRawCookies);
72
73 }
74
75 private class SimpleHttpServer extends MockHttpServer
76 {
77 public SimpleHttpServer(int listenPort, CountDownLatch startupLatch, CountDownLatch testCompleteLatch)
78 {
79 super(listenPort, startupLatch, testCompleteLatch);
80 }
81
82 @Override
83 protected void readHttpRequest(BufferedReader reader) throws Exception
84 {
85 String line = reader.readLine();
86 while (line != null)
87 {
88
89
90 if (line.indexOf(COOKIE_HEADER) > -1)
91 {
92 cookieFound = true;
93 cookieHeaders.add(line);
94 }
95
96 line = reader.readLine();
97
98
99 if (line.trim().length() == 0)
100 {
101 line = null;
102 }
103 }
104 }
105 }
106
107 @Override
108 protected int getNumPortsToFind()
109 {
110 return 1;
111 }
112 }