forked from JuliaWeb/HTTP.jl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chunking.jl
63 lines (51 loc) · 1.67 KB
/
chunking.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
module TestChunking
using Test, Sockets
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" *
"$(hex(sz + 9))\r\n" * "data: 3$(repeat("x", sz))\n\n" * "\r\n"
decoded_data = "data: 1$(repeat("x", sz))\n\n" *
"data: 2$(repeat("x", sz))\n\n" *
"data: 3$(repeat("x", sz))\n\n"
split1 = 106
split2 = 300
server = HTTP.listen!(port) do http
startwrite(http)
tcp = http.stream.io
write(tcp, encoded_data[1:split1])
flush(tcp)
sleep(1)
write(tcp, encoded_data[split1+1:split2])
flush(tcp)
sleep(1)
write(tcp, encoded_data[split2+1:end])
flush(tcp)
end
r = HTTP.get("http://127.0.0.1:$port")
@test String(r.body) == decoded_data
for wrap in (identity, BufferedInputStream)
r = ""
# 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:$port") do io
io = wrap(io)
x = split(decoded_data, "\n")
for i in 1:6
l = readline(io)
@test l == x[i]
r *= l * "\n"
end
end
end
@test r == decoded_data
end
close(server)
end
end # module