View Javadoc

1   /*
2    * $Id: HttpMethodTestCase.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.api.endpoint.InboundEndpoint;
14  import org.mule.module.client.MuleClient;
15  import org.mule.tck.DynamicPortTestCase;
16  
17  import org.apache.commons.httpclient.HttpClient;
18  import org.apache.commons.httpclient.HttpMethodBase;
19  import org.apache.commons.httpclient.HttpStatus;
20  import org.apache.commons.httpclient.methods.DeleteMethod;
21  import org.apache.commons.httpclient.methods.HeadMethod;
22  import org.apache.commons.httpclient.methods.OptionsMethod;
23  import org.apache.commons.httpclient.methods.PutMethod;
24  import org.apache.commons.httpclient.methods.TraceMethod;
25  
26  public class HttpMethodTestCase extends DynamicPortTestCase
27  {
28  
29      private HttpMethodBase method;
30      private MuleClient muleClient = null;
31          
32      @Override
33      protected void doSetUp() throws Exception
34      {
35          super.doSetUp();
36          muleClient = new MuleClient(muleContext);
37      }
38  
39      @Override
40      protected String getConfigResources()
41      {
42          return "http-method-test.xml";
43      }
44  
45      protected void doFunctionalTearDown () throws Exception
46      {
47          if (method != null)
48          {
49              method.releaseConnection();
50          }
51      }
52  
53      public void testHead() throws Exception
54      {
55          HttpClient client = new HttpClient();
56          method = new HeadMethod(((InboundEndpoint) muleClient.getMuleContext().getRegistry().lookupObject("inHttpIn")).getAddress());
57          int statusCode = client.executeMethod(method);
58          assertEquals(Integer.toString(HttpStatus.SC_OK), Integer.toString(statusCode));
59  
60      }
61  
62      public void testOptions() throws Exception
63      {
64          HttpClient client = new HttpClient();
65          method = new OptionsMethod(((InboundEndpoint) muleClient.getMuleContext().getRegistry().lookupObject("inHttpIn")).getAddress());
66          int statusCode = client.executeMethod(method);
67          assertEquals(Integer.toString(HttpStatus.SC_OK), Integer.toString(statusCode));
68      }
69  
70      public void testPut() throws Exception
71      {
72          HttpClient client = new HttpClient();
73          method = new PutMethod(((InboundEndpoint) muleClient.getMuleContext().getRegistry().lookupObject("inHttpIn")).getAddress());
74          int statusCode = client.executeMethod(method);
75          assertEquals(Integer.toString(HttpStatus.SC_OK), Integer.toString(statusCode));
76      }
77  
78      public void testDelete() throws Exception
79      {
80          HttpClient client = new HttpClient();
81          method = new DeleteMethod(((InboundEndpoint) muleClient.getMuleContext().getRegistry().lookupObject("inHttpIn")).getAddress());
82          int statusCode = client.executeMethod(method);
83          assertEquals(Integer.toString(HttpStatus.SC_OK), Integer.toString(statusCode));
84      }
85  
86      public void testTrace() throws Exception
87      {
88          HttpClient client = new HttpClient();
89          method = new TraceMethod(((InboundEndpoint) muleClient.getMuleContext().getRegistry().lookupObject("inHttpIn")).getAddress());
90          int statusCode = client.executeMethod(method);
91          assertEquals(Integer.toString(HttpStatus.SC_OK), Integer.toString(statusCode));
92      }
93  
94      public void testConnect() throws Exception
95      {
96          HttpClient client = new HttpClient();
97          method = new HttpMethodBase(((InboundEndpoint) muleClient.getMuleContext().getRegistry().lookupObject("inHttpIn")).getAddress())
98          {
99              @Override
100             public String getName()
101             {
102                 return "CONNECT";
103             }
104         };
105         int statusCode = client.executeMethod(method);
106         assertEquals(Integer.toString(HttpStatus.SC_OK), Integer.toString(statusCode));
107     }
108 
109     public void testFoo() throws Exception
110     {
111         HttpClient client = new HttpClient();
112         method = new HttpMethodBase(((InboundEndpoint) muleClient.getMuleContext().getRegistry().lookupObject("inHttpIn")).getAddress())
113         {
114             @Override
115             public String getName()
116             {
117                 return "FOO";
118             }
119         };
120         int statusCode = client.executeMethod(method);
121         assertEquals(Integer.toString(HttpStatus.SC_BAD_REQUEST), Integer.toString(statusCode));
122     }
123 
124     @Override
125     protected int getNumPortsToFind()
126     {
127         return 1;
128     }
129 }
130 
131