View Javadoc

1   /*
2    * $Id: HttpMethodTestCase.java 22518 2011-07-22 07:00:22Z claude.mamo $
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  
15  import org.mule.api.endpoint.InboundEndpoint;
16  import org.mule.module.client.MuleClient;
17  import org.mule.tck.AbstractServiceAndFlowTestCase;
18  import org.mule.tck.junit4.rule.DynamicPort;
19  
20  import java.util.Arrays;
21  import java.util.Collection;
22  
23  import org.apache.commons.httpclient.HttpClient;
24  import org.apache.commons.httpclient.HttpMethodBase;
25  import org.apache.commons.httpclient.HttpStatus;
26  import org.apache.commons.httpclient.methods.DeleteMethod;
27  import org.apache.commons.httpclient.methods.HeadMethod;
28  import org.apache.commons.httpclient.methods.OptionsMethod;
29  import org.apache.commons.httpclient.methods.PutMethod;
30  import org.apache.commons.httpclient.methods.TraceMethod;
31  import org.junit.Rule;
32  import org.junit.Test;
33  import org.junit.runners.Parameterized.Parameters;
34  
35  public class HttpMethodTestCase extends AbstractServiceAndFlowTestCase
36  {
37  
38      private HttpMethodBase method;
39      private MuleClient muleClient = null;
40  
41      @Rule
42      public DynamicPort dynamicPort = new DynamicPort("port1");
43  
44      public HttpMethodTestCase(ConfigVariant variant, String configResources)
45      {
46          super(variant, configResources);
47      }
48  
49      @Parameters
50      public static Collection<Object[]> parameters()
51      {
52          return Arrays.asList(new Object[][]{
53              {ConfigVariant.SERVICE, "http-method-test-service.xml"},
54              {ConfigVariant.FLOW, "http-method-test-flow.xml"}
55          });
56      }      
57  
58      @Override
59      protected void doSetUp() throws Exception
60      {
61          super.doSetUp();
62          muleClient = new MuleClient(muleContext);
63      }
64  
65      @Test
66      public void testHead() throws Exception
67      {
68          HttpClient client = new HttpClient();
69          method = new HeadMethod(((InboundEndpoint) muleClient.getMuleContext().getRegistry().lookupObject("inHttpIn")).getAddress());
70          int statusCode = client.executeMethod(method);
71          assertEquals(Integer.toString(HttpStatus.SC_OK), Integer.toString(statusCode));
72  
73      }
74  
75      @Test
76      public void testOptions() throws Exception
77      {
78          HttpClient client = new HttpClient();
79          method = new OptionsMethod(((InboundEndpoint) muleClient.getMuleContext().getRegistry().lookupObject("inHttpIn")).getAddress());
80          int statusCode = client.executeMethod(method);
81          assertEquals(Integer.toString(HttpStatus.SC_OK), Integer.toString(statusCode));
82      }
83  
84      @Test
85      public void testPut() throws Exception
86      {
87          HttpClient client = new HttpClient();
88          method = new PutMethod(((InboundEndpoint) muleClient.getMuleContext().getRegistry().lookupObject("inHttpIn")).getAddress());
89          int statusCode = client.executeMethod(method);
90          assertEquals(Integer.toString(HttpStatus.SC_OK), Integer.toString(statusCode));
91      }
92  
93      @Test
94      public void testDelete() throws Exception
95      {
96          HttpClient client = new HttpClient();
97          method = new DeleteMethod(((InboundEndpoint) muleClient.getMuleContext().getRegistry().lookupObject("inHttpIn")).getAddress());
98          int statusCode = client.executeMethod(method);
99          assertEquals(Integer.toString(HttpStatus.SC_OK), Integer.toString(statusCode));
100     }
101 
102     @Test
103     public void testTrace() throws Exception
104     {
105         HttpClient client = new HttpClient();
106         method = new TraceMethod(((InboundEndpoint) muleClient.getMuleContext().getRegistry().lookupObject("inHttpIn")).getAddress());
107         int statusCode = client.executeMethod(method);
108         assertEquals(Integer.toString(HttpStatus.SC_OK), Integer.toString(statusCode));
109     }
110 
111     @Test
112     public void testConnect() throws Exception
113     {
114         HttpClient client = new HttpClient();
115         method = new HttpMethodBase(((InboundEndpoint) muleClient.getMuleContext().getRegistry().lookupObject("inHttpIn")).getAddress())
116         {
117             @Override
118             public String getName()
119             {
120                 return "CONNECT";
121             }
122         };
123         int statusCode = client.executeMethod(method);
124         assertEquals(Integer.toString(HttpStatus.SC_OK), Integer.toString(statusCode));
125     }
126 
127     @Test
128     public void testFoo() throws Exception
129     {
130         HttpClient client = new HttpClient();
131         method = new HttpMethodBase(((InboundEndpoint) muleClient.getMuleContext().getRegistry().lookupObject("inHttpIn")).getAddress())
132         {
133             @Override
134             public String getName()
135             {
136                 return "FOO";
137             }
138         };
139         int statusCode = client.executeMethod(method);
140         assertEquals(Integer.toString(HttpStatus.SC_BAD_REQUEST), Integer.toString(statusCode));
141     }
142 
143 }
144 
145