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