1
2
3
4
5
6
7 package org.mule.module.spring.security;
8
9 import org.mule.tck.junit4.FunctionalTestCase;
10
11 import org.apache.commons.httpclient.Credentials;
12 import org.apache.commons.httpclient.HttpClient;
13 import org.apache.commons.httpclient.HttpStatus;
14 import org.apache.commons.httpclient.UsernamePasswordCredentials;
15 import org.apache.commons.httpclient.auth.AuthScope;
16 import org.apache.commons.httpclient.methods.PostMethod;
17 import org.apache.commons.httpclient.methods.StringRequestEntity;
18 import org.junit.Test;
19
20 import static org.junit.Assert.assertEquals;
21
22 public class AuthenticationAgainstMultipleProvidersTestCase extends FunctionalTestCase
23 {
24
25 @Override
26 protected String getConfigResources()
27 {
28 return "mule-multiple-providers-config.xml";
29 }
30
31 @Test
32 public void testProvider1() throws Exception
33 {
34 HttpClient httpClient = new HttpClient();
35 Credentials credentials = new UsernamePasswordCredentials("admin1", "admin1");
36 httpClient.getState().setCredentials(AuthScope.ANY, credentials);
37 httpClient.getParams().setAuthenticationPreemptive(true);
38
39 PostMethod postMethod = new PostMethod("http://localhost:4445");
40 postMethod.setDoAuthentication(true);
41 postMethod.setRequestEntity(new StringRequestEntity("hello", "text/html", "UTF-8"));
42
43 assertEquals(HttpStatus.SC_OK, httpClient.executeMethod(postMethod));
44 assertEquals("hello", postMethod.getResponseBodyAsString());
45
46 credentials = new UsernamePasswordCredentials("asdf", "asdf");
47 httpClient.getState().setCredentials(AuthScope.ANY, credentials);
48 assertEquals(HttpStatus.SC_UNAUTHORIZED, httpClient.executeMethod(postMethod));
49
50 credentials = new UsernamePasswordCredentials("admin2", "admin2");
51 httpClient.getState().setCredentials(AuthScope.ANY, credentials);
52 assertEquals(HttpStatus.SC_UNAUTHORIZED, httpClient.executeMethod(postMethod));
53 }
54
55 @Test
56 public void testProvider2() throws Exception
57 {
58 HttpClient httpClient = new HttpClient();
59 Credentials credentials = new UsernamePasswordCredentials("admin2", "admin2");
60 httpClient.getState().setCredentials(AuthScope.ANY, credentials);
61 httpClient.getParams().setAuthenticationPreemptive(true);
62
63 PostMethod postMethod = new PostMethod("http://localhost:4446");
64 postMethod.setDoAuthentication(true);
65 postMethod.setRequestEntity(new StringRequestEntity("hello", "text/html", "UTF-8"));
66
67 assertEquals(HttpStatus.SC_OK, httpClient.executeMethod(postMethod));
68 assertEquals("hello", postMethod.getResponseBodyAsString());
69
70 credentials = new UsernamePasswordCredentials("asdf", "asdf");
71 httpClient.getState().setCredentials(AuthScope.ANY, credentials);
72 assertEquals(HttpStatus.SC_UNAUTHORIZED, httpClient.executeMethod(postMethod));
73
74 credentials = new UsernamePasswordCredentials("admin", "admin");
75 httpClient.getState().setCredentials(AuthScope.ANY, credentials);
76 assertEquals(HttpStatus.SC_UNAUTHORIZED, httpClient.executeMethod(postMethod));
77 }
78
79 @Test
80 public void testMultipleProviders() throws Exception
81 {
82 HttpClient httpClient = new HttpClient();
83 Credentials credentials = new UsernamePasswordCredentials("admin1", "admin1");
84 httpClient.getState().setCredentials(AuthScope.ANY, credentials);
85 httpClient.getParams().setAuthenticationPreemptive(true);
86
87 PostMethod postMethod = new PostMethod("http://localhost:4447");
88 postMethod.setDoAuthentication(true);
89 postMethod.setRequestEntity(new StringRequestEntity("hello", "text/html", "UTF-8"));
90
91 assertEquals(HttpStatus.SC_OK, httpClient.executeMethod(postMethod));
92 assertEquals("hello", postMethod.getResponseBodyAsString());
93
94 credentials = new UsernamePasswordCredentials("asdf", "asdf");
95 httpClient.getState().setCredentials(AuthScope.ANY, credentials);
96 assertEquals(HttpStatus.SC_UNAUTHORIZED, httpClient.executeMethod(postMethod));
97
98 credentials = new UsernamePasswordCredentials("admin2", "admin2");
99 httpClient.getState().setCredentials(AuthScope.ANY, credentials);
100 assertEquals(HttpStatus.SC_OK, httpClient.executeMethod(postMethod));
101 assertEquals("hello", postMethod.getResponseBodyAsString());
102 }
103
104 }
105
106