View Javadoc

1   /*
2    * $Id: HttpMessageReceiverMule4456TestCase.java 19840 2010-10-05 18:32:45Z 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.issues;
12  
13  import org.mule.api.MuleEventContext;
14  import org.mule.api.MuleMessage;
15  import org.mule.module.client.MuleClient;
16  import org.mule.tck.DynamicPortTestCase;
17  import org.mule.tck.functional.EventCallback;
18  import org.mule.tck.functional.FunctionalTestComponent;
19  
20  import org.apache.commons.httpclient.HttpClient;
21  import org.apache.commons.httpclient.HttpVersion;
22  import org.apache.commons.httpclient.methods.PostMethod;
23  import org.apache.commons.httpclient.methods.RequestEntity;
24  import org.apache.commons.httpclient.methods.StringRequestEntity;
25  import org.apache.commons.httpclient.params.HttpClientParams;
26  
27  public class HttpMessageReceiverMule4456TestCase extends DynamicPortTestCase
28  {
29      private static final String MESSAGE = "test message";
30  
31      private HttpClient httpClient;
32      private MuleClient muleClient;
33  
34      @Override
35      protected boolean isGracefulShutdown()
36      {
37          return true;
38      }
39  
40      @Override
41      protected void doSetUp() throws Exception
42      {
43          super.doSetUp();
44          HttpClientParams params = new HttpClientParams();
45          params.setVersion(HttpVersion.HTTP_1_1);
46          httpClient = new HttpClient(params);
47          muleClient = new MuleClient(muleContext);
48      }
49  
50      @Override
51      protected String getConfigResources()
52      {
53          return "http-receiver-mule4456-config.xml";
54      }
55  
56      public void testAsyncPost() throws Exception
57      {
58          FunctionalTestComponent component = getFunctionalTestComponent("AsyncService");
59          component.setEventCallback(new EventCallback()
60          {
61              public void eventReceived(MuleEventContext context, Object comp) throws Exception
62              {
63                  Thread.sleep(200);
64                  context.getMessageAsString();
65              }
66          });
67  
68          PostMethod request = new PostMethod("http://localhost:" + getPorts().get(0));
69          RequestEntity entity = new StringRequestEntity(MESSAGE, "text/plain", 
70              muleContext.getConfiguration().getDefaultEncoding());
71          request.setRequestEntity(entity);
72          httpClient.executeMethod(request);
73          
74          MuleMessage message = muleClient.request("vm://out", 1000);
75          assertNotNull(message);
76          assertEquals(MESSAGE, message.getPayloadAsString());
77      }
78  
79      public void testAsyncPostWithPersistentSedaQueue() throws Exception
80      {
81          FunctionalTestComponent component = getFunctionalTestComponent("AsyncPersistentQueueService");
82          component.setEventCallback(new EventCallback()
83          {
84              public void eventReceived(MuleEventContext context, Object comp) throws Exception
85              {
86                  Thread.sleep(200);
87                  context.getMessageAsString();
88              }
89          });
90  
91          PostMethod request = new PostMethod("http://localhost:" + getPorts().get(1));
92          RequestEntity entity = new StringRequestEntity(MESSAGE, "text/plain", muleContext.getConfiguration()
93              .getDefaultEncoding());
94          request.setRequestEntity(entity);
95  
96          httpClient.executeMethod(request);
97          MuleMessage message = muleClient.request("vm://out", 1000);
98          assertNotNull(message);
99          assertEquals(MESSAGE, message.getPayloadAsString());
100     }
101 
102     @Override
103     protected int getNumPortsToFind()
104     {
105         return 2;
106     }
107 }