Skip to content

Commit 13e7a73

Browse files
K0zkaDaan Hoogland
authored andcommitted
Test for URLEncoder
- test added - source formatted Signed-off-by: Laszlo Hornyak <laszlo.hornyak@gmail.com>
1 parent c61997f commit 13e7a73

2 files changed

Lines changed: 34 additions & 24 deletions

File tree

utils/src/com/cloud/utils/encoding/URLEncoder.java

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -34,19 +34,16 @@
3434
* @author Remy Maucherat
3535
*/
3636

37-
38-
39-
4037
public class URLEncoder {
4138
protected static final char[] hexadecimal = { '0', '1', '2', '3',
4239
'4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
4340

4441
static CharsetEncoder asciiEncoder =
4542
Charset.forName("US-ASCII").newEncoder(); // or "ISO-8859-1" for ISO Latin 1
46-
43+
4744
//Array containing the safe characters set.
4845
protected BitSet safeCharacters = new BitSet(256);
49-
46+
5047
public URLEncoder() {
5148
for (char i = 'a'; i <= 'z'; i++) {
5249
addSafeCharacter(i);
@@ -59,26 +56,26 @@ public URLEncoder() {
5956
}
6057
}
6158

62-
public void addSafeCharacter(char c) {
59+
private void addSafeCharacter(char c) {
6360
safeCharacters.set(c);
6461
}
65-
62+
6663
public String encode(String path) {
6764
int maxBytesPerChar = 10;
6865
StringBuffer rewrittenPath = new StringBuffer(path.length());
69-
ByteArrayOutputStream buf = new ByteArrayOutputStream(
70-
maxBytesPerChar);
66+
ByteArrayOutputStream buf = new ByteArrayOutputStream(maxBytesPerChar);
7167
OutputStreamWriter writer = null;
7268
try {
7369
writer = new OutputStreamWriter(buf, "UTF8");
7470
} catch (Exception e) {
7571
e.printStackTrace();
7672
writer = new OutputStreamWriter(buf);
7773
}
78-
74+
7975
for (int i = 0; i < path.length(); i++) {
8076
int c = (int) path.charAt(i);
81-
//NOTICE - !isPureAscii(path.charAt(i)) check was added by CloudStack
77+
// NOTICE - !isPureAscii(path.charAt(i)) check was added by
78+
// CloudStack
8279
if (safeCharacters.get(c) || !isPureAscii(path.charAt(i))) {
8380
rewrittenPath.append((char) c);
8481
} else {
@@ -95,20 +92,19 @@ public String encode(String path) {
9592
// Converting each byte in the buffer
9693
byte toEncode = ba[j];
9794
rewrittenPath.append('%');
98-
int low = (int) (toEncode & 0x0f);
99-
int high = (int) ((toEncode & 0xf0) >> 4);
100-
rewrittenPath.append(hexadecimal[high]);
101-
rewrittenPath.append(hexadecimal[low]);
102-
}
103-
buf.reset();
95+
int low = (int) (toEncode & 0x0f);
96+
int high = (int) ((toEncode & 0xf0) >> 4);
97+
rewrittenPath.append(hexadecimal[high]);
98+
rewrittenPath.append(hexadecimal[low]);
10499
}
100+
buf.reset();
105101
}
106-
return rewrittenPath.toString();
107-
}
108-
109-
110-
//NOTICE - this part was added by CloudStack
111-
public static boolean isPureAscii(Character v) {
112-
return asciiEncoder.canEncode(v);
113102
}
103+
return rewrittenPath.toString();
104+
}
105+
106+
// NOTICE - this part was added by CloudStack
107+
public static boolean isPureAscii(Character v) {
108+
return asciiEncoder.canEncode(v);
109+
}
114110
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.cloud.utils.encoding;
2+
3+
import org.junit.Assert;
4+
import org.junit.Test;
5+
6+
public class UrlEncoderTest {
7+
@Test
8+
public void encode() {
9+
Assert.assertEquals("%2Ftmp%2F", new URLEncoder().encode("/tmp/"));
10+
Assert.assertEquals("%20", new URLEncoder().encode(" "));
11+
Assert.assertEquals("%5F", new URLEncoder().encode("_"));
12+
Assert.assertEquals("%25", new URLEncoder().encode("%"));
13+
}
14+
}

0 commit comments

Comments
 (0)