-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathhandshaketest_functions.jl
36 lines (34 loc) · 1.45 KB
/
handshaketest_functions.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# included in handshaketests.jl
import Base.BufferStream
function templaterequests()
chromeheaders = Dict{String, String}( "Connection"=>"Upgrade",
"Upgrade"=>"websocket")
firefoxheaders = Dict{String, String}("Connection"=>"keep-alive, Upgrade",
"Upgrade"=>"websocket")
chromerequest = HTTP.Request("GET", "/", collect(chromeheaders))
firefoxrequest = HTTP.Request("GET", "/", collect(firefoxheaders))
return [chromerequest, firefoxrequest]
end
convert(::Type{HTTP.Header}, pa::Pair{String,String}) = Pair(SubString(pa[1]), SubString(pa[2]))
sethd(r::HTTP.Request, pa::Pair) = sethd(r, convert(HTTP.Header, pa))
sethd(r::HTTP.Request, pa::HTTP.Header) = HTTP.setheader(r, pa)
takefirstline(buf::IOBuffer) = strip(split(buf |> take! |> String, "\r\n")[1])
takefirstline(buf::BufferStream) = strip(split(buf |> read |> String, "\r\n")[1])
"""
The dummy websocket don't use TCP. Close won't work, but we can manipulate the contents
using otherwise the same functions as TCP sockets.
"""
dummyws(server::Bool) = WebSocket(BufferStream(), server)
function dummywshandler(req, dws::WebSocket{BufferStream})
close(dws.socket)
close(dws)
end
function handshakeresponse(request::HTTP.Request)
buf = BufferStream()
c = HTTP.Connection(buf)
s = HTTP.Stream(request, c)
WebSockets.upgrade(dummywshandler, s)
close(buf)
takefirstline(buf)
end
nothing