|
| 1 | +/* |
| 2 | + * Copyright 2014 AsyncHttpClient Project |
| 3 | + * |
| 4 | + * AsyncHttpClient Project licenses this file to you under the Apache License, version 2.0 |
| 5 | + * (the "License"); you may not use this file except in compliance with the |
| 6 | + * License. You may obtain a copy of the License at: |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | + * License for the specific language governing permissions and limitations |
| 14 | + * under the License. |
| 15 | + * |
| 16 | + */ |
| 17 | +package com.ning.http.client.providers.netty; |
| 18 | + |
| 19 | +import com.ning.http.client.AsyncHttpClient; |
| 20 | +import com.ning.http.client.AsyncHttpClientConfig; |
| 21 | +import com.ning.http.client.Response; |
| 22 | +import org.jboss.netty.logging.InternalLoggerFactory; |
| 23 | +import org.jboss.netty.logging.Slf4JLoggerFactory; |
| 24 | +import org.testng.annotations.Test; |
| 25 | + |
| 26 | +import javax.net.ssl.HostnameVerifier; |
| 27 | +import javax.net.ssl.SSLSession; |
| 28 | +import java.io.IOException; |
| 29 | +import java.net.ConnectException; |
| 30 | +import java.util.concurrent.ExecutionException; |
| 31 | + |
| 32 | +import static org.testng.Assert.*; |
| 33 | + |
| 34 | +/** |
| 35 | + * |
| 36 | + */ |
| 37 | +public class NettyHostnameValidationFailureTest { |
| 38 | + |
| 39 | + @Test(groups = "standalone") |
| 40 | + public void testHostnameValidationFailure() { |
| 41 | + //InternalLoggerFactory.setDefaultFactory(new Slf4JLoggerFactory()); |
| 42 | + |
| 43 | + AsyncHttpClientConfig.Builder builder = new AsyncHttpClientConfig.Builder()// |
| 44 | + .setAllowPoolingConnection(true)// |
| 45 | + .setHostnameVerifier(new HostnameVerifier() { |
| 46 | + public boolean verify(String arg0, SSLSession arg1) { |
| 47 | + return false; |
| 48 | + } |
| 49 | + }) |
| 50 | + .setConnectionTimeoutInMs(60000); |
| 51 | + AsyncHttpClient client = new AsyncHttpClient(builder.build()); |
| 52 | + |
| 53 | + try { |
| 54 | + client.prepareGet("https://www.google.fr").execute().get(); |
| 55 | + fail("Should have thrown an exception"); |
| 56 | + } catch (ExecutionException e) { |
| 57 | + assertTrue(e.getMessage().contains("HostnameVerifier exception.")); |
| 58 | + } catch (Exception e) { |
| 59 | + fail(e.getClass().getCanonicalName()); |
| 60 | + } finally { |
| 61 | + client.close(); |
| 62 | + } |
| 63 | + } |
| 64 | +} |
0 commit comments