From 2f69df25748b033d72fde731a55c95a9e5dcb949 Mon Sep 17 00:00:00 2001 From: Naoyuki Kanezawa Date: Sun, 25 Aug 2013 01:17:08 +0900 Subject: [PATCH] add tests --- .../nkzawa/engineio/client/SocketTest.java | 1 + .../nkzawa/engineio/client/TransportTest.java | 138 ++++++++++++++++++ 2 files changed, 139 insertions(+) create mode 100644 src/test/java/com/github/nkzawa/engineio/client/TransportTest.java diff --git a/src/test/java/com/github/nkzawa/engineio/client/SocketTest.java b/src/test/java/com/github/nkzawa/engineio/client/SocketTest.java index 64e2f28..22b4a4f 100644 --- a/src/test/java/com/github/nkzawa/engineio/client/SocketTest.java +++ b/src/test/java/com/github/nkzawa/engineio/client/SocketTest.java @@ -43,6 +43,7 @@ public class SocketTest { * should not emit close on incorrect connection. * * @throws URISyntaxException + * @throws InterruptedException */ @Test public void socketClosing() throws URISyntaxException, InterruptedException { diff --git a/src/test/java/com/github/nkzawa/engineio/client/TransportTest.java b/src/test/java/com/github/nkzawa/engineio/client/TransportTest.java new file mode 100644 index 0000000..a87df39 --- /dev/null +++ b/src/test/java/com/github/nkzawa/engineio/client/TransportTest.java @@ -0,0 +1,138 @@ +package com.github.nkzawa.engineio.client; + +import com.github.nkzawa.engineio.client.transports.PollingXHR; +import com.github.nkzawa.engineio.client.transports.WebSocket; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +import java.util.HashMap; + +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertThat; + +@RunWith(JUnit4.class) +public class TransportTest { + + @Test + public void uri() { + Transport.Options opt = new Transport.Options(); + opt.path ="/engine.io"; + opt.hostname = "localhost"; + opt.secure = false; + opt.query = new HashMap() {{ + put("sid", "test"); + }}; + Polling polling = new Polling(opt); + assertThat(polling.uri(), is("http://localhost/engine.io?sid=test")); + } + + @Test + public void uriWithDefaultPort() { + Transport.Options opt = new Transport.Options(); + opt.path ="/engine.io"; + opt.hostname = "localhost"; + opt.secure = false; + opt.query = new HashMap() {{ + put("sid", "test"); + }}; + opt.port = 80; + Polling polling = new Polling(opt); + assertThat(polling.uri(), is("http://localhost/engine.io?sid=test")); + } + + @Test + public void uriWithPort() { + Transport.Options opt = new Transport.Options(); + opt.path ="/engine.io"; + opt.hostname = "localhost"; + opt.secure = false; + opt.query = new HashMap() {{ + put("sid", "test"); + }}; + opt.port = 3000; + Polling polling = new Polling(opt); + assertThat(polling.uri(), is("http://localhost:3000/engine.io?sid=test")); + } + + @Test + public void httpsUriWithDefaultPort() { + Transport.Options opt = new Transport.Options(); + opt.path ="/engine.io"; + opt.hostname = "localhost"; + opt.secure = true; + opt.query = new HashMap() {{ + put("sid", "test"); + }}; + opt.port = 443; + Polling polling = new Polling(opt); + assertThat(polling.uri(), is("https://localhost/engine.io?sid=test")); + } + + @Test + public void timestampedUri() { + Transport.Options opt = new Transport.Options(); + opt.path ="/engine.io"; + opt.hostname = "localhost"; + opt.timestampParam = "t"; + opt.timestampRequests = true; + Polling polling = new Polling(opt); + assertThat(polling.uri().matches("http://localhost/engine.io\\?t=[0-9]+"), is(true)); + } + + @Test + public void wsUri() { + Transport.Options opt = new Transport.Options(); + opt.path ="/engine.io"; + opt.hostname = "test"; + opt.secure = false; + opt.query = new HashMap() {{ + put("transport", "websocket"); + }}; + WS ws = new WS(opt); + assertThat(ws.uri(), is("ws://test/engine.io?transport=websocket")); + } + + @Test + public void wssUri() { + Transport.Options opt = new Transport.Options(); + opt.path ="/engine.io"; + opt.hostname = "test"; + opt.secure = true; + WS ws = new WS(opt); + assertThat(ws.uri(), is("wss://test/engine.io")); + } + + @Test + public void wsTimestampedUri() { + Transport.Options opt = new Transport.Options(); + opt.path ="/engine.io"; + opt.hostname = "localhost"; + opt.timestampParam = "woot"; + opt.timestampRequests = true; + WS ws = new WS(opt); + assertThat(ws.uri().matches("ws://localhost/engine.io\\?woot=[0-9]+"), is(true)); + } + + class Polling extends PollingXHR { + + public Polling(Options opts) { + super(opts); + } + + public String uri() { + return super.uri(); + } + } + + class WS extends WebSocket { + + public WS(Options opts) { + super(opts); + } + + public String uri() { + return super.uri(); + } + } +}