Skip to content

Commit ccb8452

Browse files
committed
- HttpClient failing when whitespaces were present in the URL
- Added UnitTesting (cherry picked from commit 9a74deb)
1 parent 84c3cf8 commit ccb8452

File tree

3 files changed

+130
-25
lines changed

3 files changed

+130
-25
lines changed

common/src/main/java/com/genexus/CommonUtil.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -3222,7 +3222,7 @@ public final static String escapeUnsafeChars(String path)
32223222
{
32233223
try
32243224
{
3225-
String encoded = URLEncoder.encode( Character.toString(ch), "UTF-8" );
3225+
String encoded = URLEncoder.encode( Character.toString(ch), "UTF-8" ).replaceAll("\\+", "%20");
32263226
for (int i = 0; i < encoded.length(); i++)
32273227
buf[dst++] = encoded.charAt(i);
32283228
}

java/src/main/java/com/genexus/internet/HttpClientJavaLib.java

+29-24
Original file line numberDiff line numberDiff line change
@@ -188,37 +188,42 @@ public void setURL(String stringURL) {
188188
}
189189

190190
private String getURLValid(String url) {
191-
URI uri;
192191
try
193192
{
194-
uri = new URI(url);
195-
if (!uri.isAbsolute()) // En caso que la URL pasada por parametro no sea una URL valida (en este caso seria que no sea un URL absoluta), salta una excepcion en esta linea, y se continua haciendo todo el proceso con los datos ya guardados como atributos
193+
URI uri;
194+
try {
195+
uri = new URI(url);
196+
}
197+
catch (URISyntaxException _) {
198+
url = CommonUtil.escapeUnsafeChars(url);
199+
uri = new URI(url);
200+
}
201+
if (!uri.isAbsolute()) { // En caso que la URL pasada por parametro no sea una URL valida (en este caso seria que no sea un URL absoluta), salta una excepcion en esta linea, y se continua haciendo todo el proceso con los datos ya guardados como atributos
196202
return url;
197-
else {
198-
setPrevURLhost(getHost());
199-
setPrevURLbaseURL(getBaseURL());
200-
setPrevURLport(getPort());
201-
setPrevURLsecure(getSecure());
202-
setIsURL(true);
203-
setURL(url);
204-
205-
StringBuilder relativeUri = new StringBuilder();
206-
if (uri.getPath() != null) {
207-
relativeUri.append(uri.getPath());
208-
}
209-
if (uri.getQuery() != null) {
210-
relativeUri.append('?').append(uri.getQuery());
211-
}
212-
if (uri.getFragment() != null) {
213-
relativeUri.append('#').append(uri.getFragment());
214-
}
215-
return relativeUri.toString();
216203
}
204+
setPrevURLhost(getHost());
205+
setPrevURLbaseURL(getBaseURL());
206+
setPrevURLport(getPort());
207+
setPrevURLsecure(getSecure());
208+
setIsURL(true);
209+
setURL(url);
210+
211+
StringBuilder relativeUri = new StringBuilder();
212+
if (uri.getRawPath() != null) {
213+
relativeUri.append(uri.getRawPath());
214+
}
215+
if (uri.getRawQuery() != null) {
216+
relativeUri.append('?').append(uri.getRawQuery());
217+
}
218+
if (uri.getRawFragment() != null) {
219+
relativeUri.append('#').append(uri.getRawFragment());
220+
}
221+
return relativeUri.toString();
217222
}
218-
catch (URISyntaxException e)
223+
catch (URISyntaxException _)
219224
{
220-
return url;
221225
}
226+
return url;
222227
}
223228

224229
private static SSLConnectionSocketFactory getSSLSecureInstance() {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
package com.genexus.internet;
2+
import com.genexus.Application;
3+
import com.genexus.CommonUtil;
4+
import com.genexus.sampleapp.GXcfg;
5+
import com.genexus.specific.java.Connect;
6+
import com.genexus.specific.java.LogManager;
7+
import org.junit.Assert;
8+
import org.junit.Before;
9+
import org.junit.Test;
10+
11+
public class TestHttpClient {
12+
13+
@Before
14+
public void setUp() throws Exception {
15+
Connect.init();
16+
Application.init(GXcfg.class);
17+
LogManager.initialize(".");
18+
}
19+
20+
@Test
21+
public void executeHttpGetJavaLibInvalidURI() {
22+
HttpClientJavaLib httpClient = new HttpClientJavaLib();
23+
httpClient.setTimeout(1);
24+
httpClient.execute("GET", "https://localhost/My API with Spaces");
25+
Assert.assertEquals(0, httpClient.getStatusCode());
26+
}
27+
28+
@Test
29+
public void executeHttpGetJavaLib() {
30+
HttpClientJavaLib httpClient = new HttpClientJavaLib();
31+
httpClient.setTimeout(1);
32+
httpClient.execute("GET", "https://www.google.com/");
33+
Assert.assertEquals(200, httpClient.getStatusCode());
34+
}
35+
36+
@Test
37+
public void executeHttpGetJavaLib2() {
38+
HttpClientJavaLib httpClient = new HttpClientJavaLib();
39+
httpClient.setTimeout(1);
40+
httpClient.execute("GET", "https://www.google.com?q=My API with Spaces");
41+
Assert.assertEquals(200, httpClient.getStatusCode());
42+
}
43+
44+
@Test
45+
public void executeHttpGetJavaLib3() {
46+
HttpClientJavaLib httpClient = new HttpClientJavaLib();
47+
httpClient.setTimeout(1);
48+
httpClient.execute("GET", "https://www.google.com/test api");
49+
Assert.assertEquals(404, httpClient.getStatusCode());
50+
}
51+
52+
@Test
53+
public void executeHttpGetJavaLib4() {
54+
HttpClientJavaLib httpClient = new HttpClientJavaLib();
55+
httpClient.setTimeout(1);
56+
httpClient.execute("GET", "https://www.google.com/test api?q=hello world");
57+
Assert.assertEquals(404, httpClient.getStatusCode());
58+
}
59+
60+
@Test
61+
public void executeHttpGetJavaLib5() {
62+
HttpClientJavaLib httpClient = new HttpClientJavaLib();
63+
httpClient.setTimeout(1);
64+
httpClient.execute("GET", "https://www.google.com:80/test api?q=hello world");
65+
Assert.assertEquals(404, httpClient.getStatusCode());
66+
}
67+
@Test
68+
public void executeHttpGetJavaLib6() {
69+
HttpClientJavaLib httpClient = new HttpClientJavaLib();
70+
httpClient.setTimeout(1);
71+
httpClient.execute("GET", "https://www.google.com:80");
72+
Assert.assertEquals(200, httpClient.getStatusCode());
73+
}
74+
75+
76+
@Test
77+
public void executeHttpGetJavaLibWithAccent() {
78+
HttpClientJavaLib httpClient = new HttpClientJavaLib();
79+
httpClient.setTimeout(1);
80+
httpClient.execute("GET", "https://maps.googleapis.com/maps/api/geocode/json?address=Dirección");
81+
Assert.assertEquals(200, httpClient.getStatusCode());
82+
}
83+
84+
@Test
85+
public void escapeUrlTest1() {
86+
String rawUrl = "https://maps.googleapis.com/maps/api/geocode/json?address=Dirección";
87+
String escapedSafeUrl = CommonUtil.escapeUnsafeChars(rawUrl);
88+
String expectedUrl = "https://maps.googleapis.com/maps/api/geocode/json?address=Direcci%C3%B3n";
89+
Assert.assertEquals(expectedUrl, escapedSafeUrl);
90+
}
91+
92+
@Test
93+
public void escapeUrlTest2() {
94+
String rawUrl = "https://www.google.com:80?q=My API with Spaces";
95+
String escapedSafeUrl = CommonUtil.escapeUnsafeChars(rawUrl);
96+
String expectedUrl = "https://www.google.com:80?q=My%20API%20with%20Spaces";
97+
Assert.assertEquals(expectedUrl, escapedSafeUrl);
98+
}
99+
100+
}

0 commit comments

Comments
 (0)