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.transformers;
8   
9   import org.mule.RequestContext;
10  import org.mule.api.MuleEvent;
11  import org.mule.api.MuleMessage;
12  import org.mule.api.config.MuleProperties;
13  import org.mule.tck.junit4.AbstractMuleContextTestCase;
14  import org.mule.transport.NullPayload;
15  import org.mule.transport.http.HttpConnector;
16  import org.mule.transport.http.HttpConstants;
17  import org.mule.transport.http.HttpRequest;
18  import org.mule.transport.http.RequestLine;
19  
20  import org.apache.commons.httpclient.HttpMethod;
21  import org.apache.commons.httpclient.HttpVersion;
22  import org.apache.commons.httpclient.methods.GetMethod;
23  import org.junit.Test;
24  
25  import static org.junit.Assert.assertEquals;
26  import static org.junit.Assert.assertTrue;
27  
28  public class ObjectToHttpClientMethodRequestTestCase extends AbstractMuleContextTestCase
29  {
30      private MuleMessage setupRequestContext(String url) throws Exception
31      {
32          HttpRequest request = new HttpRequest(new RequestLine("GET", url, HttpVersion.HTTP_1_1), null, "UTF-8");
33  
34          MuleEvent event = getTestEvent(request, muleContext.getEndpointFactory().getInboundEndpoint(url));
35          MuleMessage message = event.getMessage();
36          message.setOutboundProperty(HttpConnector.HTTP_METHOD_PROPERTY, HttpConstants.METHOD_GET);
37          message.setOutboundProperty(MuleProperties.MULE_ENDPOINT_PROPERTY, url);
38          RequestContext.setEvent(event);
39  
40          return message;
41      }
42  
43      private ObjectToHttpClientMethodRequest createTransformer() throws Exception
44      {
45          ObjectToHttpClientMethodRequest transformer = new ObjectToHttpClientMethodRequest();
46          transformer.setMuleContext(muleContext);
47          transformer.setEndpoint(RequestContext.getEvent().getEndpoint());
48          transformer.initialise();
49          return transformer;
50      }
51  
52      @Override
53      protected void doTearDown() throws Exception
54      {
55          RequestContext.setEvent(null);
56      }
57  
58      @Test
59      public void testUrlWithoutQuery() throws Exception
60      {
61          MuleMessage message = setupRequestContext("http://localhost:8080/services");
62          // transforming NullPayload will make sure that no body=xxx query is added
63          message.setPayload(NullPayload.getInstance());
64  
65          ObjectToHttpClientMethodRequest transformer = createTransformer();
66          Object response = transformer.transform(message);
67  
68          assertTrue(response instanceof HttpMethod);
69          HttpMethod httpMethod = (HttpMethod) response;
70  
71          assertEquals(null, httpMethod.getQueryString());
72      }
73  
74      @Test
75      public void testUrlWithQuery() throws Exception
76      {
77          MuleMessage message = setupRequestContext("http://localhost:8080/services?method=echo");
78          // transforming NullPayload will make sure that no body=xxx query is added
79          message.setPayload(NullPayload.getInstance());
80  
81          ObjectToHttpClientMethodRequest transformer = createTransformer();
82          Object response = transformer.transform(message);
83  
84          assertTrue(response instanceof HttpMethod);
85          HttpMethod httpMethod = (HttpMethod) response;
86  
87          assertEquals("method=echo", httpMethod.getQueryString());
88      }
89  
90      @Test
91      public void testUrlWithUnescapedQuery() throws Exception
92      {
93          MuleMessage message = setupRequestContext("http://mycompany.com/test?fruits=apple%20orange");
94          // transforming NullPayload will make sure that no body=xxx query is added
95          message.setPayload(NullPayload.getInstance());
96  
97          ObjectToHttpClientMethodRequest transformer = createTransformer();
98          Object response = transformer.transform(message);
99  
100         assertTrue(response instanceof HttpMethod);
101         HttpMethod httpMethod = (HttpMethod) response;
102 
103         assertEquals("fruits=apple%20orange", httpMethod.getQueryString());
104     }
105 
106     @Test
107     public void testAppendedUrl() throws Exception
108     {
109         MuleMessage message = setupRequestContext("http://mycompany.com/test?fruits=apple%20orange");
110         // transforming a payload here will add it as body=xxx query parameter
111         message.setPayload("test");
112         message.setOutboundProperty(HttpConnector.HTTP_GET_BODY_PARAM_PROPERTY, "body");
113 
114         ObjectToHttpClientMethodRequest transformer = createTransformer();
115         Object response = transformer.transform(message);
116 
117         assertTrue(response instanceof HttpMethod);
118         HttpMethod httpMethod = (HttpMethod) response;
119 
120         assertEquals("fruits=apple%20orange&body=test", httpMethod.getQueryString());
121     }
122 
123     @Test
124     public void testEncodingOfParamValueTriggeredByMessageProperty() throws Exception
125     {
126         // the payload is already encoded, switch off encoding it in the transformer
127         String encodedPayload = "encoded%20payload";
128         MuleMessage message = setupRequestContext("http://mycompany.com/");
129         message.setOutboundProperty(HttpConnector.HTTP_ENCODE_PARAMVALUE, false);
130         message.setOutboundProperty(HttpConnector.HTTP_GET_BODY_PARAM_PROPERTY, "body");
131         message.setPayload(encodedPayload);
132 
133 
134         ObjectToHttpClientMethodRequest transformer = createTransformer();
135         Object result = transformer.transform(message);
136         
137         assertTrue(result instanceof GetMethod);
138         
139         String expected = "body=" + encodedPayload;
140         assertEquals(expected, ((GetMethod) result).getQueryString());
141     }
142 }