-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added jsonp support to polling transport
Added integration tests for jsonp
- Loading branch information
Showing
8 changed files
with
352 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
121 changes: 121 additions & 0 deletions
121
engine.io-server-test/src/test/java/io/socket/engineio/server/PollingJsonpTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
package io.socket.engineio.server; | ||
|
||
import io.socket.engineio.parser.Packet; | ||
import org.junit.After; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.openqa.jetty.log.LogFactory; | ||
import org.openqa.selenium.WebDriver; | ||
import org.openqa.selenium.htmlunit.HtmlUnitDriver; | ||
import org.openqa.selenium.remote.server.SeleniumServer; | ||
import org.openqa.selenium.support.events.AbstractWebDriverEventListener; | ||
import org.openqa.selenium.support.events.EventFiringWebDriver; | ||
|
||
import java.io.FileInputStream; | ||
import java.io.IOException; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.logging.Level; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
public final class PollingJsonpTest { | ||
|
||
private static SeleniumServer sSeleniumServer; | ||
|
||
@Before | ||
public void setup() { | ||
LogFactory.getFactory().setAttribute("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.NoOpLog"); | ||
|
||
java.util.logging.Logger.getLogger("com.gargoylesoftware.htmlunit").setLevel(Level.OFF); | ||
java.util.logging.Logger.getLogger("org.apache.commons.httpclient").setLevel(Level.OFF); | ||
|
||
sSeleniumServer = new SeleniumServer(4444); | ||
sSeleniumServer.boot(); | ||
} | ||
|
||
@After | ||
public void teardown() { | ||
sSeleniumServer.stop(); | ||
} | ||
|
||
@Test | ||
public void echoTest_string() throws Exception { | ||
final ServerWrapper serverWrapper = new ServerWrapper(); | ||
|
||
try { | ||
serverWrapper.startServer(); | ||
serverWrapper.getEngineIoServer().on("connection", args -> { | ||
final EngineIoSocket socket = (EngineIoSocket) args[0]; | ||
socket.on("message", args1 -> { | ||
Packet packet = new Packet(Packet.MESSAGE); | ||
packet.data = args1[0]; | ||
socket.send(packet); | ||
}); | ||
}); | ||
|
||
assertEquals(0, executeScriptInBrowser(serverWrapper, "src/test/resources/testPollingJsonp_echo_string.js")); | ||
} finally { | ||
serverWrapper.stopServer(); | ||
} | ||
} | ||
|
||
@Test | ||
public void reverseEchoTest() throws Exception { | ||
final ServerWrapper serverWrapper = new ServerWrapper(); | ||
try { | ||
serverWrapper.startServer(); | ||
serverWrapper.getEngineIoServer().on("connection", args -> { | ||
final EngineIoSocket socket = (EngineIoSocket) args[0]; | ||
final String echoMessage = PollingTest.class.getSimpleName() + System.currentTimeMillis(); | ||
socket.on("message", args1 -> assertEquals(echoMessage, args1[0])); | ||
|
||
Packet packet = new Packet(Packet.MESSAGE); | ||
packet.data = echoMessage; | ||
socket.send(packet); | ||
}); | ||
|
||
assertEquals(0, executeScriptInBrowser(serverWrapper, "src/test/resources/testPollingJsonp_reverseEcho.js")); | ||
} finally { | ||
serverWrapper.stopServer(); | ||
} | ||
} | ||
|
||
@SuppressWarnings("ResultOfMethodCallIgnored") | ||
private int executeScriptInBrowser(ServerWrapper serverWrapper, String script) throws IOException { | ||
class ScriptResult { | ||
int result = -1; | ||
} | ||
|
||
final HtmlUnitDriver webDriver = new HtmlUnitDriver(true); | ||
final EventFiringWebDriver eventFiringWebDriver = new EventFiringWebDriver(webDriver); | ||
final ScriptResult scriptResult = new ScriptResult(); | ||
|
||
try (FileInputStream fis = new FileInputStream(script)) { | ||
final byte[] scriptBytes = new byte[fis.available()]; | ||
fis.read(scriptBytes); | ||
final String scriptContent = new String(scriptBytes, StandardCharsets.UTF_8); | ||
|
||
eventFiringWebDriver.get("http://127.0.0.1:" + serverWrapper.getPort() + "/testPollingJsonp.html"); | ||
eventFiringWebDriver.register(new AbstractWebDriverEventListener() { | ||
|
||
@Override | ||
public void afterScript(String script, WebDriver driver) { | ||
final String url = eventFiringWebDriver.getCurrentUrl(); | ||
if (url.startsWith("http://www.example.com/?result=")) { | ||
scriptResult.result = Integer.parseInt(url.substring("http://www.example.com/?result=".length())); | ||
} | ||
} | ||
}); | ||
eventFiringWebDriver.executeScript(scriptContent, serverWrapper.getPort()); | ||
|
||
try { | ||
Thread.sleep(3000); | ||
} catch (InterruptedException ignore) { | ||
} | ||
|
||
return scriptResult.result; | ||
} finally { | ||
webDriver.close(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
engine.io-server-test/src/test/resources/testPollingJsonp.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
|
||
<title>Polling Test - JSONP</title> | ||
|
||
<script type="text/javascript" src="node_modules/engine.io-client/engine.io.js"></script> | ||
<script type="text/javascript"> | ||
window.setResult = function (result) { | ||
window.location = "http://www.example.com/?result=" + result; | ||
}; | ||
</script> | ||
</head> | ||
<body> | ||
</body> | ||
</html> |
23 changes: 23 additions & 0 deletions
23
engine.io-server-test/src/test/resources/testPollingJsonp_echo_string.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
var returnError = function () { | ||
window.setResult(1); | ||
}; | ||
|
||
var port = arguments[0]; | ||
socket = eio('http://127.0.0.1:' + port, { | ||
transports: ['polling'], | ||
jsonp: true, | ||
forceJSONP: true | ||
}); | ||
socket.on('open', function () { | ||
var echoMessage = "Hello World"; | ||
socket.on('message', function (message) { | ||
if(message === echoMessage) { | ||
window.setResult(0); | ||
} else { | ||
returnError(); | ||
} | ||
}); | ||
socket.send(echoMessage); | ||
}); | ||
socket.on('error', returnError); | ||
setTimeout(returnError, 750); |
19 changes: 19 additions & 0 deletions
19
engine.io-server-test/src/test/resources/testPollingJsonp_reverseEcho.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
var returnError = function () { | ||
window.setResult(1); | ||
}; | ||
|
||
var port = arguments[0]; | ||
socket = eio('http://127.0.0.1:' + port, { | ||
transports: ['polling'], | ||
jsonp: true, | ||
forceJSONP: true | ||
}); | ||
socket.on("message", function (message) { | ||
socket.sendPacket("message", message); | ||
|
||
setTimeout(function () { | ||
window.setResult(0); | ||
}, 100); | ||
}); | ||
socket.on('error', returnError); | ||
setTimeout(returnError, 750); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.