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