View Javadoc

1   /*
2    * $Id: HttpOutboundTestCase.java 19817 2010-10-04 18:10:39Z dzapata $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.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.transport.http.HttpConstants;
15  import org.mule.util.concurrent.Latch;
16  
17  import java.io.BufferedReader;
18  import java.util.StringTokenizer;
19  
20  import edu.emory.mathcs.backport.java.util.concurrent.CountDownLatch;
21  import edu.emory.mathcs.backport.java.util.concurrent.TimeUnit;
22  
23  public class HttpOutboundTestCase extends AbstractMockHttpServerTestCase
24  {
25      //private static final int LISTEN_PORT = 60215;
26      private Latch testLatch = new Latch();
27      private String httpMethod;
28  
29      public HttpOutboundTestCase()
30      {
31          setDisposeManagerPerSuite(true);
32      }
33  
34      protected MockHttpServer getHttpServer(CountDownLatch latch)
35      {
36          return new SimpleHttpServer(getPorts().get(0), latch, testLatch);
37      }
38  
39      protected String getConfigResources()
40      {
41          return "http-outbound-config.xml";
42      }
43      
44      public void testOutboundDelete() throws Exception
45      {
46          sendHttpRequest("vm://doDelete", HttpConstants.METHOD_DELETE);
47      }
48  
49      public void testOutboundGet() throws Exception
50      {
51          sendHttpRequest("vm://doGet", HttpConstants.METHOD_GET);
52      }
53  
54      public void testOutboundHead() throws Exception
55      {
56          sendHttpRequest("vm://doHead", HttpConstants.METHOD_HEAD);
57      }
58  
59      public void testOutboundOptions() throws Exception
60      {
61          sendHttpRequest("vm://doOptions", HttpConstants.METHOD_OPTIONS);
62      }
63  
64      public void testOutboundPost() throws Exception
65      {
66          sendHttpRequest("vm://doPost", HttpConstants.METHOD_POST);
67      }
68  
69      public void testOutboundPut() throws Exception
70      {
71          sendHttpRequest("vm://doPut", HttpConstants.METHOD_PUT);
72      }
73  
74      public void testOutboundTrace() throws Exception
75      {
76          sendHttpRequest("vm://doTrace", HttpConstants.METHOD_TRACE);
77      }
78  
79      private void sendHttpRequest(String endpoint, String expectedHttpMethod) throws Exception
80      {
81          MuleClient client = new MuleClient(muleContext);
82          client.dispatch(endpoint, TEST_MESSAGE, null);
83          
84          assertTrue(testLatch.await(RECEIVE_TIMEOUT, TimeUnit.MILLISECONDS));
85          assertEquals(expectedHttpMethod, httpMethod);
86      }
87      
88      private class SimpleHttpServer extends MockHttpServer
89      {
90          public SimpleHttpServer(int listenPort, CountDownLatch startupLatch, CountDownLatch testCompleteLatch)
91          {
92              super(listenPort, startupLatch, testCompleteLatch);
93          }
94  
95          @Override
96          protected void readHttpRequest(BufferedReader reader) throws Exception
97          {
98              // first line is the HTTP request
99              String line = reader.readLine();
100             httpMethod = new StringTokenizer(line).nextToken();            
101         }
102     }
103 
104     @Override
105     protected int getNumPortsToFind()
106     {
107         return 1;
108     }
109 }