Skip to content

Commit 978e839

Browse files
committed
Add more tests for close behaviour. (#51)
* Expect the connection to be closed after sending and then receiving a close frame.
1 parent beb34ca commit 978e839

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

gems.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,4 @@
1919
gem "bake-test-external"
2020
end
2121

22-
gem "rack", "~> 3.0.0"
22+
# gem "protocol-websocket", path: "../protocol-websocket"

test/async/websocket/client.rb

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,27 @@
2020
end
2121
end
2222

23+
with '#send_close' do
24+
it "can read incoming messages and then close" do
25+
connection = Async::WebSocket::Client.connect(client_endpoint)
26+
3.times do
27+
connection.send_text("Hello World!")
28+
end
29+
30+
# This informs the server we are done echoing messages:
31+
connection.send_close
32+
33+
# Collect all the echoed messages:
34+
messages = []
35+
while message = connection.read
36+
messages << message
37+
end
38+
39+
expect(messages.size).to be == 3
40+
expect(connection).to be(:closed?)
41+
end
42+
end
43+
2344
with '#close' do
2445
it "can connect to a websocket server and close underlying client" do
2546
Async do |task|
@@ -46,6 +67,26 @@
4667
end
4768
end
4869

70+
with "#close(1001)" do
71+
let(:app) do
72+
Protocol::HTTP::Middleware.for do |request|
73+
Async::WebSocket::Adapters::HTTP.open(request) do |connection|
74+
connection.send_text("Hello World!")
75+
connection.close(1001)
76+
end
77+
end
78+
end
79+
80+
it 'closes with custom error' do
81+
connection = Async::WebSocket::Client.connect(client_endpoint)
82+
message = connection.read
83+
84+
expect do
85+
connection.read
86+
end.to raise_exception(Protocol::WebSocket::Error).and(have_attributes(code: be == 1001))
87+
end
88+
end
89+
4990
with 'missing support for websockets' do
5091
let(:app) do
5192
Protocol::HTTP::Middleware.for do |request|

0 commit comments

Comments
 (0)