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