View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
6    */
7   package org.mule.transport.http.functional;
8   
9   import org.mule.tck.junit4.rule.DynamicPort;
10  import org.mule.transport.http.HttpConstants;
11  import org.mule.util.concurrent.Latch;
12  
13  import java.io.BufferedReader;
14  import java.util.StringTokenizer;
15  
16  import edu.emory.mathcs.backport.java.util.concurrent.CountDownLatch;
17  import edu.emory.mathcs.backport.java.util.concurrent.TimeUnit;
18  
19  import org.junit.ClassRule;
20  import org.junit.Test;
21  
22  import static org.junit.Assert.assertEquals;
23  import static org.junit.Assert.assertTrue;
24  
25  public class HttpOutboundTestCase extends AbstractMockHttpServerTestCase
26  {
27      @ClassRule
28      public static DynamicPort dynamicPort = new DynamicPort("port1");
29  
30      private Latch testLatch = new Latch();
31      private String httpMethod;
32  
33      public HttpOutboundTestCase()
34      {
35          setDisposeContextPerClass(true);
36      }
37  
38      @Override
39      protected String getConfigResources()
40      {
41          return "http-outbound-config.xml";
42      }
43  
44      @Override
45      protected MockHttpServer getHttpServer(CountDownLatch latch)
46      {
47          return new SimpleHttpServer(dynamicPort.getNumber(), latch, testLatch);
48      }
49  
50      @Test
51      public void testOutboundDelete() throws Exception
52      {
53          sendHttpRequest("vm://doDelete", HttpConstants.METHOD_DELETE);
54      }
55  
56      @Test
57      public void testOutboundGet() throws Exception
58      {
59          sendHttpRequest("vm://doGet", HttpConstants.METHOD_GET);
60      }
61  
62      @Test
63      public void testOutboundHead() throws Exception
64      {
65          sendHttpRequest("vm://doHead", HttpConstants.METHOD_HEAD);
66      }
67  
68      @Test
69      public void testOutboundOptions() throws Exception
70      {
71          sendHttpRequest("vm://doOptions", HttpConstants.METHOD_OPTIONS);
72      }
73  
74      @Test
75      public void testOutboundPost() throws Exception
76      {
77          sendHttpRequest("vm://doPost", HttpConstants.METHOD_POST);
78      }
79  
80      @Test
81      public void testOutboundPut() throws Exception
82      {
83          sendHttpRequest("vm://doPut", HttpConstants.METHOD_PUT);
84      }
85  
86      @Test
87      public void testOutboundTrace() throws Exception
88      {
89          sendHttpRequest("vm://doTrace", HttpConstants.METHOD_TRACE);
90      }
91  
92      @Test
93      public void testOutboundPatch() throws Exception
94      {
95          sendHttpRequest("vm://doPatch", HttpConstants.METHOD_PATCH);
96      }
97  
98      private void sendHttpRequest(String endpoint, String expectedHttpMethod) throws Exception
99      {
100         muleContext.getClient().dispatch(endpoint, TEST_MESSAGE, null);
101 
102         assertTrue(testLatch.await(RECEIVE_TIMEOUT, TimeUnit.MILLISECONDS));
103         assertEquals(expectedHttpMethod, httpMethod);
104     }
105 
106     private class SimpleHttpServer extends MockHttpServer
107     {
108         public SimpleHttpServer(int listenPort, CountDownLatch startupLatch, CountDownLatch testCompleteLatch)
109         {
110             super(listenPort, startupLatch, testCompleteLatch);
111         }
112 
113         @Override
114         protected void readHttpRequest(BufferedReader reader) throws Exception
115         {
116             // first line is the HTTP request
117             String line = reader.readLine();
118             httpMethod = new StringTokenizer(line).nextToken();
119         }
120     }
121 }