1   /*
2    * $Id: ObjectToHttpClientMethodRequestTestCase.java 12100 2008-06-19 08:58:48Z rossmason $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.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 org.mule.RequestContext;
14  import org.mule.api.MuleEvent;
15  import org.mule.api.MuleMessage;
16  import org.mule.api.config.MuleProperties;
17  import org.mule.api.transformer.TransformerException;
18  import org.mule.tck.AbstractMuleTestCase;
19  import org.mule.transport.NullPayload;
20  import org.mule.transport.http.HttpConnector;
21  import org.mule.transport.http.HttpConstants;
22  
23  import org.apache.commons.httpclient.HttpMethod;
24  
25  public class ObjectToHttpClientMethodRequestTestCase extends AbstractMuleTestCase
26  {
27      
28      private MuleMessage setupRequestContext(String url) throws Exception
29      {
30          MuleEvent event = getTestEvent("test");
31          MuleMessage message = event.getMessage();
32          message.setStringProperty(HttpConnector.HTTP_METHOD_PROPERTY, HttpConstants.METHOD_GET);
33          message.setStringProperty(MuleProperties.MULE_ENDPOINT_PROPERTY, url);
34          RequestContext.setEvent(event);
35          
36          return message;
37      }
38  
39      // @Override
40      protected void doTearDown() throws Exception
41      {
42          RequestContext.setEvent(null);
43      }
44  
45      public void testUrlWithoutQuery() throws Exception
46      {
47          MuleMessage message = setupRequestContext("http://localhost:8080/services");
48          // transforming NullPayload will make sure that no body=xxx query is added
49          message.setPayload(NullPayload.getInstance());
50  
51          ObjectToHttpClientMethodRequest transformer = new ObjectToHttpClientMethodRequest();
52          Object response = transformer.transform(message);
53          
54          assertTrue(response instanceof HttpMethod);
55          HttpMethod httpMethod = (HttpMethod) response;
56          
57          assertEquals(null, httpMethod.getQueryString());
58      }
59      
60      public void testUrlWithQuery() throws Exception
61      {
62          MuleMessage message = setupRequestContext("http://localhost:8080/services?method=echo");
63          // transforming NullPayload will make sure that no body=xxx query is added
64          message.setPayload(NullPayload.getInstance());
65          
66          ObjectToHttpClientMethodRequest transformer = new ObjectToHttpClientMethodRequest();
67          Object response = transformer.transform(message);
68          
69          assertTrue(response instanceof HttpMethod);
70          HttpMethod httpMethod = (HttpMethod) response;
71          
72          assertEquals("method=echo", httpMethod.getQueryString());
73      }
74  
75      public void testUrlWithUnescapedQuery() throws Exception
76      {
77          MuleMessage message = setupRequestContext("http://mycompany.com/test?fruits=apple%20orange");
78          // transforming NullPayload will make sure that no body=xxx query is added
79          message.setPayload(NullPayload.getInstance());
80          
81          ObjectToHttpClientMethodRequest transformer = new ObjectToHttpClientMethodRequest();
82          Object response = transformer.transform(message);
83          
84          assertTrue(response instanceof HttpMethod);
85          HttpMethod httpMethod = (HttpMethod) response;
86          
87          assertEquals("fruits=apple%20orange", httpMethod.getQueryString());
88      }
89      
90      public void testAppendedUrl() throws Exception
91      {
92          MuleMessage message = setupRequestContext("http://mycompany.com/test?fruits=apple%20orange");
93          // transforming a payload here will add it as body=xxx query parameter
94          message.setPayload("test");
95          
96          ObjectToHttpClientMethodRequest transformer = new ObjectToHttpClientMethodRequest();
97          Object response = transformer.transform(message);
98          
99          assertTrue(response instanceof HttpMethod);
100         HttpMethod httpMethod = (HttpMethod) response;
101         
102         assertEquals("fruits=apple%20orange&body=test", httpMethod.getQueryString());
103     }
104 
105     public void testAppendedUrlWithExpressions() throws Exception
106     {
107         MuleMessage message = setupRequestContext("http://mycompany.com/test?fruits=${header:fruit1},${header:fruit2}&correlationID=${message:correlationId}");
108         // transforming a payload here will add it as body=xxx query parameter
109         message.setPayload(NullPayload.getInstance());
110         message.setCorrelationId("1234");
111         message.setProperty("fruit1", "apple");
112         message.setProperty("fruit2", "orange");
113         ObjectToHttpClientMethodRequest transformer = new ObjectToHttpClientMethodRequest();
114         Object response = transformer.transform(message);
115 
116         assertTrue(response instanceof HttpMethod);
117         HttpMethod httpMethod = (HttpMethod) response;
118 
119         assertEquals("fruits=apple,orange&correlationID=1234", httpMethod.getQueryString());
120     }
121 
122     public void testAppendedUrlWithBadExpressions() throws Exception
123     {
124         MuleMessage message = setupRequestContext("http://mycompany.com/test?param=${foo:bar}");
125         // transforming a payload here will add it as body=xxx query parameter
126         message.setPayload(NullPayload.getInstance());
127         ObjectToHttpClientMethodRequest transformer = new ObjectToHttpClientMethodRequest();
128         Object response = null;
129         try
130         {
131             response = transformer.transform(message);
132             fail("unknown evaluator was used");
133         }
134         catch (TransformerException e)
135         {
136             //Expected
137         }
138 
139         message = setupRequestContext("http://mycompany.com/test?param=${header:bar}");
140         // transforming a payload here will add it as body=xxx query parameter
141         message.setPayload(NullPayload.getInstance());
142         try
143         {
144             response = transformer.transform(message);
145             fail("Header 'bar' not set on the message");
146         }
147         catch (TransformerException e)
148         {
149             //Expected
150         }
151 
152     }
153 }
154 
155