1   /*
2    * $Id: HttpCookieTestCase.java 10953 2008-02-22 18:10:00Z dirk.olmes $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
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          // start a simple HTTP server that parses the request sent from Mule
45          new Thread(new SimpleHttpServer()).start();
46      }
47  
48      public void testCookies() throws Exception
49      {
50          // wait for the simple server thread started in doSetUp to come up
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                  // now that we are up and running, the test may send
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                      // Check that we receive a 'Cookie:' header as it would be 
79                      // send by a regular http client
80                      if (line.indexOf(COOKIE_HEADER) > -1)
81                      {
82                          cookieFound = true;
83                          break;
84                      }
85                      
86                      line = reader.readLine();
87                      // only read the header, i.e. if we encounter an empty line 
88                      // stop reading (we're only interested in the headers anyway)
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 }