1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.jboss.as.quickstarts.servlet_security_genericheader_auth;
18
19 import static org.junit.Assert.assertEquals;
20 import static org.junit.Assert.fail;
21
22 import java.io.BufferedReader;
23 import java.io.File;
24 import java.io.IOException;
25 import java.io.InputStream;
26 import java.io.InputStreamReader;
27 import java.net.MalformedURLException;
28 import java.net.URL;
29 import java.net.URLConnection;
30 import java.util.Properties;
31
32 import org.jboss.arquillian.container.test.api.Deployment;
33 import org.jboss.arquillian.junit.Arquillian;
34 import org.jboss.shrinkwrap.api.Archive;
35 import org.jboss.shrinkwrap.api.ShrinkWrap;
36 import org.jboss.shrinkwrap.api.spec.WebArchive;
37 import org.jboss.web.tomcat.security.GenericHeaderAuthenticator;
38 import org.junit.Before;
39 import org.junit.BeforeClass;
40 import org.junit.Test;
41 import org.junit.runner.RunWith;
42
43
44
45
46 @RunWith(Arquillian.class)
47 public class TestGenericHeaderAuthenticator {
48
49
50
51 private static final String WEBAPP_SRC = "src/main/webapp";
52
53
54
55 private static final String TEST_RESOURCES_SRC = "src/test/resources";
56
57
58
59
60 private static final String APP_NAME = "jboss-servlet-security-genericheader-auth";
61
62
63
64
65 private static final String PROPERTIES_FILE = "/test.properties";
66
67
68
69
70 private static final String SERVER_URL_PROPERTY = "serverUrl";
71
72
73
74
75 private static final String DEFAULT_SERVER_URL = "http://localhost:8080/";
76
77 private URL deploymentUrl;
78
79 @Deployment
80 public static Archive<?> getDeployment() {
81
82 Archive<?> archive = ShrinkWrap
83 .create(WebArchive.class, APP_NAME + ".war")
84 .addPackages(true, SecuredServlet.class.getPackage())
85 .addPackages(true, GenericHeaderAuthenticator.class.getPackage())
86 .addAsResource(new File(WEBAPP_SRC, "/index.html"))
87 .addAsResource(new File(WEBAPP_SRC, "/login.html"))
88 .addAsResource(new File(WEBAPP_SRC, "/error.html"))
89 .addAsResource(new File(TEST_RESOURCES_SRC, PROPERTIES_FILE))
90 .addAsWebInfResource(new File(WEBAPP_SRC, "WEB-INF/web.xml"))
91 .addAsWebInfResource(new File(WEBAPP_SRC, "WEB-INF/jboss-web.xml"));
92 return archive;
93 }
94
95
96 @Before
97 public void beforeTest() throws MalformedURLException {
98 Properties props = loadProperties();
99 String deploymentUrl = props.getProperty(SERVER_URL_PROPERTY);
100
101
102 if (deploymentUrl == null || deploymentUrl.isEmpty()) {
103 deploymentUrl = DEFAULT_SERVER_URL;
104 }
105
106
107 if (!deploymentUrl.endsWith("/")) {
108 deploymentUrl += "/";
109 }
110
111
112 if (!deploymentUrl.matches(".*" + APP_NAME + ".*"))
113 {
114 deploymentUrl += APP_NAME + "/SecuredServlet?unitTest=true";
115 }
116
117
118 this.deploymentUrl = new URL(deploymentUrl);
119 }
120
121 @Test
122 public void testAuthentication() {
123 try {
124 URLConnection urlConn = deploymentUrl.openConnection();
125 urlConn.setRequestProperty("sm_ssoid", "quickstartUser");
126 urlConn.setRequestProperty("Cookie", "SMSESSION=quickstartUser");
127
128 InputStream inputStream = urlConn.getInputStream();
129 BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
130 String line = br.readLine();
131 assertEquals("Servlet should respond with \"AUTHENTICATED\" message", "AUTHENTICATED", line);
132 } catch (IOException e) {
133 fail("Authentication test connection failed!");
134 }
135 }
136
137 private Properties loadProperties () {
138 try {
139 Properties props = new Properties();
140 InputStream is = getClass().getResourceAsStream(PROPERTIES_FILE);
141 try {
142 props.load(is);
143 } finally {
144 is.close();
145 }
146 return props;
147 } catch (IOException e) {
148 fail("Failed to load test.properties from classpath!");
149 return null;
150 }
151 }
152 }