|
| 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 | +packagecom.ning.http.client.providers.netty; |
| 18 | + |
| 19 | +importcom.ning.http.client.AsyncHttpClient; |
| 20 | +importcom.ning.http.client.AsyncHttpClientConfig; |
| 21 | +importcom.ning.http.client.Response; |
| 22 | +importorg.jboss.netty.logging.InternalLoggerFactory; |
| 23 | +importorg.jboss.netty.logging.Slf4JLoggerFactory; |
| 24 | +importorg.testng.annotations.Test; |
| 25 | + |
| 26 | +importjavax.net.ssl.HostnameVerifier; |
| 27 | +importjavax.net.ssl.SSLSession; |
| 28 | +importjava.io.IOException; |
| 29 | +importjava.net.ConnectException; |
| 30 | +importjava.util.concurrent.ExecutionException; |
| 31 | + |
| 32 | +importstaticorg.testng.Assert.*; |
| 33 | + |
| 34 | +/** |
| 35 | + * |
| 36 | + */ |
| 37 | +publicclassNettyHostnameValidationFailureTest { |
| 38 | + |
| 39 | +@Test(groups ="standalone") |
| 40 | +publicvoidtestHostnameValidationFailure() { |
| 41 | +//InternalLoggerFactory.setDefaultFactory(new Slf4JLoggerFactory()); |
| 42 | + |
| 43 | +AsyncHttpClientConfig.Builderbuilder =newAsyncHttpClientConfig.Builder()// |
| 44 | + .setAllowPoolingConnection(true)// |
| 45 | + .setHostnameVerifier(newHostnameVerifier() { |
| 46 | +publicbooleanverify(Stringarg0,SSLSessionarg1) { |
| 47 | +returnfalse; |
| 48 | + } |
| 49 | + }) |
| 50 | + .setConnectionTimeoutInMs(60000); |
| 51 | +AsyncHttpClientclient =newAsyncHttpClient(builder.build()); |
| 52 | + |
| 53 | +try { |
| 54 | +client.prepareGet("https://www.google.fr").execute().get(); |
| 55 | +fail("Should have thrown an exception"); |
| 56 | + }catch (ExecutionExceptione) { |
| 57 | +assertTrue(e.getMessage().contains("HostnameVerifier exception.")); |
| 58 | + }catch (Exceptione) { |
| 59 | +fail(e.getClass().getCanonicalName()); |
| 60 | + }finally { |
| 61 | +client.close(); |
| 62 | + } |
| 63 | + } |
| 64 | +} |