This site contains the documentation that is relevant to older WSO2 product versions and offerings.
For the latest WSO2 documentation, visit https://wso2.com/documentation/.

Using SimpleHttpServer in Integration Tests

SimpleHttpServer can be used to intercept messages from carbon servers to backend servers. A sample test case using SimpleHttpServer for message interception is shown below:

public class POXOverServletTransportTestCase extends ESBIntegrationTest {

     private ServerConfigurationManager serverConfigurationManager;

     @BeforeClass(alwaysRun = true)
    public void uploadSynapseConfig() throws Exception {
        super.init();
        serverConfigurationManager = new ServerConfigurationManager(esbContext.getContextUrls().getBackEndUrl());
        serverConfigurationManager.applyConfiguration(new File(getESBResourceLocation() + File.separator
                + "synapseconfig" + File.separator + "servletTransport" + File.separator + "pox_servlet_transport_axis2.xml"));
        esbContext.login(); //login again after server restart
        loadESBConfigurationFrom(File.separator + "artifacts" + File.separator + "ESB"
                + File.separator + "synapseconfig" + File.separator + "servletTransport" + File.separator + "soap_2_pox.xml");
    }

     @SetEnvironment(executionEnvironments = {ExecutionEnvironment.STANDALONE})
    @Test(groups = "wso2.esb", description = "Tests SOAP to POX Conversion")
    public void testSoapToPOXConversion() throws IOException, InterruptedException, XPathExpressionException {
        SimpleHttpServer httpServer = new SimpleHttpServer();
        try {
            httpServer.start();
            Thread.sleep(5000);
        } catch (IOException e) {
            log.error("Error while starting the HTTP server", e);
        }
        TestRequestInterceptor interceptor = new TestRequestInterceptor();
        httpServer.getRequestHandler().setInterceptor(interceptor);
        OMElement response = axis2Client.sendSimpleStockQuoteRequest(
                getProxyServiceURLHttp("SOAP2POX"),
                null, "WSO2");
        log.info("Response received: " + response);
        Assert.assertEquals(interceptor.getLastRequestURI(), "/services/SimpleStockQuoteService");
        try {
            httpServer.stop();
        } catch (IOException e) {
            log.warn("Error while shutting down the HTTP server", e);
        }
    }

     @AfterMethod(alwaysRun = true)
    public void cleanUp() throws Exception {
        try {
            super.cleanup();
        } finally {
            Thread.sleep(3000);
            serverConfigurationManager.restoreToLastConfiguration();
        }
    }

     private static class TestRequestInterceptor implements RequestInterceptor {
        private String lastRequestURI;

         public void requestReceived(HttpRequest request) {
            lastRequestURI = request.getRequestLine().getUri();
        }

         public String getLastRequestURI() {
            return lastRequestURI;
        }
    }
}