View Javadoc

1   /*
2    * $Id: HttpOutboundTestCase.java 22924 2011-09-12 22:08:33Z pablo.kraan $
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 static org.junit.Assert.assertEquals;
14  import static org.junit.Assert.assertTrue;
15  
16  import java.io.BufferedReader;
17  import java.util.Arrays;
18  import java.util.Collection;
19  import java.util.StringTokenizer;
20  import java.util.concurrent.CountDownLatch;
21  import java.util.concurrent.TimeUnit;
22  
23  import org.junit.ClassRule;
24  import org.junit.Test;
25  import org.junit.runners.Parameterized.Parameters;
26  import org.mule.module.client.MuleClient;
27  import org.mule.tck.junit4.rule.DynamicPort;
28  import org.mule.transport.http.HttpConstants;
29  import org.mule.util.concurrent.Latch;
30  
31  public class HttpOutboundTestCase extends AbstractMockHttpServerTestCase
32  {
33  
34      @ClassRule
35      public static DynamicPort dynamicPort = new DynamicPort("port1");
36  
37      private Latch testLatch = new Latch();
38      private String httpMethod;
39  
40      public HttpOutboundTestCase(ConfigVariant variant, String configResources)
41      {
42          super(variant, configResources);
43          setDisposeContextPerClass(true);
44      }
45  
46      @Parameters
47      public static Collection<Object[]> parameters()
48      {
49          return Arrays.asList(new Object[][]{{ConfigVariant.SERVICE, "http-outbound-config-service.xml"},
50              {ConfigVariant.FLOW, "http-outbound-config-flow.xml"}});
51      }
52  
53      @Override
54      protected MockHttpServer getHttpServer(CountDownLatch latch)
55      {
56          return new SimpleHttpServer(dynamicPort.getNumber(), latch, testLatch);
57      }
58  
59      @Test
60      public void testOutboundDelete() throws Exception
61      {
62          sendHttpRequest("vm://doDelete", HttpConstants.METHOD_DELETE);
63      }
64  
65      @Test
66      public void testOutboundGet() throws Exception
67      {
68          sendHttpRequest("vm://doGet", HttpConstants.METHOD_GET);
69      }
70  
71      @Test
72      public void testOutboundHead() throws Exception
73      {
74          sendHttpRequest("vm://doHead", HttpConstants.METHOD_HEAD);
75      }
76  
77      @Test
78      public void testOutboundOptions() throws Exception
79      {
80          sendHttpRequest("vm://doOptions", HttpConstants.METHOD_OPTIONS);
81      }
82  
83      @Test
84      public void testOutboundPost() throws Exception
85      {
86          sendHttpRequest("vm://doPost", HttpConstants.METHOD_POST);
87      }
88  
89      @Test
90      public void testOutboundPut() throws Exception
91      {
92          sendHttpRequest("vm://doPut", HttpConstants.METHOD_PUT);
93      }
94  
95      @Test
96      public void testOutboundTrace() throws Exception
97      {
98          sendHttpRequest("vm://doTrace", HttpConstants.METHOD_TRACE);
99      }
100 
101     private void sendHttpRequest(String endpoint, String expectedHttpMethod) throws Exception
102     {
103         MuleClient client = new MuleClient(muleContext);
104         client.dispatch(endpoint, TEST_MESSAGE, null);
105 
106         assertTrue(testLatch.await(RECEIVE_TIMEOUT, TimeUnit.MILLISECONDS));
107         assertEquals(expectedHttpMethod, httpMethod);
108     }
109 
110     private class SimpleHttpServer extends MockHttpServer
111     {
112         public SimpleHttpServer(int listenPort, CountDownLatch startupLatch, CountDownLatch testCompleteLatch)
113         {
114             super(listenPort, startupLatch, testCompleteLatch);
115         }
116 
117         @Override
118         protected void readHttpRequest(BufferedReader reader) throws Exception
119         {
120             // first line is the HTTP request
121             String line = reader.readLine();
122             httpMethod = new StringTokenizer(line).nextToken();
123         }
124     }
125 }