8000 100% coverage for Connection. · zarqman/async-websocket@ebbd887 · GitHub
[go: up one dir, main page]

Skip to content

Commit ebbd887

Browse files
committed
100% coverage for Connection.
1 parent 9e937a0 commit ebbd887

File tree

5 files changed

+68
-54
lines changed

5 files changed

+68
-54
lines changed

lib/async/websocket/adapters/http.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ def self.open(request, headers: [], protocols: [], handler: Connection, extensio
3535
end
3636

3737
response = Response.for(request, headers, protocol: protocol, **options) do |stream|
38-
3938
framer = Protocol::WebSocket::Framer.new(stream)
4039
connection = handler.call(framer, protocol, extensions)
4140

lib/async/websocket/connect_response.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@ def initialize(request, headers = nil, protocol: nil, &block)
1818
if protocol
1919
headers.add(SEC_WEBSOCKET_PROTOCOL, protocol)
2020
end
21-
21+
2222
body = Async::HTTP::Body::Hijack.wrap(request, &block)
23+
2324
super(request.version, 200, headers, body)
2425
end
2526
end

lib/async/websocket/connection.rb

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,6 @@ def reusable?
4141
end
4242

4343
attr :protocol
44-
45-
def call
46-
self.close
47-
end
4844
end
4945
end
5046
end

lib/async/websocket/upgrade_response.rb

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,17 @@ def initialize(request, headers = nil, protocol: nil, &block)
1919
if accept_nounce = request.headers[SEC_WEBSOCKET_KEY]&.first
2020
headers.add(SEC_WEBSOCKET_ACCEPT, Nounce.accept_digest(accept_nounce))
2121
status = 101
22+
23+
if protocol
24+
headers.add(SEC_WEBSOCKET_PROTOCOL, protocol)
25+
end
26+
27+
body = Async::HTTP::Body::Hijack.wrap(request, &block)
28+
29+
super(request.version, 101, headers, body, PROTOCOL)
2230
else
23-
status = 400
31+
super(request.version, 400, headers)
2432
end
25-
26-
if protocol
27-
headers.add(SEC_WEBSOCKET_PROTOCOL, protocol)
28-
end
29-
30-
body = Async::HTTP::Body::Hijack.wrap(request, &block)
31-
super(request.version, status, headers, body, PROTOCOL)
3233
end
3334
end
3435
end

test/async/websocket/server.rb

Lines changed: 57 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -15,75 +15,7 6D47 1 @@
1515
ServerExamples = Sus::Shared('a websocket server') do
1616
let(:websocket_client) {Async::WebSocket::Client.open(client_endpoint)}
1717

18-
with 'generic application' do
19-
let(:message) {"Hello World"}
18+
it "can establish connection" do
19+
connection = websocket_client.connect(endpoint.authority, "/server")
20+
21+
connection.send_text("Hello World!")
22+
message = connection.read
23+
expect(message.to_str).to be == "Hello World!"
24+
25+
connection.close
26+
end
27+
28+
it "can establish connection with block" do
29+
websocket_client.connect(endpoint.authority, "/server") do |connection|
30+
connection.send_text("Hello World!")
31+
message = connection.read
32+
expect(message.to_str).to be == "Hello World!"
33+
end
34+
end
35+
36+
with "headers" do
37+
let(:headers) {{"foo" => "bar"}}
2038

2139
let(:app) do
2240
Protocol::HTTP::Middleware.for do |request|
2341
Async::WebSocket::Adapters::HTTP.open(request) do |connection|
24-
connection.send_text(message)
42+
message = Protocol::WebSocket::JSONMessage.generate(request.headers.fields)
43+
message.send(connection)
44+
2545
connection.close
2646
end or Protocol::HTTP::Response[404, {}, []]
2747
end
2848
end
2949

30-
it "can establish connection" do
31-
connection = websocket_client.connect(endpoint.authority, "/server")
50+
it "can send headers" do
51+
connection = websocket_client.connect(endpoint.authority, "/headers", headers: headers)
3252

3353
begin
34-
expect(connection.read).to be == message
54+
json_message = Protocol::WebSocket::JSONMessage.wrap(connection.read)
55+
56+
expect(json_message.to_h).to have_keys(*headers.keys)
3557
expect(connection.read).to be_nil
3658
expect(connection).to be(:closed?)
3759
ensure
3860
connection.close
3961
end
4062
end
41-
42-
with "headers" do
43-
let(:headers) {{"foo" => "bar"}}
44-
45-
let(:app) do
46-
Protocol::HTTP::Middleware.for do |request|
47-
Async::WebSocket::Adapters::HTTP.open(request) do |connection|
48-
message = Protocol::WebSocket::JSONMessage.generate(request.headers.fields)
49-
message.send(connection)
50-
51-
connection.close
52-
end or Protocol::HTTP::Response[404, {}, []]
53-
end
54-
end
55-
56-
it "can send headers" do
57-
connection = websocket_client.connect(endpoint.authority, "/headers", headers: headers)
58-
59-
begin
60-
json_message = Protocol::WebSocket::JSONMessage.wrap(connection.read)
61-
62-
expect(json_message.to_h).to have_keys(*headers.keys)
63-
expect(connection.read).to be_nil
64-
expect(connection).to be(:closed?)
65-
ensure
66-
connection.close
67-
end
68-
end
69-
end
7063
end
7164

7265
with 'server middleware' do
7366
let(:app) do
7467
Protocol::HTTP::Middleware.build do
75-
use Async::WebSocket::Server do |connection|
76-
connection.send_text("Hello World")
68+
use Async::WebSocket::Server, protocols: ['echo', 'baz'] do |connection|
69+
connection.send_text("protocol: #{connection.protocol}")
7770
connection.close
7871
end
7972
end
8073
end
8174

82-
it "can establish connection" do
83-
connection = websocket_client.connect(endpoint.authority, "/server")
75+
it "can establish connection with explicit protocol" do
76+
connection = websocket_client.connect(endpoint.authority, "/server", protocols: ['echo', 'foo', 'bar'])
77+
78+
# The negotiated protocol:
79+
expect(connection.protocol).to be == 'echo'
8480

8581
begin
86-
expect(connection.read).to be == "Hello World"
82+
expect(connection.read).to be == "protocol: echo"
8783
expect(connection.read).to be_nil
8884
expect(connection).to be(:closed?)
8985
ensure
@@ -96,9 +92,30 @@
9692
describe Async::WebSocket::Server do
9793
include Sus::Fixtures::Async::HTTP::ServerContext
9894

95+
let(:app) do
96+
Protocol::HTTP::Middleware.for do |request|
97+
Async::WebSocket::Adapters::HTTP.open(request) do |connection|
98+
while message = connection.read
99+
connection.write(message)
100+
end
101+
end or Protocol::HTTP::Response[404, {}, []]
102+
end
103+
end
104+
99105
with 'http/1' do
100106
let(:protocol) {Async::HTTP::Protocol::HTTP1}
101107
it_behaves_like ServerExamples
108+
109+
it "fails with bad request if missing nounce" do
110+
request = Protocol::HTTP::Request["GET", "/", {
111+
"upgrade" => "websocket",
112+
"connection" => "upgrade",
113+
}]
114+
115+
response = client.call(request)
116+
117+
expect(response).to be(:bad_request?)
118+
end
102119
end
103120

104121
with 'http/2' do

0 commit comments

Comments
 (0)
0