From 01e40264a818da0a5fac4e4b13789b79bef580cd Mon Sep 17 00:00:00 2001 From: Sxci Date: Wed, 18 Nov 2020 11:52:37 +0800 Subject: [PATCH] add ssl test; --- src/main/java/com/qiniu/http/Client.java | 30 +++++---- src/test/java/test/com/qiniu/HttpTest.java | 73 ++++++++++++++++++++++ 2 files changed, 92 insertions(+), 11 deletions(-) diff --git a/src/main/java/com/qiniu/http/Client.java b/src/main/java/com/qiniu/http/Client.java index 51c78eddb..fe93f657d 100755 --- a/src/main/java/com/qiniu/http/Client.java +++ b/src/main/java/com/qiniu/http/Client.java @@ -79,36 +79,43 @@ public void connectStart(Call call, InetSocketAddress inetSocketAddress, Proxy p tag.ip = inetSocketAddress + ""; } }); + /* + * + * List interceptors = new ArrayList<>(); + * interceptors.addAll(client.interceptors()); + * interceptors.add(new RetryAndFollowUpInterceptor(client)); + * interceptors.add(new BridgeInterceptor(client.cookieJar())); + * interceptors.add(new CacheInterceptor(client.internalCache())); + * interceptors.add(new ConnectInterceptor(client)); + * if (!forWebSocket) { + * interceptors.addAll(client.networkInterceptors()); + * } + * interceptors.add(new CallServerInterceptor(forWebSocket)); + * */ builder.addNetworkInterceptor(new Interceptor() { @Override public okhttp3.Response intercept(Chain chain) throws IOException { + // 此时已建立好链接. 获取 ip 放前面, chain.proceed(request) 潜在异常不影响 RemoteSocketAddress 获取 // Request request = chain.request(); - okhttp3.Response response = chain.proceed(request); - IpTag tag = (IpTag) request.tag(); + IpTag tag = (IpTag) request.tag(); // 一定存在 tag // try { tag.ip = chain.connection().socket().getRemoteSocketAddress() + ""; } catch (Exception e) { // ingore } - return response; + return chain.proceed(request); } }); builder.addInterceptor(new Interceptor() { @Override public okhttp3.Response intercept(Chain chain) throws IOException { Request request = chain.request(); - okhttp3.Response response = null; - IOException ex = null; try { - response = chain.proceed(request); + return chain.proceed(request); } catch (IOException e) { IpTag tag = (IpTag) request.tag(); - ex = new IOException(e + " on " + tag.ip, e); + throw new IOException(e + " on " + tag.ip, e); } - if (ex != null) { - throw ex; - } - return response; } }); if (dns != null) { @@ -118,6 +125,7 @@ public List lookup(String hostname) throws UnknownHostException { try { return dns.lookup(hostname); } catch (Exception e) { + // ingore } return okhttp3.Dns.SYSTEM.lookup(hostname); } diff --git a/src/test/java/test/com/qiniu/HttpTest.java b/src/test/java/test/com/qiniu/HttpTest.java index 33f21cfcf..0a4f13f5f 100644 --- a/src/test/java/test/com/qiniu/HttpTest.java +++ b/src/test/java/test/com/qiniu/HttpTest.java @@ -3,13 +3,19 @@ import com.qiniu.common.Constants; import com.qiniu.common.QiniuException; import com.qiniu.http.Client; +import com.qiniu.http.Dns; import com.qiniu.http.ProxyConfiguration; import com.qiniu.http.Response; +import com.qiniu.storage.Configuration; import okhttp3.OkHttpClient; import org.junit.Assert; import org.junit.Test; import java.lang.reflect.Field; +import java.net.InetAddress; +import java.net.UnknownHostException; +import java.util.ArrayList; +import java.util.List; import java.util.concurrent.TimeUnit; public class HttpTest { @@ -99,6 +105,55 @@ public void testProxy() { } } + @Test + public void testSSL() throws NoSuchFieldException, IllegalAccessException { + // www.baidu.com 的 ip,预期报错,报错信息有显示连接的 ip 以及端口 + Configuration cfg = new Configuration(); + cfg.dns = new Dns() { + @Override + public List lookup(String hostname) throws UnknownHostException { + if (hostname.equalsIgnoreCase("www.qiniu.com")) { + List as = new ArrayList<>(); +// byte[] b1 = new byte[] {14, (byte)215, (byte)177, 38}; +// byte[] b2 = new byte[] {61, (byte)135, (byte)185, 32}; +// InetAddress addr1 = InetAddress.getByAddress(b1); +// System.out.println("addr1: " + addr1); +// as.add(addr1); +// as.add(InetAddress.getByAddress(hostname, b2)); + List addresses1 = okhttp3.Dns.SYSTEM.lookup("www.baidu.com"); + for (InetAddress ad : addresses1) { + System.out.println("ad: " + ad); + InetAddress ad1 = InetAddress.getByAddress(hostname, ad.getAddress()); + System.out.println("ad1: " + ad1); + as.add(ad1); + } + return as; + } + return okhttp3.Dns.SYSTEM.lookup(hostname); + } + }; + + Client client0 = new Client(cfg); + try { + Response res = client0.get("https://www.qiniu.com/?v=12345"); + } catch (QiniuException e) { + System.out.println(e.getMessage()); + Assert.assertTrue("https, must have port 443", e.getMessage().indexOf(":443") > 0); + e.printStackTrace(); + } + + try { + Response res = client0.get("https://www.qiniu.com/?v=12345"); + String r = res.toString(); + System.out.println(r); + Assert.assertTrue("https, must have port 443", r.indexOf(":443") > 0); + } catch (QiniuException e) { + System.out.println(e.getMessage()); + Assert.assertTrue("https, must have port 443", e.getMessage().indexOf(":443") > 0); + e.printStackTrace(); + } + } + @Test public void testTimeout() throws NoSuchFieldException, IllegalAccessException { Client client0 = new Client(); @@ -108,6 +163,7 @@ public void testTimeout() throws NoSuchFieldException, IllegalAccessException { System.out.println(r); Assert.assertTrue("https, must have port 443", r.indexOf(":443") > 0); } catch (QiniuException e) { + System.out.println(e.getMessage()); e.printStackTrace(); Assert.fail("should be ok"); } @@ -143,6 +199,7 @@ public void testTimeout() throws NoSuchFieldException, IllegalAccessException { client.get("http://rs.qbox.me/?v=12"); Assert.fail("should be timeout"); } catch (QiniuException e) { + System.out.println(e.getMessage()); e.printStackTrace(); Assert.assertTrue("http, must have port 80", e.getMessage().indexOf(":80") > 10); } @@ -150,6 +207,7 @@ public void testTimeout() throws NoSuchFieldException, IllegalAccessException { client.get("http://rs.qbox.me/?v=12"); Assert.fail("should be timeout"); } catch (QiniuException e) { + System.out.println(e.getMessage()); e.printStackTrace(); Assert.assertTrue("http, must have port 80", e.getMessage().indexOf(":80") > 10); } @@ -163,6 +221,21 @@ public void testTimeout() throws NoSuchFieldException, IllegalAccessException { try { client.get("https://rs.qbox.me/?v=we"); Assert.fail("should be timeout"); + } catch (QiniuException e) { + System.out.println(e.getMessage()); + e.printStackTrace(); + Assert.assertTrue("https, must have port 443", e.getMessage().indexOf(":443") > 10); + } + try { + client.post("http://rs.qbox.me/?v=we", "", null); + Assert.fail("should be timeout"); + } catch (QiniuException e) { + e.printStackTrace(); + Assert.assertTrue("http, must have port 80", e.getMessage().indexOf(":80") > 10); + } + try { + client.post("https://rs.qbox.me/?v=we", "", null); + Assert.fail("should be timeout"); } catch (QiniuException e) { e.printStackTrace(); Assert.assertTrue("https, must have port 443", e.getMessage().indexOf(":443") > 10);