PageRenderTime 97ms CodeModel.GetById 34ms RepoModel.GetById 0ms app.codeStats 0ms

/testsuite/integration/basic/src/test/java/org/jboss/as/test/integration/web/security/WebSecurityFORMTestCase.java

https://github.com/smcgowan/wildfly
Java | 157 lines | 100 code | 22 blank | 35 comment | 12 complexity | d65fed2278c5dc65571dc7ee8b6b13ba MD5 | raw file
  1. /*
  2. * JBoss, Home of Professional Open Source.
  3. * Copyright (c) 2011, Red Hat, Inc., and individual contributors
  4. * as indicated by the @author tags. See the copyright.txt file in the
  5. * distribution for a full listing of individual contributors.
  6. *
  7. * This is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU Lesser General Public License as
  9. * published by the Free Software Foundation; either version 2.1 of
  10. * the License, or (at your option) any later version.
  11. *
  12. * This software is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this software; if not, write to the Free
  19. * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  20. * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  21. */
  22. package org.jboss.as.test.integration.web.security;
  23. import static org.junit.Assert.assertEquals;
  24. import java.net.URL;
  25. import java.util.ArrayList;
  26. import java.util.List;
  27. import org.apache.http.Header;
  28. import org.apache.http.HttpEntity;
  29. import org.apache.http.HttpResponse;
  30. import org.apache.http.NameValuePair;
  31. import org.apache.http.StatusLine;
  32. import org.apache.http.client.entity.UrlEncodedFormEntity;
  33. import org.apache.http.client.methods.HttpGet;
  34. import org.apache.http.client.methods.HttpPost;
  35. import org.apache.http.cookie.Cookie;
  36. import org.apache.http.impl.client.DefaultHttpClient;
  37. import org.apache.http.message.BasicNameValuePair;
  38. import org.apache.http.protocol.HTTP;
  39. import org.apache.http.util.EntityUtils;
  40. import org.jboss.arquillian.container.test.api.Deployment;
  41. import org.jboss.arquillian.container.test.api.RunAsClient;
  42. import org.jboss.arquillian.junit.Arquillian;
  43. import org.jboss.shrinkwrap.api.spec.WebArchive;
  44. import org.junit.runner.RunWith;
  45. /**
  46. * Unit Test web security
  47. *
  48. * @author Anil Saldhana
  49. */
  50. @RunWith(Arquillian.class)
  51. @RunAsClient
  52. public class WebSecurityFORMTestCase extends WebSecurityPasswordBasedBase {
  53. @Deployment
  54. public static WebArchive deployment() {
  55. // FIXME hack to get things prepared before the deployment happens
  56. try {
  57. // create required security domains
  58. createSecurityDomain();
  59. } catch (Exception e) {
  60. throw new RuntimeException(e);
  61. }
  62. ClassLoader tccl = Thread.currentThread().getContextClassLoader();
  63. URL webxml = tccl.getResource("web-secure.war/web.xml");
  64. WebArchive war = WebSecurityPasswordBasedBase.create("web-secure.war", SecuredServlet.class, true, webxml);
  65. war.addAsWebResource(tccl.getResource("web-secure.war/login.jsp"), "login.jsp");
  66. war.addAsWebResource(tccl.getResource("web-secure.war/error.jsp"), "error.jsp");
  67. war.addAsWebInfResource("web-secure-basic.war/jboss-web.xml", "jboss-web.xml");
  68. WebSecurityPasswordBasedBase.printWar(war);
  69. return war;
  70. }
  71. protected void makeCall(String user, String pass, int expectedStatusCode) throws Exception {
  72. DefaultHttpClient httpclient = new DefaultHttpClient();
  73. try {
  74. HttpGet httpget = new HttpGet(URL);
  75. HttpResponse response = httpclient.execute(httpget);
  76. HttpEntity entity = response.getEntity();
  77. if (entity != null)
  78. EntityUtils.consume(entity);
  79. // We should get the Login Page
  80. StatusLine statusLine = response.getStatusLine();
  81. System.out.println("Login form get: " + statusLine);
  82. assertEquals(200, statusLine.getStatusCode());
  83. System.out.println("Initial set of cookies:");
  84. List<Cookie> cookies = httpclient.getCookieStore().getCookies();
  85. if (cookies.isEmpty()) {
  86. System.out.println("None");
  87. } else {
  88. for (int i = 0; i < cookies.size(); i++) {
  89. System.out.println("- " + cookies.get(i).toString());
  90. }
  91. }
  92. // We should now login with the user name and password
  93. HttpPost httpost = new HttpPost(URL + "/j_security_check");
  94. List<NameValuePair> nvps = new ArrayList<NameValuePair>();
  95. nvps.add(new BasicNameValuePair("j_username", user));
  96. nvps.add(new BasicNameValuePair("j_password", pass));
  97. httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
  98. response = httpclient.execute(httpost);
  99. entity = response.getEntity();
  100. if (entity != null)
  101. EntityUtils.consume(entity);
  102. statusLine = response.getStatusLine();
  103. // Post authentication - we have a 302
  104. assertEquals(302, statusLine.getStatusCode());
  105. Header locationHeader = response.getFirstHeader("Location");
  106. String location = locationHeader.getValue();
  107. HttpGet httpGet = new HttpGet(location);
  108. response = httpclient.execute(httpGet);
  109. entity = response.getEntity();
  110. if (entity != null)
  111. EntityUtils.consume(entity);
  112. System.out.println("Post logon cookies:");
  113. cookies = httpclient.getCookieStore().getCookies();
  114. if (cookies.isEmpty()) {
  115. System.out.println("None");
  116. } else {
  117. for (int i = 0; i < cookies.size(); i++) {
  118. System.out.println("- " + cookies.get(i).toString());
  119. }
  120. }
  121. // Either the authentication passed or failed based on the expected status code
  122. statusLine = response.getStatusLine();
  123. assertEquals(expectedStatusCode, statusLine.getStatusCode());
  124. } finally {
  125. // When HttpClient instance is no longer needed,
  126. // shut down the connection manager to ensure
  127. // immediate deallocation of all system resources
  128. httpclient.getConnectionManager().shutdown();
  129. }
  130. }
  131. @Override
  132. public String getContextPath() {
  133. return "web-secure";
  134. }
  135. }