1
2
3
4
5
6
7
8
9
10
11 package org.mule.test.integration.construct;
12
13 import org.mule.api.MuleMessage;
14 import org.mule.module.client.MuleClient;
15 import org.mule.tck.AbstractServiceAndFlowTestCase;
16 import org.mule.tck.junit4.rule.DynamicPort;
17 import org.mule.util.StringUtils;
18
19 import java.util.Arrays;
20 import java.util.Collection;
21 import java.util.Collections;
22 import java.util.HashMap;
23 import java.util.Map;
24
25 import org.junit.Rule;
26 import org.junit.Test;
27 import org.junit.runners.Parameterized.Parameters;
28
29 import static org.junit.Assert.assertEquals;
30
31 public class HttpProxyTestCase extends AbstractServiceAndFlowTestCase
32 {
33 @Rule
34 public DynamicPort port1 = new DynamicPort("port1");
35
36 @Rule
37 public DynamicPort port2 = new DynamicPort("port2");
38
39 public HttpProxyTestCase(ConfigVariant variant, String configResources)
40 {
41 super(variant, configResources);
42
43 }
44
45 @Parameters
46 public static Collection<Object[]> parameters()
47 {
48 return Arrays.asList(new Object[][]{{ConfigVariant.SERVICE,
49 "org/mule/test/integration/construct/http-proxy-config.xml"}
50
51 });
52 }
53
54 private MuleClient muleClient;
55
56 @Override
57 protected void doSetUp() throws Exception
58 {
59 super.doSetUp();
60 muleClient = new MuleClient(muleContext);
61 }
62
63 @Test
64 public void testDirect() throws Exception
65 {
66 testDirectRequest(0);
67 }
68
69 @Test
70 public void testEndpointChildren() throws Exception
71 {
72 testDirectRequest(1);
73 }
74
75 @Test
76 public void testExceptionStrategy() throws Exception
77 {
78 testDirectRequest(2);
79 }
80
81 @Test
82 public void testTransforming() throws Exception
83 {
84 testRequest(3, "fooinbarout");
85 }
86
87 @Test
88 public void testInheritance() throws Exception
89 {
90 testRequest(4, "fooinbarout");
91 }
92
93 @Test
94 public void testDynamicAddress() throws Exception
95 {
96 testExtraHeadersRequest(5, Collections.singletonMap("proxyTarget", "bar-appender"));
97 }
98
99 @SuppressWarnings("unchecked")
100 @Test
101 public void testPathExtensions() throws Exception
102 {
103 testRequest(1, "/extension", "foobar", Collections.EMPTY_MAP);
104 testRequest(1, "?name=value", "foobar", Collections.EMPTY_MAP);
105 testRequest(1, "/other?name=value", "foobar", Collections.EMPTY_MAP);
106 }
107
108 private void testDirectRequest(final int proxyId) throws Exception
109 {
110 testRequest(proxyId, "foobar");
111 }
112
113 private void testExtraHeadersRequest(final int proxyId, final Map<String, String> extraHeaders)
114 throws Exception
115 {
116 testRequest(proxyId, "foobar", extraHeaders);
117 }
118
119 @SuppressWarnings("unchecked")
120 private void testRequest(final int proxyId, final String expectedResult) throws Exception
121 {
122 testRequest(proxyId, expectedResult, Collections.EMPTY_MAP);
123 }
124
125 private void testRequest(final int proxyId,
126 final String expectedResult,
127 final Map<String, String> extraHeaders) throws Exception
128 {
129 testRequest(proxyId, StringUtils.EMPTY, expectedResult, extraHeaders);
130 }
131
132 private void testRequest(final int proxyId,
133 final String pathExtension,
134 final String expectedResult,
135 final Map<String, String> extraHeaders) throws Exception
136 {
137 final Map<String, String> headers = new HashMap<String, String>(Collections.singletonMap(
138 "X-Custom-Header", "w00t"));
139 headers.putAll(extraHeaders);
140
141 final MuleMessage result = muleClient.send("http://localhost:" + port1.getNumber() + "/http-proxy/"
142 + proxyId + pathExtension, "foo", headers,
143 getTestTimeoutSecs() * 1000);
144 assertEquals(expectedResult, result.getPayloadAsString());
145
146 final int contentLength = getContentLength(result);
147 assertEquals(expectedResult.length(), contentLength);
148
149 assertEquals("w00tbaz", result.getInboundProperty("X-Custom-Header-Response"));
150 assertEquals("/bar-appender" + pathExtension, result.getInboundProperty("X-Actual-Request-Path"));
151 }
152
153 private int getContentLength(final MuleMessage result)
154 {
155 final Object messageProperty = result.getInboundProperty("Content-Length");
156 return Integer.parseInt(messageProperty.toString());
157 }
158 }