Built-in Java HTTPS server for integration testing
I am writing integration tests for a Mule ESB application that connects to an external API over HTTPS. I would like to poke fun at the external API and perform a complex integration test during Maven build. My question is about setting up an embedded HTTPS server. I tried to use Jersey, but it only provides HTTP. I have looked at this example
https://github.com/jersey/jersey/tree/master/examples/https-clientserver-grizzly
and I was wondering if there is still some manual step, or if it automatically configures everything on every build run.
Any suggestions or ideas?
Edit. My ultimate goal is to deploy a JAX-RS service to an embedded server accessible over HTTPS. And it doesn't require client keys / certificate configuration.
Edit 2. Now my problem is about certificates. The point of integration tests is to mock external components and check if my application is working. Now if I have configured the built-in HTTPS server and used the built-in cmd certificate, I need to add the SSL configuration in the client (as @Ryan Hoegg pointed out). This is not what I would ideally want: is there a solution to make it work without changing my client application code? This should be a general Java question that is outside the scope of Mule.
source to share
I am using confluex-mock-http for this. It uses Jetty under the hood, but it does all the configuration for you. Creating a MockHttpsServer starts the HTTPS server immediately:
public static final int PORT = 1443;
private MockHttpsServer mockServer;
@Before
public void initHttps() {
mockServer = new MockHttpsServer(PORT);
mockServer.respondTo(get("a-service/resource")).withBody(expectedResponse);
}
You can configure the Mule application to trust the certificate used by the mock server. A reliable JKS store is available in the classpath and you can provide it for your HTTPS connector like this:
<https:connector name="someHttpsConnector">
<https:tls-server path="confluex-mock.truststore" storePassword="confluex" />
</https:connector>
I think some older versions of mule have problems with this approach because I needed to use a workaround here .
EDIT: This configuration can only be enabled for the HTTPS connector when tests are run using the Spring profile :
<spring:beans profile="test">
<mule>
<!-- connector configuration goes here -->
</mule>
</spring:beans>
One way to make sure the profile is indeed active when the tests run is to simply set it in the @BeforeClass method:
@BeforeClass
public void initEnvironment() {
System.setProperty("spring.profiles.active", "test");
}
source to share
Why don't you just use Mule for this task?
Create an additional XML Mule file that exposes the JAX-RS service over HTTPS. The idea is to not include this in production code (i.e. link to it from mule-context.xml), but rather load it from a test case.
You can send it as a configuration side-by-side with production configuration in the FunctionalTestCase class .
Your service layout might look something like this (excluding the JAX-RS annotated bean that provides the actual course implementation).
<flow name="mock-service">
<https:inbound-endpoint exchange-pattern="request-response" host="localhost" port="8085" path="mockapi"/>
<jersey:resources>
<component>
<spring-object bean="mockApiBean"/>
</component>
</jersey:resources>
</flow>
source to share