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

https://github.com/aliyun/aliyun-openapi-java-sdk · Java · 209 lines · 179 code · 30 blank · 0 comment · 4 complexity · e779962d73ff720ae620572fe2513da4 MD5 · raw file

  1. package com.aliyuncs.http;
  2. import static org.mockito.Mockito.mock;
  3. import java.net.Proxy;
  4. import java.util.HashMap;
  5. import java.util.Map;
  6. import org.apache.http.HttpHost;
  7. import org.junit.Assert;
  8. import org.junit.Rule;
  9. import org.junit.Test;
  10. import org.junit.rules.ExpectedException;
  11. import org.mockito.Mockito;
  12. import com.aliyuncs.exceptions.ClientException;
  13. public class HttpUtilTest {
  14. @Rule
  15. public ExpectedException thrown = ExpectedException.none();
  16. @Test
  17. public void testHttpDebug() {
  18. HttpUtil httpUtil = new HttpUtil();
  19. Assert.assertEquals(HttpUtil.getIsHttpDebug(), "sdk".equalsIgnoreCase(System.getenv("DEBUG")));
  20. Assert.assertEquals(HttpUtil.getIsHttpContentDebug(), "sdk".equalsIgnoreCase(System.getenv("DEBUG")));
  21. if (HttpUtil.getIsHttpDebug()) {
  22. HttpUtil.setIsHttpDebug(false);
  23. Assert.assertFalse(HttpUtil.getIsHttpDebug());
  24. } else {
  25. HttpUtil.setIsHttpDebug(true);
  26. Assert.assertTrue(HttpUtil.getIsHttpDebug());
  27. }
  28. if (HttpUtil.getIsHttpContentDebug()) {
  29. HttpUtil.setIsHttpContentDebug(false);
  30. Assert.assertFalse(HttpUtil.getIsHttpContentDebug());
  31. } else {
  32. HttpUtil.setIsHttpContentDebug(true);
  33. Assert.assertTrue(HttpUtil.getIsHttpContentDebug());
  34. }
  35. HttpUtil.setIsHttpDebug("sdk".equalsIgnoreCase(System.getenv("DEBUG")));
  36. HttpUtil.setIsHttpContentDebug("sdk".equalsIgnoreCase(System.getenv("DEBUG")));
  37. }
  38. @Test
  39. public void testDebugHttpRequest() throws ClientException {
  40. HttpRequest request = mock(HttpRequest.class);
  41. Mockito.when(request.getSysMethod()).thenReturn(MethodType.GET);
  42. Mockito.when(request.getSysUrl()).thenReturn("http://test.domain");
  43. Map<String, String> requestHeaders = new HashMap<String, String>();
  44. Mockito.when(request.getHttpContentString()).thenReturn("request body");
  45. requestHeaders.put("test1", "test1");
  46. requestHeaders.put("test2", "test2");
  47. Mockito.when(request.getSysHeaders()).thenReturn(requestHeaders);
  48. String exceptString = "> GET HTTP/1.1\n> Host : test.domain\n> test2 : test2\n> test1 : test1\n> "
  49. + "Request URL : http://test.domain\n> \nrequest body";
  50. HttpUtil.setIsHttpDebug(true);
  51. HttpUtil.setIsHttpContentDebug(true);
  52. Assert.assertEquals(HttpUtil.debugHttpRequest(request), exceptString);
  53. HttpUtil.setIsHttpContentDebug(false);
  54. exceptString = "> GET HTTP/1.1\n> Host : test.domain\n> test2 : test2\n> test1 : test1\n> "
  55. + "Request URL : http://test.domain\n> ";
  56. Assert.assertEquals(HttpUtil.debugHttpRequest(request), exceptString);
  57. HttpUtil.setIsHttpDebug(false);
  58. Assert.assertNull(HttpUtil.debugHttpRequest(request));
  59. HttpUtil.setIsHttpContentDebug(true);
  60. Assert.assertNull(HttpUtil.debugHttpRequest(request));
  61. HttpUtil.setIsHttpDebug("sdk".equalsIgnoreCase(System.getenv("DEBUG")));
  62. HttpUtil.setIsHttpContentDebug("sdk".equalsIgnoreCase(System.getenv("DEBUG")));
  63. }
  64. @Test
  65. public void testDebugHttpResponse() throws ClientException {
  66. HttpResponse response = mock(HttpResponse.class);
  67. Mockito.when(response.getStatus()).thenReturn(200);
  68. Mockito.when(response.getSysUrl()).thenReturn("http://test.domain");
  69. Mockito.when(response.getHttpContentString()).thenReturn("response body");
  70. Map<String, String> reasponseHeaders = new HashMap<String, String>();
  71. reasponseHeaders.put("test1", "test1");
  72. reasponseHeaders.put("test2", "test2");
  73. Mockito.when(response.getSysHeaders()).thenReturn(reasponseHeaders);
  74. String exceptString = "< HTTP/1.1 200\n< test2 : test2\n< test1 : test1\n< \nresponse body";
  75. HttpUtil.setIsHttpDebug(true);
  76. HttpUtil.setIsHttpContentDebug(true);
  77. Assert.assertEquals(HttpUtil.debugHttpResponse(response), exceptString);
  78. HttpUtil.setIsHttpContentDebug(false);
  79. exceptString = "< HTTP/1.1 200\n< test2 : test2\n< test1 : test1\n< ";
  80. Assert.assertEquals(HttpUtil.debugHttpResponse(response), exceptString);
  81. HttpUtil.setIsHttpDebug(false);
  82. Assert.assertNull(HttpUtil.debugHttpResponse(response));
  83. HttpUtil.setIsHttpContentDebug(true);
  84. Assert.assertNull(HttpUtil.debugHttpResponse(response));
  85. HttpUtil.setIsHttpDebug("sdk".equalsIgnoreCase(System.getenv("DEBUG")));
  86. HttpUtil.setIsHttpContentDebug("sdk".equalsIgnoreCase(System.getenv("DEBUG")));
  87. }
  88. @Test
  89. public void testDebugHttpRquestException() throws ClientException {
  90. HttpRequest request = mock(HttpRequest.class);
  91. Mockito.when(request.getSysMethod()).thenReturn(MethodType.GET);
  92. Mockito.when(request.getSysUrl()).thenReturn("httpss://test.domain/jdj");
  93. Map<String, String> requestHeaders = new HashMap<String, String>();
  94. Mockito.when(request.getHttpContentString()).thenReturn("request body");
  95. requestHeaders.put("test1", "test1");
  96. requestHeaders.put("test2", "test2");
  97. Mockito.when(request.getSysHeaders()).thenReturn(requestHeaders);
  98. String exceptString = "> GET httpss://test.domain/jdj\n> Host : httpss://test.domain/jdj\n> "
  99. + "test2 : test2\n> test1 : test1\n> Request URL : httpss://test.domain/jdj\n> \nrequest body";
  100. HttpUtil.setIsHttpDebug(true);
  101. HttpUtil.setIsHttpContentDebug(true);
  102. Assert.assertEquals(HttpUtil.debugHttpRequest(request), exceptString);
  103. Mockito.when(request.getSysUrl()).thenReturn("http://test.domain/jdj");
  104. Mockito.doThrow(ClientException.class).when(request).getHttpContentString();
  105. Mockito.when(request.getSysEncoding()).thenReturn("HHH");
  106. exceptString = "> GET HTTP/1.1\n> Host : test.domain\n> "
  107. + "test2 : test2\n> test1 : test1\n> Request URL : http://test.domain/jdj\n> \n"
  108. + "Can not parse response due to unsupported encoding : HHH";
  109. Assert.assertEquals(HttpUtil.debugHttpRequest(request), exceptString);
  110. HttpUtil.setIsHttpDebug("sdk".equalsIgnoreCase(System.getenv("DEBUG")));
  111. HttpUtil.setIsHttpContentDebug("sdk".equalsIgnoreCase(System.getenv("DEBUG")));
  112. }
  113. @Test
  114. public void testDebugHttpResponseException() throws ClientException {
  115. HttpResponse response = mock(HttpResponse.class);
  116. Mockito.when(response.getStatus()).thenReturn(200);
  117. Mockito.doThrow(ClientException.class).when(response).getHttpContentString();
  118. Mockito.when(response.getSysEncoding()).thenReturn("HHH");
  119. Map<String, String> reasponseHeaders = new HashMap<String, String>();
  120. reasponseHeaders.put("test1", "test1");
  121. reasponseHeaders.put("test2", "test2");
  122. Mockito.when(response.getSysHeaders()).thenReturn(reasponseHeaders);
  123. String exceptString = "< HTTP/1.1 200\n< test2 : test2\n< test1 : test1\n< \n"
  124. + "Can not parse response due to unsupported encoding : HHH";
  125. HttpUtil.setIsHttpDebug(true);
  126. HttpUtil.setIsHttpContentDebug(true);
  127. Assert.assertEquals(HttpUtil.debugHttpResponse(response), exceptString);
  128. HttpUtil.setIsHttpDebug("sdk".equalsIgnoreCase(System.getenv("DEBUG")));
  129. HttpUtil.setIsHttpContentDebug("sdk".equalsIgnoreCase(System.getenv("DEBUG")));
  130. }
  131. @Test
  132. public void testGetJDKProxyException() throws ClientException {
  133. HttpRequest request = mock(HttpRequest.class);
  134. thrown.expect(ClientException.class);
  135. Proxy proxy = HttpUtil.getJDKProxy("http0://www.aliyun.com", null, request);
  136. Assert.assertNotNull(proxy);
  137. }
  138. @Test
  139. public void testGetJDKProxyEnvProxyHasUserInfo() throws ClientException {
  140. HttpRequest request = mock(HttpRequest.class);
  141. Proxy proxy = HttpUtil.getJDKProxy(null, "http://user:passwd@www.aliyun.com", request);
  142. Assert.assertNotNull(proxy);
  143. }
  144. @Test
  145. public void testGetJDKProxyEnvProxyNoUserInfo() throws ClientException {
  146. HttpRequest request = mock(HttpRequest.class);
  147. Proxy proxy = HttpUtil.getJDKProxy(null, "http://www.aliyun.com:80", request);
  148. Assert.assertNotNull(proxy);
  149. }
  150. @Test
  151. public void testGetApacheProxyException() throws ClientException {
  152. HttpRequest request = mock(HttpRequest.class);
  153. thrown.expect(ClientException.class);
  154. HttpHost proxy = HttpUtil.getApacheProxy("http0://www.aliyun.com", null, request);
  155. Assert.assertNotNull(proxy);
  156. }
  157. @Test
  158. public void testGetApacheProxyEnvProxyHasUserInfo() throws ClientException {
  159. HttpRequest request = mock(HttpRequest.class);
  160. HttpHost proxy = HttpUtil.getApacheProxy(null, "http://user:passwd@www.aliyun.com", request);
  161. Assert.assertNotNull(proxy);
  162. }
  163. @Test
  164. public void testGetApacheProxyEnvProxyNoUserInfo() throws ClientException {
  165. HttpRequest request = mock(HttpRequest.class);
  166. HttpHost proxy = HttpUtil.getApacheProxy(null, "http://www.aliyun.com:80", request);
  167. Assert.assertNotNull(proxy);
  168. }
  169. @Test
  170. public void testNeedProxyHasClientProxy() {
  171. boolean need = HttpUtil.needProxy("http://targethost.com", "http://www.aliyun.com", null);
  172. Assert.assertTrue(need);
  173. }
  174. @Test
  175. public void testNeedProxyHasEnvProxyList() {
  176. boolean need = HttpUtil.needProxy("http://targethost.com", "", "http://www.aliyun.com,http://targethost.com");
  177. Assert.assertFalse(need);
  178. }
  179. }