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

Feat: support disable HTTP/2 in requests #175

Merged
merged 1 commit into from
Jan 25, 2024
Merged
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
2 changes: 1 addition & 1 deletion src/main/java/com/aliyun/tea/okhttp/ClientHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public static OkHttpClient getOkHttpClient(String host, int port, Map<String, Ob

public static OkHttpClient creatClient(Map<String, Object> map) {
OkHttpClientBuilder builder = new OkHttpClientBuilder();
builder = builder.connectTimeout(map).readTimeout(map).connectionPool(map).certificate(map).proxy(map).proxyAuthenticator(map);
builder = builder.protocols(map).connectTimeout(map).readTimeout(map).connectionPool(map).certificate(map).proxy(map).proxyAuthenticator(map);
OkHttpClient client = builder.buildOkHttpClient();
return client;
}
Expand Down
16 changes: 16 additions & 0 deletions src/main/java/com/aliyun/tea/okhttp/OkHttpClientBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import java.security.SecureRandom;
import java.security.cert.Certificate;
import java.security.cert.CertificateFactory;
import java.util.Collections;
import java.util.Map;
import java.util.concurrent.TimeUnit;

Expand All @@ -27,6 +28,21 @@ public OkHttpClientBuilder() {
builder = new OkHttpClient().newBuilder();
}

public OkHttpClientBuilder protocols(Map<String, Object> map) {
if (map.containsKey("disableHttp2") && null != map.get("disableHttp2")) {
Object object = map.get("disableHttp2");
boolean disableHttp2 = false;
try {
disableHttp2 = Boolean.parseBoolean(String.valueOf(object));
} catch (Exception ignored) {
}
if (disableHttp2) {
this.builder.protocols(Collections.singletonList(Protocol.HTTP_1_1));
}
}
return this;
}

public OkHttpClientBuilder connectTimeout(Map<String, Object> map) {
Object object = map.get("connectTimeout");
long timeout;
Expand Down
44 changes: 34 additions & 10 deletions src/test/java/com/aliyun/tea/TeaTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,17 @@
import com.aliyun.tea.interceptor.InterceptorChain;
import com.aliyun.tea.interceptor.MockRequestInterceptor;
import com.aliyun.tea.interceptor.MockResponseInterceptor;
import com.aliyun.tea.interceptor.MockRuntimeOptionsInterceptor;
import com.aliyun.tea.okhttp.ClientHelper;
import com.aliyun.tea.utils.X509TrustManagerImp;
import okhttp3.Protocol;
import org.junit.Assert;
import org.junit.Test;
import org.powermock.reflect.Whitebox;

import javax.net.ssl.SSLSocketFactory;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.nio.charset.StandardCharsets;
import java.security.cert.X509Certificate;
import java.util.HashMap;
import java.util.Map;

import static org.mockito.Mockito.mock;

public class TeaTest {
@Test
public void init() {
Expand Down Expand Up @@ -104,6 +95,39 @@ public void doActionTest() {
}
}

@Test
public void doActionProtocolsTest() {
ClientHelper.clients.clear();
TeaRequest request = new TeaRequest();
Map<String, String> map = new HashMap<>();
map.put("host", "github.com");
request.protocol = "http";
request.headers = map;
request.method = "GET";
Map<String, Object> runtimeOptions = new HashMap<>();
TeaResponse response = Tea.doAction(request, runtimeOptions);
Assert.assertEquals(200, response.statusCode);
Assert.assertEquals(Protocol.HTTP_2, response.response.protocol());

ClientHelper.clients.clear();
runtimeOptions.put("disableHttp2", "test");
response = Tea.doAction(request, runtimeOptions);
Assert.assertEquals(200, response.statusCode);
Assert.assertEquals(Protocol.HTTP_2, response.response.protocol());

ClientHelper.clients.clear();
runtimeOptions.put("disableHttp2", false);
response = Tea.doAction(request, runtimeOptions);
Assert.assertEquals(200, response.statusCode);
Assert.assertEquals(Protocol.HTTP_2, response.response.protocol());

ClientHelper.clients.clear();
runtimeOptions.put("disableHttp2", true);
response = Tea.doAction(request, runtimeOptions);
Assert.assertEquals(200, response.statusCode);
Assert.assertEquals(Protocol.HTTP_1_1, response.response.protocol());
}

@Test
public void doActionWithProxyTest() {
ClientHelper.clients.clear();
Expand Down
23 changes: 23 additions & 0 deletions src/test/java/com/aliyun/tea/okhttp/OkHttpClientBuilderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.aliyun.tea.TeaException;
import com.aliyun.tea.utils.TrueHostnameVerifier;
import okhttp3.OkHttpClient;
import okhttp3.Protocol;
import org.junit.Assert;
import org.junit.Test;
import org.mockito.Mockito;
Expand Down Expand Up @@ -144,4 +145,26 @@ public void proxyTest() throws Exception {
clientBuilder.proxy(map);
Mockito.verify(clientBuilder, Mockito.times(5)).proxy(map);
}

@Test
public void protocolsTest() {
map.clear();
OkHttpClientBuilder clientBuilder = new OkHttpClientBuilder();
OkHttpClient client = clientBuilder.protocols(map).buildOkHttpClient();
Assert.assertEquals(2, client.protocols().size());
Assert.assertTrue(client.protocols().contains(Protocol.HTTP_2));
Assert.assertTrue(client.protocols().contains(Protocol.HTTP_1_1));
// HTTP_1_0 在 OkHttp 中已不支持
Assert.assertFalse(client.protocols().contains(Protocol.HTTP_1_0));

map.clear();
clientBuilder = new OkHttpClientBuilder();
map.put("disableHttp2", true);
client = clientBuilder.protocols(map).buildOkHttpClient();
Assert.assertEquals(1, client.protocols().size());
Assert.assertFalse(client.protocols().contains(Protocol.HTTP_2));
Assert.assertTrue(client.protocols().contains(Protocol.HTTP_1_1));
// HTTP_1_0 在 OkHttp 中已不支持
Assert.assertFalse(client.protocols().contains(Protocol.HTTP_1_0));
}
}
Loading