From aec454a2e9a35b2526a59577fe8f06f1e26fcf36 Mon Sep 17 00:00:00 2001 From: Samuel Williams Date: Wed, 11 Sep 2024 14:33:23 +1200 Subject: [PATCH] Modernize gem. --- .github/workflows/rubocop.yaml | 20 ++----- .github/workflows/test-coverage.yaml | 6 +- .rubocop.yml | 7 +++ async-websocket.gemspec | 6 +- config/sus.rb | 4 +- examples/binance/client.rb | 6 +- examples/chat/client.rb | 6 +- examples/chat/config.ru | 10 ++-- examples/chat/multi-client.rb | 12 ++-- examples/mud/client.rb | 6 +- examples/mud/config.ru | 2 +- examples/rack/client.rb | 6 +- examples/rack/config.ru | 2 +- fixtures/async/websocket/rack_application.rb | 6 +- .../websocket/rack_application/config.ru | 6 +- gems.rb | 2 +- lib/async/websocket.rb | 8 +-- lib/async/websocket/adapters/http.rb | 8 +-- lib/async/websocket/adapters/rack.rb | 8 +-- lib/async/websocket/adapters/rails.rb | 4 +- lib/async/websocket/client.rb | 14 ++--- lib/async/websocket/connect_request.rb | 14 ++--- lib/async/websocket/connect_response.rb | 6 +- lib/async/websocket/connection.rb | 8 +-- lib/async/websocket/error.rb | 4 +- lib/async/websocket/request.rb | 8 +-- lib/async/websocket/response.rb | 8 +-- lib/async/websocket/server.rb | 6 +- lib/async/websocket/upgrade_request.rb | 16 +++-- lib/async/websocket/upgrade_response.rb | 8 +-- test/async/websocket/adapters/rack.rb | 16 ++--- test/async/websocket/client.rb | 58 +++++++++---------- test/async/websocket/connection.rb | 2 +- test/async/websocket/request.rb | 6 +- test/async/websocket/response.rb | 4 +- test/async/websocket/server.rb | 28 ++++----- 36 files changed, 167 insertions(+), 174 deletions(-) diff --git a/.github/workflows/rubocop.yaml b/.github/workflows/rubocop.yaml index edfd9bc..287c06d 100644 --- a/.github/workflows/rubocop.yaml +++ b/.github/workflows/rubocop.yaml @@ -1,4 +1,4 @@ -name: Test External +name: RuboCop on: [push, pull_request] @@ -9,26 +9,14 @@ env: CONSOLE_OUTPUT: XTerm jobs: - test: - name: ${{matrix.ruby}} on ${{matrix.os}} - runs-on: ${{matrix.os}}-latest - - strategy: - matrix: - os: - - ubuntu - - macos - - ruby: - - "3.1" - - "3.2" - - "3.3" + check: + runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - uses: ruby/setup-ruby@v1 with: - ruby-version: ${{matrix.ruby}} + ruby-version: ruby bundler-cache: true - name: Run RuboCop diff --git a/.github/workflows/test-coverage.yaml b/.github/workflows/test-coverage.yaml index d7e5b24..ffa0927 100644 --- a/.github/workflows/test-coverage.yaml +++ b/.github/workflows/test-coverage.yaml @@ -33,8 +33,8 @@ jobs: - name: Run tests timeout-minutes: 5 run: bundle exec bake test - - - uses: actions/upload-artifact@v3 + + - uses: actions/upload-artifact@v4 with: name: coverage-${{matrix.os}}-${{matrix.ruby}} path: .covered.db @@ -50,7 +50,7 @@ jobs: ruby-version: "3.3" bundler-cache: true - - uses: actions/download-artifact@v3 + - uses: actions/download-artifact@v4 - name: Validate coverage timeout-minutes: 5 diff --git a/.rubocop.yml b/.rubocop.yml index 442c667..3b8d476 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -16,6 +16,9 @@ Layout/IndentationConsistency: Enabled: true EnforcedStyle: normal +Layout/BlockAlignment: + Enabled: true + Layout/EndAlignment: Enabled: true EnforcedStyleAlignWith: start_of_line @@ -44,3 +47,7 @@ Layout/EmptyLinesAroundModuleBody: Style/FrozenStringLiteralComment: Enabled: true + +Style/StringLiterals: + Enabled: true + EnforcedStyle: double_quotes diff --git a/async-websocket.gemspec b/async-websocket.gemspec index 936fa6f..cde9ccd 100644 --- a/async-websocket.gemspec +++ b/async-websocket.gemspec @@ -10,8 +10,8 @@ Gem::Specification.new do |spec| spec.authors = ["Samuel Williams", "Simon Crocker", "Olle Jonsson", "Thomas Morgan", "Aurora Nockert", "Bryan Powell", "Emily Love Mills", "Gleb Sinyavskiy", "Janko Marohnić", "Juan Antonio Martín Lucas", "Michel Boaventura", "Peter Runich", "Ryu Sato"] spec.license = "MIT" - spec.cert_chain = ['release.cert'] - spec.signing_key = File.expand_path('~/.gem/release.pem') + spec.cert_chain = ["release.cert"] + spec.signing_key = File.expand_path("~/.gem/release.pem") spec.homepage = "https://github.com/socketry/async-websocket" @@ -21,7 +21,7 @@ Gem::Specification.new do |spec| "source_code_uri" => "https://github.com/socketry/async-websocket.git", } - spec.files = Dir.glob(['{lib}/**/*', '*.md'], File::FNM_DOTMATCH, base: __dir__) + spec.files = Dir.glob(["{lib}/**/*", "*.md"], File::FNM_DOTMATCH, base: __dir__) spec.required_ruby_version = ">= 3.1" diff --git a/config/sus.rb b/config/sus.rb index 9905469..be5cd75 100644 --- a/config/sus.rb +++ b/config/sus.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true # Released under the MIT License. -# Copyright, 2022, by Samuel Williams. +# Copyright, 2022-2024, by Samuel Williams. -require 'covered/sus' +require "covered/sus" include Covered::Sus diff --git a/examples/binance/client.rb b/examples/binance/client.rb index b29942b..cdb55df 100755 --- a/examples/binance/client.rb +++ b/examples/binance/client.rb @@ -4,9 +4,9 @@ # Released under the MIT License. # Copyright, 2021-2024, by Samuel Williams. -require 'async' -require 'async/http' -require 'async/websocket' +require "async" +require "async/http" +require "async/websocket" URL = "wss://stream.binance.com:9443/ws/btcusdt@bookTicker" diff --git a/examples/chat/client.rb b/examples/chat/client.rb index 015a49f..f7296a1 100755 --- a/examples/chat/client.rb +++ b/examples/chat/client.rb @@ -4,9 +4,9 @@ # Released under the MIT License. # Copyright, 2018-2024, by Samuel Williams. -require 'async' -require 'async/http/endpoint' -require_relative '../../lib/async/websocket/client' +require "async" +require "async/http/endpoint" +require_relative "../../lib/async/websocket/client" USER = ARGV.pop || "anonymous" URL = ARGV.pop || "https://localhost:8080" diff --git a/examples/chat/config.ru b/examples/chat/config.ru index 5d9c8e5..9284dcb 100755 --- a/examples/chat/config.ru +++ b/examples/chat/config.ru @@ -1,11 +1,11 @@ #!/usr/bin/env -S falcon serve --bind https://localhost:8080 --count 1 -c # frozen_string_literal: true -require_relative '../../lib/async/websocket/adapters/rack' -require 'async/clock' -require 'async/semaphore' +require_relative "../../lib/async/websocket/adapters/rack" +require "async/clock" +require "async/semaphore" -require 'set' +require "set" # GC.disable @@ -70,7 +70,7 @@ class Room end def start_profile - require 'ruby-prof' unless defined?(RubyProf) + require "ruby-prof" unless defined?(RubyProf) return false if @profile diff --git a/examples/chat/multi-client.rb b/examples/chat/multi-client.rb index d64ede3..c991814 100755 --- a/examples/chat/multi-client.rb +++ b/examples/chat/multi-client.rb @@ -4,13 +4,13 @@ # Released under the MIT License. # Copyright, 2019-2024, by Samuel Williams. -require 'async' -require 'async/semaphore' -require 'async/clock' -require 'async/http/endpoint' -require_relative '../../lib/async/websocket/client' +require "async" +require "async/semaphore" +require "async/clock" +require "async/http/endpoint" +require_relative "../../lib/async/websocket/client" -require 'samovar' +require "samovar" # GC.disable GC::Profiler.enable diff --git a/examples/mud/client.rb b/examples/mud/client.rb index e8a586d..800966c 100755 --- a/examples/mud/client.rb +++ b/examples/mud/client.rb @@ -5,9 +5,9 @@ # Copyright, 2019-2024, by Samuel Williams. # Copyright, 2020, by Juan Antonio Martín Lucas. -require 'async' -require 'async/http/endpoint' -require 'async/websocket/client' +require "async" +require "async/http/endpoint" +require "async/websocket/client" USER = ARGV.pop || "anonymous" URL = ARGV.pop || "http://127.0.0.1:7070" diff --git a/examples/mud/config.ru b/examples/mud/config.ru index 24c7c28..086a2e5 100755 --- a/examples/mud/config.ru +++ b/examples/mud/config.ru @@ -1,7 +1,7 @@ #!/usr/bin/env -S falcon serve --count 1 --bind http://127.0.0.1:7070 -c # frozen_string_literal: true -require 'async/websocket/adapters/rack' +require "async/websocket/adapters/rack" class Room def initialize(name, description = nil) diff --git a/examples/rack/client.rb b/examples/rack/client.rb index baa067d..b14bef2 100755 --- a/examples/rack/client.rb +++ b/examples/rack/client.rb @@ -4,9 +4,9 @@ # Released under the MIT License. # Copyright, 2019-2024, by Samuel Williams. -require 'async' -require 'async/http/endpoint' -require 'async/websocket/client' +require "async" +require "async/http/endpoint" +require "async/websocket/client" URL = ARGV.pop || "http://127.0.0.1:7070" diff --git a/examples/rack/config.ru b/examples/rack/config.ru index 6ecc3e4..675ef28 100755 --- a/examples/rack/config.ru +++ b/examples/rack/config.ru @@ -1,7 +1,7 @@ #!/usr/bin/env -S falcon serve --bind http://127.0.0.1:7070 --count 1 -c # frozen_string_literal: true -require 'async/websocket/adapters/rack' +require "async/websocket/adapters/rack" app = lambda do |env| response = Async::WebSocket::Adapters::Rack.open(env) do |connection| diff --git a/fixtures/async/websocket/rack_application.rb b/fixtures/async/websocket/rack_application.rb index e7298f0..a88e220 100644 --- a/fixtures/async/websocket/rack_application.rb +++ b/fixtures/async/websocket/rack_application.rb @@ -3,8 +3,8 @@ # Released under the MIT License. # Copyright, 2022-2024, by Samuel Williams. -require 'sus/fixtures/async/http/server_context' -require 'protocol/rack/adapter' +require "sus/fixtures/async/http/server_context" +require "protocol/rack/adapter" module Async module WebSocket @@ -12,7 +12,7 @@ module RackApplication include Sus::Fixtures::Async::HTTP::ServerContext def builder - Rack::Builder.parse_file(File.expand_path('rack_application/config.ru', __dir__)) + Rack::Builder.parse_file(File.expand_path("rack_application/config.ru", __dir__)) end def app diff --git a/fixtures/async/websocket/rack_application/config.ru b/fixtures/async/websocket/rack_application/config.ru index f6cd30b..2ac9fb5 100644 --- a/fixtures/async/websocket/rack_application/config.ru +++ b/fixtures/async/websocket/rack_application/config.ru @@ -1,8 +1,8 @@ #!/usr/bin/env -S falcon serve --bind http://localhost:7070 --count 1 -c # frozen_string_literal: true -require 'async/websocket/adapters/rack' -require 'set' +require "async/websocket/adapters/rack" +require "set" $connections = Set.new @@ -26,7 +26,7 @@ end use ClosedLogger run do |env| - Async::WebSocket::Adapters::Rack.open(env, protocols: ['ws']) do |connection| + Async::WebSocket::Adapters::Rack.open(env, protocols: ["ws"]) do |connection| $connections << connection begin diff --git a/gems.rb b/gems.rb index 678f099..3f85b17 100644 --- a/gems.rb +++ b/gems.rb @@ -3,7 +3,7 @@ # Released under the MIT License. # Copyright, 2015-2024, by Samuel Williams. -source 'https://rubygems.org' +source "https://rubygems.org" gemspec diff --git a/lib/async/websocket.rb b/lib/async/websocket.rb index 761640c..d1b7590 100644 --- a/lib/async/websocket.rb +++ b/lib/async/websocket.rb @@ -1,8 +1,8 @@ # frozen_string_literal: true # Released under the MIT License. -# Copyright, 2015-2023, by Samuel Williams. +# Copyright, 2015-2024, by Samuel Williams. -require_relative 'websocket/version' -require_relative 'websocket/server' -require_relative 'websocket/client' +require_relative "websocket/version" +require_relative "websocket/server" +require_relative "websocket/client" diff --git a/lib/async/websocket/adapters/http.rb b/lib/async/websocket/adapters/http.rb index bf17a58..f8a3336 100644 --- a/lib/async/websocket/adapters/http.rb +++ b/lib/async/websocket/adapters/http.rb @@ -1,13 +1,13 @@ # frozen_string_literal: true # Released under the MIT License. -# Copyright, 2021-2023, by Samuel Williams. +# Copyright, 2021-2024, by Samuel Williams. # Copyright, 2021, by Aurora Nockert. -require_relative '../connection' -require_relative '../response' +require_relative "../connection" +require_relative "../response" -require 'protocol/websocket/extensions' +require "protocol/websocket/extensions" module Async module WebSocket diff --git a/lib/async/websocket/adapters/rack.rb b/lib/async/websocket/adapters/rack.rb index 5dae3dc..05e489d 100644 --- a/lib/async/websocket/adapters/rack.rb +++ b/lib/async/websocket/adapters/rack.rb @@ -1,11 +1,11 @@ # frozen_string_literal: true # Released under the MIT License. -# Copyright, 2019-2022, by Samuel Williams. +# Copyright, 2019-2024, by Samuel Williams. -require_relative 'http' -require 'protocol/rack/request' -require 'protocol/rack/adapter' +require_relative "http" +require "protocol/rack/request" +require "protocol/rack/adapter" module Async module WebSocket diff --git a/lib/async/websocket/adapters/rails.rb b/lib/async/websocket/adapters/rails.rb index 5c10325..bf5c278 100644 --- a/lib/async/websocket/adapters/rails.rb +++ b/lib/async/websocket/adapters/rails.rb @@ -1,10 +1,10 @@ # frozen_string_literal: true # Released under the MIT License. -# Copyright, 2021-2023, by Samuel Williams. +# Copyright, 2021-2024, by Samuel Williams. # Copyright, 2023, by Emily Love Mills. -require_relative 'rack' +require_relative "rack" module Async module WebSocket diff --git a/lib/async/websocket/client.rb b/lib/async/websocket/client.rb index 729283b..beb3b9a 100644 --- a/lib/async/websocket/client.rb +++ b/lib/async/websocket/client.rb @@ -6,16 +6,16 @@ # Copyright, 2019, by Janko Marohnić. # Copyright, 2023, by Thomas Morgan. -require_relative 'request' -require_relative 'connection' +require_relative "request" +require_relative "connection" -require 'protocol/websocket/headers' -require 'protocol/websocket/extensions' -require 'protocol/http/middleware' +require "protocol/websocket/headers" +require "protocol/websocket/extensions" +require "protocol/http/middleware" -require 'async/http/client' +require "async/http/client" -require 'delegate' +require "delegate" module Async module WebSocket diff --git a/lib/async/websocket/connect_request.rb b/lib/async/websocket/connect_request.rb index 5969503..a70a0b5 100644 --- a/lib/async/websocket/connect_request.rb +++ b/lib/async/websocket/connect_request.rb @@ -1,15 +1,15 @@ # frozen_string_literal: true # Released under the MIT License. -# Copyright, 2019-2023, by Samuel Williams. +# Copyright, 2019-2024, by Samuel Williams. # Copyright, 2023, by Thomas Morgan. -require 'protocol/http/request' -require 'protocol/http/headers' -require 'protocol/websocket/headers' -require 'protocol/http/body/readable' +require "protocol/http/request" +require "protocol/http/headers" +require "protocol/websocket/headers" +require "protocol/http/body/readable" -require 'async/variable' +require "async/variable" module Async module WebSocket @@ -75,7 +75,7 @@ def initialize(request, protocols: [], version: 13, &block) headers.add(SEC_WEBSOCKET_VERSION, String(version)) if protocols.any? - headers.add(SEC_WEBSOCKET_PROTOCOL, protocols.join(',')) + headers.add(SEC_WEBSOCKET_PROTOCOL, protocols.join(",")) end super(request.scheme, request.authority, ::Protocol::HTTP::Methods::CONNECT, request.path, nil, headers, body, PROTOCOL) diff --git a/lib/async/websocket/connect_response.rb b/lib/async/websocket/connect_response.rb index 987e830..1af2c66 100644 --- a/lib/async/websocket/connect_response.rb +++ b/lib/async/websocket/connect_response.rb @@ -1,10 +1,10 @@ # frozen_string_literal: true # Released under the MIT License. -# Copyright, 2019-2023, by Samuel Williams. +# Copyright, 2019-2024, by Samuel Williams. -require 'protocol/http/response' -require 'async/http/body/hijack' +require "protocol/http/response" +require "async/http/body/hijack" module Async module WebSocket diff --git a/lib/async/websocket/connection.rb b/lib/async/websocket/connection.rb index 6eb6eb4..42c591b 100644 --- a/lib/async/websocket/connection.rb +++ b/lib/async/websocket/connection.rb @@ -1,13 +1,13 @@ # frozen_string_literal: true # Released under the MIT License. -# Copyright, 2018-2023, by Samuel Williams. +# Copyright, 2018-2024, by Samuel Williams. # Copyright, 2019, by Janko Marohnić. -require 'protocol/websocket/connection' -require 'protocol/websocket/headers' +require "protocol/websocket/connection" +require "protocol/websocket/headers" -require 'json' +require "json" module Async module WebSocket diff --git a/lib/async/websocket/error.rb b/lib/async/websocket/error.rb index 7c147e3..8d79223 100644 --- a/lib/async/websocket/error.rb +++ b/lib/async/websocket/error.rb @@ -1,9 +1,9 @@ # frozen_string_literal: true # Released under the MIT License. -# Copyright, 2019-2023, by Samuel Williams. +# Copyright, 2019-2024, by Samuel Williams. -require 'protocol/websocket/error' +require "protocol/websocket/error" module Async module WebSocket diff --git a/lib/async/websocket/request.rb b/lib/async/websocket/request.rb index 0649077..f800c91 100644 --- a/lib/async/websocket/request.rb +++ b/lib/async/websocket/request.rb @@ -1,11 +1,11 @@ # frozen_string_literal: true # Released under the MIT License. -# Copyright, 2019-2023, by Samuel Williams. +# Copyright, 2019-2024, by Samuel Williams. -require_relative 'connect_request' -require_relative 'upgrade_request' -require_relative 'error' +require_relative "connect_request" +require_relative "upgrade_request" +require_relative "error" module Async module WebSocket diff --git a/lib/async/websocket/response.rb b/lib/async/websocket/response.rb index 70de875..934dcd2 100644 --- a/lib/async/websocket/response.rb +++ b/lib/async/websocket/response.rb @@ -1,12 +1,12 @@ # frozen_string_literal: true # Released under the MIT License. -# Copyright, 2019-2023, by Samuel Williams. +# Copyright, 2019-2024, by Samuel Williams. -require_relative 'upgrade_response' -require_relative 'connect_response' +require_relative "upgrade_response" +require_relative "connect_response" -require_relative 'error' +require_relative "error" module Async module WebSocket diff --git a/lib/async/websocket/server.rb b/lib/async/websocket/server.rb index cafe5d3..96d59d5 100644 --- a/lib/async/websocket/server.rb +++ b/lib/async/websocket/server.rb @@ -4,10 +4,10 @@ # Copyright, 2018-2024, by Samuel Williams. # Copyright, 2019, by Simon Crocker. -require_relative 'connection' -require_relative 'response' +require_relative "connection" +require_relative "response" -require 'protocol/http/middleware' +require "protocol/http/middleware" module Async module WebSocket diff --git a/lib/async/websocket/upgrade_request.rb b/lib/async/websocket/upgrade_request.rb index b0c8553..72306ed 100644 --- a/lib/async/websocket/upgrade_request.rb +++ b/lib/async/websocket/upgrade_request.rb @@ -5,15 +5,13 @@ # Copyright, 2023, by Thomas Morgan. # Copyright, 2024, by Ryu Sato. -require 'protocol/http/middleware' -require 'protocol/http/request' +require "protocol/http/middleware" +require "protocol/http/request" -require 'protocol/http/headers' -require 'protocol/websocket/headers' +require "protocol/http/headers" +require "protocol/websocket/headers" -require 'securerandom' - -require_relative 'error' +require_relative "error" module Async module WebSocket @@ -65,7 +63,7 @@ def initialize(request, protocols: [], version: 13, &block) headers.add(SEC_WEBSOCKET_VERSION, String(version)) if protocols.any? - headers.add(SEC_WEBSOCKET_PROTOCOL, protocols.join(',')) + headers.add(SEC_WEBSOCKET_PROTOCOL, protocols.join(",")) end super(request.scheme, request.authority, ::Protocol::HTTP::Methods::GET, request.path, nil, headers, nil, PROTOCOL) @@ -82,7 +80,7 @@ def call(connection) end end - verified = accept_digest && Array(response.protocol).map(&:downcase) == %w(websocket) && response.headers['connection']&.include?('upgrade') + verified = accept_digest && Array(response.protocol).map(&:downcase) == %w(websocket) && response.headers["connection"]&.include?("upgrade") return Wrapper.new(response, verified: verified) end diff --git a/lib/async/websocket/upgrade_response.rb b/lib/async/websocket/upgrade_response.rb index 0fd5e4b..18f96dc 100644 --- a/lib/async/websocket/upgrade_response.rb +++ b/lib/async/websocket/upgrade_response.rb @@ -1,12 +1,12 @@ # frozen_string_literal: true # Released under the MIT License. -# Copyright, 2019-2023, by Samuel Williams. +# Copyright, 2019-2024, by Samuel Williams. # Copyright, 2023, by Thomas Morgan. -require 'async/http/body/hijack' -require 'protocol/http/response' -require 'protocol/websocket/headers' +require "async/http/body/hijack" +require "protocol/http/response" +require "protocol/websocket/headers" module Async module WebSocket diff --git a/test/async/websocket/adapters/rack.rb b/test/async/websocket/adapters/rack.rb index 7d682ad..1a71e82 100644 --- a/test/async/websocket/adapters/rack.rb +++ b/test/async/websocket/adapters/rack.rb @@ -4,18 +4,18 @@ # Copyright, 2015-2024, by Samuel Williams. # Copyright, 2019, by Simon Crocker. -require 'async/websocket' -require 'async/websocket/client' -require 'async/websocket/adapters/rack' -require 'async/websocket/rack_application' +require "async/websocket" +require "async/websocket/client" +require "async/websocket/adapters/rack" +require "async/websocket/rack_application" describe Async::WebSocket::Adapters::Rack do it "can determine whether a rack env is a websocket request" do expect(Async::WebSocket::Adapters::Rack.websocket?(Rack::MockRequest.env_for("/"))).to be == false - expect(Async::WebSocket::Adapters::Rack.websocket?(Rack::MockRequest.env_for("/", 'HTTP_CONNECTION' => 'upgrade', 'HTTP_UPGRADE' => 'websocket'))).to be == true + expect(Async::WebSocket::Adapters::Rack.websocket?(Rack::MockRequest.env_for("/", "HTTP_CONNECTION" => "upgrade", "HTTP_UPGRADE" => "websocket"))).to be == true end - with 'rack application' do + with "rack application" do include Async::WebSocket::RackApplication it "can make non-websocket connection to server" do @@ -50,8 +50,8 @@ end it "should negotiate protocol" do - Async::WebSocket::Client.connect(client_endpoint, protocols: ['ws']) do |connection| - expect(connection.protocol).to be == 'ws' + Async::WebSocket::Client.connect(client_endpoint, protocols: ["ws"]) do |connection| + expect(connection.protocol).to be == "ws" end end end diff --git a/test/async/websocket/client.rb b/test/async/websocket/client.rb index 1405794..de04295 100644 --- a/test/async/websocket/client.rb +++ b/test/async/websocket/client.rb @@ -1,13 +1,13 @@ # frozen_string_literal: true # Released under the MIT License. -# Copyright, 2023, by Samuel Williams. +# Copyright, 2023-2024, by Samuel Williams. # Copyright, 2023, by Thomas Morgan. -require 'async/websocket/client' -require 'async/websocket/adapters/http' +require "async/websocket/client" +require "async/websocket/adapters/http" -require 'sus/fixtures/async/http/server_context' +require "sus/fixtures/async/http/server_context" ClientExamples = Sus::Shared("a websocket client") do let(:app) do @@ -26,7 +26,7 @@ end end - with '#send_close' do + with "#send_close" do it "can read incoming messages and then close" do connection = Async::WebSocket::Client.connect(client_endpoint) 3.times do @@ -49,7 +49,7 @@ end end - with '#close' do + 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) @@ -87,7 +87,7 @@ end end - it 'closes with custom error' do + it "closes with custom error" do connection = Async::WebSocket::Client.connect(client_endpoint) message = connection.read @@ -97,7 +97,7 @@ end end - with '#connect' do + with "#connect" do let(:app) do Protocol::HTTP::Middleware.for do |request| Async::WebSocket::Adapters::HTTP.open(request) do |connection| @@ -113,15 +113,15 @@ it "fully populates the request" do connection = Async::WebSocket::Client.connect(client_endpoint) expect(connection.read.to_str).to be =~ /authority: localhost:\d+/ - expect(connection.read.to_str).to be == 'path: /' + expect(connection.read.to_str).to be == "path: /" expect(connection.read.to_str).to be == 'protocol: ["websocket"]' - expect(connection.read.to_str).to be == 'scheme: http' + expect(connection.read.to_str).to be == "scheme: http" ensure connection&.close end end - with 'missing support for websockets' do + with "missing support for websockets" do let(:app) do Protocol::HTTP::Middleware.for do |request| Protocol::HTTP::Response[404, {}, []] @@ -135,7 +135,7 @@ end end - with 'deliberate failure response' do + with "deliberate failure response" do let(:app) do Protocol::HTTP::Middleware.for do |request| Protocol::HTTP::Response[401, {}, ["You are not allowed!"]] @@ -154,7 +154,7 @@ end FailedToNegotiate = Sus::Shared("a failed websocket request") do - it 'raises an error' do + it "raises an error" do expect do Async::WebSocket::Client.connect(client_endpoint) {} end.to raise_exception(Async::WebSocket::ConnectionError, message: be =~ /Failed to negotiate connection/) @@ -164,63 +164,63 @@ describe Async::WebSocket::Client do include Sus::Fixtures::Async::HTTP::ServerContext - with 'http/1' do + with "http/1" do let(:protocol) {Async::HTTP::Protocol::HTTP1} it_behaves_like ClientExamples def valid_headers(request) { - 'connection' => 'upgrade', - 'upgrade' => 'websocket', - 'sec-websocket-accept' => Protocol::WebSocket::Headers::Nounce.accept_digest(request.headers['sec-websocket-key'].first) + "connection" => "upgrade", + "upgrade" => "websocket", + "sec-websocket-accept" => Protocol::WebSocket::Headers::Nounce.accept_digest(request.headers["sec-websocket-key"].first) } end - with 'invalid connection header' do + with "invalid connection header" do let(:app) do Protocol::HTTP::Middleware.for do |request| - Protocol::HTTP::Response[101, valid_headers(request).except('connection'), []] + Protocol::HTTP::Response[101, valid_headers(request).except("connection"), []] end end it_behaves_like FailedToNegotiate end - with 'invalid upgrade header' do + with "invalid upgrade header" do let(:app) do Protocol::HTTP::Middleware.for do |request| - Protocol::HTTP::Response[101, valid_headers(request).except('upgrade'), []] + Protocol::HTTP::Response[101, valid_headers(request).except("upgrade"), []] end end it_behaves_like FailedToNegotiate end - with 'invalid sec-websocket-accept header' do + with "invalid sec-websocket-accept header" do let(:app) do Protocol::HTTP::Middleware.for do |request| - Protocol::HTTP::Response[101, valid_headers(request).merge('sec-websocket-accept'=>'wrong-digest'), []] + Protocol::HTTP::Response[101, valid_headers(request).merge("sec-websocket-accept"=>"wrong-digest"), []] end end - it 'raises an error' do + it "raises an error" do expect do Async::WebSocket::Client.connect(client_endpoint) {} end.to raise_exception(Async::WebSocket::ProtocolError, message: be =~ /Invalid accept digest/) end end - with 'missing sec-websocket-accept header' do + with "missing sec-websocket-accept header" do let(:app) do Protocol::HTTP::Middleware.for do |request| - Protocol::HTTP::Response[101, valid_headers(request).except('sec-websocket-accept'), []] + Protocol::HTTP::Response[101, valid_headers(request).except("sec-websocket-accept"), []] end end it_behaves_like FailedToNegotiate end - with 'invalid status' do + with "invalid status" do let(:app) do Protocol::HTTP::Middleware.for do |request| Protocol::HTTP::Response[403, valid_headers(request), []] @@ -231,11 +231,11 @@ def valid_headers(request) end end - with 'http/2' do + with "http/2" do let(:protocol) {Async::HTTP::Protocol::HTTP2} it_behaves_like ClientExamples - with 'invalid status' do + with "invalid status" do let(:app) do Protocol::HTTP::Middleware.for do |request| Protocol::HTTP::Response[403, {}, []] diff --git a/test/async/websocket/connection.rb b/test/async/websocket/connection.rb index 0cd3234..29b062d 100644 --- a/test/async/websocket/connection.rb +++ b/test/async/websocket/connection.rb @@ -4,7 +4,7 @@ # Copyright, 2015-2024, by Samuel Williams. # Copyright, 2019, by Simon Crocker. -require 'async/websocket/connection' +require "async/websocket/connection" describe Async::WebSocket::Connection do let(:framer) {Protocol::WebSocket::Framer.new(nil)} diff --git a/test/async/websocket/request.rb b/test/async/websocket/request.rb index eadefed..d29e489 100644 --- a/test/async/websocket/request.rb +++ b/test/async/websocket/request.rb @@ -1,9 +1,9 @@ # frozen_string_literal: true # Released under the MIT License. -# Copyright, 2023, by Samuel Williams. +# Copyright, 2023-2024, by Samuel Williams. -require 'async/websocket/request' +require "async/websocket/request" describe Async::WebSocket::Request do let(:request) {subject.new("https", "localhost", "/")} @@ -27,7 +27,7 @@ end.to raise_exception(Async::WebSocket::UnsupportedVersionError, message: be =~ /Unsupported HTTP version/) end - with '#to_s' do + with "#to_s" do it "should generate string representation" do expect(request.to_s).to be =~ %r{https://localhost/} end diff --git a/test/async/websocket/response.rb b/test/async/websocket/response.rb index bdd0615..42b8104 100644 --- a/test/async/websocket/response.rb +++ b/test/async/websocket/response.rb @@ -1,9 +1,9 @@ # frozen_string_literal: true # Released under the MIT License. -# Copyright, 2023, by Samuel Williams. +# Copyright, 2023-2024, by Samuel Williams. -require 'async/websocket/response' +require "async/websocket/response" describe Async::WebSocket::Response do it "fails if the version is not recognized" do diff --git a/test/async/websocket/server.rb b/test/async/websocket/server.rb index a94c45e..f5f865c 100644 --- a/test/async/websocket/server.rb +++ b/test/async/websocket/server.rb @@ -3,15 +3,15 @@ # Released under the MIT License. # Copyright, 2019-2024, by Samuel Williams. -require 'protocol/http/middleware/builder' +require "protocol/http/middleware/builder" -require 'async/websocket/client' -require 'async/websocket/server' -require 'async/websocket/adapters/http' +require "async/websocket/client" +require "async/websocket/server" +require "async/websocket/adapters/http" -require 'sus/fixtures/async/http/server_context' +require "sus/fixtures/async/http/server_context" -ServerExamples = Sus::Shared('a websocket server') do +ServerExamples = Sus::Shared("a websocket server") do it "can establish connection" do connection = websocket_client.connect(endpoint.authority, "/server") @@ -75,10 +75,10 @@ end end - with 'server middleware' do + with "server middleware" do let(:app) do Protocol::HTTP::Middleware.build do - use Async::WebSocket::Server, protocols: ['echo', 'baz'] do |connection| + use Async::WebSocket::Server, protocols: ["echo", "baz"] do |connection| connection.send_text("protocol: #{connection.protocol}") connection.close end @@ -86,10 +86,10 @@ end it "can establish connection with explicit protocol" do - connection = websocket_client.connect(endpoint.authority, "/server", protocols: ['echo', 'foo', 'bar']) + connection = websocket_client.connect(endpoint.authority, "/server", protocols: ["echo", "foo", "bar"]) # The negotiated protocol: - expect(connection.protocol).to be == 'echo' + expect(connection.protocol).to be == "echo" begin expect(connection.read).to be == "protocol: echo" @@ -117,7 +117,7 @@ end end - with 'http/1' do + with "http/1" do let(:protocol) {Async::HTTP::Protocol::HTTP1} it_behaves_like ServerExamples @@ -134,7 +134,7 @@ let(:timeout) {nil} - with 'broken server' do + with "broken server" do let(:app) do Protocol::HTTP::Middleware.for do |request| response = Async::WebSocket::Adapters::HTTP.open(request) do |connection| @@ -145,7 +145,7 @@ if response response.tap do - response.headers.set('sec-websocket-accept', '2badsheep') + response.headers.set("sec-websocket-accept", "2badsheep") end else Protocol::HTTP::Response[404, {}, []] @@ -161,7 +161,7 @@ end end - with 'http/2' do + with "http/2" do let(:protocol) {Async::HTTP::Protocol::HTTP2} it_behaves_like ServerExamples end