add tests for Util

This commit is contained in:
Naoyuki Kanezawa
2013-04-24 00:51:23 +09:00
parent e3f5e95eea
commit 789f94e11a
4 changed files with 81 additions and 18 deletions

View File

@@ -16,7 +16,6 @@ import java.util.*;
import java.util.concurrent.*; import java.util.concurrent.*;
import java.util.logging.Logger; import java.util.logging.Logger;
public abstract class Socket extends Emitter { public abstract class Socket extends Emitter {
private static final Logger logger = Logger.getLogger("engine.io-client:socket"); private static final Logger logger = Logger.getLogger("engine.io-client:socket");
@@ -520,9 +519,9 @@ public abstract class Socket extends Emitter {
opts.secure = "https".equals(uri.getScheme()) || "wss".equals(uri.getScheme()); opts.secure = "https".equals(uri.getScheme()) || "wss".equals(uri.getScheme());
opts.port = uri.getPort(); opts.port = uri.getPort();
String query = uri.getQuery(); String query = uri.getRawQuery();
if (query != null) { if (query != null) {
opts.query = uri.getQuery(); opts.query = query;
} }
return opts; return opts;

View File

@@ -15,12 +15,8 @@ public class Util {
StringBuilder str = new StringBuilder(); StringBuilder str = new StringBuilder();
for (Map.Entry<String, String> entry : obj.entrySet()) { for (Map.Entry<String, String> entry : obj.entrySet()) {
if (str.length() > 0) str.append("&"); if (str.length() > 0) str.append("&");
try { str.append(encodeURIComponent(entry.getKey())).append("=")
str.append(URLEncoder.encode(entry.getKey(), "UTF-8")).append("=") .append(encodeURIComponent(entry.getValue()));
.append(URLEncoder.encode(entry.getValue(), "UTF-8"));
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
} }
return str.toString(); return str.toString();
} }
@@ -30,14 +26,31 @@ public class Util {
String[] pairs = qs.split("&"); String[] pairs = qs.split("&");
for (String _pair : pairs) { for (String _pair : pairs) {
String[] pair = _pair.split("="); String[] pair = _pair.split("=");
try { qry.put(decodeURIComponent(pair[0]),
qry.put(URLDecoder.decode(pair[0], "UTF-8"), pair.length > 0 ? decodeURIComponent(pair[1]) : "");
pair.length > 0 ? URLDecoder.decode(pair[1], "UTF-8") : "");
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
} }
return qry; return qry;
} }
public static String encodeURIComponent(String str) {
try {
return URLEncoder.encode(str, "UTF-8")
.replace("+", "%20")
.replace("%21", "!")
.replace("%27", "'")
.replace("%28", "(")
.replace("%29", ")")
.replace("%7E", "~");
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
}
public static String decodeURIComponent(String str) {
try {
return URLDecoder.decode(str, "UTF-8");
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
}
} }

View File

@@ -18,6 +18,7 @@ import static org.junit.Assert.assertThat;
@RunWith(JUnit4.class) @RunWith(JUnit4.class)
public class SocketTest { public class SocketTest {
final static int TIMEOUT = 3000;
final static int PORT = 3000; final static int PORT = 3000;
private Process serverProcess; private Process serverProcess;
@@ -79,7 +80,7 @@ public class SocketTest {
serverService.awaitTermination(3000, TimeUnit.MILLISECONDS); serverService.awaitTermination(3000, TimeUnit.MILLISECONDS);
} }
@Test @Test(timeout = TIMEOUT)
public void openAndClose() throws URISyntaxException, InterruptedException { public void openAndClose() throws URISyntaxException, InterruptedException {
final BlockingQueue<String> events = new LinkedBlockingQueue<String>(); final BlockingQueue<String> events = new LinkedBlockingQueue<String>();
@@ -106,7 +107,7 @@ public class SocketTest {
assertThat(events.take(), is("onclose")); assertThat(events.take(), is("onclose"));
} }
@Test @Test(timeout = TIMEOUT)
public void messages() throws URISyntaxException, InterruptedException { public void messages() throws URISyntaxException, InterruptedException {
final BlockingQueue<String> events = new LinkedBlockingQueue<String>(); final BlockingQueue<String> events = new LinkedBlockingQueue<String>();

View File

@@ -0,0 +1,50 @@
package com.github.nkzawa.engineio.client;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
@RunWith(JUnit4.class)
public class UtilTest {
@Test
public void qs() {
Map<String, String> obj;
obj = new HashMap<String, String>() {{
put("a", "b");
}};
assertThat(Util.qs(obj), is("a=b"));
obj = new LinkedHashMap<String, String>() {{
put("a", "b");
put("c", "d");
}};
assertThat(Util.qs(obj), is("a=b&c=d"));
obj = new LinkedHashMap<String, String>() {{
put("a", "b");
put("c", "tobi rocks");
}};
assertThat(Util.qs(obj), is("a=b&c=tobi%20rocks"));
}
@Test
public void encodeURIComponent() {
assertThat(Util.encodeURIComponent(" ~'()! "), is("%20~'()!%20"));
assertThat(Util.encodeURIComponent("+:;"), is("%2B%3A%3B"));
}
@Test
public void decodeURIComponent() {
assertThat(Util.decodeURIComponent("%20%7E%27%28%29%21%20"), is(" ~'()! "));
assertThat(Util.decodeURIComponent("%2B%3A%3B"), is("+:;"));
}
}