View Javadoc

1   /*
2    * $Id: ObjectToHttpClientMethodRequestTestCase.java 22542 2011-07-22 20:50:01Z dfeist $
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.transformers;
12  
13  import static org.junit.Assert.assertEquals;
14  import static org.junit.Assert.assertTrue;
15  
16  import org.mule.RequestContext;
17  import org.mule.api.MuleEvent;
18  import org.mule.api.MuleMessage;
19  import org.mule.api.config.MuleProperties;
20  import org.mule.api.endpoint.InboundEndpoint;
21  import org.mule.tck.junit4.AbstractMuleContextTestCase;
22  import org.mule.transport.NullPayload;
23  import org.mule.transport.http.HttpConnector;
24  import org.mule.transport.http.HttpConstants;
25  import org.mule.transport.http.HttpRequest;
26  import org.mule.transport.http.RequestLine;
27  
28  import org.apache.commons.httpclient.HttpMethod;
29  import org.apache.commons.httpclient.HttpVersion;
30  import org.apache.commons.httpclient.methods.GetMethod;
31  import org.apache.commons.httpclient.methods.PostMethod;
32  import org.apache.commons.httpclient.methods.PutMethod;
33  import org.junit.Test;
34  
35  public class ObjectToHttpClientMethodRequestTestCase extends AbstractMuleContextTestCase
36  {
37      
38      private InboundEndpoint endpoint;
39      
40      private MuleMessage setupRequestContext(final String url, final String method) throws Exception
41      {
42          HttpRequest request = new HttpRequest(new RequestLine(method, url, HttpVersion.HTTP_1_1), null, "UTF-8");
43          
44          endpoint = muleContext.getEndpointFactory().getInboundEndpoint(url);
45          
46          MuleEvent event = getTestEvent(request, endpoint);
47          MuleMessage message = event.getMessage();
48          message.setOutboundProperty(HttpConnector.HTTP_METHOD_PROPERTY, method);
49          message.setOutboundProperty(MuleProperties.MULE_ENDPOINT_PROPERTY, url);
50          RequestContext.setEvent(event);
51  
52          return message;
53      }
54  
55      private ObjectToHttpClientMethodRequest createTransformer() throws Exception
56      {
57          ObjectToHttpClientMethodRequest transformer = new ObjectToHttpClientMethodRequest();
58          transformer.setMuleContext(muleContext);
59          transformer.setEndpoint(endpoint);
60          transformer.initialise();
61          return transformer;
62      }
63  
64      @Override
65      protected void doTearDown() throws Exception
66      {
67          RequestContext.setEvent(null);
68      }
69  
70      @Test
71      public void testUrlWithoutQuery() throws Exception
72      {
73          MuleMessage message = setupRequestContext("http://localhost:8080/services", HttpConstants.METHOD_GET);
74          // transforming NullPayload will make sure that no body=xxx query is added
75          message.setPayload(NullPayload.getInstance());
76  
77          ObjectToHttpClientMethodRequest transformer = createTransformer();
78          Object response = transformer.transform(message);
79  
80          assertTrue(response instanceof HttpMethod);
81          HttpMethod httpMethod = (HttpMethod) response;
82  
83          assertEquals(null, httpMethod.getQueryString());
84      }
85  
86      @Test
87      public void testUrlWithQuery() throws Exception
88      {
89          MuleMessage message = setupRequestContext("http://localhost:8080/services?method=echo", HttpConstants.METHOD_GET);
90          // transforming NullPayload will make sure that no body=xxx query is added
91          message.setPayload(NullPayload.getInstance());
92  
93          ObjectToHttpClientMethodRequest transformer = createTransformer();
94          Object response = transformer.transform(message);
95  
96          assertTrue(response instanceof HttpMethod);
97          HttpMethod httpMethod = (HttpMethod) response;
98  
99          assertEquals("method=echo", httpMethod.getQueryString());
100     }
101 
102     @Test
103     public void testUrlWithUnescapedQuery() throws Exception
104     {
105         MuleMessage message = setupRequestContext("http://mycompany.com/test?fruits=apple%20orange", HttpConstants.METHOD_GET);
106         // transforming NullPayload will make sure that no body=xxx query is added
107         message.setPayload(NullPayload.getInstance());
108 
109         ObjectToHttpClientMethodRequest transformer = createTransformer();
110         Object response = transformer.transform(message);
111 
112         assertTrue(response instanceof HttpMethod);
113         HttpMethod httpMethod = (HttpMethod) response;
114 
115         assertEquals("fruits=apple%20orange", httpMethod.getQueryString());
116     }
117 
118     @Test
119     public void testAppendedUrl() throws Exception
120     {
121         MuleMessage message = setupRequestContext("http://mycompany.com/test?fruits=apple%20orange", HttpConstants.METHOD_GET);
122         // transforming a payload here will add it as body=xxx query parameter
123         message.setPayload("test");
124         message.setOutboundProperty(HttpConnector.HTTP_GET_BODY_PARAM_PROPERTY, "body");
125 
126         ObjectToHttpClientMethodRequest transformer = createTransformer();
127         Object response = transformer.transform(message);
128 
129         assertTrue(response instanceof HttpMethod);
130         HttpMethod httpMethod = (HttpMethod) response;
131 
132         assertEquals("fruits=apple%20orange&body=test", httpMethod.getQueryString());
133     }
134 
135     @Test
136     public void testEncodingOfParamValueTriggeredByMessageProperty() throws Exception
137     {
138         // the payload is already encoded, switch off encoding it in the transformer
139         String encodedPayload = "encoded%20payload";
140         MuleMessage message = setupRequestContext("http://mycompany.com/", "GET");
141         message.setOutboundProperty(HttpConnector.HTTP_ENCODE_PARAMVALUE, false);
142         message.setOutboundProperty(HttpConnector.HTTP_GET_BODY_PARAM_PROPERTY, "body");
143         message.setPayload(encodedPayload);
144 
145 
146         ObjectToHttpClientMethodRequest transformer = createTransformer();
147         Object result = transformer.transform(message);
148 
149         assertTrue(result instanceof GetMethod);
150 
151         String expected = "body=" + encodedPayload;
152         assertEquals(expected, ((GetMethod) result).getQueryString());
153     }
154 
155     public void testPostMethod() throws Exception
156     {
157     	final MuleMessage message = setupRequestContext("http://localhost:8080/services", HttpConstants.METHOD_POST);
158     	final String contentType = "text/plain";
159 
160         message.setPayload("I'm a payload");
161         message.setInvocationProperty(HttpConstants.HEADER_CONTENT_TYPE, contentType);
162 
163         final ObjectToHttpClientMethodRequest transformer = createTransformer();
164         final Object response = transformer.transform(message);
165 
166         assertTrue(response instanceof PostMethod);
167         final HttpMethod httpMethod = (HttpMethod) response;
168         assertEquals(null, httpMethod.getQueryString());
169 
170         assertEquals(contentType, httpMethod.getRequestHeader(HttpConstants.HEADER_CONTENT_TYPE).getValue());
171     }
172 
173     public void testPutMethod() throws Exception
174     {
175     	final MuleMessage message = setupRequestContext("http://localhost:8080/services", HttpConstants.METHOD_PUT);
176     	final String contentType = "text/plain";
177 
178         message.setPayload("I'm a payload");
179         message.setInvocationProperty(HttpConstants.HEADER_CONTENT_TYPE, contentType);
180 
181         final ObjectToHttpClientMethodRequest transformer = createTransformer();
182         final Object response = transformer.transform(message);
183 
184         assertTrue(response instanceof PutMethod);
185         final HttpMethod httpMethod = (HttpMethod) response;
186         assertEquals(null, httpMethod.getQueryString());
187 
188         assertEquals(contentType, httpMethod.getRequestHeader(HttpConstants.HEADER_CONTENT_TYPE).getValue());
189     }
190 }