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.api.endpoint.InboundEndpoint;
10  import org.mule.tck.junit4.FunctionalTestCase;
11  import org.mule.tck.junit4.rule.DynamicPort;
12  import org.mule.transport.http.HttpConstants;
13  import org.mule.transport.http.PatchMethod;
14  
15  import org.apache.commons.httpclient.HttpClient;
16  import org.apache.commons.httpclient.HttpMethodBase;
17  import org.apache.commons.httpclient.HttpStatus;
18  import org.apache.commons.httpclient.methods.DeleteMethod;
19  import org.apache.commons.httpclient.methods.HeadMethod;
20  import org.apache.commons.httpclient.methods.OptionsMethod;
21  import org.apache.commons.httpclient.methods.PutMethod;
22  import org.apache.commons.httpclient.methods.TraceMethod;
23  import org.junit.ClassRule;
24  import org.junit.Test;
25  
26  import static org.junit.Assert.assertEquals;
27  
28  public class HttpMethodTestCase extends FunctionalTestCase
29  {
30      @ClassRule
31      public static DynamicPort dynamicPort = new DynamicPort("port1");
32  
33      private HttpClient client;
34  
35      public HttpMethodTestCase()
36      {
37          super();
38          setDisposeContextPerClass(true);
39          client = new HttpClient();
40      }
41  
42      @Override
43      protected String getConfigResources()
44      {
45          return "http-method-test.xml";
46      }
47  
48      @Test
49      public void testHead() throws Exception
50      {
51          HeadMethod method = new HeadMethod(getHttpEndpointAddress());
52          int statusCode = client.executeMethod(method);
53          assertEquals(HttpStatus.SC_OK, statusCode);
54      }
55  
56      @Test
57      public void testOptions() throws Exception
58      {
59          OptionsMethod method = new OptionsMethod(getHttpEndpointAddress());
60          int statusCode = client.executeMethod(method);
61          assertEquals(HttpStatus.SC_OK, statusCode);
62      }
63  
64      @Test
65      public void testPut() throws Exception
66      {
67          PutMethod method = new PutMethod(getHttpEndpointAddress());
68          int statusCode = client.executeMethod(method);
69          assertEquals(HttpStatus.SC_OK, statusCode);
70      }
71  
72      @Test
73      public void testDelete() throws Exception
74      {
75          DeleteMethod method = new DeleteMethod(getHttpEndpointAddress());
76          int statusCode = client.executeMethod(method);
77          assertEquals(HttpStatus.SC_OK, statusCode);
78      }
79  
80      @Test
81      public void testTrace() throws Exception
82      {
83          TraceMethod method = new TraceMethod(getHttpEndpointAddress());
84          int statusCode = client.executeMethod(method);
85          assertEquals(HttpStatus.SC_OK, statusCode);
86      }
87  
88      @Test
89      public void testConnect() throws Exception
90      {
91          CustomHttpMethod method = new CustomHttpMethod(HttpConstants.METHOD_CONNECT, getHttpEndpointAddress());
92          int statusCode = client.executeMethod(method);
93          assertEquals(HttpStatus.SC_OK, statusCode);
94      }
95  
96      @Test
97      public void testPatch() throws Exception
98      {
99          PatchMethod method = new PatchMethod(getHttpEndpointAddress());
100         int statusCode = client.executeMethod(method);
101         assertEquals(HttpStatus.SC_OK, statusCode);
102     }
103 
104     @Test
105     public void testFoo() throws Exception
106     {
107         CustomHttpMethod method = new CustomHttpMethod("FOO", getHttpEndpointAddress());
108         int statusCode = client.executeMethod(method);
109         assertEquals(HttpStatus.SC_BAD_REQUEST, statusCode);
110     }
111 
112     private String getHttpEndpointAddress()
113     {
114         InboundEndpoint httpEndpoint = muleContext.getRegistry().lookupObject("inHttpIn");
115         return httpEndpoint.getAddress();
116     }
117 
118     private static class CustomHttpMethod extends HttpMethodBase
119     {
120         private final String method;
121 
122         public CustomHttpMethod(String method, String url)
123         {
124             super(url);
125             this.method = method;
126         }
127 
128         @Override
129         public String getName()
130         {
131             return method;
132         }
133     }
134 }