|
| 1 | +package com.genexus.internet; |
| 2 | + |
| 3 | +import com.genexus.Application; |
| 4 | +import com.genexus.sampleapp.GXcfg; |
| 5 | +import com.genexus.specific.java.Connect; |
| 6 | +import com.genexus.specific.java.LogManager; |
| 7 | +import org.junit.After; |
| 8 | +import org.junit.Assert; |
| 9 | +import org.junit.Before; |
| 10 | +import org.junit.Test; |
| 11 | + |
| 12 | +public class TestHttpClientProxy { |
| 13 | + |
| 14 | + private static final String PROXY_HOST_PROP = "http.proxyHost"; |
| 15 | + private static final String PROXY_PORT_PROP = "http.proxyPort"; |
| 16 | + private static final String HTTPS_PROXY_HOST_PROP = "https.proxyHost"; |
| 17 | + private static final String HTTPS_PROXY_PORT_PROP = "https.proxyPort"; |
| 18 | + private static final String NON_PROXY_HOSTS_PROP = "http.nonProxyHosts"; |
| 19 | + |
| 20 | + private String origHttpProxyHost; |
| 21 | + private String origHttpProxyPort; |
| 22 | + private String origHttpsProxyHost; |
| 23 | + private String origHttpsProxyPort; |
| 24 | + private String origNonProxyHosts; |
| 25 | + |
| 26 | + @Before |
| 27 | + public void setUp() throws Exception { |
| 28 | + Connect.init(); |
| 29 | + Application.init(GXcfg.class); |
| 30 | + LogManager.initialize("."); |
| 31 | + |
| 32 | + origHttpProxyHost = System.getProperty(PROXY_HOST_PROP); |
| 33 | + origHttpProxyPort = System.getProperty(PROXY_PORT_PROP); |
| 34 | + origHttpsProxyHost = System.getProperty(HTTPS_PROXY_HOST_PROP); |
| 35 | + origHttpsProxyPort = System.getProperty(HTTPS_PROXY_PORT_PROP); |
| 36 | + origNonProxyHosts = System.getProperty(NON_PROXY_HOSTS_PROP); |
| 37 | + } |
| 38 | + |
| 39 | + @After |
| 40 | + public void tearDown() { |
| 41 | + restoreProperty(PROXY_HOST_PROP, origHttpProxyHost); |
| 42 | + restoreProperty(PROXY_PORT_PROP, origHttpProxyPort); |
| 43 | + restoreProperty(HTTPS_PROXY_HOST_PROP, origHttpsProxyHost); |
| 44 | + restoreProperty(HTTPS_PROXY_PORT_PROP, origHttpsProxyPort); |
| 45 | + restoreProperty(NON_PROXY_HOSTS_PROP, origNonProxyHosts); |
| 46 | + } |
| 47 | + |
| 48 | + private void restoreProperty(String key, String originalValue) { |
| 49 | + if (originalValue == null) { |
| 50 | + System.clearProperty(key); |
| 51 | + } else { |
| 52 | + System.setProperty(key, originalValue); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + public void httpRequestUsesProxyFromSystemProperties() { |
| 58 | + // Set proxy to a non-existent proxy server (localhost on an unlikely port). |
| 59 | + // If the client respects the system property, it will try to connect through |
| 60 | + // this proxy and fail, resulting in a non-200 status code. |
| 61 | + System.setProperty(PROXY_HOST_PROP, "127.0.0.1"); |
| 62 | + System.setProperty(PROXY_PORT_PROP, "19999"); |
| 63 | + |
| 64 | + HttpClientJavaLib httpClient = new HttpClientJavaLib(); |
| 65 | + httpClient.setTimeout(1); |
| 66 | + httpClient.execute("GET", "http://www.google.com/"); |
| 67 | + |
| 68 | + // The request should fail because it tried to route through the non-existent proxy |
| 69 | + Assert.assertNotEquals("Request should fail when routed through non-existent proxy", |
| 70 | + 200, httpClient.getStatusCode()); |
| 71 | + } |
| 72 | + |
| 73 | + @Test |
| 74 | + public void httpsRequestUsesProxyFromSystemProperties() { |
| 75 | + // Set HTTPS proxy to a non-existent proxy server |
| 76 | + System.setProperty(HTTPS_PROXY_HOST_PROP, "127.0.0.1"); |
| 77 | + System.setProperty(HTTPS_PROXY_PORT_PROP, "19999"); |
| 78 | + |
| 79 | + HttpClientJavaLib httpClient = new HttpClientJavaLib(); |
| 80 | + httpClient.setTimeout(1); |
| 81 | + httpClient.execute("GET", "https://www.google.com/"); |
| 82 | + |
| 83 | + // The request should fail because it tried to route through the non-existent proxy |
| 84 | + Assert.assertNotEquals("HTTPS request should fail when routed through non-existent proxy", |
| 85 | + 200, httpClient.getStatusCode()); |
| 86 | + } |
| 87 | + |
| 88 | + @Test |
| 89 | + public void requestSucceedsWithoutProxySystemProperties() { |
| 90 | + // Ensure no proxy system properties are set |
| 91 | + System.clearProperty(PROXY_HOST_PROP); |
| 92 | + System.clearProperty(PROXY_PORT_PROP); |
| 93 | + System.clearProperty(HTTPS_PROXY_HOST_PROP); |
| 94 | + System.clearProperty(HTTPS_PROXY_PORT_PROP); |
| 95 | + |
| 96 | + HttpClientJavaLib httpClient = new HttpClientJavaLib(); |
| 97 | + httpClient.setTimeout(1); |
| 98 | + httpClient.execute("GET", "https://www.google.com/"); |
| 99 | + |
| 100 | + // Without proxy, direct connection should succeed |
| 101 | + Assert.assertEquals("Direct request without proxy should succeed", |
| 102 | + 200, httpClient.getStatusCode()); |
| 103 | + } |
| 104 | + |
| 105 | + @Test |
| 106 | + public void explicitProxyOverridesSystemProperties() { |
| 107 | + // Set system properties to a valid-looking proxy (that we won't actually hit) |
| 108 | + System.setProperty(PROXY_HOST_PROP, "10.255.255.1"); |
| 109 | + System.setProperty(PROXY_PORT_PROP, "3128"); |
| 110 | + |
| 111 | + HttpClientJavaLib httpClient = new HttpClientJavaLib(); |
| 112 | + httpClient.setTimeout(1); |
| 113 | + |
| 114 | + // Explicit proxy should take precedence over system properties |
| 115 | + httpClient.setProxyServerHost("127.0.0.1"); |
| 116 | + httpClient.setProxyServerPort(19999); |
| 117 | + httpClient.execute("GET", "http://www.google.com/"); |
| 118 | + |
| 119 | + // Should fail because the explicit proxy (127.0.0.1:19999) doesn't exist |
| 120 | + Assert.assertNotEquals("Explicit proxy should be used instead of system property proxy", |
| 121 | + 200, httpClient.getStatusCode()); |
| 122 | + } |
| 123 | + |
| 124 | + @Test |
| 125 | + public void nonProxyHostsBypassesProxy() { |
| 126 | + // Set proxy to a non-existent proxy |
| 127 | + System.setProperty(HTTPS_PROXY_HOST_PROP, "127.0.0.1"); |
| 128 | + System.setProperty(HTTPS_PROXY_PORT_PROP, "19999"); |
| 129 | + // But exclude google.com from proxy |
| 130 | + System.setProperty(NON_PROXY_HOSTS_PROP, "*.google.com"); |
| 131 | + |
| 132 | + HttpClientJavaLib httpClient = new HttpClientJavaLib(); |
| 133 | + httpClient.setTimeout(1); |
| 134 | + httpClient.execute("GET", "https://www.google.com/"); |
| 135 | + |
| 136 | + // Should succeed because google.com is in the non-proxy list |
| 137 | + Assert.assertEquals("Request to non-proxy host should bypass proxy and succeed", |
| 138 | + 200, httpClient.getStatusCode()); |
| 139 | + } |
| 140 | +} |
0 commit comments