Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add ssl test; #502

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 19 additions & 11 deletions src/main/java/com/qiniu/http/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,36 +79,43 @@ public void connectStart(Call call, InetSocketAddress inetSocketAddress, Proxy p
tag.ip = inetSocketAddress + "";
}
});
/*
*
* List<Interceptor> 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) {
Expand All @@ -118,6 +125,7 @@ public List<InetAddress> lookup(String hostname) throws UnknownHostException {
try {
return dns.lookup(hostname);
} catch (Exception e) {
// ingore
}
return okhttp3.Dns.SYSTEM.lookup(hostname);
}
Expand Down
73 changes: 73 additions & 0 deletions src/test/java/test/com/qiniu/HttpTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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<InetAddress> lookup(String hostname) throws UnknownHostException {
if (hostname.equalsIgnoreCase("www.qiniu.com")) {
List<InetAddress> 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<InetAddress> 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();
Expand All @@ -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");
}
Expand Down Expand Up @@ -143,13 +199,15 @@ 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);
}
try {
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);
}
Expand All @@ -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);
Expand Down