Search in sources :

Example 1 with SimpleHttpServer

use of org.apache.synapse.transport.utils.http.server.SimpleHttpServer in project wso2-synapse by wso2.

the class RequestResponseTest method testBackendResponse.

/**
 * Test the Source Handler respond to client.
 * Send a message to http listener and get the response from backend server.
 */
@Test
public void testBackendResponse() throws Exception {
    final SimpleHttpServer helloServer = new SimpleHttpServer();
    try {
        helloServer.start();
        PowerMockito.mockStatic(AxisEngine.class);
        PowerMockito.doAnswer(new Answer<Void>() {

            public Void answer(InvocationOnMock invocation) throws Exception {
                MessageContext axis2MessageContext = invocation.getArgument(0);
                if (axis2MessageContext.getServiceContext() == null) {
                    // request path
                    axis2MessageContext.setProperty("TransportURL", helloServer.getServerUrl());
                    inMsgContext = axis2MessageContext;
                } else {
                    // response path
                    axis2MessageContext.removeProperty("TransportURL");
                    axis2MessageContext.setTo(null);
                    axis2MessageContext.setProperty(org.apache.axis2.Constants.OUT_TRANSPORT_INFO, inMsgContext.getProperty(org.apache.axis2.Constants.OUT_TRANSPORT_INFO));
                    axis2MessageContext.getOperationContext().setProperty(org.apache.axis2.Constants.RESPONSE_WRITTEN, "SKIP");
                }
                ServiceUtils.receive(axis2MessageContext);
                return null;
            }
        }).when(AxisEngine.class, "receive", any(MessageContext.class));
        HttpClient client = new HttpClient();
        PostMethod method = new PostMethod(ServiceUtils.getServiceEndpoint("myservice", HOST, PORT));
        method.setRequestHeader("Content-Type", "application/xml");
        StringRequestEntity stringRequestEntity = new StringRequestEntity("<msg>hello Server</msg>", "application/xml", "UTF-8");
        method.setRequestEntity(stringRequestEntity);
        int responseCode = client.executeMethod(method);
        String response = method.getResponseBodyAsString();
        Assert.assertEquals("Response code mismatched", 200, responseCode);
        Assert.assertEquals("Response", "<msg>hello</msg>", response);
    } finally {
        helloServer.stop();
    }
}
Also used : Answer(org.mockito.stubbing.Answer) StringRequestEntity(org.apache.commons.httpclient.methods.StringRequestEntity) PostMethod(org.apache.commons.httpclient.methods.PostMethod) InvocationOnMock(org.mockito.invocation.InvocationOnMock) SimpleHttpServer(org.apache.synapse.transport.utils.http.server.SimpleHttpServer) HttpClient(org.apache.commons.httpclient.HttpClient) MessageContext(org.apache.axis2.context.MessageContext) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 2 with SimpleHttpServer

use of org.apache.synapse.transport.utils.http.server.SimpleHttpServer in project wso2-synapse by wso2.

the class NHttpTransportListenerTest method testBackendResponse.

/**
 * Test the Source Handler respond to client.
 * Send a message to http listener and get the response from backend server.
 */
@Test
public void testBackendResponse() throws Exception {
    final SimpleHttpServer helloServer = new SimpleHttpServer();
    try {
        helloServer.start();
        RequestURIBasedDispatcher requestURIBasedDispatcher = Mockito.mock(RequestURIBasedDispatcher.class);
        Mockito.when(requestURIBasedDispatcher.findService(any(MessageContext.class))).thenReturn(new AxisService("myservice"));
        PowerMockito.whenNew(RequestURIBasedDispatcher.class).withNoArguments().thenReturn(requestURIBasedDispatcher);
        PowerMockito.mockStatic(AxisEngine.class);
        PowerMockito.doAnswer(new Answer<Void>() {

            public Void answer(InvocationOnMock invocation) throws Exception {
                MessageContext axis2MessageContext = invocation.getArgument(0);
                if (axis2MessageContext.getServiceContext() == null) {
                    // request path
                    ServiceContext svcCtx = new ServiceContext();
                    OperationContext opCtx = new OperationContext(new InOutAxisOperation(), svcCtx);
                    axis2MessageContext.setServiceContext(svcCtx);
                    axis2MessageContext.setOperationContext(opCtx);
                    axis2MessageContext.setProperty("TransportURL", helloServer.getServerUrl());
                    inMsgContext = axis2MessageContext;
                } else {
                    // response path
                    axis2MessageContext.setTo(null);
                    axis2MessageContext.setProperty("synapse.isresponse", true);
                    axis2MessageContext.removeProperty("TransportURL");
                    axis2MessageContext.setProperty(org.apache.axis2.Constants.OUT_TRANSPORT_INFO, inMsgContext.getProperty(org.apache.axis2.Constants.OUT_TRANSPORT_INFO));
                    axis2MessageContext.getOperationContext().setProperty(org.apache.axis2.Constants.RESPONSE_WRITTEN, "SKIP");
                }
                HttpCoreNIOSender sender = new HttpCoreNIOSender();
                ConfigurationContext cfgCtx = new ConfigurationContext(new AxisConfiguration());
                sender.init(cfgCtx, new TransportOutDescription("http"));
                sender.invoke(axis2MessageContext);
                return null;
            }
        }).when(AxisEngine.class, "receive", any(MessageContext.class));
        HttpClient client = new HttpClient();
        PostMethod method = new PostMethod(ServiceUtils.getServiceEndpoint("myservice", HOST, PORT));
        method.setRequestHeader("Content-Type", "application/xml");
        StringRequestEntity stringRequestEntity = new StringRequestEntity("<msg>hello server</msg>", "application/xml", "UTF-8");
        method.setRequestEntity(stringRequestEntity);
        int responseCode = client.executeMethod(method);
        Assert.assertEquals("Response code mismatched", 200, responseCode);
        String response = method.getResponseBodyAsString();
        Assert.assertEquals("Response", "<msg>hello</msg>", response);
    } finally {
        helloServer.stop();
    }
}
Also used : OperationContext(org.apache.axis2.context.OperationContext) ConfigurationContext(org.apache.axis2.context.ConfigurationContext) AxisConfiguration(org.apache.axis2.engine.AxisConfiguration) StringRequestEntity(org.apache.commons.httpclient.methods.StringRequestEntity) RequestURIBasedDispatcher(org.apache.axis2.dispatchers.RequestURIBasedDispatcher) PostMethod(org.apache.commons.httpclient.methods.PostMethod) ServiceContext(org.apache.axis2.context.ServiceContext) AxisService(org.apache.axis2.description.AxisService) Answer(org.mockito.stubbing.Answer) InvocationOnMock(org.mockito.invocation.InvocationOnMock) SimpleHttpServer(org.apache.synapse.transport.utils.http.server.SimpleHttpServer) HttpClient(org.apache.commons.httpclient.HttpClient) MessageContext(org.apache.axis2.context.MessageContext) InOutAxisOperation(org.apache.axis2.description.InOutAxisOperation) TransportOutDescription(org.apache.axis2.description.TransportOutDescription) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

MessageContext (org.apache.axis2.context.MessageContext)2 HttpClient (org.apache.commons.httpclient.HttpClient)2 PostMethod (org.apache.commons.httpclient.methods.PostMethod)2 StringRequestEntity (org.apache.commons.httpclient.methods.StringRequestEntity)2 SimpleHttpServer (org.apache.synapse.transport.utils.http.server.SimpleHttpServer)2 Test (org.junit.Test)2 InvocationOnMock (org.mockito.invocation.InvocationOnMock)2 Answer (org.mockito.stubbing.Answer)2 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)2 ConfigurationContext (org.apache.axis2.context.ConfigurationContext)1 OperationContext (org.apache.axis2.context.OperationContext)1 ServiceContext (org.apache.axis2.context.ServiceContext)1 AxisService (org.apache.axis2.description.AxisService)1 InOutAxisOperation (org.apache.axis2.description.InOutAxisOperation)1 TransportOutDescription (org.apache.axis2.description.TransportOutDescription)1 RequestURIBasedDispatcher (org.apache.axis2.dispatchers.RequestURIBasedDispatcher)1 AxisConfiguration (org.apache.axis2.engine.AxisConfiguration)1