View Javadoc
1   /*
2    * JBoss, Home of Professional Open Source
3    * Copyright 2014, Red Hat, Inc. and/or its affiliates, and individual
4    * contributors by the @authors tag. See the copyright.txt in the
5    * distribution for a full listing of individual contributors.
6    *
7    * Licensed under the Apache License, Version 2.0 (the "License");
8    * you may not use this file except in compliance with the License.
9    * You may obtain a copy of the License at
10   * http://www.apache.org/licenses/LICENSE-2.0
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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   * Simple set of tests to demonstrate the GenericHeaderAuthenticator functionality.
45   */
46  @RunWith(Arquillian.class)
47  public class TestGenericHeaderAuthenticator {
48      /**
49       * The location of the WebApp source folder so we know where to find the web.xml when deploying using Arquillian.
50       */
51      private static final String WEBAPP_SRC = "src/main/webapp";
52      /**
53       * The location of the test resources folder so we know where to find the properties file.
54       */
55      private static final String TEST_RESOURCES_SRC = "src/test/resources";
56  
57      /**
58       * The name of the WAR Archive that will be used by Arquillian to deploy the application.
59       */
60      private static final String APP_NAME = "jboss-servlet-security-genericheader-auth";
61  
62      /**
63       * Properties file location
64       */
65      private static final String PROPERTIES_FILE = "/test.properties";
66      
67      /**
68       * The name for the Server URL Property.
69       */
70      private static final String SERVER_URL_PROPERTY = "serverUrl";
71  
72      /**
73       * The Default Server URL if one isn't specified as a System Property
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         // Check that the server URL property was set. If it wasn't then use the default.
102         if (deploymentUrl == null || deploymentUrl.isEmpty()) {
103             deploymentUrl = DEFAULT_SERVER_URL;
104         }
105         
106         // Ensure that the URL ends with a forward slash
107         if (!deploymentUrl.endsWith("/")) {
108             deploymentUrl += "/";
109         }
110         
111         // Ensure the App Name is specified in the URL
112         if (!deploymentUrl.matches(".*" + APP_NAME + ".*"))
113         {
114             deploymentUrl += APP_NAME + "/SecuredServlet?unitTest=true";
115         }
116         
117         // Set the deployment url
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 }