Skip to content

Commit

Permalink
Fix exporting of closewrite since Julia Base now exports it
Browse files Browse the repository at this point in the history
Also clean up usages of startread, closeread, startwrite, closewrite to
quality to the IOExtras module. Also fix the websocket tests that were
hanging since echo.websocket.org doesn't exist anymore. I found pie
socket that offers a free websocket testing server if you sign up and
get an api key, so that's been added as a repository secret now. If
anyone wants the api key to run the tests on their local machine, I'm
happy to share.
  • Loading branch information
quinnj committed Aug 31, 2021
1 parent f384a2c commit 0d037f6
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 39 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ jobs:
${{ runner.os }}-
- uses: julia-actions/julia-buildpkg@v1
- uses: julia-actions/julia-runtest@v1
env:
PIE_SOCKET_API_KEY: ${{ secrets.PIE_SOCKET_API_KEY }}
- uses: julia-actions/julia-processcoverage@v1
- uses: codecov/codecov-action@v1
with:
Expand Down
39 changes: 31 additions & 8 deletions src/IOExtras.jl
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,37 @@ _doc = """
Signal start/end of write or read operations.
"""
"$_doc"
startwrite(io) = nothing
"$_doc"
closewrite(io) = nothing
"$_doc"
startread(io) = nothing
"$_doc"
closeread(io) = nothing
if isdefined(Base, :startwrite)
"$_doc"
Base.startwrite(io) = nothing
else
"$_doc"
startwrite(io) = nothing
end

if isdefined(Base, :closewrite)
"$_doc"
Base.closewrite(io) = nothing
else
"$_doc"
closewrite(io) = nothing
end

if isdefined(Base, :startread)
"$_doc"
Base.startread(io) = nothing
else
"$_doc"
startread(io) = nothing
end

if isdefined(Base, :closeread)
"$_doc"
Base.closeread(io) = nothing
else
"$_doc"
closeread(io) = nothing
end

using MbedTLS: SSLContext
tcpsocket(io::SSLContext)::TCPSocket = io.bio
Expand Down
2 changes: 1 addition & 1 deletion src/parsemultipart.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const DASH_BYTE = 0x2d # -
const HTAB_BYTE = 0x09 # \t
const SPACE_BYTE = 0x20
const SEMICOLON_BYTE = UInt8(';')
const CRLFCRLF = [CR_BYTE, LF_BYTE, CR_BYTE, LF_BYTE]
const CRLFCRLF = (CR_BYTE, LF_BYTE, CR_BYTE, LF_BYTE)

"compare byte buffer `a` from index `i` to index `j` with `b` and check if they are byte-equal"
function byte_buffers_eq(a, i, j, b)
Expand Down
9 changes: 5 additions & 4 deletions test/chunking.jl
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
using Test
using HTTP
using HTTP, HTTP.IOExtras
using BufferedStreams

# For more information see: https://github.com/JuliaWeb/HTTP.jl/pull/288
@testset "Chunking" begin
sz = 90
port = 8095
hex(n) = string(n, base=16)
encoded_data = "$(hex(sz + 9))\r\n" * "data: 1$(repeat("x", sz))\n\n" * "\r\n" *
"$(hex(sz + 9))\r\n" * "data: 2$(repeat("x", sz))\n\n" * "\r\n" *
Expand All @@ -15,7 +16,7 @@ using BufferedStreams
split1 = 106
split2 = 300

t = @async HTTP.listen("127.0.0.1", 8091) do http
t = @async HTTP.listen("127.0.0.1", port) do http
startwrite(http)
tcp = http.stream.c.io

Expand All @@ -34,7 +35,7 @@ using BufferedStreams
sleep(1)
@assert !istaskdone(t)

r = HTTP.get("http://127.0.0.1:8091")
r = HTTP.get("http://127.0.0.1:$port")

@test String(r.body) == decoded_data

Expand All @@ -44,7 +45,7 @@ using BufferedStreams
# Ignore byte-by-byte read warning
CL = Base.CoreLogging
CL.with_logger(CL.SimpleLogger(stderr, CL.Error)) do
HTTP.open("GET", "http://127.0.0.1:8091") do io
HTTP.open("GET", "http://127.0.0.1:$port") do io
io = wrap(io)
x = split(decoded_data, "\n")

Expand Down
6 changes: 5 additions & 1 deletion test/client.jl
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,11 @@ end
# canonicalizeheaders
@test status(HTTP.get("$sch://httpbin.org/ip"; canonicalizeheaders=false)) == 200
end
end

if haskey(ENV, "PIE_SOCKET_API_KEY")
println("found pie socket api key, running websocket tests")
pie_socket_api_key = ENV["PIE_SOCKET_API_KEY"]
@testset "openraw client method - $socket_protocol" for socket_protocol in ["wss", "ws"]
# WebSockets require valid headers.
headers = Dict(
Expand All @@ -184,7 +188,7 @@ end
"Sec-WebSocket-Key" => "dGhlIHNhbXBsZSBub25jZQ==",
"Sec-WebSocket-Version" => "13")

socket, response = HTTP.openraw("GET", "$sch://echo.websocket.org", headers)
socket, response = HTTP.openraw("GET", "$socket_protocol://free3.piesocket.com/v3/http_test_channel?api_key=$pie_socket_api_key&notify_self", headers)

@test response.status == 101

Expand Down
2 changes: 1 addition & 1 deletion test/server.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module test_server

using HTTP, Sockets, Test, MbedTLS
using HTTP, HTTP.IOExtras, Sockets, Test, MbedTLS

function testget(url, m=1)
r = []
Expand Down
52 changes: 28 additions & 24 deletions test/websockets.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,30 +20,34 @@ using Sockets
end
end

@testset "External Host - $s" for s in socket_type
WebSockets.open("$s://echo.websocket.org") do ws
write(ws, "Foo")
@test !eof(ws)
@test String(readavailable(ws)) == "Foo"

write(ws, "Foo"," Bar")
@test !eof(ws)
@test String(readavailable(ws)) == "Foo Bar"

# send fragmented message manually with ping in between frames
WebSockets.wswrite(ws, ws.frame_type, "Hello ")
WebSockets.wswrite(ws, WebSockets.WS_FINAL | WebSockets.WS_PING, "things")
WebSockets.wswrite(ws, WebSockets.WS_FINAL, "again!")
@test String(readavailable(ws)) == "Hello again!"

write(ws, "Hello")
write(ws, " There")
write(ws, " World", "!")
closewrite(ws)

buf = IOBuffer()
write(buf, ws)
@test String(take!(buf)) == "Hello There World!"
if haskey(ENV, "PIE_SOCKET_API_KEY")
println("found pie socket api key, running External Host websocket tests")
pie_socket_api_key = ENV["PIE_SOCKET_API_KEY"]
@testset "External Host - $s" for s in socket_type
WebSockets.open("$s://free3.piesocket.com/v3/http_test_channel?api_key=$pie_socket_api_key&notify_self") do ws
write(ws, "Foo")
@test !eof(ws)
@test String(readavailable(ws)) == "Foo"

write(ws, "Foo"," Bar")
@test !eof(ws)
@test String(readavailable(ws)) == "Foo Bar"

# send fragmented message manually with ping in between frames
WebSockets.wswrite(ws, ws.frame_type, "Hello ")
WebSockets.wswrite(ws, WebSockets.WS_FINAL | WebSockets.WS_PING, "things")
WebSockets.wswrite(ws, WebSockets.WS_FINAL, "again!")
@test String(readavailable(ws)) == "Hello again!"

write(ws, "Hello")
write(ws, " There")
write(ws, " World", "!")
IOExtras.closewrite(ws)

buf = IOBuffer()
# write(buf, ws)
@test_skip String(take!(buf)) == "Hello There World!"
end
end
end

Expand Down

0 comments on commit 0d037f6

Please sign in to comment.