diff --git a/.github/workflows/test-external.yaml b/.github/workflows/test-external.yaml index cbff6759..0d186b8f 100644 --- a/.github/workflows/test-external.yaml +++ b/.github/workflows/test-external.yaml @@ -20,9 +20,9 @@ jobs: - macos ruby: - - "3.0" - "3.1" - "3.2" + - "3.3" steps: - uses: actions/checkout@v3 diff --git a/config/external.yaml b/config/external.yaml index d94b9192..8cc0f3a1 100644 --- a/config/external.yaml +++ b/config/external.yaml @@ -3,7 +3,7 @@ falcon: command: bundle exec bake test async-rest: url: https://github.com/socketry/async-rest.git - command: bundle exec rspec + command: bundle exec sus async-websocket: url: https://github.com/socketry/async-websocket.git command: bundle exec sus diff --git a/fixtures/async/http/a_protocol.rb b/fixtures/async/http/a_protocol.rb index c9a40c9e..62c90e11 100644 --- a/fixtures/async/http/a_protocol.rb +++ b/fixtures/async/http/a_protocol.rb @@ -190,9 +190,7 @@ module HTTP elsif request.method == 'GET' expect(request.body).to be_nil - ::Protocol::HTTP::Response[200, { - 'remote-address' => request.remote_address.inspect - }, ["#{request.method} #{request.version}"]] + ::Protocol::HTTP::Response[200, {'my-header' => 'my-value'}, ["#{request.method} #{request.version}"]] else ::Protocol::HTTP::Response[200, {}, ["Hello World"]] end @@ -276,8 +274,8 @@ def after tempfile.close end - it "has remote-address header" do - expect(response.headers['remote-address']).not.to be_nil + it "has response header" do + expect(response.headers['my-header']).to be == ['my-value'] end it "has protocol version" do diff --git a/lib/async/http/client.rb b/lib/async/http/client.rb index 2c571588..93485f7b 100755 --- a/lib/async/http/client.rb +++ b/lib/async/http/client.rb @@ -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 diff --git a/lib/async/http/protocol/http1/connection.rb b/lib/async/http/protocol/http1/connection.rb index 1275c6a9..a12a6271 100755 --- a/lib/async/http/protocol/http1/connection.rb +++ b/lib/async/http/protocol/http1/connection.rb @@ -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? diff --git a/lib/async/http/protocol/http2/connection.rb b/lib/async/http/protocol/http2/connection.rb index 0068ac23..73f9b75e 100644 --- a/lib/async/http/protocol/http2/connection.rb +++ b/lib/async/http/protocol/http2/connection.rb @@ -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 diff --git a/lib/async/http/server.rb b/lib/async/http/server.rb index 1a8d4bd7..7d5fb1e1 100755 --- a/lib/async/http/server.rb +++ b/lib/async/http/server.rb @@ -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 diff --git a/lib/async/http/version.rb b/lib/async/http/version.rb index 1e2bf3da..4ff55e22 100644 --- a/lib/async/http/version.rb +++ b/lib/async/http/version.rb @@ -5,6 +5,6 @@ module Async module HTTP - VERSION = "0.64.1" + VERSION = "0.64.2" end end diff --git a/test/async/http/client.rb b/test/async/http/client.rb index 3a4cdb91..367096ef 100644 --- a/test/async/http/client.rb +++ b/test/async/http/client.rb @@ -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 diff --git a/test/async/http/protocol/http11.rb b/test/async/http/protocol/http11.rb index ab36016c..711823bc 100755 --- a/test/async/http/protocol/http11.rb +++ b/test/async/http/protocol/http11.rb @@ -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 == "#" + 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} @@ -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 diff --git a/test/async/http/protocol/http2.rb b/test/async/http/protocol/http2.rb index f914653f..7e6ba775 100644 --- a/test/async/http/protocol/http2.rb +++ b/test/async/http/protocol/http2.rb @@ -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 == "#" + 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}