8000 Add support for passing protocols, normalize usage of options between… · destructobeam/async-websocket@7d05664 · GitHub
[go: up one dir, main page]

Skip to content

Commit 7d05664

Browse files
committed
Add support for passing protocols, normalize usage of options between client and server implementations.
1 parent cc5ae67 commit 7d05664

File tree

7 files changed

+105
-36
lines changed

7 files changed

+105
-36
lines changed

lib/async/websocket/client.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,15 @@ module Async
2424
module WebSocket
2525
# This is a basic synchronous websocket client:
2626
class Client < Connection
27-
def initialize(socket, url = "ws://.", headers = {})
27+
def initialize(socket, url = "ws://.", **options)
2828
@url = url
2929

30-
super socket, build_client(headers)
30+
super socket, build_client(**options)
3131
end
3232

33-
def build_client(headers)
34-
::WebSocket::Driver.client(self).tap do |client|
35-
headers.each do |key, value|
33+
def build_client(headers: nil, **options)
34+
::WebSocket::Driver.client(self, options).tap do |client|
35+
headers&.each do |key, value|
3636
client.set_header(key, value)
3737
end
3838
end

lib/async/websocket/server.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,21 +23,21 @@
2323
module Async
2424
module WebSocket
2525
class Server < Connection
26-
def initialize(env, socket, options)
26+
def initialize(env, socket, **options)
2727
scheme = env['rack.url_scheme'] == 'https' ? 'wss' : 'ws'
28-
@url = "#{scheme}://#{env['HTTP_HOST']}#{env['REQUEST_URI']}"
2928

29+
@url = "#{scheme}://#{env['HTTP_HOST']}#{env['REQUEST_URI']}"
3030
@env = env
3131

32-
super socket, ::WebSocket::Driver.rack(self, options)
32+
super(socket, ::WebSocket::Driver.rack(self, options))
3333
end
3434

3535
attr :env
3636
attr :url
3737

3838
HIJACK_RESPONSE = [-1, {}, []].freeze
3939

40-
def self.open(env, options = {})
40+
def self.open(env, **options)
4141
if ::WebSocket::Driver.websocket?(env)
4242
return nil unless env['rack.hijack?']
4343

spec/async/websocket/client_spec.rb

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,23 @@
1+
# Copyright, 2019, by Samuel G. D. Williams. <http://www.codeotaku.com>
2+
#
3+
# Permission is hereby granted, free of charge, to any person obtaining a copy
4+
# of this software and associated documentation files (the "Software"), to deal
5+
# in the Software without restriction, including without limitation the rights
6+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
# copies of the Software, and to permit persons to whom the Software is
8+
# furnished to do so, subject to the following conditions:
9+
#
10+
# The above copyright notice and this permission notice shall be included in
11+
# all copies or substantial portions of the Software.
12+
#
13+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19+
# THE SOFTWARE.
20+
121
require "async/websocket/client"
222

323
RSpec.describe Async::WebSocket::Client do
@@ -19,7 +39,7 @@
1939
expect(client_double).to receive(:set_header).with(key, value)
2040
end
2141

22-
described_class.new(double(write: nil), "", headers)
42+
described_class.new(double(write: nil), "", headers: headers)
2343
end
2444

2545
context "without passing headers" do

spec/async/websocket/connection_spec.rb

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,12 @@
2626
require 'falcon/adapters/rack'
2727
require 'async/http/url_endpoint'
2828

29-
RSpec.describe Async::WebSocket::Connection, timeout: 5 do
29+
require 'pry'
30+
31+
RSpec.describe Async::WebSocket::Connection, timeout: nil do
3032
include_context Async::RSpec::Reactor
3133

32-
let(:server_address) {Async::HTTP::URLEndpoint.parse("http://localhost:9000", :reuse_port => true)}
34+
let(:server_address) {Async::HTTP::URLEndpoint.parse("http://localhost:9000")}
3335
let(:app) {Rack::Builder.parse_file(File.expand_path('../connection_spec.ru', __FILE__)).first}
3436
let(:server) {Falcon::Server.new(Falcon::Server.middleware(app, verbose: true), server_address)}
3537

@@ -55,14 +57,22 @@
5557
server_task.stop
5658
end
5759

58-
it "should send back Sec-WebScoket-Protocol header" do
60+
it "should send back Sec-WebSocket-Protocol header" do
5961
server_task = reactor.async do
6062
server.run
6163
end
6264

63-
# Should send and receive Sec-WebScoket-Protocol header as
65+
# Should send and receive Sec-WebSocket-Protocol header as
6466
# `ws`
6567

68+
server_address.connect do |socket|
69+
client = Async::WebSocket::Client.new(socket, protocols: ['ws'])
70+
71+
expect(client.next_event).to be_kind_of(WebSocket::Driver::OpenEvent)
72+
73+
expect(client.driver.protocol).to be == 'ws'
74+
end
75+
6676
server_task.stop
6777
end
6878
end

spec/async/websocket/connection_spec.ru

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,24 @@
1+
# Copyright, 2019, by Samuel G. D. Williams. <http://www.codeotaku.com>
2+
#
3+
# Permission is hereby granted, free of charge, to any person obtaining a copy
4+
# of this software and associated documentation files (the "Software"), to deal
5+
# in the Software without restriction, including without limitation the rights
6+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
# copies of the Software, and to permit persons to whom the Software is
8+
# furnished to do so, subject to the following conditions:
9+
#
10+
# The above copyright notice and this permission notice shall be included in
11+
# all copies or substantial portions of the Software.
12+
#
13+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19+
# THE SOFTWARE.
120

2-
require 'async/websocket/server'
3-
4-
class Upgrade
5-
def initialize(app)
6-
@app = app
7-
end
8-
9-
def call(env)
10-
result = Async::WebSocket::Server.open(env, :protocols => %w[ws]) do |server|
11-
read, write = IO.pipe
12-
13-
Process.spawn("ls -lah", :out => write)
14-
write.close
15-
16-
read.each_line do |line|
17-
server.send_text(line)
18-
end
19-
20-
end or @app.call(env)
21-
end
22-
end
21+
require_relative 'upgrade'
2322

2423
use Upgrade
2524

spec/async/websocket/upgrade.rb

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Copyright, 2019, by Samuel G. D. Williams. <http://www.codeotaku.com>
2+
#
3+
# Permission is hereby granted, free of charge, to any person obtaining a copy
4+
# of this software and associated documentation files (the "Software"), to deal
5+
# in the Software without restriction, including without limitation the rights
6+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
# copies of the Software, and to permit persons to whom the Software is
8+
# furnished to do so, subject to the following conditions:
9+
#
10+
# The above copyright notice and this permission notice shall be included in
11+
# all copies or substantial portions of the Software.
12+
#
13+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19+
# THE SOFTWARE.
20+
21+
require 'async/websocket/server'
22+
23+
class Upgrade
24+
def initialize(app)
25+
@app = app
26+
end
27+
28+
def call(env)
29+
result = Async::WebSocket::Server.open(env, protocols: ['ws']) do |server|
30+
read, write = IO.pipe
31+
32+
Process.spawn("ls -lah", :out => write)
33+
write.close
34+
35+
read.each_line do |line|
36+
server.send_text(line)
37+
end
38+
39+
end or @app.call(env)
40+
end
41+
end

spec/spec_helper.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11

22
require 'covered/rspec'
3-
require "async/websocket"
43

54
# Shared rspec helpers:
65
require "async/rspec"

0 commit comments

Comments
 (0)
0