Skip to content

Commit

Permalink
Improved support for #as_json and #to_json.
Browse files Browse the repository at this point in the history
- `Async::HTTP::Client` and `Async::HTTP::Server`.
- `Async::HTTP::Protocol::*::Connection` for HTTP/1 and HTTP/2.
  • Loading branch information
ioquatix committed Apr 22, 2024
1 parent 735efbe commit 8afd1f1
Show file tree
Hide file tree
Showing 7 changed files with 134 additions and 8 deletions.
14 changes: 14 additions & 0 deletions lib/async/http/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,20 @@ def initialize(endpoint, protocol: endpoint.protocol, scheme: endpoint.scheme, a
@authority = authority
end

def as_json(...)
{
endpoint: @endpoint.to_s,
protocol: @protocol,
retries: @retries,
scheme: @scheme,
authority: @authority,
}
end

def to_json(...)
as_json.to_json(...)
end

attr :endpoint
attr :protocol

Expand Down
12 changes: 12 additions & 0 deletions lib/async/http/protocol/http1/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,18 @@ def initialize(stream, version)
@version = version
end

def to_s
"\#<#{self.class} negotiated #{@version}, currently #{@ready ? 'ready' : 'in-use'}>"
end

def as_json(...)
to_s
end

def to_json(...)
as_json.to_json(...)
end

attr :version

def http1?
Expand Down
10 changes: 9 additions & 1 deletion lib/async/http/protocol/http2/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,15 @@ def initialize(*)
end

def to_s
"\#<#{self.class} #{@streams.count} active streams>"
"\#<#{self.class} #{@count} requests, #{@streams.count} active streams>"
end

def as_json(...)
to_s
end

def to_json(...)
as_json.to_json(...)
end

attr :stream
Expand Down
12 changes: 12 additions & 0 deletions lib/async/http/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,18 @@ def initialize(app, endpoint, protocol: endpoint.protocol, scheme: endpoint.sche
@scheme = scheme
end

def as_json(...)
{
endpoint: @endpoint.to_s,
protocol: @protocol,
scheme: @scheme,
}
end

def to_json(...)
as_json.to_json(...)
end

attr :endpoint
attr :protocol
attr :scheme
Expand Down
34 changes: 34 additions & 0 deletions test/async/http/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,40 @@
response.read
expect(response).to be(:success?)
end

with 'client' do
with "#as_json" do
it "generates a JSON representation" do
expect(client.as_json).to be == {
endpoint: client.endpoint.to_s,
protocol: client.protocol,
retries: client.retries,
scheme: endpoint.scheme,
authority: endpoint.authority,
}
end

it 'generates a JSON string' do
expect(JSON.dump(client)).to be == client.to_json
end
end
end

with 'server' do
with "#as_json" do
it "generates a JSON representation" do
expect(server.as_json).to be == {
endpoint: server.endpoint.to_s,
protocol: server.protocol,
scheme: server.scheme,
}
end

it 'generates a JSON string' do
expect(JSON.dump(server)).to be == server.to_json
end
end
end
end

with 'non-existant host' do
Expand Down
37 changes: 30 additions & 7 deletions test/async/http/protocol/http11.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,29 @@
describe Async::HTTP::Protocol::HTTP11 do
it_behaves_like Async::HTTP::AProtocol

with '#as_json' do
include Sus::Fixtures::Async::HTTP::ServerContext
let(:protocol) {subject}

it "generates a JSON representation" do
response = client.get("/")
connection = response.connection

expect(connection.as_json).to be == "#<Async::HTTP::Protocol::HTTP1::Client negotiated HTTP/1.1, currently ready>"
ensure
response&.close
end

it "generates a JSON string" do
response = client.get("/")
connection = response.connection

expect(JSON.dump(connection)).to be == connection.to_json
ensure
response&.close
end
end

with 'server' do
include Sus::Fixtures::Async::HTTP::ServerContext
let(:protocol) {subject}
Expand Down Expand Up @@ -90,13 +113,13 @@ def around
::Protocol::HTTP::Middleware.for do |request|
peer = request.hijack!

peer.write(
"#{request.version} 200 It worked!\r\n" +
"connection: close\r\n" +
"\r\n" +
"Hello World!"
)
peer.close
peer.write(
"#{request.version} 200 It worked!\r\n" +
"connection: close\r\n" +
"\r\n" +
"Hello World!"
)
peer.close

::Protocol::HTTP::Response[-1, {}, body]
end
Expand Down
23 changes: 23 additions & 0 deletions test/async/http/protocol/http2.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,29 @@
describe Async::HTTP::Protocol::HTTP2 do
it_behaves_like Async::HTTP::AProtocol

with '#as_json' do
include Sus::Fixtures::Async::HTTP::ServerContext
let(:protocol) {subject}

it "generates a JSON representation" do
response = client.get("/")
connection = response.connection

expect(connection.as_json).to be == "#<Async::HTTP::Protocol::HTTP2::Client 1 requests, 0 active streams>"
ensure
response&.close
end

it "generates a JSON string" do
response = client.get("/")
connection = response.connection

expect(JSON.dump(connection)).to be == connection.to_json
ensure
response&.close
end
end

with 'server' do
include Sus::Fixtures::Async::HTTP::ServerContext
let(:protocol) {subject}
Expand Down

0 comments on commit 8afd1f1

Please sign in to comment.