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 import org.mule.tck.FunctionalTestCase;
15
16 import java.io.BufferedReader;
17 import java.io.InputStream;
18 import java.io.InputStreamReader;
19 import java.io.OutputStream;
20 import java.net.ServerSocket;
21 import java.net.Socket;
22
23 import edu.emory.mathcs.backport.java.util.concurrent.CountDownLatch;
24 import edu.emory.mathcs.backport.java.util.concurrent.TimeUnit;
25
26 public class HttpCookieTestCase extends FunctionalTestCase
27 {
28 private static final int LISTEN_PORT = 60212;
29 private static final String COOKIE_HEADER = "Cookie:";
30
31 private CountDownLatch simpleServerLatch = new CountDownLatch(1);
32 private CountDownLatch latch = new CountDownLatch(1);
33 private boolean cookieFound = false;
34
35 protected String getConfigResources()
36 {
37 return "http-cookie-test.xml";
38 }
39
40 protected void doSetUp() throws Exception
41 {
42 super.doSetUp();
43
44
45 new Thread(new SimpleHttpServer()).start();
46 }
47
48 public void testCookies() throws Exception
49 {
50
51 assertTrue(simpleServerLatch.await(RECEIVE_TIMEOUT, TimeUnit.MILLISECONDS));
52
53 MuleClient client = new MuleClient();
54 client.send("vm://vm-in", "foobar", null);
55
56 assertTrue(latch.await(RECEIVE_TIMEOUT, TimeUnit.MILLISECONDS));
57 assertTrue(cookieFound);
58 }
59
60 private class SimpleHttpServer extends Object implements Runnable
61 {
62 public void run()
63 {
64 try
65 {
66 ServerSocket serverSocket = new ServerSocket(LISTEN_PORT);
67
68
69 simpleServerLatch.countDown();
70
71 Socket socket = serverSocket.accept();
72 InputStream in = socket.getInputStream();
73 BufferedReader reader = new BufferedReader(new InputStreamReader(in));
74
75 String line = reader.readLine();
76 while (line != null)
77 {
78
79
80 if (line.indexOf(COOKIE_HEADER) > -1)
81 {
82 cookieFound = true;
83 break;
84 }
85
86 line = reader.readLine();
87
88
89 if (line.trim().length() == 0)
90 {
91 line = null;
92 }
93 }
94
95 OutputStream out = socket.getOutputStream();
96 out.write("HTTP/1.1 200 OK\n\n".getBytes());
97
98 in.close();
99 out.close();
100 socket.close();
101 serverSocket.close();
102 }
103 catch (Exception ex)
104 {
105 throw new RuntimeException(ex);
106 }
107 finally
108 {
109 latch.countDown();
110 }
111 }
112 }
113 }