1
2
3
4
5
6
7
8
9
10 package org.mule.module.cxf;
11
12 import static org.junit.Assert.assertEquals;
13
14 import org.mule.api.MuleMessage;
15 import org.mule.module.client.MuleClient;
16 import org.mule.tck.AbstractServiceAndFlowTestCase;
17 import org.mule.tck.junit4.rule.DynamicPort;
18 import org.mule.transport.http.HttpConnector;
19
20 import java.util.Arrays;
21 import java.util.Collection;
22
23 import org.apache.commons.httpclient.Credentials;
24 import org.apache.commons.httpclient.HttpClient;
25 import org.apache.commons.httpclient.UsernamePasswordCredentials;
26 import org.apache.commons.httpclient.auth.AuthScope;
27 import org.apache.commons.httpclient.methods.PostMethod;
28 import org.apache.commons.httpclient.methods.StringRequestEntity;
29 import org.junit.Rule;
30 import org.junit.Test;
31 import org.junit.runners.Parameterized.Parameters;
32
33
34 public class HttpSecurityTestCase extends AbstractServiceAndFlowTestCase
35 {
36
37 private static String soapRequest =
38 "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:unk=\"http://unknown.namespace/\">" +
39 "<soapenv:Header/>" +
40 "<soapenv:Body>" +
41 "<unk:echo>" +
42 "<arg0>asdf</arg0>" +
43 "</unk:echo>" +
44 "</soapenv:Body>" +
45 "</soapenv:Envelope>";
46
47 @Rule
48 public DynamicPort dynamicPort1 = new DynamicPort("port1");
49
50 @Rule
51 public DynamicPort dynamicPort2 = new DynamicPort("port2");
52
53 public HttpSecurityTestCase(ConfigVariant variant, String configResources)
54 {
55 super(variant, configResources);
56 }
57
58 @Parameters
59 public static Collection<Object[]> parameters()
60 {
61 return Arrays.asList(new Object[][]{
62 {ConfigVariant.SERVICE, "http-security-conf-service.xml"},
63 {ConfigVariant.FLOW, "http-security-conf-flow.xml"}
64 });
65 }
66
67
68
69
70
71 @Test
72 public void testBasicAuth() throws Exception
73 {
74 HttpClient client = new HttpClient();
75 Credentials credentials = new UsernamePasswordCredentials("admin", "admin");
76 client.getState().setCredentials(AuthScope.ANY, credentials);
77 client.getParams().setAuthenticationPreemptive(true);
78
79 PostMethod method = new PostMethod("https://localhost:" + dynamicPort2.getNumber() + "/services/Echo");
80 method.setDoAuthentication(true);
81 StringRequestEntity requestEntity = new StringRequestEntity(soapRequest, "text/plain", "UTF-8");
82 method.setRequestEntity(requestEntity);
83
84 int result = client.executeMethod(method);
85
86 assertEquals(200, result);
87 System.out.println(method.getResponseBodyAsString());
88
89 credentials = new UsernamePasswordCredentials("admin", "adminasd");
90 client.getState().setCredentials(AuthScope.ANY, credentials);
91 client.getParams().setAuthenticationPreemptive(true);
92
93 result = client.executeMethod(method);
94
95 assertEquals(401, result);
96 }
97
98 @Test
99 public void testBasicAuthWithCxfClient() throws Exception
100 {
101 MuleClient client = new MuleClient(muleContext);
102
103 MuleMessage result = client.send("cxf:http://admin:admin@localhost:" + dynamicPort1.getNumber() + "/services/Echo?method=echo", "Hello", null);
104
105 final int status = result.getInboundProperty(HttpConnector.HTTP_STATUS_PROPERTY, 0);
106 assertEquals(200, status);
107 }
108
109 }