1   /*
2    * $Id: ObjectToHttpClientMethodRequestTestCase.java 11728 2008-05-13 07:31:11Z dirk.olmes $
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.providers.http.transformers;
12  
13  import org.mule.config.MuleProperties;
14  import org.mule.impl.RequestContext;
15  import org.mule.providers.NullPayload;
16  import org.mule.providers.http.HttpConnector;
17  import org.mule.providers.http.HttpConstants;
18  import org.mule.tck.AbstractMuleTestCase;
19  import org.mule.umo.UMOEvent;
20  
21  import org.apache.commons.httpclient.HttpMethod;
22  
23  public class ObjectToHttpClientMethodRequestTestCase extends AbstractMuleTestCase
24  {
25      
26      private void setupRequestContext(String url) throws Exception
27      {
28          UMOEvent event = getTestEvent("test");
29          event.getMessage().setStringProperty(HttpConnector.HTTP_METHOD_PROPERTY, HttpConstants.METHOD_GET);
30          event.getMessage().setStringProperty(MuleProperties.MULE_ENDPOINT_PROPERTY, url);
31          RequestContext.setEvent(event);
32      }
33  
34      // @Override
35      protected void doTearDown() throws Exception
36      {
37          RequestContext.setEvent(null);
38      }
39  
40      public void testUrlWithoutQuery() throws Exception
41      {
42          setupRequestContext("http://localhost:8080/services");
43  
44          ObjectToHttpClientMethodRequest transformer = new ObjectToHttpClientMethodRequest();
45          // transforming NullPayload will make sure that no body=xxx query is added
46          Object response = transformer.transform(NullPayload.getInstance());
47          
48          assertTrue(response instanceof HttpMethod);
49          HttpMethod httpMethod = (HttpMethod) response;
50          
51          assertEquals(null, httpMethod.getQueryString());
52      }
53      
54      public void testUrlWithQuery() throws Exception
55      {
56          setupRequestContext("http://localhost:8080/services?method=echo");
57          
58          ObjectToHttpClientMethodRequest transformer = new ObjectToHttpClientMethodRequest();
59          // transforming NullPayload will make sure that no body=xxx query is added
60          Object response = transformer.transform(NullPayload.getInstance());
61          
62          assertTrue(response instanceof HttpMethod);
63          HttpMethod httpMethod = (HttpMethod) response;
64          
65          assertEquals("method=echo", httpMethod.getQueryString());
66      }
67  
68      public void testUrlWithUnescapedQuery() throws Exception
69      {
70          setupRequestContext("http://mycompany.com/test?fruits=apple%20orange");
71          
72          ObjectToHttpClientMethodRequest transformer = new ObjectToHttpClientMethodRequest();
73          // transforming NullPayload will make sure that no body=xxx query is added
74          Object response = transformer.transform(NullPayload.getInstance());
75          
76          assertTrue(response instanceof HttpMethod);
77          HttpMethod httpMethod = (HttpMethod) response;
78          
79          assertEquals("fruits=apple%20orange", httpMethod.getQueryString());
80      }
81      
82      public void testAppendedUrl() throws Exception
83      {
84          setupRequestContext("http://mycompany.com/test?fruits=apple%20orange");
85          
86          ObjectToHttpClientMethodRequest transformer = new ObjectToHttpClientMethodRequest();
87          // transforming a payload here will add it as body=xxx query parameter
88          Object response = transformer.transform("test");
89          
90          assertTrue(response instanceof HttpMethod);
91          HttpMethod httpMethod = (HttpMethod) response;
92          
93          assertEquals("fruits=apple%20orange&body=test", httpMethod.getQueryString());
94      }
95  }
96  
97