1
2
3
4
5
6
7 package org.mule.transport.soap.axis;
8
9 import org.mule.api.MuleMessage;
10 import org.mule.api.config.MuleProperties;
11 import org.mule.api.exception.MessagingExceptionHandler;
12 import org.mule.api.transport.PropertyScope;
13 import org.mule.module.client.MuleClient;
14 import org.mule.tck.junit4.FunctionalTestCase;
15 import org.mule.tck.junit4.rule.DynamicPort;
16 import org.mule.transport.http.HttpConnector;
17 import org.mule.transport.servlet.MuleReceiverServlet;
18
19 import java.util.HashMap;
20 import java.util.Map;
21
22 import javax.servlet.ServletContextEvent;
23 import javax.servlet.ServletContextListener;
24
25 import org.junit.Rule;
26 import org.junit.Test;
27 import org.mortbay.jetty.Server;
28 import org.mortbay.jetty.servlet.Context;
29 import org.mortbay.jetty.servlet.ServletHolder;
30
31 import static org.junit.Assert.assertEquals;
32 import static org.junit.Assert.assertNotNull;
33 import static org.junit.Assert.assertTrue;
34
35 public class AxisServletWithSecurityTestCase extends FunctionalTestCase
36 {
37
38 public static int HTTP_PORT = -1;
39
40 private Server httpServer;
41
42 @Rule
43 public DynamicPort dynamicPort = new DynamicPort("port1");
44
45 @Override
46 protected String getConfigResources()
47 {
48 return "axis-servlet-security-config.xml";
49 }
50
51 @Override
52 protected void doSetUp() throws Exception
53 {
54 HTTP_PORT = dynamicPort.getNumber();
55 httpServer = new Server(HTTP_PORT);
56
57 Context c = new Context(httpServer, "/", Context.SESSIONS);
58 c.addServlet(new ServletHolder(new MuleReceiverServlet()), "/services/*");
59 c.addEventListener(new ServletContextListener() {
60 public void contextInitialized(ServletContextEvent sce)
61 {
62 sce.getServletContext().setAttribute(MuleProperties.MULE_CONTEXT_PROPERTY, muleContext);
63 }
64
65 public void contextDestroyed(ServletContextEvent sce) { }
66 });
67
68 httpServer.start();
69 }
70
71 @Override
72 protected void doTearDown() throws Exception
73 {
74 if (httpServer != null)
75 {
76 httpServer.stop();
77 httpServer.destroy();
78 }
79 }
80
81 @Test
82 public void testSecurityWithServletsUsingGet() throws Exception
83 {
84 Map<String, Object> props = new HashMap<String, Object>();
85 props.put(HttpConnector.HTTP_METHOD_PROPERTY, "GET");
86 MuleClient client = new MuleClient(muleContext);
87 MuleMessage result = client.send("http://ross:ross@localhost:" + HTTP_PORT
88 + "/services/mycomponent?method=echo", "test", props);
89
90 String status = result.getProperty(HttpConnector.HTTP_STATUS_PROPERTY, PropertyScope.INBOUND);
91 assertEquals(401, new Integer(status).intValue());
92
93 MessagingExceptionHandler exceptionListener =
94 muleContext.getRegistry().lookupService("mycomponent").getExceptionListener();
95 assertTrue(exceptionListener instanceof UnitTestExceptionStrategy);
96
97 UnitTestExceptionStrategy utExStrat = (UnitTestExceptionStrategy)exceptionListener;
98 assertEquals(1, utExStrat.getMessagingExceptions().size());
99
100 assertNotNull(result);
101
102 }
103
104 }