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