Skip to content

Commit

Permalink
Add ConnectionError with response.
Browse files Browse the repository at this point in the history
  • Loading branch information
ioquatix committed Jul 19, 2024
1 parent de416c5 commit 9746e62
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 5 deletions.
1 change: 1 addition & 0 deletions async-websocket.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ Gem::Specification.new do |spec|
spec.required_ruby_version = ">= 3.1"

spec.add_dependency "async-http", "~> 0.54"
spec.add_dependency "protocol-http", ">= 0.28.1"
spec.add_dependency "protocol-rack", "~> 0.5"
spec.add_dependency "protocol-websocket", "~> 0.15"
end
4 changes: 1 addition & 3 deletions lib/async/websocket/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,7 @@ def connect(authority, path, scheme: @delegate.scheme, headers: nil, handler: Co
response = request.call(connection)

unless response.stream?
response.close

raise ProtocolError, "Failed to negotiate connection: #{response.status}"
raise ConnectionError.new("Failed to negotiate connection!", response.unwrap)
end

protocol = response.headers[SEC_WEBSOCKET_PROTOCOL]&.first
Expand Down
4 changes: 4 additions & 0 deletions lib/async/websocket/connect_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ def close
@response.close
end

def unwrap
@response.buffered!
end

attr_accessor :response
attr_accessor :stream

Expand Down
11 changes: 11 additions & 0 deletions lib/async/websocket/error.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,16 @@ class Error < ::Protocol::WebSocket::Error

class UnsupportedVersionError < Error
end

class ConnectionError < Error
def initialize(message, response)
super(message)

@response = response
end

# The failed HTTP response.
attr :response
end
end
end
4 changes: 4 additions & 0 deletions lib/async/websocket/upgrade_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ def close
@response.close
end

def unwrap
@response.buffered!
end

attr_accessor :response

def stream?
Expand Down
23 changes: 21 additions & 2 deletions test/async/websocket/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
end

connection.close
rescue Protocol::WebSocket::ClosedError
# Ignore this error.
end or Protocol::HTTP::Response[404, {}, []]
end
end
Expand Down Expand Up @@ -125,7 +127,24 @@
it "raises an error when the server doesn't support websockets" do
expect do
Async::WebSocket::Client.connect(client_endpoint) {}
end.to raise_exception(Async::WebSocket::ProtocolError, message: be =~ /Failed to negotiate connection/)
end.to raise_exception(Async::WebSocket::ConnectionError, message: be =~ /Failed to negotiate connection/)
end
end

with 'deliberate failure response' do
let(:app) do
Protocol::HTTP::Middleware.for do |request|
Protocol::HTTP::Response[401, {}, ["You are not allowed!"]]
end
end

it "raises a connection error when the server responds with an error" do
begin
Async::WebSocket::Client.connect(client_endpoint) {}
rescue Async::WebSocket::ConnectionError => error
expect(error.response.status).to be == 401
expect(error.response.read).to be == "You are not allowed!"
end
end
end
end
Expand All @@ -134,7 +153,7 @@
it 'raises an error' do
expect do
Async::WebSocket::Client.connect(client_endpoint) {}
end.to raise_exception(Async::WebSocket::ProtocolError, message: be =~ /Failed to negotiate connection/)
end.to raise_exception(Async::WebSocket::ConnectionError, message: be =~ /Failed to negotiate connection/)
end
end

Expand Down

0 comments on commit 9746e62

Please sign in to comment.