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