Skip to content

Commit 8afd1f1

Browse files
committed
Improved support for #as_json and #to_json.
- `Async::HTTP::Client` and `Async::HTTP::Server`. - `Async::HTTP::Protocol::*::Connection` for HTTP/1 and HTTP/2.
1 parent 735efbe commit 8afd1f1

File tree

7 files changed

+134
-8
lines changed

7 files changed

+134
-8
lines changed

lib/async/http/client.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,20 @@ def initialize(endpoint, protocol: endpoint.protocol, scheme: endpoint.scheme, a
4242
@authority = authority
4343
end
4444

45+
def as_json(...)
46+
{
47+
endpoint: @endpoint.to_s,
48+
protocol: @protocol,
49+
retries: @retries,
50+
scheme: @scheme,
51+
authority: @authority,
52+
}
53+
end
54+
55+
def to_json(...)
56+
as_json.to_json(...)
57+
end
58+
4559
attr :endpoint
4660
attr :protocol
4761

lib/async/http/protocol/http1/connection.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,18 @@ def initialize(stream, version)
2020
@version = version
2121
end
2222

23+
def to_s
24+
"\#<#{self.class} negotiated #{@version}, currently #{@ready ? 'ready' : 'in-use'}>"
25+
end
26+
27+
def as_json(...)
28+
to_s
29+
end
30+
31+
def to_json(...)
32+
as_json.to_json(...)
33+
end
34+
2335
attr :version
2436

2537
def http1?

lib/async/http/protocol/http2/connection.rb

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,15 @@ def initialize(*)
3636
end
3737

3838
def to_s
39-
"\#<#{self.class} #{@streams.count} active streams>"
39+
"\#<#{self.class} #{@count} requests, #{@streams.count} active streams>"
40+
end
41+
42+
def as_json(...)
43+
to_s
44+
end
45+
46+
def to_json(...)
47+
as_json.to_json(...)
4048
end
4149

4250
attr :stream

lib/async/http/server.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,18 @@ def initialize(app, endpoint, protocol: endpoint.protocol, scheme: endpoint.sche
2828
@scheme = scheme
2929
end
3030

31+
def as_json(...)
32+
{
33+
endpoint: @endpoint.to_s,
34+
protocol: @protocol,
35+
scheme: @scheme,
36+
}
37+
end
38+
39+
def to_json(...)
40+
as_json.to_json(...)
41+
end
42+
3143
attr :endpoint
3244
attr :protocol
3345
attr :scheme

test/async/http/client.rb

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,40 @@
2323
response.read
2424
expect(response).to be(:success?)
2525
end
26+
27+
with 'client' do
28+
with "#as_json" do
29+
it "generates a JSON representation" do
30+
expect(client.as_json).to be == {
31+
endpoint: client.endpoint.to_s,
32+
protocol: client.protocol,
33+
retries: client.retries,
34+
scheme: endpoint.scheme,
35+
authority: endpoint.authority,
36+
}
37+
end
38+
39+
it 'generates a JSON string' do
40+
expect(JSON.dump(client)).to be == client.to_json
41+
end
42+
end
43+
end
44+
45+
with 'server' do
46+
with "#as_json" do
47+
it "generates a JSON representation" do
48+
expect(server.as_json).to be == {
49+
endpoint: server.endpoint.to_s,
50+
protocol: server.protocol,
51+
scheme: server.scheme,
52+
}
53+
end
54+
55+
it 'generates a JSON string' do
56+
expect(JSON.dump(server)).to be == server.to_json
57+
end
58+
end
59+
end
2660
end
2761

2862
with 'non-existant host' do

test/async/http/protocol/http11.rb

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,29 @@
1111
describe Async::HTTP::Protocol::HTTP11 do
1212
it_behaves_like Async::HTTP::AProtocol
1313

14+
with '#as_json' do
15+
include Sus::Fixtures::Async::HTTP::ServerContext
16+
let(:protocol) {subject}
17+
18+
it "generates a JSON representation" do
19+
response = client.get("/")
20+
connection = response.connection
21+
22+
expect(connection.as_json).to be == "#<Async::HTTP::Protocol::HTTP1::Client negotiated HTTP/1.1, currently ready>"
23+
ensure
24+
response&.close
25+
end
26+
27+
it "generates a JSON string" do
28+
response = client.get("/")
29+
connection = response.connection
30+
31+
expect(JSON.dump(connection)).to be == connection.to_json
32+
ensure
33+
response&.close
34+
end
35+
end
36+
1437
with 'server' do
1538
include Sus::Fixtures::Async::HTTP::ServerContext
1639
let(:protocol) {subject}
@@ -90,13 +113,13 @@ def around
90113
::Protocol::HTTP::Middleware.for do |request|
91114
peer = request.hijack!
92115

93-
peer.write(
94-
"#{request.version} 200 It worked!\r\n" +
95-
"connection: close\r\n" +
96-
"\r\n" +
97-
"Hello World!"
98-
)
99-
peer.close
116+
peer.write(
117+
"#{request.version} 200 It worked!\r\n" +
118+
"connection: close\r\n" +
119+
"\r\n" +
120+
"Hello World!"
121+
)
122+
peer.close
100123

101124
::Protocol::HTTP::Response[-1, {}, body]
102125
end

test/async/http/protocol/http2.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,29 @@
99
describe Async::HTTP::Protocol::HTTP2 do
1010
it_behaves_like Async::HTTP::AProtocol
1111

12+
with '#as_json' do
13+
include Sus::Fixtures::Async::HTTP::ServerContext
14+
let(:protocol) {subject}
15+
16+
it "generates a JSON representation" do
17+
response = client.get("/")
18+
connection = response.connection
19+
20+
expect(connection.as_json).to be == "#<Async::HTTP::Protocol::HTTP2::Client 1 requests, 0 active streams>"
21+
ensure
22+
response&.close
23+
end
24+
25+
it "generates a JSON string" do
26+
response = client.get("/")
27+
connection = response.connection
28+
29+
expect(JSON.dump(connection)).to be == connection.to_json
30+
ensure
31+
response&.close
32+
end
33+
end
34+
1235
with 'server' do
1336
include Sus::Fixtures::Async::HTTP::ServerContext
1437
let(:protocol) {subject}

0 commit comments

Comments
 (0)