My mule configurations files often contain proxy settings for connectors that are used to communicate to the outside world. However during development and testing I can’t use the normal proxy, since i’m mocking the outside world.
Littleproxy is a java proxy that you can use to replace the real proxy with a testing proxy. This avoids you having to install a proxy on your development machine and on the continuous integration server.
The following is part of a mule config. It defines an http connector that is configured to talk through a proxy.
<http:connector name="http.connector"/>
<http:connector name="http.connector.proxy"
proxyHostname="localhost" proxyPort="50103"/>
<flow name="http.gateway.and.proxy">
<http:inbound-endpoint host="localhost" port="50102"
path="gateway.in"
exchange-pattern="request-response"
connector-ref="http.connector">
</http:inbound-endpoint>
<logger message="http.gateway - #[groovy:message.toString()]"/>
<http:outbound-endpoint host="localhost" port="50100"
path="dummy.service"
connector-ref="http.connector.proxy">
</http:outbound-endpoint>
</flow>
Include little proxy in your maven pom.xml:
<dependency>
<groupId>org.littleshoot</groupId>
<artifactId>littleproxy</artifactId>
<version>0.4</version>
</dependency>
Here’s the unit test. First i start the proxy (you could also use @Before or @BeforeClass), and then i run a regular mule unit test:
public void testShouldReturnHelloWorldThroughGatewayAndProxy()
throws Exception {
String msg = "Hi!";
HttpProxyServer proxyServer = new DefaultHttpProxyServer(50103);
proxyServer.start();
/*
* send message
*/
MuleClient client = new MuleClient(muleContext);
MuleMessage result = client.send(
"http://localhost:50102/gateway.in?connector=http.connector"
, msg, null);
String payload = result.getPayloadAsString();
proxyServer.stop();
Assert.assertTrue("Did not receive HelloWorld!: " + payload, "HelloWorld!".equals(payload));
}