You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hc.apache.org by ol...@apache.org on 2017/01/29 19:22:20 UTC
svn commit: r1780849 [2/2] - in /httpcomponents/httpclient/trunk: ./
httpclient5-cache/ httpclient5-fluent/ httpclient5-testing/
httpclient5-testing/docker/ httpclient5-testing/docker/apache-httpd/
httpclient5-testing/docker/squid/ httpclient5-testing/...
Added: httpcomponents/httpclient/trunk/httpclient5-testing/src/test/java/org/apache/hc/client5/testing/external/HttpClientCompatibilityTest.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient5-testing/src/test/java/org/apache/hc/client5/testing/external/HttpClientCompatibilityTest.java?rev=1780849&view=auto
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient5-testing/src/test/java/org/apache/hc/client5/testing/external/HttpClientCompatibilityTest.java (added)
+++ httpcomponents/httpclient/trunk/httpclient5-testing/src/test/java/org/apache/hc/client5/testing/external/HttpClientCompatibilityTest.java Sun Jan 29 19:22:20 2017
@@ -0,0 +1,270 @@
+/*
+ * ====================================================================
+ * 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.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Apache Software Foundation. For more
+ * information on the Apache Software Foundation, please see
+ * <http://www.apache.org/>.
+ *
+ */
+package org.apache.hc.client5.testing.external;
+
+import java.util.Objects;
+import java.util.concurrent.TimeUnit;
+
+import javax.net.ssl.SSLContext;
+
+import org.apache.hc.client5.http.auth.AuthScope;
+import org.apache.hc.client5.http.auth.Credentials;
+import org.apache.hc.client5.http.auth.UsernamePasswordCredentials;
+import org.apache.hc.client5.http.config.RequestConfig;
+import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManager;
+import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManagerBuilder;
+import org.apache.hc.client5.http.impl.sync.BasicCredentialsProvider;
+import org.apache.hc.client5.http.impl.sync.CloseableHttpClient;
+import org.apache.hc.client5.http.impl.sync.HttpClients;
+import org.apache.hc.client5.http.methods.HttpGet;
+import org.apache.hc.client5.http.methods.HttpOptions;
+import org.apache.hc.client5.http.protocol.HttpClientContext;
+import org.apache.hc.client5.http.ssl.SSLConnectionSocketFactory;
+import org.apache.hc.core5.http.ClassicHttpResponse;
+import org.apache.hc.core5.http.HeaderElements;
+import org.apache.hc.core5.http.HttpHeaders;
+import org.apache.hc.core5.http.HttpHost;
+import org.apache.hc.core5.http.HttpRequest;
+import org.apache.hc.core5.http.HttpStatus;
+import org.apache.hc.core5.http.io.entity.EntityUtils;
+import org.apache.hc.core5.ssl.SSLContexts;
+import org.apache.hc.core5.util.TextUtils;
+
+public class HttpClientCompatibilityTest {
+
+ public static void main(final String... args) throws Exception {
+ final HttpClientCompatibilityTest[] tests = new HttpClientCompatibilityTest[] {
+ new HttpClientCompatibilityTest(
+ new HttpHost("localhost", 8080, "http"), null, null),
+ new HttpClientCompatibilityTest(
+ new HttpHost("test-httpd", 8080, "http"), new HttpHost("localhost", 8888), null),
+ new HttpClientCompatibilityTest(
+ new HttpHost("test-httpd", 8080, "http"), new HttpHost("localhost", 8889),
+ new UsernamePasswordCredentials("squid", "nopassword".toCharArray())),
+ new HttpClientCompatibilityTest(
+ new HttpHost("localhost", 8443, "https"), null, null),
+ new HttpClientCompatibilityTest(
+ new HttpHost("test-httpd", 8443, "https"), new HttpHost("localhost", 8888), null),
+ new HttpClientCompatibilityTest(
+ new HttpHost("test-httpd", 8443, "https"), new HttpHost("localhost", 8889),
+ new UsernamePasswordCredentials("squid", "nopassword".toCharArray()))
+ };
+ for (final HttpClientCompatibilityTest test: tests) {
+ try {
+ test.execute();
+ } finally {
+ test.shutdown();
+ }
+ }
+ }
+
+ private final HttpHost target;
+ private final HttpHost proxy;
+ private final BasicCredentialsProvider credentialsProvider;
+ private final PoolingHttpClientConnectionManager connManager;
+ private final CloseableHttpClient client;
+
+ HttpClientCompatibilityTest(
+ final HttpHost target,
+ final HttpHost proxy,
+ final Credentials proxyCreds) throws Exception {
+ this.target = target;
+ this.proxy = proxy;
+ this.credentialsProvider = new BasicCredentialsProvider();
+ final RequestConfig requestConfig = RequestConfig.custom()
+ .setProxy(proxy)
+ .build();
+ if (proxy != null && proxyCreds != null) {
+ this.credentialsProvider.setCredentials(new AuthScope(proxy), proxyCreds);
+ }
+ final SSLContext sslContext = SSLContexts.custom()
+ .loadTrustMaterial(getClass().getResource("/test-ca.keystore"), "nopassword".toCharArray()).build();
+ this.connManager = PoolingHttpClientConnectionManagerBuilder.create()
+ .setSSLSocketFactory(new SSLConnectionSocketFactory(sslContext))
+ .build();
+ this.client = HttpClients.custom()
+ .setConnectionManager(this.connManager)
+ .setDefaultRequestConfig(requestConfig)
+ .build();
+ }
+
+ void shutdown() throws Exception {
+ client.close();
+ }
+
+ enum TestResult {OK, NOK};
+
+ private void logResult(final TestResult result, final HttpRequest request, final String message) {
+ final StringBuilder buf = new StringBuilder();
+ buf.append(result);
+ if (buf.length() == 2) {
+ buf.append(" ");
+ }
+ buf.append(": ").append(target);
+ if (proxy != null) {
+ buf.append(" via ").append(proxy);
+ }
+ buf.append(": ");
+ buf.append(request.getMethod()).append(" ").append(request.getRequestUri());
+ if (message != null && !TextUtils.isBlank(message)) {
+ buf.append(" -> ").append(message);
+ }
+ System.out.println(buf.toString());
+ }
+
+ void execute() {
+
+ // Initial ping
+ {
+ final HttpClientContext context = HttpClientContext.create();
+ context.setCredentialsProvider(credentialsProvider);
+ final HttpOptions options = new HttpOptions("*");
+ try (ClassicHttpResponse response = client.execute(target, options, context)) {
+ final int code = response.getCode();
+ EntityUtils.consume(response.getEntity());
+ if (code == HttpStatus.SC_OK) {
+ logResult(TestResult.OK, options, Objects.toString(response.getFirstHeader("server")));
+ } else {
+ logResult(TestResult.NOK, options, "(status " + code + ")");
+ }
+ } catch (Exception ex) {
+ logResult(TestResult.NOK, options, "(" + ex.getMessage() + ")");
+ }
+ }
+ // Basic GET requests
+ {
+ connManager.closeIdle(0, TimeUnit.MILLISECONDS);
+ final HttpClientContext context = HttpClientContext.create();
+ context.setCredentialsProvider(credentialsProvider);
+ final String[] requestUris = new String[] {"/", "/news.html", "/status.html"};
+ for (String requestUri: requestUris) {
+ final HttpGet httpGet = new HttpGet(requestUri);
+ try (ClassicHttpResponse response = client.execute(target, httpGet, context)) {
+ final int code = response.getCode();
+ EntityUtils.consume(response.getEntity());
+ if (code == HttpStatus.SC_OK) {
+ logResult(TestResult.OK, httpGet, "200");
+ } else {
+ logResult(TestResult.NOK, httpGet, "(status " + code + ")");
+ }
+ } catch (Exception ex) {
+ logResult(TestResult.NOK, httpGet, "(" + ex.getMessage() + ")");
+ }
+ }
+ }
+ // Wrong target auth scope
+ {
+ connManager.closeIdle(0, TimeUnit.MILLISECONDS);
+ credentialsProvider.setCredentials(
+ new AuthScope("otherhost", AuthScope.ANY_PORT, "Restricted Files"),
+ new UsernamePasswordCredentials("testuser", "nopassword".toCharArray()));
+ final HttpClientContext context = HttpClientContext.create();
+ context.setCredentialsProvider(credentialsProvider);
+
+ final HttpGet httpGetSecret = new HttpGet("/private/big-secret.txt");
+ try (ClassicHttpResponse response = client.execute(target, httpGetSecret, context)) {
+ final int code = response.getCode();
+ EntityUtils.consume(response.getEntity());
+ if (code == HttpStatus.SC_UNAUTHORIZED) {
+ logResult(TestResult.OK, httpGetSecret, "401 (wrong target auth scope)");
+ } else {
+ logResult(TestResult.NOK, httpGetSecret, "(status " + code + ")");
+ }
+ } catch (Exception ex) {
+ logResult(TestResult.NOK, httpGetSecret, "(" + ex.getMessage() + ")");
+ }
+ }
+ // Wrong target credentials
+ {
+ connManager.closeIdle(0, TimeUnit.MILLISECONDS);
+ credentialsProvider.setCredentials(
+ new AuthScope(target),
+ new UsernamePasswordCredentials("testuser", "wrong password".toCharArray()));
+ final HttpClientContext context = HttpClientContext.create();
+ context.setCredentialsProvider(credentialsProvider);
+
+ final HttpGet httpGetSecret = new HttpGet("/private/big-secret.txt");
+ try (ClassicHttpResponse response = client.execute(target, httpGetSecret, context)) {
+ final int code = response.getCode();
+ EntityUtils.consume(response.getEntity());
+ if (code == HttpStatus.SC_UNAUTHORIZED) {
+ logResult(TestResult.OK, httpGetSecret, "401 (wrong target creds)");
+ } else {
+ logResult(TestResult.NOK, httpGetSecret, "(status " + code + ")");
+ }
+ } catch (Exception ex) {
+ logResult(TestResult.NOK, httpGetSecret, "(" + ex.getMessage() + ")");
+ }
+ }
+ // Correct target credentials
+ {
+ connManager.closeIdle(0, TimeUnit.MILLISECONDS);
+ credentialsProvider.setCredentials(
+ new AuthScope(target),
+ new UsernamePasswordCredentials("testuser", "nopassword".toCharArray()));
+ final HttpClientContext context = HttpClientContext.create();
+ context.setCredentialsProvider(credentialsProvider);
+
+ final HttpGet httpGetSecret = new HttpGet("/private/big-secret.txt");
+ try (ClassicHttpResponse response = client.execute(target, httpGetSecret, context)) {
+ final int code = response.getCode();
+ EntityUtils.consume(response.getEntity());
+ if (code == HttpStatus.SC_OK) {
+ logResult(TestResult.OK, httpGetSecret, "200 (correct target creds)");
+ } else {
+ logResult(TestResult.NOK, httpGetSecret, "(status " + code + ")");
+ }
+ } catch (Exception ex) {
+ logResult(TestResult.NOK, httpGetSecret, "(" + ex.getMessage() + ")");
+ }
+ }
+ // Correct target credentials (no keep-alive)
+ {
+ connManager.closeIdle(0, TimeUnit.MILLISECONDS);
+ credentialsProvider.setCredentials(
+ new AuthScope(target),
+ new UsernamePasswordCredentials("testuser", "nopassword".toCharArray()));
+ final HttpClientContext context = HttpClientContext.create();
+ context.setCredentialsProvider(credentialsProvider);
+
+ final HttpGet httpGetSecret = new HttpGet("/private/big-secret.txt");
+ httpGetSecret.setHeader(HttpHeaders.CONNECTION, HeaderElements.CLOSE);
+ try (ClassicHttpResponse response = client.execute(target, httpGetSecret, context)) {
+ final int code = response.getCode();
+ EntityUtils.consume(response.getEntity());
+ if (code == HttpStatus.SC_OK) {
+ logResult(TestResult.OK, httpGetSecret, "200 (correct target creds / no keep-alive)");
+ } else {
+ logResult(TestResult.NOK, httpGetSecret, "(status " + code + ")");
+ }
+ } catch (Exception ex) {
+ logResult(TestResult.NOK, httpGetSecret, "(" + ex.getMessage() + ")");
+ }
+ }
+ }
+
+}
Propchange: httpcomponents/httpclient/trunk/httpclient5-testing/src/test/java/org/apache/hc/client5/testing/external/HttpClientCompatibilityTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: httpcomponents/httpclient/trunk/httpclient5-testing/src/test/java/org/apache/hc/client5/testing/external/HttpClientCompatibilityTest.java
------------------------------------------------------------------------------
svn:keywords = Date Revision
Propchange: httpcomponents/httpclient/trunk/httpclient5-testing/src/test/java/org/apache/hc/client5/testing/external/HttpClientCompatibilityTest.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: httpcomponents/httpclient/trunk/httpclient5-testing/src/test/resources/log4j2-debug.xml.template
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient5-testing/src/test/resources/log4j2-debug.xml.template?rev=1780849&view=auto
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient5-testing/src/test/resources/log4j2-debug.xml.template (added)
+++ httpcomponents/httpclient/trunk/httpclient5-testing/src/test/resources/log4j2-debug.xml.template Sun Jan 29 19:22:20 2017
@@ -0,0 +1,31 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ 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.
+-->
+<Configuration status="WARN" name="XMLConfigTest">
+ <Appenders>
+ <Console name="STDOUT">
+ <PatternLayout pattern="%d %-5level [%t][%logger]%notEmpty{[%markerSimpleName]} %msg%n%xThrowable"/>
+ </Console>
+ </Appenders>
+ <Loggers>
+ <Root level="warn">
+ <AppenderRef ref="STDOUT"/>
+ </Root>
+ <Logger name="org.apache.hc.client5.http.headers" level="debug"/>
+ <Logger name="org.apache.hc.client5.http.wire" level="error"/>
+ </Loggers>
+</Configuration>
\ No newline at end of file
Added: httpcomponents/httpclient/trunk/httpclient5-testing/src/test/resources/log4j2.xml
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient5-testing/src/test/resources/log4j2.xml?rev=1780849&view=auto
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient5-testing/src/test/resources/log4j2.xml (added)
+++ httpcomponents/httpclient/trunk/httpclient5-testing/src/test/resources/log4j2.xml Sun Jan 29 19:22:20 2017
@@ -0,0 +1,29 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ 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.
+-->
+<Configuration status="WARN" name="XMLConfigTest">
+ <Appenders>
+ <Console name="STDOUT">
+ <PatternLayout pattern="%d %-5level [%t][%logger]%notEmpty{[%markerSimpleName]} %msg%n%xThrowable" />
+ </Console>
+ </Appenders>
+ <Loggers>
+ <Root level="fatal">
+ <AppenderRef ref="STDOUT" />
+ </Root>
+ </Loggers>
+</Configuration>
\ No newline at end of file
Propchange: httpcomponents/httpclient/trunk/httpclient5-testing/src/test/resources/log4j2.xml
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: httpcomponents/httpclient/trunk/httpclient5-testing/src/test/resources/log4j2.xml
------------------------------------------------------------------------------
svn:keywords = Date Revision
Propchange: httpcomponents/httpclient/trunk/httpclient5-testing/src/test/resources/log4j2.xml
------------------------------------------------------------------------------
svn:mime-type = text/xml
Added: httpcomponents/httpclient/trunk/httpclient5-testing/src/test/resources/test-ca.keystore
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient5-testing/src/test/resources/test-ca.keystore?rev=1780849&view=auto
==============================================================================
Binary files httpcomponents/httpclient/trunk/httpclient5-testing/src/test/resources/test-ca.keystore (added) and httpcomponents/httpclient/trunk/httpclient5-testing/src/test/resources/test-ca.keystore Sun Jan 29 19:22:20 2017 differ
Modified: httpcomponents/httpclient/trunk/httpclient5/pom.xml
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient5/pom.xml?rev=1780849&r1=1780848&r2=1780849&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient5/pom.xml (original)
+++ httpcomponents/httpclient/trunk/httpclient5/pom.xml Sun Jan 29 19:22:20 2017
@@ -126,7 +126,7 @@
<quiet>true</quiet>
<source>${maven.compiler.source}</source>
<links>
- <link>http://docs.oracle.com/javase/6/docs/api/</link>
+ <link>http://docs.oracle.com/javase/7/docs/api/</link>
<link>http://hc.apache.org/httpcomponents-core-ga/httpcore/apidocs/</link>
</links>
</configuration>
Modified: httpcomponents/httpclient/trunk/httpclient5/src/main/java/org/apache/hc/client5/http/impl/sync/InternalHttpClient.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient5/src/main/java/org/apache/hc/client5/http/impl/sync/InternalHttpClient.java?rev=1780849&r1=1780848&r2=1780849&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient5/src/main/java/org/apache/hc/client5/http/impl/sync/InternalHttpClient.java (original)
+++ httpcomponents/httpclient/trunk/httpclient5/src/main/java/org/apache/hc/client5/http/impl/sync/InternalHttpClient.java Sun Jan 29 19:22:20 2017
@@ -140,10 +140,10 @@ class InternalHttpClient extends Closeab
execAware = (HttpExecutionAware) request;
}
try {
- if (request.getScheme() == null) {
+ if (request.getScheme() == null && target != null) {
request.setScheme(target.getSchemeName());
}
- if (request.getAuthority() == null) {
+ if (request.getAuthority() == null && target != null) {
request.setAuthority(new URIAuthority(target));
}
final HttpClientContext localcontext = HttpClientContext.adapt(
Modified: httpcomponents/httpclient/trunk/pom.xml
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/pom.xml?rev=1780849&r1=1780848&r2=1780849&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/pom.xml (original)
+++ httpcomponents/httpclient/trunk/pom.xml Sun Jan 29 19:22:20 2017
@@ -161,6 +161,7 @@
<module>httpclient5-fluent</module>
<module>httpclient5-cache</module>
<module>httpclient5-win</module>
+ <module>httpclient5-testing</module>
<module>httpclient5-osgi</module>
</modules>