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