diff --git a/async-websocket.gemspec b/async-websocket.gemspec index 23331ad..ff23c82 100644 --- a/async-websocket.gemspec +++ b/async-websocket.gemspec @@ -24,6 +24,6 @@ Gem::Specification.new do |spec| spec.add_development_dependency "bundler" spec.add_development_dependency "covered" - spec.add_development_dependency "sus", "~> 0.16.0" + spec.add_development_dependency "sus", "~> 0.18" spec.add_development_dependency "sus-fixtures-async-http", "~> 0.2.3" end diff --git a/gems.rb b/gems.rb index 6eac629..bc04589 100644 --- a/gems.rb +++ b/gems.rb @@ -19,4 +19,4 @@ gem "bake-test-external" end -gem "rack", "~> 3.0.0" +# gem "protocol-websocket", path: "../protocol-websocket" diff --git a/lib/async/websocket/client.rb b/lib/async/websocket/client.rb index 4472762..e21339a 100644 --- a/lib/async/websocket/client.rb +++ b/lib/async/websocket/client.rb @@ -41,8 +41,8 @@ def initialize(client, connection) super(connection) end - def close - super + def close(...) + super(...) if @client @client.close diff --git a/lib/async/websocket/version.rb b/lib/async/websocket/version.rb index 9f41810..dda41ad 100644 --- a/lib/async/websocket/version.rb +++ b/lib/async/websocket/version.rb @@ -5,6 +5,6 @@ module Async module WebSocket - VERSION = "0.24.0" + VERSION = "0.25.0" end end diff --git a/test/async/websocket/client.rb b/test/async/websocket/client.rb index 7b70c64..ae71654 100644 --- a/test/async/websocket/client.rb +++ b/test/async/websocket/client.rb @@ -20,16 +20,73 @@ end end - it "can connect to a websocket server and close underlying client" do - Async do |task| + with '#send_close' do + it "can read incoming messages and then close" do + connection = Async::WebSocket::Client.connect(client_endpoint) + 3.times do + connection.send_text("Hello World!") + end + + # This informs the server we are done echoing messages: + connection.send_close + + # Collect all the echoed messages: + messages = [] + while message = connection.read + messages << message + end + + expect(messages.size).to be == 3 + expect(connection).to be(:closed?) + ensure + connection&.close + end + end + + with '#close' do + it "can connect to a websocket server and close underlying client" do + Async do |task| + connection = Async::WebSocket::Client.connect(client_endpoint) + connection.send_text("Hello World!") + message = connection.read + expect(message.to_str).to be == "Hello World!" + + connection.close + expect(task.children).to be(:empty?) + end.wait + end + + it "can connect to a websocket server and close underlying client with an error code" do + Async do |task| + connection = Async::WebSocket::Client.connect(client_endpoint) + connection.send_text("Hello World!") + message = connection.read + expect(message.to_str).to be == "Hello World!" + + connection.close(Protocol::WebSocket::Error::GOING_AWAY, "Bye!") + expect(task.children).to be(:empty?) + end.wait + end + end + + with "#close(1001)" do + let(:app) do + Protocol::HTTP::Middleware.for do |request| + Async::WebSocket::Adapters::HTTP.open(request) do |connection| + connection.send_text("Hello World!") + connection.close(1001) + end + end + end + + it 'closes with custom error' do connection = Async::WebSocket::Client.connect(client_endpoint) - connection.send_text("Hello World!") message = connection.read - expect(message.to_str).to be == "Hello World!" - connection.close - expect(task.children).to be(:empty?) - end.wait + expect do + connection.read + end.to raise_exception(Protocol::WebSocket::Error).and(have_attributes(code: be == 1001)) + end end with 'missing support for websockets' do