|
1 | 1 | package com.aliyuncs.http;
|
2 | 2 |
|
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 | + |
4 | 9 | import org.apache.http.HttpHost;
|
5 | 10 | import org.junit.Assert;
|
6 | 11 | import org.junit.Rule;
|
7 | 12 | import org.junit.Test;
|
8 | 13 | import org.junit.rules.ExpectedException;
|
9 | 14 | import org.mockito.Mockito;
|
10 | 15 |
|
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; |
16 | 17 |
|
17 | 18 | public class HttpUtilTest {
|
18 | 19 |
|
@@ -52,14 +53,16 @@ public void testDebugHttpRequest() throws ClientException {
|
52 | 53 | requestHeaders.put("test1", "test1");
|
53 | 54 | requestHeaders.put("test2", "test2");
|
54 | 55 | 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"; |
56 | 58 |
|
57 | 59 | HttpUtil.setIsHttpDebug(true);
|
58 | 60 | HttpUtil.setIsHttpContentDebug(true);
|
59 | 61 | Assert.assertEquals(HttpUtil.debugHttpRequest(request), exceptString);
|
60 | 62 |
|
61 | 63 | 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> "; |
63 | 66 | Assert.assertEquals(HttpUtil.debugHttpRequest(request), exceptString);
|
64 | 67 |
|
65 | 68 | HttpUtil.setIsHttpDebug(false);
|
@@ -102,6 +105,52 @@ public void testDebugHttpResponse() throws ClientException {
|
102 | 105 | HttpUtil.setIsHttpContentDebug("sdk".equalsIgnoreCase(System.getenv("DEBUG")));
|
103 | 106 | }
|
104 | 107 |
|
| 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 | + |
105 | 154 | @Test
|
106 | 155 | public void testGetJDKProxyException() throws ClientException {
|
107 | 156 | HttpRequest request = mock(HttpRequest.class);
|
@@ -157,5 +206,4 @@ public void testNeedProxyHasEnvProxyList() {
|
157 | 206 | boolean need = HttpUtil.needProxy("http://targethost.com", "", "http://www.aliyun.com,http://targethost.com");
|
158 | 207 | Assert.assertFalse(need);
|
159 | 208 | }
|
160 |
| - |
161 | 209 | }
|
0 commit comments