Skip to content

Commit

Permalink
Merge pull request #74 from hustf/addBrowseTest2
Browse files Browse the repository at this point in the history
Added browser tests. Firefox, Chrome, PhantomJS, Safari, Internet Explorer
  • Loading branch information
hustf authored Nov 3, 2017
2 parents 78987bd + 4cfbf67 commit e1f094c
Show file tree
Hide file tree
Showing 14 changed files with 869 additions and 13 deletions.
3 changes: 1 addition & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ os:
- linux
- osx
julia:
- 0.4
- 0.5
- nightly
- 0.6
sudo: false
notifications:
email: false
Expand Down
30 changes: 20 additions & 10 deletions appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
environment:
matrix:
- JULIA_URL: "https://julialang-s3.julialang.org/bin/winnt/x86/0.4/julia-0.4-latest-win32.exe"
- JULIA_URL: "https://julialang-s3.julialang.org/bin/winnt/x64/0.4/julia-0.4-latest-win64.exe"
- JULIA_URL: "https://julialang-s3.julialang.org/bin/winnt/x86/0.5/julia-0.5-latest-win32.exe"
- JULIA_URL: "https://julialang-s3.julialang.org/bin/winnt/x64/0.6/julia-0.6-latest-win64.exe"
- JULIA_URL: "https://julialang-s3.julialang.org/bin/winnt/x64/0.5/julia-0.5-latest-win64.exe"
- JULIA_URL: "https://julialangnightlies-s3.julialang.org/bin/winnt/x86/julia-latest-win32.exe"
- JULIA_URL: "https://julialangnightlies-s3.julialang.org/bin/winnt/x64/julia-latest-win64.exe"

- JULIA_URL: "https://julialang-s3.julialang.org/bin/winnt/x86/0.6/julia-0.6-latest-win32.exe"
- JULIA_URL: "https://julialang-s3.julialang.org/bin/winnt/x86/0.5/julia-0.5-latest-win32.exe"
# HttpCommon not building yet - JULIA_URL: "https://julialangnightlies-s3.julialang.org/bin/winnt/x64/julia-latest-win64.exe"
# HttpCommon not building yet - JULIA_URL: "https://julialangnightlies-s3.julialang.org/bin/winnt/x86/julia-latest-win32.exe"
branches:
only:
- master
Expand All @@ -18,7 +17,11 @@ notifications:
on_build_failure: false
on_build_status_changed: false

# Not installing PhantomJS. It is being replaced by chrome headless browser in the long run.
install:
- choco install firefox
- choco install googlechrome
- choco install ie11
- ps: "[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls12"
# Download most recent Julia Windows binary
- ps: (new-object net.webclient).DownloadFile(
Expand All @@ -30,8 +33,15 @@ install:
build_script:
# Need to convert from shallow to complete for Pkg.clone to work
- IF EXIST .git\shallow (git fetch --unshallow)
- C:\projects\julia\bin\julia -e "versioninfo();
Pkg.clone(pwd(), \"WebSockets\"); Pkg.build(\"WebSockets\")"

- ps: |
Write-Host "This is PowerShell - STDERR output will be red. Redirect! "
$env:PATH+=";C:\projects\julia\bin\"
julia -e 'versioninfo();redirect_stderr(STDOUT);println(STDOUT, \"stdout\"); println(STDERR, \"stderr\")'
julia -e 'redirect_stderr(STDOUT);Pkg.init()'
julia --depwarn=no -e 'redirect_stderr(STDOUT);Pkg.add(\"HttpServer\");using HttpServer;'
julia -e 'redirect_stderr(STDOUT);Pkg.clone(pwd(), \"WebSockets\");Pkg.build(\"WebSockets\")'
Write-Host "This is PowerShell, build script finished."
test_script:
- C:\projects\julia\bin\julia -e "Pkg.test(\"WebSockets\")"
- ps: |
Write-Host "This is PS"
julia -e 'redirect_stderr(STDOUT);info(\"Local websockets directory:\", Pkg.dir(\"WebSockets\"));Pkg.test(\"WebSockets\")'
6 changes: 6 additions & 0 deletions test/REQUIRE
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
julia 0.5
Compat 0.9.5
HttpCommon 0.0.3
HttpServer 0.0.4
Codecs
MbedTLS
190 changes: 190 additions & 0 deletions test/browsertest.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>WebSockets browsertest.html</title>
</head>
<body>
<p>Test tries to open some websockets, browser says hello from browser, and continues to echo messages from server.</p>
<p id = "init"></p>
<p>&nbsp;&nbsp;This is <b id = "browserid"> ... </b></p>
<div id = "ws1" style="border:thick solid black;"><p>ws1 Websocket</p></div>
<div id = "ws2" style="border:thick solid black;"><p>ws2 Websocket: websocket-testprotocol</p></div>
<div id = "ws3" style="border:thick solid black;"><p>ws3 Websocket: server_denies_protocol</p></div>
</body>
<script>

function addwebsocket_test(){console.log("Add websocket general");
var wsuri = document.URL.replace("http:","ws:");
return new WebSocket(wsuri)
} // addwebsocket_test

function addwebsocket_subprotocol_test(){console.log("Add websocket acceptable subprotocol");
var wsuri = document.URL.replace("http:","ws:");
return new WebSocket(wsuri, "websocket-testprotocol")
} // addwebsocket_subprotocol_test

function addwebsocket_server_denies(){console.log("Add websocket inacceptable subprotocol");
var wsuri = document.URL.replace("http:","ws:");
return new WebSocket(wsuri, "server_denies")
} // addwebsocket_server_denies

var codeDesc ={1000:"Normal",
1001:"Going Away",
1002:"Protocol Error",
1003:"Unsupported Data",
1004:"Reserved",
1005:"No Status Recvd- reserved",
1006:"Abnormal Closure- reserved",
1007:"Invalid frame payload data",
1008:"Policy Violation",
1009:"Message too big",
1010:"Missing Extension",
1011:"Internal Error",
1012:"Service Restart",
1013:"Try Again Later",
1014:"Bad Gateway",
1015:"TLS Handshake"};

var readystateDesc ={0:"CONNECTING",
1:"OPEN",
2:"CLOSING",
3:"CLOSED"};

window.onload= function(){
document.getElementById("init").innerHTML = "<p>:window.onload";
document.getElementById("browserid").innerHTML = getBrowser();

ws1 = addwebsocket_test();
ws1.onclose = function(e){
document.getElementById("ws1").innerHTML +=
"<p>: ws1.onclose event called with " +
"wasClean: " + e.wasClean + "; " +
"code: " + e.code + "; " + codeDesc[e.code] + "; " +
"reason: " + e.reason + "; " +
"<br>&nbsp;&nbsp;Websocket state is now " + ws1.readyState +
" " + readystateDesc[ws1.readyState] + ".</p>";
}
ws1.onerror = function(e){
document.getElementById("ws1").innerHTML += "<p>: ws1.onerror. " +
"<br>&nbsp;&nbsp;Websocket state is now " + ws1.readyState +
" " + readystateDesc[ws2.readyState] + ".</p>";
}
ws1.onopen = function(){
document.getElementById("ws1").innerHTML += "<p>: ws1.onopen. " +
"<br>&nbsp;&nbsp;Websocket state is now " + ws1.readyState +
" " + readystateDesc[ws1.readyState] + ".</p>";
msg = "Hello from socket ws1 in browser " + getBrowser();
document.getElementById("ws1").innerHTML += "<p>&nbsp;&nbsp;..Sending message: " + msg + "</p>";
ws1.send(msg);
}
ws1.onmessage = function(e){
document.getElementById("ws1").innerHTML += "<p>: ws1.onmessage: " + e.data + " </p>";
if(e.data=="YOU hang up!"){
document.getElementById("ws1").innerHTML += "<p>&nbsp;&nbsp; (hanging up, no echo).</p>";
ws1.close()
} else {
document.getElementById("ws1").innerHTML += "<p>&nbsp;&nbsp; echo: " + e.data + " </p>";
ws1.send(e.data)
}
}

ws2 = addwebsocket_subprotocol_test();
ws2.onclose = function(e){
document.getElementById("ws2").innerHTML +=
"<p>: ws2.onclose event called with " +
"wasClean: " + e.wasClean + "; " +
"code: " + e.code + "; " + codeDesc[e.code] + "; " +
"reason: " + e.reason + "; " +
"<br>&nbsp;&nbsp;Websocket state is now " + ws2.readyState +
" " + readystateDesc[ws2.readyState] + ".</p>";
}
ws2.onerror = function(e){
document.getElementById("ws2").innerHTML += "<p>: ws2.onerror. " +
"<br>&nbsp;&nbsp;Websocket state is now " + ws2.readyState +
" " + readystateDesc[ws2.readyState] + ".</p>";
}
ws2.onopen = function(){
document.getElementById("ws2").innerHTML += "<p>: ws2.onopen. " +
"<br>&nbsp;&nbsp;Websocket state is now " + ws2.readyState +
" " + readystateDesc[ws2.readyState] + ".</p>";
msg = "Hello from socket ws2 in browser " + getBrowser();
document.getElementById("ws2").innerHTML += "<p>&nbsp;&nbsp;..Sending message: " + msg + "</p>";
ws2.send(msg);
}
ws2.onmessage = function(e){
document.getElementById("ws2").innerHTML += "<p>: ws2.onmessage: " + e.data + " </p>";
if(e.data=="YOU hang up!"){
document.getElementById("ws2").innerHTML += "<p>&nbsp;&nbsp; (hanging up, no echo).</p>";
ws2.close()
} else {
document.getElementById("ws2").innerHTML += "<p>&nbsp;&nbsp; echo: " + e.data + " </p>";
ws2.send(e.data)
}
}

ws3 = addwebsocket_server_denies();
ws3.onclose = function(e){
document.getElementById("ws3").innerHTML +=
"<p>: ws3.onclose event called with " +
"wasClean: " + e.wasClean + "; " +
"code: " + e.code + "; " + codeDesc[e.code] + "; " +
"reason: " + e.reason + "; " +
"<br>&nbsp;&nbsp;Websocket state is now " + ws3.readyState +
" " + readystateDesc[ws3.readyState] + ".</p>";
}
ws3.onerror = function(e){
document.getElementById("ws3").innerHTML += "<p>: ws3.onerror. " +
"<br>&nbsp;&nbsp;Websocket state is now " + ws3.readyState +
" " + readystateDesc[ws3.readyState] + ".</p>";
}
ws3.onopen = function(){
document.getElementById("ws3").innerHTML += "<p>: ws3.onopen. " +
"<br>&nbsp;&nbsp;Websocket state is now " + ws3.readyState +
" " + readystateDesc[ws3.readyState] + ".</p>";
msg = "Hello from socket 3 in browser " + getBrowser();
document.getElementById("ws3").innerHTML += "<br>&nbsp;&nbsp;..Sending message: " + msg + "</p>";
ws3.send(msg);
}
ws3.onmessage = function(e){
document.getElementById("ws3").innerHTML += "<p>: ws3.onmessage: " + e.data + " </p>";
if(e.data=="YOU hang up!"){
document.getElementById("ws3").innerHTML += "<p>&nbsp;&nbsp; (hanging up, no echo).</p>";
ws3.close()
} else {
document.getElementById("ws3").innerHTML += "<p>&nbsp;&nbsp; echo: " + e.data + " </p>";
ws3.send(e.data)
}
}
} // window.onload

// https://stackoverflow.com/questions/9847580/how-to-detect-safari-chrome-ie-firefox-and-opera-browser
// small change Chrome > Blink.
var getBrowser = function() {
var isPhantom = (navigator.userAgent == 'PhantomJS')
// Opera 8.0+
var isOpera = (!!window.opr && !!opr.addons) || !!window.opera || navigator.userAgent.indexOf(' OPR/') >= 0;
// Firefox 1.0+
var isFirefox = typeof InstallTrigger !== 'undefined';
// Safari 3.0+ "[object HTMLElementConstructor]"
var isSafari = /constructor/i.test(window.HTMLElement) || (function (p) { return p.toString() === "[object SafariRemoteNotification]"; })(!window['safari'] || (typeof safari !== 'undefined' && safari.pushNotification));
// Internet Explorer 6-11
var isIE = /*@cc_on!@*/false || !!document.documentMode;
// Edge 20+
var isEdge = !isIE && !!window.StyleMedia;
// Chrome 1+
var isChrome = !!window.chrome && !!window.chrome.webstore;
// Blink engine detection
var isBlink = (isChrome || isOpera) && !!window.CSS;
if(isPhantom){return "Phantom"};
if(isChrome){return "Chrome"};
if(isBlink){return "Blink"};
if(isEdge){return "Edge"};
if(isIE){return "Internet Explorer"};
if(isSafari){return "Safari"};
if(isFirefox){return "Firefox"};
if(isOpera){return "Opera"};
return "unknown";
}
</script>
</html>
60 changes: 60 additions & 0 deletions test/browsertest.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# Included in runtests at the end.

const WEBSOCKETS = Dict{Int, WebSockets.WebSocket}()
const WEBSOCKETS_SUBPROTOCOL = Dict{Int, WebSockets.WebSocket}()
const RECEIVED_WS_MSGS = Dict{Int, Vector{String}}()
global noofresponders = 0

include("functions_server.jl")

server = start_ws_server_async()

# Give the server 5 seconds to get going.
sleep(5)
info("We waited 5 seconds after starting the server.")
for (ke, va) in WEBSOCKETS
if isopen(va)
info("Somebody opened a websocket during that time. Closing it now.")
close(va)
end
end

info("This OS is ", Sys.KERNEL)
include("functions_open_browsers.jl")
noofbrowsers = open_all_browsers()
const CLOSEAFTER = Base.Dates.Second(15)
t0 = now()
while now()-t0 < CLOSEAFTER && length(keys(RECEIVED_WS_MSGS)) < noofbrowsers * 2
sleep(1)
end
info("Received messages on ", length(keys(RECEIVED_WS_MSGS)), " sockets.")
info("Tell the special sockets to initiate a close.")
for (ke, va) in WEBSOCKETS_SUBPROTOCOL
writeto(ke, "YOU hang up!")
end

sleep(10)
info("Closing down after ", now()-t0, " including 10 seconds pause.")
countstillopen = 0
for (ke, va) in WEBSOCKETS
if isopen(va)
info(" Websocket to close: ", va)
countstillopen +=1
close(va)
end
end
sleep(5)
close(server)
server = nothing
info("Openened $noofbrowsers, from which $noofresponders requested the HTML page.")
info("Received messages for each websocket:")
display(RECEIVED_WS_MSGS)
println()
@test countstillopen == noofresponders
countmessages = 0
for (ke, va) in RECEIVED_WS_MSGS
countmessages += length(va)
end
@test length(keys(RECEIVED_WS_MSGS)) == noofresponders * 2
@test countmessages == noofresponders * 6
nothing
Binary file added test/favicon.ico
Binary file not shown.
Loading

0 comments on commit e1f094c

Please sign in to comment.