fix default port detection

This commit is contained in:
Naoyuki Kanezawa
2015-01-25 05:43:27 +09:00
parent db8528e790
commit 2dc81e00d6
3 changed files with 25 additions and 43 deletions

View File

@@ -103,7 +103,7 @@ public class Socket extends Emitter {
private boolean timestampRequests; private boolean timestampRequests;
private boolean upgrading; private boolean upgrading;
private boolean rememberUpgrade; private boolean rememberUpgrade;
private int port; /*package*/ int port;
private int policyPort; private int policyPort;
private int prevBufferLen; private int prevBufferLen;
private long pingInterval; private long pingInterval;
@@ -176,6 +176,9 @@ public class Socket extends Emitter {
} }
if (pieces.length > 1) { if (pieces.length > 1) {
opts.port = Integer.parseInt(pieces[pieces.length - 1]); opts.port = Integer.parseInt(pieces[pieces.length - 1]);
} else if (opts.port == -1) {
// if no port is specified manually, use the protocol default
opts.port = this.secure ? 443 : 80;
} }
} }
} }
@@ -808,6 +811,8 @@ public class Socket extends Emitter {
filteredUpgrades.add(upgrade); filteredUpgrades.add(upgrade);
} }
} }
return filteredUpgrades; return filteredUpgrades;
} }

View File

@@ -140,8 +140,8 @@ public abstract class Transport extends Emitter {
public String timestampParam; public String timestampParam;
public boolean secure; public boolean secure;
public boolean timestampRequests; public boolean timestampRequests;
public int port; public int port = -1;
public int policyPort; public int policyPort = -1;
public Map<String, String> query; public Map<String, String> query;
public SSLContext sslContext; public SSLContext sslContext;
protected Socket socket; protected Socket socket;

View File

@@ -1,6 +1,5 @@
package com.github.nkzawa.engineio.client; package com.github.nkzawa.engineio.client;
import com.github.nkzawa.emitter.Emitter;
import com.github.nkzawa.engineio.client.transports.Polling; import com.github.nkzawa.engineio.client.transports.Polling;
import com.github.nkzawa.engineio.client.transports.WebSocket; import com.github.nkzawa.engineio.client.transports.WebSocket;
import org.junit.Test; import org.junit.Test;
@@ -10,10 +9,6 @@ import org.junit.runners.JUnit4;
import java.net.URISyntaxException; import java.net.URISyntaxException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat; import static org.junit.Assert.assertThat;
@@ -34,41 +29,23 @@ public class SocketTest {
assertThat(socket.filterUpgrades(upgrades), is(expected)); assertThat(socket.filterUpgrades(upgrades), is(expected));
} }
/** public void properlyParseHttpUriWithoutPort() throws URISyntaxException {
* should emit close on incorrect connection. Socket client = new Socket("http://localhost");
* assertThat(client.port, is(80));
* @throws URISyntaxException
* @throws InterruptedException
*/
@Test
public void socketClosing() throws URISyntaxException, InterruptedException {
final BlockingQueue<Object> values = new LinkedBlockingQueue<Object>();
Socket socket = new Socket("ws://0.0.0.0:8080");
final boolean[] closed = {false};
socket.once(Socket.EVENT_ERROR, new Emitter.Listener() {
@Override
public void call(Object... args) {
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
values.offer(closed[0]);
}
}, 20);
}
});
socket.on(Socket.EVENT_CLOSE, new Emitter.Listener() {
@Override
public void call(Object... args) {
closed[0] = true;
}
});
socket.open();
assertThat((Boolean)values.take(), is(true));
} }
public void properlyParseHttpsUriWithoutPort() throws URISyntaxException {
Socket client = new Socket("http://localhost");
assertThat(client.port, is(443));
}
public void properlyParseWssUriWithoutPort() throws URISyntaxException {
Socket client = new Socket("http://localhost");
assertThat(client.port, is(443));
}
public void properlyParseWssUriWithPort() throws URISyntaxException {
Socket client = new Socket("http://localhost:2020");
assertThat(client.port, is(2020));
}
} }