Skip to content

Commit

Permalink
#67 - tests
Browse files Browse the repository at this point in the history
  • Loading branch information
majek committed May 18, 2012
1 parent 11299e6 commit 7c800db
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 1 deletion.
4 changes: 3 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ tests/html/lib/%.js: tests/html/src/%.coffee

build_tests: tests/html/lib/sockjs.js tests/html/lib/tests.js \
tests/html/lib/unittests.js tests/html/lib/domtests.js \
tests/html/lib/endtoendtests.js
tests/html/lib/endtoendtests.js \
tests/html/lib/utils.js \
tests/html/lib/heartbeattests.js

test: tests
tests: build_tests
Expand Down
1 change: 1 addition & 0 deletions tests/html/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ <h1>SockJS</h1>
<li><a href="example-cursors.html">Cursors example</a></li>
<li><a href="smoke-latency.html">Smoketest: latency</a></li>
<li><a href="smoke-reconnect.html">Smoketest: reconnect</a></li>
<li><a href="tests-heartbeat.html">Heartbeat tests (optional)</a></li>
</ul>
</body>
</html>
57 changes: 57 additions & 0 deletions tests/html/src/heartbeattests.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@


heartbeat_test_factory = (protocol) ->
module(protocol)
if not SockJS[protocol] or not SockJS[protocol].enabled()
test "[unsupported by client]", ->
log('Unsupported protocol (by client): "' + protocol + '"')
return
if client_opts.disabled_transports and
arrIndexOf(client_opts.disabled_transports, protocol) isnt -1
test "[disabled by config]", ->
log('Disabled by config: "' + protocol + '"')
return

asyncTest 'single heartbeat', ->
expect(3)
r = newSockJS('/echo', protocol)
r.onopen = (e) ->
ok(true)
r._sendHeartbeat()
r.send('a')
r.onmessage = (e) ->
equal(e.data, 'a')
r.close()
r.onclose = (e) ->
log(JSON.stringify(e))
ok(true)
start()

asyncTest 'round trip', ->
expect(3)
r = newSockJS('/heartbeat', protocol)
r.onopen = (e) ->
ok(true)
r._sendHeartbeat()
r.onmessage = (e) ->
equal(e.data, 'heartbeat')
r.close()
r.onclose = () ->
ok(true)
start()

asyncTest 'server heartbeat', ->
expect(3)
r = newSockJS('/heartbeat', protocol)
r.onopen = (e) ->
ok(true)
r.onheartbeat = (e) ->
ok(true)
r.close()
r.onclose = () ->
ok(true)
start()


for protocol in protocols
heartbeat_test_factory(protocol)
44 changes: 44 additions & 0 deletions tests/html/tests-heartbeat.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta charset="UTF-8" />

<script type="text/javascript" src="lib/sockjs.js"></script>

<script type="text/javascript" src="static/jquery.min.js"></script>

<link rel="stylesheet" href="static/qunit.css" type="text/css" media="screen">
<script type="text/javascript" src="static/qunit.min.js"></script>

<script type="text/javascript" src="config.js"></script>
<script type="text/javascript" src="lib/utils.js"></script>
<script type="text/javascript" src="lib/heartbeattests.js"></script>
</head>
<body>
<p>
<h1 id="qunit-header">SockJS Heartbeat tests</h1>
<h2 id="qunit-banner"></h2>
<div id="qunit-testrunner-toolbar"></div>
<h2 id="qunit-userAgent"></h2>
<ol id="qunit-tests"></ol>
<div id="qunit-fixture"></div>
</p>
<br>
<code id="logs" style="height:200px; overflow:auto; display: block; border: 1px gray solid;">
</code>

<script>
function log(a, e) {
if ('console' in window && 'log' in window.console) {
console.log(a, e);
}
$('#logs').append($("<code>").text(a));
$('#logs').append($("<pre>").text(JSON.stringify(e, null, 4)));
$('#logs').append($("<br>"));
$('#logs').scrollTop($('#logs').scrollTop()+10000);
}
log('Running against: ' + client_opts.url);
</script>
</body>
</html>

0 comments on commit 7c800db

Please sign in to comment.