You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cxf.apache.org by co...@apache.org on 2014/07/10 18:44:57 UTC
git commit: Upgrading system tests to use newer HttpClient API
Repository: cxf-fediz
Updated Branches:
refs/heads/master 940c54a65 -> 79c744df1
Upgrading system tests to use newer HttpClient API
Project: http://git-wip-us.apache.org/repos/asf/cxf-fediz/repo
Commit: http://git-wip-us.apache.org/repos/asf/cxf-fediz/commit/79c744df
Tree: http://git-wip-us.apache.org/repos/asf/cxf-fediz/tree/79c744df
Diff: http://git-wip-us.apache.org/repos/asf/cxf-fediz/diff/79c744df
Branch: refs/heads/master
Commit: 79c744df1824893331f73f03358c047d7f4f0433
Parents: 940c54a
Author: Colm O hEigeartaigh <co...@apache.org>
Authored: Thu Jul 10 17:44:35 2014 +0100
Committer: Colm O hEigeartaigh <co...@apache.org>
Committed: Thu Jul 10 17:44:35 2014 +0100
----------------------------------------------------------------------
.../fediz/integrationtests/AbstractTests.java | 141 ++-------------
.../fediz/integrationtests/HTTPTestUtils.java | 176 +++++++++++++++++++
.../cxf/fediz/integrationtests/BadWReqTest.java | 125 +------------
.../cxf/fediz/integrationtests/TomcatTest.java | 121 +------------
.../cxf/fediz/integrationtests/WReqTest.java | 125 +------------
5 files changed, 190 insertions(+), 498 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/cxf-fediz/blob/79c744df/systests/tests/src/test/java/org/apache/cxf/fediz/integrationtests/AbstractTests.java
----------------------------------------------------------------------
diff --git a/systests/tests/src/test/java/org/apache/cxf/fediz/integrationtests/AbstractTests.java b/systests/tests/src/test/java/org/apache/cxf/fediz/integrationtests/AbstractTests.java
index b2e80f3..f2b715a 100644
--- a/systests/tests/src/test/java/org/apache/cxf/fediz/integrationtests/AbstractTests.java
+++ b/systests/tests/src/test/java/org/apache/cxf/fediz/integrationtests/AbstractTests.java
@@ -19,33 +19,7 @@
package org.apache.cxf.fediz.integrationtests;
-import java.io.File;
-import java.io.FileInputStream;
-import java.security.KeyStore;
-import java.util.ArrayList;
-import java.util.List;
-
-import net.htmlparser.jericho.Element;
-import net.htmlparser.jericho.FormField;
-import net.htmlparser.jericho.FormFields;
-import net.htmlparser.jericho.HTMLElementName;
-import net.htmlparser.jericho.Source;
import org.apache.cxf.fediz.core.ClaimTypes;
-import org.apache.http.Consts;
-import org.apache.http.HttpEntity;
-import org.apache.http.HttpResponse;
-import org.apache.http.NameValuePair;
-import org.apache.http.auth.AuthScope;
-import org.apache.http.auth.UsernamePasswordCredentials;
-import org.apache.http.client.entity.UrlEncodedFormEntity;
-import org.apache.http.client.methods.HttpGet;
-import org.apache.http.client.methods.HttpPost;
-import org.apache.http.conn.scheme.Scheme;
-import org.apache.http.conn.ssl.SSLSocketFactory;
-import org.apache.http.impl.client.DefaultHttpClient;
-import org.apache.http.impl.client.LaxRedirectStrategy;
-import org.apache.http.message.BasicNameValuePair;
-import org.apache.http.util.EntityUtils;
import org.junit.Assert;
public abstract class AbstractTests {
@@ -65,7 +39,8 @@ public abstract class AbstractTests {
String url = "https://localhost:" + getRpHttpsPort() + "/fedizhelloworld/secure/fedservlet";
String user = "alice";
String password = "ecila";
- String response = sendHttpGet(url, user, password);
+ String response =
+ HTTPTestUtils.sendHttpGet(url, user, password, Integer.parseInt(getIdpHttpsPort()));
Assert.assertTrue("Principal not " + user, response.indexOf("userPrincipal=" + user) > 0);
Assert.assertTrue("User " + user + " does not have role Admin", response.indexOf("role:Admin=false") > 0);
@@ -89,7 +64,8 @@ public abstract class AbstractTests {
String url = "https://localhost:" + getRpHttpsPort() + "/fedizhelloworld/secure/fedservlet";
String user = "bob";
String password = "bob";
- String response = sendHttpGet(url, user, password);
+ String response =
+ HTTPTestUtils.sendHttpGet(url, user, password, Integer.parseInt(getIdpHttpsPort()));
Assert.assertTrue("Principal not " + user, response.indexOf("userPrincipal=" + user) > 0);
Assert.assertTrue("User " + user + " does not have role Admin", response.indexOf("role:Admin=true") > 0);
@@ -112,7 +88,8 @@ public abstract class AbstractTests {
String url = "https://localhost:" + getRpHttpsPort() + "/fedizhelloworld/secure/fedservlet";
String user = "ted";
String password = "det";
- String response = sendHttpGet(url, user, password);
+ String response =
+ HTTPTestUtils.sendHttpGet(url, user, password, Integer.parseInt(getIdpHttpsPort()));
Assert.assertTrue("Principal not " + user, response.indexOf("userPrincipal=" + user) > 0);
Assert.assertTrue("User " + user + " does not have role Admin", response.indexOf("role:Admin=false") > 0);
@@ -135,18 +112,17 @@ public abstract class AbstractTests {
String url = "https://localhost:" + getRpHttpsPort() + "/fedizhelloworld/secure/admin/fedservlet";
String user = "alice";
String password = "ecila";
- sendHttpGet(url, user, password, 200, 403);
+ HTTPTestUtils.sendHttpGet(url, user, password, 200, 403, Integer.parseInt(getIdpHttpsPort()));
}
-// @org.junit.Ignore
@org.junit.Test
public void testUserAliceWrongPassword() throws Exception {
String url = "https://localhost:" + getRpHttpsPort() + "/fedizhelloworld/secure/fedservlet";
String user = "alice";
String password = "alice";
-// sendHttpGet(url, user, password, 500, 0);
+ // sendHttpGet(url, user, password, 500, 0);
//[FIXED] Fix IDP return code from 500 to 401
- sendHttpGet(url, user, password, 401, 0);
+ HTTPTestUtils.sendHttpGet(url, user, password, 401, 0, Integer.parseInt(getIdpHttpsPort()));
}
@org.junit.Test
@@ -154,105 +130,8 @@ public abstract class AbstractTests {
String url = "https://localhost:" + getRpHttpsPort() + "/fedizhelloworld/secure/admin/fedservlet";
String user = "ted";
String password = "det";
- sendHttpGet(url, user, password, 200, 403);
- }
-
- private String sendHttpGet(String url, String user, String password) throws Exception {
- return sendHttpGet(url, user, password, 200, 200);
+ HTTPTestUtils.sendHttpGet(url, user, password, 200, 403, Integer.parseInt(getIdpHttpsPort()));
}
- private String sendHttpGet(String url, String user, String password, int returnCodeIDP, int returnCodeRP)
- throws Exception {
- DefaultHttpClient httpclient = new DefaultHttpClient();
- try {
- httpclient.getCredentialsProvider().setCredentials(
- new AuthScope("localhost", Integer.parseInt(getIdpHttpsPort())),
- new UsernamePasswordCredentials(user, password));
-
- KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
- FileInputStream instream = new FileInputStream(new File("./target/test-classes/client.jks"));
- try {
- trustStore.load(instream, "clientpass".toCharArray());
- } finally {
- try {
- instream.close();
- } catch (Exception ex) {
- ex.printStackTrace();
- }
- }
-
- SSLSocketFactory socketFactory = new SSLSocketFactory(trustStore);
- Scheme schIdp = new Scheme("https", Integer.parseInt(getIdpHttpsPort()), socketFactory);
- httpclient.getConnectionManager().getSchemeRegistry().register(schIdp);
- Scheme schRp = new Scheme("https", Integer.parseInt(getRpHttpsPort()), socketFactory);
- httpclient.getConnectionManager().getSchemeRegistry().register(schRp);
-
- HttpGet httpget = new HttpGet(url);
-
- HttpResponse response = httpclient.execute(httpget);
- HttpEntity entity = response.getEntity();
-
- System.out.println(response.getStatusLine());
- if (entity != null) {
- System.out.println("Response content length: " + entity.getContentLength());
- }
- Assert.assertTrue("IDP HTTP Response code: " + response.getStatusLine().getStatusCode()
- + " [Expected: " + returnCodeIDP + "]",
- returnCodeIDP == response.getStatusLine().getStatusCode());
-
- if (response.getStatusLine().getStatusCode() != 200) {
- return null;
- }
-
- // Redirect to a POST is not supported without user interaction
- // http://www.ietf.org/rfc/rfc2616.txt
- // If the 301 status code is received in response to a request other
- // than GET or HEAD, the user agent MUST NOT automatically redirect the
- // request unless it can be confirmed by the user, since this might
- // change the conditions under which the request was issued.
-
- httpclient.setRedirectStrategy(new LaxRedirectStrategy());
-
- Source source = new Source(EntityUtils.toString(entity));
- List <NameValuePair> nvps = new ArrayList <NameValuePair>();
- FormFields formFields = source.getFormFields();
-
- List<Element> forms = source.getAllElements(HTMLElementName.FORM);
- Assert.assertEquals("Only one form expected but got " + forms.size(), 1, forms.size());
- String postUrl = forms.get(0).getAttributeValue("action");
-
- Assert.assertNotNull("Form field 'wa' not found", formFields.get("wa"));
- Assert.assertNotNull("Form field 'wresult' not found", formFields.get("wresult"));
-
- for (FormField formField : formFields) {
- if (formField.getUserValueCount() != 0) {
- nvps.add(new BasicNameValuePair(formField.getName(),
- formField.getValues().get(0)));
- }
- }
- HttpPost httppost = new HttpPost(postUrl);
- httppost.setEntity(new UrlEncodedFormEntity(nvps, Consts.UTF_8));
-
- response = httpclient.execute(httppost);
-
- entity = response.getEntity();
- System.out.println(response.getStatusLine());
- Assert.assertTrue("RP HTTP Response code: " + response.getStatusLine().getStatusCode()
- + " [Expected: " + returnCodeRP + "]",
- returnCodeRP == response.getStatusLine().getStatusCode());
-
- if (entity != null) {
- System.out.println("Response content length: " + entity.getContentLength());
- }
-
- return EntityUtils.toString(entity);
- } finally {
- // When HttpClient instance is no longer needed,
- // shut down the connection manager to ensure
- // immediate deallocation of all system resources
- httpclient.getConnectionManager().shutdown();
- }
-
- }
}
http://git-wip-us.apache.org/repos/asf/cxf-fediz/blob/79c744df/systests/tests/src/test/java/org/apache/cxf/fediz/integrationtests/HTTPTestUtils.java
----------------------------------------------------------------------
diff --git a/systests/tests/src/test/java/org/apache/cxf/fediz/integrationtests/HTTPTestUtils.java b/systests/tests/src/test/java/org/apache/cxf/fediz/integrationtests/HTTPTestUtils.java
new file mode 100644
index 0000000..af57bd1
--- /dev/null
+++ b/systests/tests/src/test/java/org/apache/cxf/fediz/integrationtests/HTTPTestUtils.java
@@ -0,0 +1,176 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.cxf.fediz.integrationtests;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.security.KeyStore;
+import java.util.ArrayList;
+import java.util.List;
+
+import javax.net.ssl.SSLContext;
+
+import net.htmlparser.jericho.Element;
+import net.htmlparser.jericho.FormField;
+import net.htmlparser.jericho.FormFields;
+import net.htmlparser.jericho.HTMLElementName;
+import net.htmlparser.jericho.Source;
+
+import org.apache.http.Consts;
+import org.apache.http.HttpEntity;
+import org.apache.http.HttpResponse;
+import org.apache.http.NameValuePair;
+import org.apache.http.auth.AuthScope;
+import org.apache.http.auth.UsernamePasswordCredentials;
+import org.apache.http.client.CredentialsProvider;
+import org.apache.http.client.entity.UrlEncodedFormEntity;
+import org.apache.http.client.methods.HttpGet;
+import org.apache.http.client.methods.HttpPost;
+import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
+import org.apache.http.conn.ssl.SSLContextBuilder;
+import org.apache.http.conn.ssl.TrustSelfSignedStrategy;
+import org.apache.http.impl.client.BasicCredentialsProvider;
+import org.apache.http.impl.client.CloseableHttpClient;
+import org.apache.http.impl.client.HttpClientBuilder;
+import org.apache.http.impl.client.LaxRedirectStrategy;
+import org.apache.http.message.BasicNameValuePair;
+import org.apache.http.util.EntityUtils;
+import org.junit.Assert;
+
+/**
+ * Some basic HTTP-based functionality for use in the tests
+ */
+public final class HTTPTestUtils {
+
+ private HTTPTestUtils() {
+ // complete
+ }
+
+ public static String sendHttpGet(String url, String user,
+ String password, int idpPort) throws Exception {
+ return sendHttpGet(url, user, password, 200, 200, idpPort);
+ }
+
+ public static String sendHttpGet(String url, String user, String password,
+ int returnCodeIDP, int returnCodeRP, int idpPort)
+ throws Exception {
+
+ CloseableHttpClient httpClient = null;
+ try {
+ CredentialsProvider credsProvider = new BasicCredentialsProvider();
+ credsProvider.setCredentials(
+ new AuthScope("localhost", idpPort),
+ new UsernamePasswordCredentials(user, password));
+
+ KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
+ FileInputStream instream = new FileInputStream(new File("./target/test-classes/client.jks"));
+ try {
+ trustStore.load(instream, "clientpass".toCharArray());
+ } finally {
+ try {
+ instream.close();
+ } catch (Exception ex) {
+ ex.printStackTrace();
+ }
+ }
+
+ SSLContextBuilder sslContextBuilder = new SSLContextBuilder();
+ sslContextBuilder.loadTrustMaterial(trustStore, new TrustSelfSignedStrategy());
+ sslContextBuilder.loadKeyMaterial(trustStore, "clientpass".toCharArray());
+
+ SSLContext sslContext = sslContextBuilder.build();
+ SSLConnectionSocketFactory sslSocketFactory =
+ new SSLConnectionSocketFactory(sslContext);
+
+ HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
+ httpClientBuilder.setDefaultCredentialsProvider(credsProvider);
+ httpClientBuilder.setSSLSocketFactory(sslSocketFactory);
+ httpClientBuilder.setRedirectStrategy(new LaxRedirectStrategy());
+
+ httpClient = httpClientBuilder.build();
+
+ HttpGet httpget = new HttpGet(url);
+
+ HttpResponse response = httpClient.execute(httpget);
+ HttpEntity entity = response.getEntity();
+
+ System.out.println(response.getStatusLine());
+ if (entity != null) {
+ System.out.println("Response content length: " + entity.getContentLength());
+ }
+ Assert.assertTrue("IDP HTTP Response code: " + response.getStatusLine().getStatusCode()
+ + " [Expected: " + returnCodeIDP + "]",
+ returnCodeIDP == response.getStatusLine().getStatusCode());
+
+ if (response.getStatusLine().getStatusCode() != 200) {
+ return null;
+ }
+
+ // Redirect to a POST is not supported without user interaction
+ // http://www.ietf.org/rfc/rfc2616.txt
+ // If the 301 status code is received in response to a request other
+ // than GET or HEAD, the user agent MUST NOT automatically redirect the
+ // request unless it can be confirmed by the user, since this might
+ // change the conditions under which the request was issued.
+
+ Source source = new Source(EntityUtils.toString(entity));
+ List <NameValuePair> nvps = new ArrayList <NameValuePair>();
+ FormFields formFields = source.getFormFields();
+
+ List<Element> forms = source.getAllElements(HTMLElementName.FORM);
+ Assert.assertEquals("Only one form expected but got " + forms.size(), 1, forms.size());
+ String postUrl = forms.get(0).getAttributeValue("action");
+
+ Assert.assertNotNull("Form field 'wa' not found", formFields.get("wa"));
+ Assert.assertNotNull("Form field 'wresult' not found", formFields.get("wresult"));
+
+ for (FormField formField : formFields) {
+ if (formField.getUserValueCount() != 0) {
+ nvps.add(new BasicNameValuePair(formField.getName(),
+ formField.getValues().get(0)));
+ }
+ }
+ HttpPost httppost = new HttpPost(postUrl);
+ httppost.setEntity(new UrlEncodedFormEntity(nvps, Consts.UTF_8));
+
+ response = httpClient.execute(httppost);
+
+ entity = response.getEntity();
+ System.out.println(response.getStatusLine());
+ Assert.assertTrue("RP HTTP Response code: " + response.getStatusLine().getStatusCode()
+ + " [Expected: " + returnCodeRP + "]",
+ returnCodeRP == response.getStatusLine().getStatusCode());
+
+ if (entity != null) {
+ System.out.println("Response content length: " + entity.getContentLength());
+ }
+
+ return EntityUtils.toString(entity);
+ } finally {
+ // When HttpClient instance is no longer needed,
+ // shut down the connection manager to ensure
+ // immediate deallocation of all system resources
+ if (httpClient != null) {
+ httpClient.close();
+ }
+ }
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/cxf-fediz/blob/79c744df/systests/tomcat7/src/test/java/org/apache/cxf/fediz/integrationtests/BadWReqTest.java
----------------------------------------------------------------------
diff --git a/systests/tomcat7/src/test/java/org/apache/cxf/fediz/integrationtests/BadWReqTest.java b/systests/tomcat7/src/test/java/org/apache/cxf/fediz/integrationtests/BadWReqTest.java
index 0c9d7fb..71d684c 100644
--- a/systests/tomcat7/src/test/java/org/apache/cxf/fediz/integrationtests/BadWReqTest.java
+++ b/systests/tomcat7/src/test/java/org/apache/cxf/fediz/integrationtests/BadWReqTest.java
@@ -21,37 +21,12 @@ package org.apache.cxf.fediz.integrationtests;
import java.io.File;
-import java.io.FileInputStream;
-import java.security.KeyStore;
-import java.util.ArrayList;
-import java.util.List;
-
-import net.htmlparser.jericho.Element;
-import net.htmlparser.jericho.FormField;
-import net.htmlparser.jericho.FormFields;
-import net.htmlparser.jericho.HTMLElementName;
-import net.htmlparser.jericho.Source;
import org.apache.catalina.Context;
import org.apache.catalina.LifecycleState;
import org.apache.catalina.connector.Connector;
import org.apache.catalina.startup.Tomcat;
import org.apache.cxf.fediz.tomcat.FederationAuthenticator;
-import org.apache.http.Consts;
-import org.apache.http.HttpEntity;
-import org.apache.http.HttpResponse;
-import org.apache.http.NameValuePair;
-import org.apache.http.auth.AuthScope;
-import org.apache.http.auth.UsernamePasswordCredentials;
-import org.apache.http.client.entity.UrlEncodedFormEntity;
-import org.apache.http.client.methods.HttpGet;
-import org.apache.http.client.methods.HttpPost;
-import org.apache.http.conn.scheme.Scheme;
-import org.apache.http.conn.ssl.SSLSocketFactory;
-import org.apache.http.impl.client.DefaultHttpClient;
-import org.apache.http.impl.client.LaxRedirectStrategy;
-import org.apache.http.message.BasicNameValuePair;
-import org.apache.http.util.EntityUtils;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.BeforeClass;
@@ -209,105 +184,7 @@ public class BadWReqTest {
String url = "https://localhost:" + getRpHttpsPort() + "/fedizhelloworld/secure/fedservlet";
String user = "alice";
String password = "ecila";
- sendHttpGet(url, user, password);
+ HTTPTestUtils.sendHttpGet(url, user, password, 500, 403, Integer.parseInt(getIdpHttpsPort()));
}
- private String sendHttpGet(String url, String user, String password) throws Exception {
- return sendHttpGet(url, user, password, 500, 403);
- }
-
- private String sendHttpGet(String url, String user, String password, int returnCodeIDP, int returnCodeRP)
- throws Exception {
- DefaultHttpClient httpclient = new DefaultHttpClient();
- try {
- httpclient.getCredentialsProvider().setCredentials(
- new AuthScope("localhost", Integer.parseInt(getIdpHttpsPort())),
- new UsernamePasswordCredentials(user, password));
-
- KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
- FileInputStream instream = new FileInputStream(new File("./target/test-classes/client.jks"));
- try {
- trustStore.load(instream, "clientpass".toCharArray());
- } finally {
- try {
- instream.close();
- } catch (Exception ex) {
- ex.printStackTrace();
- }
- }
-
- SSLSocketFactory socketFactory = new SSLSocketFactory(trustStore);
- Scheme schIdp = new Scheme("https", Integer.parseInt(getIdpHttpsPort()), socketFactory);
- httpclient.getConnectionManager().getSchemeRegistry().register(schIdp);
- Scheme schRp = new Scheme("https", Integer.parseInt(getRpHttpsPort()), socketFactory);
- httpclient.getConnectionManager().getSchemeRegistry().register(schRp);
-
- HttpGet httpget = new HttpGet(url);
-
- HttpResponse response = httpclient.execute(httpget);
- HttpEntity entity = response.getEntity();
-
- System.out.println(response.getStatusLine());
- if (entity != null) {
- System.out.println("Response content length: " + entity.getContentLength());
- }
- Assert.assertTrue("IDP HTTP Response code: " + response.getStatusLine().getStatusCode()
- + " [Expected: " + returnCodeIDP + "]",
- returnCodeIDP == response.getStatusLine().getStatusCode());
-
- if (response.getStatusLine().getStatusCode() != 200) {
- return null;
- }
-
- // Redirect to a POST is not supported without user interaction
- // http://www.ietf.org/rfc/rfc2616.txt
- // If the 301 status code is received in response to a request other
- // than GET or HEAD, the user agent MUST NOT automatically redirect the
- // request unless it can be confirmed by the user, since this might
- // change the conditions under which the request was issued.
-
- httpclient.setRedirectStrategy(new LaxRedirectStrategy());
-
- Source source = new Source(EntityUtils.toString(entity));
- List <NameValuePair> nvps = new ArrayList <NameValuePair>();
- FormFields formFields = source.getFormFields();
-
- List<Element> forms = source.getAllElements(HTMLElementName.FORM);
- Assert.assertEquals("Only one form expected but got " + forms.size(), 1, forms.size());
- String postUrl = forms.get(0).getAttributeValue("action");
-
- Assert.assertNotNull("Form field 'wa' not found", formFields.get("wa"));
- Assert.assertNotNull("Form field 'wresult' not found", formFields.get("wresult"));
-
- for (FormField formField : formFields) {
- if (formField.getUserValueCount() != 0) {
- nvps.add(new BasicNameValuePair(formField.getName(),
- formField.getValues().get(0)));
- }
- }
- HttpPost httppost = new HttpPost(postUrl);
- httppost.setEntity(new UrlEncodedFormEntity(nvps, Consts.UTF_8));
-
- response = httpclient.execute(httppost);
-
- entity = response.getEntity();
- System.out.println(response.getStatusLine());
- Assert.assertTrue("RP HTTP Response code: " + response.getStatusLine().getStatusCode()
- + " [Expected: " + returnCodeRP + "]",
- returnCodeRP == response.getStatusLine().getStatusCode());
-
- if (entity != null) {
- System.out.println("Response content length: " + entity.getContentLength());
- }
-
- return EntityUtils.toString(entity);
- } finally {
- // When HttpClient instance is no longer needed,
- // shut down the connection manager to ensure
- // immediate deallocation of all system resources
- httpclient.getConnectionManager().shutdown();
- }
-
- }
-
}
http://git-wip-us.apache.org/repos/asf/cxf-fediz/blob/79c744df/systests/tomcat7/src/test/java/org/apache/cxf/fediz/integrationtests/TomcatTest.java
----------------------------------------------------------------------
diff --git a/systests/tomcat7/src/test/java/org/apache/cxf/fediz/integrationtests/TomcatTest.java b/systests/tomcat7/src/test/java/org/apache/cxf/fediz/integrationtests/TomcatTest.java
index cd83d0b..163c97b 100644
--- a/systests/tomcat7/src/test/java/org/apache/cxf/fediz/integrationtests/TomcatTest.java
+++ b/systests/tomcat7/src/test/java/org/apache/cxf/fediz/integrationtests/TomcatTest.java
@@ -21,16 +21,6 @@ package org.apache.cxf.fediz.integrationtests;
import java.io.File;
-import java.io.FileInputStream;
-import java.security.KeyStore;
-import java.util.ArrayList;
-import java.util.List;
-
-import net.htmlparser.jericho.Element;
-import net.htmlparser.jericho.FormField;
-import net.htmlparser.jericho.FormFields;
-import net.htmlparser.jericho.HTMLElementName;
-import net.htmlparser.jericho.Source;
import org.apache.catalina.Context;
import org.apache.catalina.LifecycleState;
@@ -38,21 +28,6 @@ import org.apache.catalina.connector.Connector;
import org.apache.catalina.startup.Tomcat;
import org.apache.cxf.fediz.core.ClaimTypes;
import org.apache.cxf.fediz.tomcat.FederationAuthenticator;
-import org.apache.http.Consts;
-import org.apache.http.HttpEntity;
-import org.apache.http.HttpResponse;
-import org.apache.http.NameValuePair;
-import org.apache.http.auth.AuthScope;
-import org.apache.http.auth.UsernamePasswordCredentials;
-import org.apache.http.client.entity.UrlEncodedFormEntity;
-import org.apache.http.client.methods.HttpGet;
-import org.apache.http.client.methods.HttpPost;
-import org.apache.http.conn.scheme.Scheme;
-import org.apache.http.conn.ssl.SSLSocketFactory;
-import org.apache.http.impl.client.DefaultHttpClient;
-import org.apache.http.impl.client.LaxRedirectStrategy;
-import org.apache.http.message.BasicNameValuePair;
-import org.apache.http.util.EntityUtils;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.BeforeClass;
@@ -210,7 +185,8 @@ public class TomcatTest extends AbstractTests {
String url = "https://localhost:" + getRpHttpsPort() + "/fedizhelloworld/secure/fedservlet";
String user = "alice";
String password = "ecila";
- String response = sendHttpGetClientAuth(url, user, password, 200, 200);
+ String response =
+ HTTPTestUtils.sendHttpGet(url, user, password, 200, 200, Integer.parseInt(getIdpHttpsPort()));
Assert.assertTrue("Principal not " + user, response.indexOf("userPrincipal=" + user) > 0);
Assert.assertTrue("User " + user + " does not have role Admin", response.indexOf("role:Admin=false") > 0);
@@ -229,97 +205,4 @@ public class TomcatTest extends AbstractTests {
}
- private String sendHttpGetClientAuth(String url, String user, String password, int returnCodeIDP, int returnCodeRP)
- throws Exception {
- DefaultHttpClient httpclient = new DefaultHttpClient();
- try {
- httpclient.getCredentialsProvider().setCredentials(
- new AuthScope("localhost", Integer.parseInt(getIdpHttpsPort())),
- new UsernamePasswordCredentials(user, password));
-
- KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
- FileInputStream instream = new FileInputStream(new File("./target/test-classes/client.jks"));
- try {
- trustStore.load(instream, "clientpass".toCharArray());
- } finally {
- try {
- instream.close();
- } catch (Exception ex) {
- ex.printStackTrace();
- }
- }
-
- SSLSocketFactory socketFactory = new SSLSocketFactory(trustStore, "clientpass", trustStore);
- Scheme schIdp = new Scheme("https", Integer.parseInt(getIdpHttpsPort()), socketFactory);
- httpclient.getConnectionManager().getSchemeRegistry().register(schIdp);
- Scheme schRp = new Scheme("https", Integer.parseInt(getRpHttpsPort()), socketFactory);
- httpclient.getConnectionManager().getSchemeRegistry().register(schRp);
-
- HttpGet httpget = new HttpGet(url);
-
- HttpResponse response = httpclient.execute(httpget);
- HttpEntity entity = response.getEntity();
-
- System.out.println(response.getStatusLine());
- if (entity != null) {
- System.out.println("Response content length: " + entity.getContentLength());
- }
- Assert.assertTrue("IDP HTTP Response code: " + response.getStatusLine().getStatusCode()
- + " [Expected: " + returnCodeIDP + "]",
- returnCodeIDP == response.getStatusLine().getStatusCode());
-
- if (response.getStatusLine().getStatusCode() != 200) {
- return null;
- }
-
- // Redirect to a POST is not supported without user interaction
- // http://www.ietf.org/rfc/rfc2616.txt
- // If the 301 status code is received in response to a request other
- // than GET or HEAD, the user agent MUST NOT automatically redirect the
- // request unless it can be confirmed by the user, since this might
- // change the conditions under which the request was issued.
-
- httpclient.setRedirectStrategy(new LaxRedirectStrategy());
-
- Source source = new Source(EntityUtils.toString(entity));
- List <NameValuePair> nvps = new ArrayList <NameValuePair>();
- FormFields formFields = source.getFormFields();
-
- List<Element> forms = source.getAllElements(HTMLElementName.FORM);
- Assert.assertEquals("Only one form expected but got " + forms.size(), 1, forms.size());
- String postUrl = forms.get(0).getAttributeValue("action");
-
- Assert.assertNotNull("Form field 'wa' not found", formFields.get("wa"));
- Assert.assertNotNull("Form field 'wresult' not found", formFields.get("wresult"));
-
- for (FormField formField : formFields) {
- if (formField.getUserValueCount() != 0) {
- nvps.add(new BasicNameValuePair(formField.getName(),
- formField.getValues().get(0)));
- }
- }
- HttpPost httppost = new HttpPost(postUrl);
- httppost.setEntity(new UrlEncodedFormEntity(nvps, Consts.UTF_8));
-
- response = httpclient.execute(httppost);
-
- entity = response.getEntity();
- System.out.println(response.getStatusLine());
- Assert.assertTrue("RP HTTP Response code: " + response.getStatusLine().getStatusCode()
- + " [Expected: " + returnCodeRP + "]",
- returnCodeRP == response.getStatusLine().getStatusCode());
-
- if (entity != null) {
- System.out.println("Response content length: " + entity.getContentLength());
- }
-
- return EntityUtils.toString(entity);
- } finally {
- // When HttpClient instance is no longer needed,
- // shut down the connection manager to ensure
- // immediate deallocation of all system resources
- httpclient.getConnectionManager().shutdown();
- }
-
- }
}
http://git-wip-us.apache.org/repos/asf/cxf-fediz/blob/79c744df/systests/tomcat7/src/test/java/org/apache/cxf/fediz/integrationtests/WReqTest.java
----------------------------------------------------------------------
diff --git a/systests/tomcat7/src/test/java/org/apache/cxf/fediz/integrationtests/WReqTest.java b/systests/tomcat7/src/test/java/org/apache/cxf/fediz/integrationtests/WReqTest.java
index d6f3a7a..d3aa47f 100644
--- a/systests/tomcat7/src/test/java/org/apache/cxf/fediz/integrationtests/WReqTest.java
+++ b/systests/tomcat7/src/test/java/org/apache/cxf/fediz/integrationtests/WReqTest.java
@@ -21,16 +21,6 @@ package org.apache.cxf.fediz.integrationtests;
import java.io.File;
-import java.io.FileInputStream;
-import java.security.KeyStore;
-import java.util.ArrayList;
-import java.util.List;
-
-import net.htmlparser.jericho.Element;
-import net.htmlparser.jericho.FormField;
-import net.htmlparser.jericho.FormFields;
-import net.htmlparser.jericho.HTMLElementName;
-import net.htmlparser.jericho.Source;
import org.apache.catalina.Context;
import org.apache.catalina.LifecycleState;
@@ -38,21 +28,6 @@ import org.apache.catalina.connector.Connector;
import org.apache.catalina.startup.Tomcat;
import org.apache.cxf.fediz.core.ClaimTypes;
import org.apache.cxf.fediz.tomcat.FederationAuthenticator;
-import org.apache.http.Consts;
-import org.apache.http.HttpEntity;
-import org.apache.http.HttpResponse;
-import org.apache.http.NameValuePair;
-import org.apache.http.auth.AuthScope;
-import org.apache.http.auth.UsernamePasswordCredentials;
-import org.apache.http.client.entity.UrlEncodedFormEntity;
-import org.apache.http.client.methods.HttpGet;
-import org.apache.http.client.methods.HttpPost;
-import org.apache.http.conn.scheme.Scheme;
-import org.apache.http.conn.ssl.SSLSocketFactory;
-import org.apache.http.impl.client.DefaultHttpClient;
-import org.apache.http.impl.client.LaxRedirectStrategy;
-import org.apache.http.message.BasicNameValuePair;
-import org.apache.http.util.EntityUtils;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.BeforeClass;
@@ -209,7 +184,7 @@ public class WReqTest {
String url = "https://localhost:" + getRpHttpsPort() + "/fedizhelloworld/secure/fedservlet";
String user = "alice";
String password = "ecila";
- String response = sendHttpGet(url, user, password);
+ String response = HTTPTestUtils.sendHttpGet(url, user, password, Integer.parseInt(getIdpHttpsPort()));
Assert.assertTrue("Principal not " + user, response.indexOf("userPrincipal=" + user) > 0);
Assert.assertTrue("User " + user + " does not have role Admin", response.indexOf("role:Admin=false") > 0);
@@ -228,102 +203,4 @@ public class WReqTest {
}
- private String sendHttpGet(String url, String user, String password) throws Exception {
- return sendHttpGet(url, user, password, 200, 200);
- }
-
- private String sendHttpGet(String url, String user, String password, int returnCodeIDP, int returnCodeRP)
- throws Exception {
- DefaultHttpClient httpclient = new DefaultHttpClient();
- try {
- httpclient.getCredentialsProvider().setCredentials(
- new AuthScope("localhost", Integer.parseInt(getIdpHttpsPort())),
- new UsernamePasswordCredentials(user, password));
-
- KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
- FileInputStream instream = new FileInputStream(new File("./target/test-classes/client.jks"));
- try {
- trustStore.load(instream, "clientpass".toCharArray());
- } finally {
- try {
- instream.close();
- } catch (Exception ex) {
- ex.printStackTrace();
- }
- }
-
- SSLSocketFactory socketFactory = new SSLSocketFactory(trustStore);
- Scheme schIdp = new Scheme("https", Integer.parseInt(getIdpHttpsPort()), socketFactory);
- httpclient.getConnectionManager().getSchemeRegistry().register(schIdp);
- Scheme schRp = new Scheme("https", Integer.parseInt(getRpHttpsPort()), socketFactory);
- httpclient.getConnectionManager().getSchemeRegistry().register(schRp);
-
- HttpGet httpget = new HttpGet(url);
-
- HttpResponse response = httpclient.execute(httpget);
- HttpEntity entity = response.getEntity();
-
- System.out.println(response.getStatusLine());
- if (entity != null) {
- System.out.println("Response content length: " + entity.getContentLength());
- }
- Assert.assertTrue("IDP HTTP Response code: " + response.getStatusLine().getStatusCode()
- + " [Expected: " + returnCodeIDP + "]",
- returnCodeIDP == response.getStatusLine().getStatusCode());
-
- if (response.getStatusLine().getStatusCode() != 200) {
- return null;
- }
-
- // Redirect to a POST is not supported without user interaction
- // http://www.ietf.org/rfc/rfc2616.txt
- // If the 301 status code is received in response to a request other
- // than GET or HEAD, the user agent MUST NOT automatically redirect the
- // request unless it can be confirmed by the user, since this might
- // change the conditions under which the request was issued.
-
- httpclient.setRedirectStrategy(new LaxRedirectStrategy());
-
- Source source = new Source(EntityUtils.toString(entity));
- List <NameValuePair> nvps = new ArrayList <NameValuePair>();
- FormFields formFields = source.getFormFields();
-
- List<Element> forms = source.getAllElements(HTMLElementName.FORM);
- Assert.assertEquals("Only one form expected but got " + forms.size(), 1, forms.size());
- String postUrl = forms.get(0).getAttributeValue("action");
-
- Assert.assertNotNull("Form field 'wa' not found", formFields.get("wa"));
- Assert.assertNotNull("Form field 'wresult' not found", formFields.get("wresult"));
-
- for (FormField formField : formFields) {
- if (formField.getUserValueCount() != 0) {
- nvps.add(new BasicNameValuePair(formField.getName(),
- formField.getValues().get(0)));
- }
- }
- HttpPost httppost = new HttpPost(postUrl);
- httppost.setEntity(new UrlEncodedFormEntity(nvps, Consts.UTF_8));
-
- response = httpclient.execute(httppost);
-
- entity = response.getEntity();
- System.out.println(response.getStatusLine());
- Assert.assertTrue("RP HTTP Response code: " + response.getStatusLine().getStatusCode()
- + " [Expected: " + returnCodeRP + "]",
- returnCodeRP == response.getStatusLine().getStatusCode());
-
- if (entity != null) {
- System.out.println("Response content length: " + entity.getContentLength());
- }
-
- return EntityUtils.toString(entity);
- } finally {
- // When HttpClient instance is no longer needed,
- // shut down the connection manager to ensure
- // immediate deallocation of all system resources
- httpclient.getConnectionManager().shutdown();
- }
-
- }
-
}