1
2
3
4
5
6
7 package org.mule.transport.http.functional;
8
9 import org.mule.api.MuleEventContext;
10 import org.mule.api.MuleMessage;
11 import org.mule.tck.functional.EventCallback;
12 import org.mule.tck.functional.FunctionalTestComponent;
13 import org.mule.tck.junit4.FunctionalTestCase;
14 import org.mule.tck.junit4.rule.DynamicPort;
15 import org.mule.transport.http.HttpConstants;
16
17 import edu.emory.mathcs.backport.java.util.concurrent.CountDownLatch;
18 import edu.emory.mathcs.backport.java.util.concurrent.TimeUnit;
19 import org.apache.commons.httpclient.HttpClient;
20 import org.apache.commons.httpclient.HttpMethod;
21 import org.apache.commons.httpclient.HttpStatus;
22 import org.apache.commons.httpclient.NameValuePair;
23 import org.apache.commons.httpclient.methods.GetMethod;
24 import org.apache.commons.httpclient.methods.PostMethod;
25 import org.junit.Rule;
26 import org.junit.Test;
27
28 import static org.junit.Assert.assertEquals;
29 import static org.junit.Assert.assertNotNull;
30 import static org.junit.Assert.assertTrue;
31 import static org.junit.Assert.fail;
32
33 public class HttpPersistentQueueTestCase extends FunctionalTestCase
34 {
35 private CountDownLatch messageDidArrive = new CountDownLatch(1);
36 private int port = -1;
37
38 @Rule
39 public DynamicPort dynamicPort = new DynamicPort("port1");
40
41 @Override
42 protected String getConfigResources()
43 {
44 return "http-persistent-queue.xml";
45 }
46
47 @Override
48 protected void doSetUp() throws Exception
49 {
50 super.doSetUp();
51
52 FunctionalTestComponent testComponent = (FunctionalTestComponent) getComponent("PersistentQueueAsync");
53 assertNotNull(testComponent);
54 testComponent.setEventCallback(new Callback(messageDidArrive));
55 port = dynamicPort.getNumber();
56 }
57
58 @Test
59 public void testPersistentMessageDeliveryWithGet() throws Exception
60 {
61 GetMethod method = new GetMethod("http://localhost:" + port + "/services/Echo?foo=bar");
62 method.addRequestHeader(HttpConstants.HEADER_CONNECTION, "close");
63 doTestPersistentMessageDelivery(method);
64 }
65
66 @Test
67 public void testPersistentMessageDeliveryWithPost() throws Exception
68 {
69 PostMethod method = new PostMethod("http://localhost:" + port + "/services/Echo");
70 method.addRequestHeader(HttpConstants.HEADER_CONNECTION, "close");
71 method.addParameter(new NameValuePair("foo", "bar"));
72 doTestPersistentMessageDelivery(method);
73 }
74
75 private void doTestPersistentMessageDelivery(HttpMethod httpMethod) throws Exception
76 {
77 HttpClient client = new HttpClient();
78 int rc = client.executeMethod(httpMethod);
79
80 assertEquals(HttpStatus.SC_OK, rc);
81 assertTrue(messageDidArrive.await(RECEIVE_TIMEOUT, TimeUnit.MILLISECONDS));
82 }
83
84 private static class Callback implements EventCallback
85 {
86 private CountDownLatch messageDidArrive;
87
88 public Callback(CountDownLatch latch)
89 {
90 super();
91 messageDidArrive = latch;
92 }
93
94 public void eventReceived(MuleEventContext context, Object component) throws Exception
95 {
96 MuleMessage message = context.getMessage();
97
98 Object httpMethod = message.getInboundProperty("http.method");
99 if (HttpConstants.METHOD_GET.equals(httpMethod))
100 {
101 assertEquals("/services/Echo?foo=bar", message.getPayloadAsString());
102 }
103 else if (HttpConstants.METHOD_POST.equals(httpMethod))
104 {
105 assertEquals("foo=bar", message.getPayloadAsString());
106 }
107 else
108 {
109 fail("invalid HTTP method : " + httpMethod);
110 }
111
112 assertEquals("true", message.getInboundProperty(HttpConstants.HEADER_CONNECTION));
113 assertEquals("true", message.getInboundProperty(HttpConstants.HEADER_KEEP_ALIVE));
114
115 messageDidArrive.countDown();
116 }
117 }
118
119 }