Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2688f31

Browse files
committed
add http debug info
1 parent 137bb06 commit 2688f31

File tree

2 files changed

+98
-24
lines changed

2 files changed

+98
-24
lines changed

aliyun-java-sdk-core/src/main/java/com/aliyuncs/http/HttpUtil.java

Lines changed: 41 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,23 @@
11
package com.aliyuncs.http;
22

3-
import com.aliyuncs.exceptions.ClientException;
4-
import com.aliyuncs.utils.StringUtils;
5-
import org.apache.commons.logging.Log;
6-
import org.apache.commons.logging.LogFactory;
7-
import org.apache.http.HttpHost;
8-
9-
import javax.xml.bind.DatatypeConverter;
103
import java.io.IOException;
114
import java.net.InetSocketAddress;
5+
import java.net.MalformedURLException;
126
import java.net.Proxy;
137
import java.net.SocketAddress;
148
import java.net.URL;
159
import java.util.Map;
1610
import java.util.Map.Entry;
1711

12+
import javax.xml.bind.DatatypeConverter;
13+
14+
import org.apache.commons.logging.Log;
15+
import org.apache.commons.logging.LogFactory;
16+
import org.apache.http.HttpHost;
17+
18+
import com.aliyuncs.exceptions.ClientException;
19+
import com.aliyuncs.utils.StringUtils;
20+
1821
public class HttpUtil {
1922

2023
private final static Log log = LogFactory.getLog(HttpUtil.class);
@@ -45,16 +48,32 @@ public static void setIsHttpContentDebug(Boolean isHttpContentDebug) {
4548
HttpUtil.isHttpContentDebug = isHttpContentDebug;
4649
}
4750

48-
public static String debugHttpRequest(HttpRequest request) throws ClientException {
51+
public static String debugHttpRequest(HttpRequest request) {
4952
if (isHttpDebug) {
50-
String protocol = request.getSysUrl().split("://")[0].toUpperCase() + "/1.1";
51-
StringBuilder debugString = new StringBuilder("> " + request.getSysMethod() + " " + protocol + "\n> ");
53+
StringBuilder debugString = new StringBuilder();
54+
55+
String sysUrl = request.getSysUrl();
56+
URL url = null;
57+
try {
58+
url = new URL(sysUrl);
59+
debugString.append("> " + request.getSysMethod() + " " + url.getProtocol().toUpperCase() + "/1.1\n> ");
60+
debugString.append("Host : " + url.getHost() + "\n> ");
61+
} catch (MalformedURLException e) {
62+
debugString.append("> " + request.getSysMethod() + " " + sysUrl + "\n> ");
63+
debugString.append("Host : " + sysUrl + "\n> ");
64+
}
5265
Map<String, String> requestHeaders = request.getSysHeaders();
5366
for (Entry<String, String> entry : requestHeaders.entrySet()) {
5467
debugString.append(entry.getKey() + " : " + entry.getValue() + "\n> ");
5568
}
69+
debugString.append("Request URL : " + sysUrl + "\n> ");
5670
if (isHttpContentDebug) {
57-
debugString.append("\n" + request.getHttpContentString());
71+
try {
72+
debugString.append("\n" + request.getHttpContentString());
73+
} catch (ClientException e) {
74+
debugString.append("\n" + "Can not parse response due to unsupported encoding : " + request
75+
.getSysEncoding());
76+
}
5877
}
5978
log.info("\n" + debugString);
6079
return debugString.toString();
@@ -63,16 +82,22 @@ public static String debugHttpRequest(HttpRequest request) throws ClientExceptio
6382
}
6483
}
6584

66-
public static String debugHttpResponse(HttpResponse response) throws ClientException {
85+
public static String debugHttpResponse(HttpResponse response) {
6786
if (isHttpDebug) {
87+
StringBuilder debugString = new StringBuilder();
6888
String protocol = "HTTP/1.1";
69-
StringBuilder debugString = new StringBuilder("< " + protocol + " " + response.getStatus() + "\n< ");
89+
debugString.append("< " + protocol + " " + response.getStatus() + "\n< ");
7090
Map<String, String> responseHeaders = response.getSysHeaders();
7191
for (Entry<String, String> entry : responseHeaders.entrySet()) {
7292
debugString.append(entry.getKey() + " : " + entry.getValue() + "\n< ");
7393
}
7494
if (isHttpContentDebug) {
75-
debugString.append("\n" + response.getHttpContentString());
95+
try {
96+
debugString.append("\n" + response.getHttpContentString());
97+
} catch (ClientException e) {
98+
debugString.append("\n" + "Can not parse response due to unsupported encoding : " + response
99+
.getSysEncoding());
100+
}
76101
}
77102
log.info("\n" + debugString);
78103
return debugString.toString();
@@ -110,7 +135,8 @@ public static Proxy getJDKProxy(String clientProxy, String envProxy, HttpRequest
110135
return proxy;
111136
}
112137

113-
public static HttpHost getApacheProxy(String clientProxy, String envProxy, HttpRequest request) throws ClientException {
138+
public static HttpHost getApacheProxy(String clientProxy, String envProxy, HttpRequest request)
139+
throws ClientException {
114140
try {
115141
String proxyStr = (!StringUtils.isEmpty(clientProxy) ? clientProxy : envProxy);
116142
if (StringUtils.isEmpty(proxyStr)) {

aliyun-java-sdk-core/src/test/java/com/aliyuncs/http/HttpUtilTest.java

Lines changed: 57 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
package com.aliyuncs.http;
22

3-
import com.aliyuncs.exceptions.ClientException;
3+
import static org.mockito.Mockito.mock;
4+
5+
import java.net.Proxy;
6+
import java.util.HashMap;
7+
import java.util.Map;
8+
49
import org.apache.http.HttpHost;
510
import org.junit.Assert;
611
import org.junit.Rule;
712
import org.junit.Test;
813
import org.junit.rules.ExpectedException;
914
import org.mockito.Mockito;
1015

11-
import java.net.Proxy;
12-
import java.util.HashMap;
13-
import java.util.Map;
14-
15-
import static org.mockito.Mockito.mock;
16+
import com.aliyuncs.exceptions.ClientException;
1617

1718
public class HttpUtilTest {
1819

@@ -52,14 +53,16 @@ public void testDebugHttpRequest() throws ClientException {
5253
requestHeaders.put("test1", "test1");
5354
requestHeaders.put("test2", "test2");
5455
Mockito.when(request.getSysHeaders()).thenReturn(requestHeaders);
55-
String exceptString = "> GET HTTP/1.1\n> test2 : test2\n> test1 : test1\n> \nrequest body";
56+
String exceptString = "> GET HTTP/1.1\n> Host : test.domain\n> test2 : test2\n> test1 : test1\n> "
57+
+ "Request URL : http://test.domain\n> \nrequest body";
5658

5759
HttpUtil.setIsHttpDebug(true);
5860
HttpUtil.setIsHttpContentDebug(true);
5961
Assert.assertEquals(HttpUtil.debugHttpRequest(request), exceptString);
6062

6163
HttpUtil.setIsHttpContentDebug(false);
62-
exceptString = "> GET HTTP/1.1\n> test2 : test2\n> test1 : test1\n> ";
64+
exceptString = "> GET HTTP/1.1\n> Host : test.domain\n> test2 : test2\n> test1 : test1\n> "
65+
+ "Request URL : http://test.domain\n> ";
6366
Assert.assertEquals(HttpUtil.debugHttpRequest(request), exceptString);
6467

6568
HttpUtil.setIsHttpDebug(false);
@@ -102,6 +105,52 @@ public void testDebugHttpResponse() throws ClientException {
102105
HttpUtil.setIsHttpContentDebug("sdk".equalsIgnoreCase(System.getenv("DEBUG")));
103106
}
104107

108+
@Test
109+
public void testDebugHttpRquestException() throws ClientException {
110+
HttpRequest request = mock(HttpRequest.class);
111+
Mockito.when(request.getSysMethod()).thenReturn(MethodType.GET);
112+
Mockito.when(request.getSysUrl()).thenReturn("httpss://test.domain/jdj");
113+
Map<String, String> requestHeaders = new HashMap<String, String>();
114+
Mockito.when(request.getHttpContentString()).thenReturn("request body");
115+
requestHeaders.put("test1", "test1");
116+
requestHeaders.put("test2", "test2");
117+
Mockito.when(request.getSysHeaders()).thenReturn(requestHeaders);
118+
String exceptString = "> GET httpss://test.domain/jdj\n> Host : httpss://test.domain/jdj\n> "
119+
+ "test2 : test2\n> test1 : test1\n> Request URL : httpss://test.domain/jdj\n> \nrequest body";
120+
HttpUtil.setIsHttpDebug(true);
121+
HttpUtil.setIsHttpContentDebug(true);
122+
Assert.assertEquals(HttpUtil.debugHttpRequest(request), exceptString);
123+
124+
Mockito.when(request.getSysUrl()).thenReturn("http://test.domain/jdj");
125+
Mockito.doThrow(ClientException.class).when(request).getHttpContentString();
126+
Mockito.when(request.getSysEncoding()).thenReturn("HHH");
127+
exceptString = "> GET HTTP/1.1\n> Host : test.domain\n> "
128+
+ "test2 : test2\n> test1 : test1\n> Request URL : http://test.domain/jdj\n> \n"
129+
+ "Can not parse response due to unsupported encoding : HHH";
130+
Assert.assertEquals(HttpUtil.debugHttpRequest(request), exceptString);
131+
HttpUtil.setIsHttpDebug("sdk".equalsIgnoreCase(System.getenv("DEBUG")));
132+
HttpUtil.setIsHttpContentDebug("sdk".equalsIgnoreCase(System.getenv("DEBUG")));
133+
}
134+
135+
@Test
136+
public void testDebugHttpResponseException() throws ClientException {
137+
HttpResponse response = mock(HttpResponse.class);
138+
Mockito.when(response.getStatus()).thenReturn(200);
139+
Mockito.doThrow(ClientException.class).when(response).getHttpContentString();
140+
Mockito.when(response.getSysEncoding()).thenReturn("HHH");
141+
Map<String, String> reasponseHeaders = new HashMap<String, String>();
142+
reasponseHeaders.put("test1", "test1");
143+
reasponseHeaders.put("test2", "test2");
144+
Mockito.when(response.getSysHeaders()).thenReturn(reasponseHeaders);
145+
String exceptString = "< HTTP/1.1 200\n< test2 : test2\n< test1 : test1\n< \n"
146+
+ "Can not parse response due to unsupported encoding : HHH";
147+
HttpUtil.setIsHttpDebug(true);
148+
HttpUtil.setIsHttpContentDebug(true);
149+
Assert.assertEquals(HttpUtil.debugHttpResponse(response), exceptString);
150+
HttpUtil.setIsHttpDebug("sdk".equalsIgnoreCase(System.getenv("DEBUG")));
151+
HttpUtil.setIsHttpContentDebug("sdk".equalsIgnoreCase(System.getenv("DEBUG")));
152+
}
153+
105154
@Test
106155
public void testGetJDKProxyException() throws ClientException {
107156
HttpRequest request = mock(HttpRequest.class);
@@ -157,5 +206,4 @@ public void testNeedProxyHasEnvProxyList() {
157206
boolean need = HttpUtil.needProxy("http://targethost.com", "", "http://www.aliyun.com,http://targethost.com");
158207
Assert.assertFalse(need);
159208
}
160-
161209
}

0 commit comments

Comments
 (0)