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/puma/config.ru b/examples/puma/config.ru index a325757..f917813 100644 --- a/examples/puma/config.ru +++ b/examples/puma/config.ru @@ -1,15 +1,17 @@ -require 'protocol/http/middleware' -require_relative '../../lib/protocol/rack' +# frozen_string_literal: true + +require "protocol/http/middleware" +require_relative "../../lib/protocol/rack" # Your native application: middleware = Protocol::HTTP::Middleware::HelloWorld run proc{|env| - # Convert the rack request to a compatible rich request object: - request = Protocol::Rack::Request[env] - - # Call your application - response = middleware.call(request) - - Protocol::Rack::Adapter.make_response(env, response) + # Convert the rack request to a compatible rich request object: + request = Protocol::Rack::Request[env] + + # Call your application + response = middleware.call(request) + + Protocol::Rack::Adapter.make_response(env, response) } diff --git a/examples/server/server.rb b/examples/server/server.rb index 605ace2..43d945f 100755 --- a/examples/server/server.rb +++ b/examples/server/server.rb @@ -2,25 +2,25 @@ # frozen_string_literal: true # Released under the MIT License. -# Copyright, 2022, by Samuel Williams. +# Copyright, 2022-2024, by Samuel Williams. -require 'async' -require 'async/http/server' -require 'async/http/client' -require 'async/http/endpoint' -require_relative '../../lib/protocol/rack/adapter' +require "async" +require "async/http/server" +require "async/http/client" +require "async/http/endpoint" +require_relative "../../lib/protocol/rack/adapter" app = proc{|env| [200, {}, ["Hello World"]]} middleware = Protocol::Rack::Adapter.new(app) Async do - endpoint = Async::HTTP::Endpoint.parse("http://localhost:9292") - - server_task = Async(transient: true) do - server = Async::HTTP::Server.new(middleware, endpoint) - server.run - end - - client = Async::HTTP::Client.new(endpoint) - pp client.get("/").read + endpoint = Async::HTTP::Endpoint.parse("http://localhost:9292") + + server_task = Async(transient: true) do + server = Async::HTTP::Server.new(middleware, endpoint) + server.run + end + + client = Async::HTTP::Client.new(endpoint) + pp client.get("/").read end diff --git a/fixtures/server_context.rb b/fixtures/server_context.rb index 4fd556a..f00deda 100644 --- a/fixtures/server_context.rb +++ b/fixtures/server_context.rb @@ -1,15 +1,15 @@ # frozen_string_literal: true # Released under the MIT License. -# Copyright, 2022, by Samuel Williams. +# Copyright, 2022-2024, by Samuel Williams. -require 'sus/fixtures/async/http/server_context' +require "sus/fixtures/async/http/server_context" module ServerContext include Sus::Fixtures::Async::HTTP::ServerContext def app - ->(env){[200, {}, ['Hello World!']]} + ->(env){[200, {}, ["Hello World!"]]} end def middleware diff --git a/gems.rb b/gems.rb index 9b39576..339ab5a 100644 --- a/gems.rb +++ b/gems.rb @@ -8,10 +8,10 @@ gemspec group :maintenance, optional: true do - gem 'bake-modernize' - gem 'bake-gem' + gem "bake-modernize" + gem "bake-gem" - gem 'utopia-project' + gem "utopia-project" end group :test do diff --git a/lib/protocol/rack.rb b/lib/protocol/rack.rb index 0cd3ce9..d236709 100644 --- a/lib/protocol/rack.rb +++ b/lib/protocol/rack.rb @@ -1,9 +1,9 @@ # frozen_string_literal: true # Released under the MIT License. -# Copyright, 2022, by Samuel Williams. +# Copyright, 2022-2024, by Samuel Williams. -require_relative 'rack/version' -require_relative 'rack/adapter' -require_relative 'rack/request' -require_relative 'rack/response' +require_relative "rack/version" +require_relative "rack/adapter" +require_relative "rack/request" +require_relative "rack/response" diff --git a/lib/protocol/rack/adapter.rb b/lib/protocol/rack/adapter.rb index 05ec2f8..59ff856 100644 --- a/lib/protocol/rack/adapter.rb +++ b/lib/protocol/rack/adapter.rb @@ -3,10 +3,10 @@ # Released under the MIT License. # Copyright, 2022-2024, by Samuel Williams. -require 'rack' +require "rack" -require_relative 'adapter/rack2' -require_relative 'adapter/rack3' +require_relative "adapter/rack2" +require_relative "adapter/rack3" module Protocol module Rack diff --git a/lib/protocol/rack/adapter/generic.rb b/lib/protocol/rack/adapter/generic.rb index ac2aa48..14c0612 100644 --- a/lib/protocol/rack/adapter/generic.rb +++ b/lib/protocol/rack/adapter/generic.rb @@ -3,11 +3,11 @@ # Released under the MIT License. # Copyright, 2022-2024, by Samuel Williams. -require 'console' +require "console" -require_relative '../constants' -require_relative '../input' -require_relative '../response' +require_relative "../constants" +require_relative "../input" +require_relative "../response" module Protocol module Rack @@ -66,7 +66,7 @@ def unwrap_headers(headers, env) # @parameter request [Protocol::HTTP::Request] The incoming request. # @parameter env [Hash] The rack `env`. def unwrap_request(request, env) - if content_type = request.headers.delete('content-type') + if content_type = request.headers.delete("content-type") env[CGI::CONTENT_TYPE] = content_type end @@ -78,7 +78,7 @@ def unwrap_request(request, env) self.unwrap_headers(request.headers, env) # For the sake of compatibility, we set the `HTTP_UPGRADE` header to the requested protocol. - if protocol = request.protocol and request.version.start_with?('HTTP/1') + if protocol = request.protocol and request.version.start_with?("HTTP/1") env[CGI::HTTP_UPGRADE] = Array(protocol).join(",") end @@ -146,13 +146,13 @@ def failure_response(exception) def self.extract_protocol(env, response, headers) if protocol = response.protocol # This is the newer mechanism for protocol upgrade: - if env['rack.protocol'] - headers['rack.protocol'] = protocol + if env["rack.protocol"] + headers["rack.protocol"] = protocol # Older mechanism for protocol upgrade: elsif env[CGI::HTTP_UPGRADE] - headers['upgrade'] = protocol - headers['connection'] = 'upgrade' + headers["upgrade"] = protocol + headers["connection"] = "upgrade" end end end diff --git a/lib/protocol/rack/adapter/rack2.rb b/lib/protocol/rack/adapter/rack2.rb index 2b43639..81deef8 100644 --- a/lib/protocol/rack/adapter/rack2.rb +++ b/lib/protocol/rack/adapter/rack2.rb @@ -3,27 +3,27 @@ # Released under the MIT License. # Copyright, 2022-2024, by Samuel Williams. -require 'console' +require "console" -require_relative 'generic' -require_relative '../rewindable' +require_relative "generic" +require_relative "../rewindable" module Protocol module Rack module Adapter class Rack2 < Generic - RACK_VERSION = 'rack.version' - RACK_MULTITHREAD = 'rack.multithread' - RACK_MULTIPROCESS = 'rack.multiprocess' - RACK_RUN_ONCE = 'rack.run_once' + RACK_VERSION = "rack.version" + RACK_MULTITHREAD = "rack.multithread" + RACK_MULTIPROCESS = "rack.multiprocess" + RACK_RUN_ONCE = "rack.run_once" def self.wrap(app) Rewindable.new(self.new(app)) end def make_environment(request) - request_path, query_string = request.path.split('?', 2) - server_name, server_port = (request.authority || '').split(':', 2) + request_path, query_string = request.path.split("?", 2) + server_name, server_port = (request.authority || "").split(":", 2) env = { RACK_VERSION => [2, 0], @@ -44,7 +44,7 @@ def make_environment(request) CGI::REQUEST_METHOD => request.method, # The initial portion of the request URL's “path” that corresponds to the application object, so that the application knows its virtual “location”. This may be an empty string, if the application corresponds to the “root” of the server. - CGI::SCRIPT_NAME => '', + CGI::SCRIPT_NAME => "", # The remainder of the request URL's “path”, designating the virtual “location” of the request's target within the application. This may be an empty string, if the request URL targets the application root and does not have a trailing slash. This value may be percent-encoded when originating from a URL. CGI::PATH_INFO => request_path, @@ -52,7 +52,7 @@ def make_environment(request) CGI::REQUEST_URI => request.path, # The portion of the request URL that follows the ?, if any. May be empty, but is always required! - CGI::QUERY_STRING => query_string || '', + CGI::QUERY_STRING => query_string || "", # The server protocol (e.g. HTTP/1.1): CGI::SERVER_PROTOCOL => request.version, @@ -103,7 +103,7 @@ def wrap_headers(fields) fields.each do |key, value| key = key.downcase - if key.start_with?('rack.') + if key.start_with?("rack.") meta[key] = value elsif value.is_a?(String) value.split("\n").each do |value| diff --git a/lib/protocol/rack/adapter/rack3.rb b/lib/protocol/rack/adapter/rack3.rb index 3f4c322..7058022 100644 --- a/lib/protocol/rack/adapter/rack3.rb +++ b/lib/protocol/rack/adapter/rack3.rb @@ -3,9 +3,9 @@ # Released under the MIT License. # Copyright, 2022-2024, by Samuel Williams. -require 'console' +require "console" -require_relative 'generic' +require_relative "generic" module Protocol module Rack @@ -20,8 +20,8 @@ def self.parse_file(...) end def make_environment(request) - request_path, query_string = request.path.split('?', 2) - server_name, server_port = (request.authority || '').split(':', 2) + request_path, query_string = request.path.split("?", 2) + server_name, server_port = (request.authority || "").split(":", 2) env = { PROTOCOL_HTTP_REQUEST => request, @@ -40,7 +40,7 @@ def make_environment(request) CGI::REQUEST_METHOD => request.method, # The initial portion of the request URL's “path” that corresponds to the application object, so that the application knows its virtual “location”. This may be an empty string, if the application corresponds to the “root” of the server. - CGI::SCRIPT_NAME => '', + CGI::SCRIPT_NAME => "", # The remainder of the request URL's “path”, designating the virtual “location” of the request's target within the application. This may be an empty string, if the request URL targets the application root and does not have a trailing slash. This value may be percent-encoded when originating from a URL. CGI::PATH_INFO => request_path, @@ -48,7 +48,7 @@ def make_environment(request) CGI::REQUEST_URI => request.path, # The portion of the request URL that follows the ?, if any. May be empty, but is always required! - CGI::QUERY_STRING => query_string || '', + CGI::QUERY_STRING => query_string || "", # The server protocol (e.g. HTTP/1.1): CGI::SERVER_PROTOCOL => request.version, @@ -75,7 +75,7 @@ def wrap_headers(fields) fields.each do |key, value| key = key.downcase - if key.start_with?('rack.') + if key.start_with?("rack.") meta[key] = value elsif value.is_a?(Array) value.each do |value| diff --git a/lib/protocol/rack/body.rb b/lib/protocol/rack/body.rb index 0b03081..3c72721 100644 --- a/lib/protocol/rack/body.rb +++ b/lib/protocol/rack/body.rb @@ -1,16 +1,16 @@ # frozen_string_literal: true # Released under the MIT License. -# Copyright, 2022, by Samuel Williams. +# Copyright, 2022-2024, by Samuel Williams. -require_relative 'body/streaming' -require_relative 'body/enumerable' -require 'protocol/http/body/completable' +require_relative "body/streaming" +require_relative "body/enumerable" +require "protocol/http/body/completable" module Protocol module Rack module Body - CONTENT_LENGTH = 'content-length' + CONTENT_LENGTH = "content-length" def self.wrap(env, status, headers, body, input = nil) # In no circumstance do we want this header propagating out: diff --git a/lib/protocol/rack/body/enumerable.rb b/lib/protocol/rack/body/enumerable.rb index a7b2fd0..b668a03 100644 --- a/lib/protocol/rack/body/enumerable.rb +++ b/lib/protocol/rack/body/enumerable.rb @@ -1,10 +1,10 @@ # frozen_string_literal: true # Released under the MIT License. -# Copyright, 2022, by Samuel Williams. +# Copyright, 2022-2024, by Samuel Williams. -require 'protocol/http/body/readable' -require 'protocol/http/body/file' +require "protocol/http/body/readable" +require "protocol/http/body/file" module Protocol module Rack @@ -13,7 +13,7 @@ module Body # # The `rack` body must respond to `each` and must only yield `String` values. If the body responds to `close`, it will be called after iteration. class Enumerable < ::Protocol::HTTP::Body::Readable - CONTENT_LENGTH = 'content-length'.freeze + CONTENT_LENGTH = "content-length".freeze # Wraps an array into a buffered body. # @parameter body [Object] The `rack` response body. diff --git a/lib/protocol/rack/body/input_wrapper.rb b/lib/protocol/rack/body/input_wrapper.rb index c6aafa6..6492b3e 100644 --- a/lib/protocol/rack/body/input_wrapper.rb +++ b/lib/protocol/rack/body/input_wrapper.rb @@ -1,10 +1,10 @@ # frozen_string_literal: true # Released under the MIT License. -# Copyright, 2022, by Samuel Williams. +# Copyright, 2022-2024, by Samuel Williams. -require 'protocol/http/body/readable' -require 'protocol/http/body/stream' +require "protocol/http/body/readable" +require "protocol/http/body/stream" module Protocol module Rack diff --git a/lib/protocol/rack/body/streaming.rb b/lib/protocol/rack/body/streaming.rb index 1d1a303..5e695ae 100644 --- a/lib/protocol/rack/body/streaming.rb +++ b/lib/protocol/rack/body/streaming.rb @@ -3,7 +3,7 @@ # Released under the MIT License. # Copyright, 2022-2024, by Samuel Williams. -require 'protocol/http/body/streamable' +require "protocol/http/body/streamable" module Protocol module Rack diff --git a/lib/protocol/rack/constants.rb b/lib/protocol/rack/constants.rb index 572e030..e989278 100644 --- a/lib/protocol/rack/constants.rb +++ b/lib/protocol/rack/constants.rb @@ -10,37 +10,37 @@ module Rack # CGI keys : module CGI - HTTP_HOST = 'HTTP_HOST' + HTTP_HOST = "HTTP_HOST" HTTP_UPGRADE = "HTTP_UPGRADE" - PATH_INFO = 'PATH_INFO' - REQUEST_METHOD = 'REQUEST_METHOD' - REQUEST_PATH = 'REQUEST_PATH' - REQUEST_URI = 'REQUEST_URI' - SCRIPT_NAME = 'SCRIPT_NAME' - QUERY_STRING = 'QUERY_STRING' - SERVER_PROTOCOL = 'SERVER_PROTOCOL' - SERVER_NAME = 'SERVER_NAME' - SERVER_PORT = 'SERVER_PORT' - REMOTE_ADDR = 'REMOTE_ADDR' - CONTENT_TYPE = 'CONTENT_TYPE' - CONTENT_LENGTH = 'CONTENT_LENGTH' + PATH_INFO = "PATH_INFO" + REQUEST_METHOD = "REQUEST_METHOD" + REQUEST_PATH = "REQUEST_PATH" + REQUEST_URI = "REQUEST_URI" + SCRIPT_NAME = "SCRIPT_NAME" + QUERY_STRING = "QUERY_STRING" + SERVER_PROTOCOL = "SERVER_PROTOCOL" + SERVER_NAME = "SERVER_NAME" + SERVER_PORT = "SERVER_PORT" + REMOTE_ADDR = "REMOTE_ADDR" + CONTENT_TYPE = "CONTENT_TYPE" + CONTENT_LENGTH = "CONTENT_LENGTH" - HTTP_COOKIE = 'HTTP_COOKIE' + HTTP_COOKIE = "HTTP_COOKIE" # Header constants: - HTTP_X_FORWARDED_PROTO = 'HTTP_X_FORWARDED_PROTO' + HTTP_X_FORWARDED_PROTO = "HTTP_X_FORWARDED_PROTO" end # Rack environment variables: - RACK_ERRORS = 'rack.errors' - RACK_LOGGER = 'rack.logger' - RACK_INPUT = 'rack.input' - RACK_URL_SCHEME = 'rack.url_scheme' - RACK_PROTOCOL = 'rack.protocol' - RACK_RESPONSE_FINISHED = 'rack.response_finished' + RACK_ERRORS = "rack.errors" + RACK_LOGGER = "rack.logger" + RACK_INPUT = "rack.input" + RACK_URL_SCHEME = "rack.url_scheme" + RACK_PROTOCOL = "rack.protocol" + RACK_RESPONSE_FINISHED = "rack.response_finished" # Rack hijack support: - RACK_IS_HIJACK = 'rack.hijack?' - RACK_HIJACK = 'rack.hijack' + RACK_IS_HIJACK = "rack.hijack?" + RACK_HIJACK = "rack.hijack" end end diff --git a/lib/protocol/rack/input.rb b/lib/protocol/rack/input.rb index a7bc997..46a527f 100644 --- a/lib/protocol/rack/input.rb +++ b/lib/protocol/rack/input.rb @@ -4,7 +4,7 @@ # Copyright, 2022-2024, by Samuel Williams. # Copyright, 2023, by Genki Takiuchi. -require 'protocol/http/body/stream' +require "protocol/http/body/stream" module Protocol module Rack diff --git a/lib/protocol/rack/request.rb b/lib/protocol/rack/request.rb index 4426b53..75773a6 100644 --- a/lib/protocol/rack/request.rb +++ b/lib/protocol/rack/request.rb @@ -1,38 +1,38 @@ # frozen_string_literal: true # Released under the MIT License. -# Copyright, 2022-2023, by Samuel Williams. +# Copyright, 2022-2024, by Samuel Williams. -require 'protocol/http/request' -require 'protocol/http/headers' +require "protocol/http/request" +require "protocol/http/headers" -require_relative 'constants' -require_relative 'body/input_wrapper' +require_relative "constants" +require_relative "body/input_wrapper" module Protocol module Rack class Request < ::Protocol::HTTP::Request def self.[](env) - env['protocol.http.request'] ||= new(env) + env["protocol.http.request"] ||= new(env) end def initialize(env) @env = env super( - @env['rack.url_scheme'], - @env['HTTP_HOST'], - @env['REQUEST_METHOD'], - @env['PATH_INFO'], - @env['SERVER_PROTOCOL'], + @env["rack.url_scheme"], + @env["HTTP_HOST"], + @env["REQUEST_METHOD"], + @env["PATH_INFO"], + @env["SERVER_PROTOCOL"], self.class.headers(@env), - Body::InputWrapper.new(@env['rack.input']), + Body::InputWrapper.new(@env["rack.input"]), self.class.protocol(@env) ) end def self.protocol(env) - if protocols = env['rack.protocol'] + if protocols = env["rack.protocol"] return Array(protocols) elsif protocols = env[CGI::HTTP_UPGRADE] return protocols.split(/\s*,\s*/) @@ -42,9 +42,9 @@ def self.protocol(env) def self.headers(env) headers = ::Protocol::HTTP::Headers.new env.each do |key, value| - if key.start_with?('HTTP_') - next if key == 'HTTP_HOST' - headers[key[5..-1].gsub('_', '-').downcase] = value + if key.start_with?("HTTP_") + next if key == "HTTP_HOST" + headers[key[5..-1].gsub("_", "-").downcase] = value end end diff --git a/lib/protocol/rack/response.rb b/lib/protocol/rack/response.rb index 3cbf44b..a2dfbe1 100644 --- a/lib/protocol/rack/response.rb +++ b/lib/protocol/rack/response.rb @@ -3,12 +3,12 @@ # Released under the MIT License. # Copyright, 2022-2024, by Samuel Williams. -require_relative 'body' -require_relative 'constants' +require_relative "body" +require_relative "constants" # require 'time' -require 'protocol/http/response' -require 'protocol/http/headers' +require "protocol/http/response" +require "protocol/http/headers" module Protocol module Rack @@ -26,12 +26,12 @@ module Rack class Response < ::Protocol::HTTP::Response # HTTP hop headers which *should* not be passed through the proxy. HOP_HEADERS = [ - 'connection', - 'keep-alive', - 'public', - 'proxy-authenticate', - 'transfer-encoding', - 'upgrade', + "connection", + "keep-alive", + "public", + "proxy-authenticate", + "transfer-encoding", + "upgrade", ] # Wrap a rack response. @@ -46,7 +46,7 @@ def self.wrap(env, status, headers, meta, body, request = nil) Console.warn(self, "Ignoring hop headers!", ignored: ignored) end - if hijack_body = meta['rack.hijack'] + if hijack_body = meta["rack.hijack"] body = hijack_body end diff --git a/lib/protocol/rack/rewindable.rb b/lib/protocol/rack/rewindable.rb index 32d8f7e..957a915 100644 --- a/lib/protocol/rack/rewindable.rb +++ b/lib/protocol/rack/rewindable.rb @@ -1,10 +1,10 @@ # frozen_string_literal: true # Released under the MIT License. -# Copyright, 2022, by Samuel Williams. +# Copyright, 2022-2024, by Samuel Williams. -require 'protocol/http/body/rewindable' -require 'protocol/http/middleware' +require "protocol/http/body/rewindable" +require "protocol/http/middleware" module Protocol module Rack @@ -18,7 +18,7 @@ class Rewindable < ::Protocol::HTTP::Middleware multipart/mixed }x - POST = 'POST' + POST = "POST" # Initialize the rewindable middleware. # @parameter app [Protocol::HTTP::Middleware] The middleware to wrap. @@ -30,7 +30,7 @@ def initialize(app) # @parameter request [Protocol::HTTP::Request] # @returns [Boolean] def needs_rewind?(request) - content_type = request.headers['content-type'] + content_type = request.headers["content-type"] if request.method == POST and content_type.nil? return true diff --git a/test/protocol/rack/adapter.rb b/test/protocol/rack/adapter.rb index ae42757..23518ed 100644 --- a/test/protocol/rack/adapter.rb +++ b/test/protocol/rack/adapter.rb @@ -3,11 +3,11 @@ # Released under the MIT License. # Copyright, 2022-2024, by Samuel Williams. -require 'rack/lint' +require "rack/lint" -require 'protocol/rack/adapter' -require 'disable_console_context' -require 'server_context' +require "protocol/rack/adapter" +require "disable_console_context" +require "server_context" describe Protocol::Rack::Adapter do let(:rackup_path) {File.expand_path(".adapter/config.ru", __dir__)} @@ -20,38 +20,38 @@ describe Protocol::Rack::Adapter::Generic do let(:adapter) {subject.new(lambda{})} - with '#unwrap_headers' do - with 'cookie header' do - let(:fields) {[['cookie', 'a=b'], ['cookie', 'x=y']]} + with "#unwrap_headers" do + with "cookie header" do + let(:fields) {[["cookie", "a=b"], ["cookie", "x=y"]]} let(:env) {Hash.new} it "should merge duplicate headers" do adapter.unwrap_headers(fields, env) # I'm not convinced this is standard behaviour: - expect(env).to be == {'HTTP_COOKIE' => "a=b;x=y"} + expect(env).to be == {"HTTP_COOKIE" => "a=b;x=y"} end end - with 'multiple accept headers' do - let(:fields) {[['accept', 'text/html'], ['accept', 'application/json']]} + with "multiple accept headers" do + let(:fields) {[["accept", "text/html"], ["accept", "application/json"]]} let(:env) {Hash.new} it "should merge duplicate headers" do adapter.unwrap_headers(fields, env) - expect(env).to be == {'HTTP_ACCEPT' => "text/html,application/json"} + expect(env).to be == {"HTTP_ACCEPT" => "text/html,application/json"} end end end end -Adapter = Sus::Shared('an adapter') do +Adapter = Sus::Shared("an adapter") do include ServerContext let(:protocol) {subject} - with 'successful response' do + with "successful response" do let(:app) do Rack::Lint.new( lambda do |env| @@ -67,7 +67,7 @@ end end - with 'HTTP_HOST' do + with "HTTP_HOST" do let(:app) do lambda do |env| [200, {}, ["HTTP_HOST: #{env['HTTP_HOST']}"]] @@ -81,12 +81,12 @@ end end - with 'connection: close', timeout: 1 do + with "connection: close", timeout: 1 do include DisableConsoleContext let(:app) do lambda do |env| - [200, {'connection' => 'close'}, ["Hello World!"]] + [200, {"connection" => "close"}, ["Hello World!"]] end end @@ -97,10 +97,10 @@ end end - with 'non-string header value' do + with "non-string header value" do let(:app) do lambda do |env| - [200, {'x-custom' => 123}, ["Hello World!"]] + [200, {"x-custom" => 123}, ["Hello World!"]] end end @@ -108,11 +108,11 @@ it "get valid response" do expect(response.read).to be == "Hello World!" - expect(response.headers['x-custom']).to be == ['123'] + expect(response.headers["x-custom"]).to be == ["123"] end end - with 'REQUEST_URI', timeout: 1 do + with "REQUEST_URI", timeout: 1 do let(:app) do lambda do |env| [200, {}, ["REQUEST_URI: #{env['REQUEST_URI']}"]] @@ -126,7 +126,7 @@ end end - with 'streaming response' do + with "streaming response" do let(:app) do lambda do |env| body = lambda do |stream| diff --git a/test/protocol/rack/adapter/generic.rb b/test/protocol/rack/adapter/generic.rb index 98cc354..f6f83bb 100644 --- a/test/protocol/rack/adapter/generic.rb +++ b/test/protocol/rack/adapter/generic.rb @@ -1,12 +1,12 @@ # frozen_string_literal: true # Released under the MIT License. -# Copyright, 2022-2023, by Samuel Williams. +# Copyright, 2022-2024, by Samuel Williams. -require 'protocol/rack/adapter/generic' -require 'protocol/http/request' +require "protocol/rack/adapter/generic" +require "protocol/http/request" -require 'disable_console_context' +require "disable_console_context" describe Protocol::Rack::Adapter::Generic do let(:app) {->(env){[200, {}, []]}} @@ -16,7 +16,7 @@ expect(adapter).not.to be_nil end - with 'an exception' do + with "an exception" do let(:exception) {StandardError.new("Something went wrong")} let(:response) {adapter.failure_response(exception)} @@ -25,22 +25,22 @@ end end - with 'a request with a content-type header' do + with "a request with a content-type header" do let(:env) {Hash.new} - let(:request) {Protocol::HTTP::Request["GET", "/", {'content-type' => 'text/plain'}, nil]} + let(:request) {Protocol::HTTP::Request["GET", "/", {"content-type" => "text/plain"}, nil]} it "can unwrap requests with the CONTENT_TYPE key" do adapter.unwrap_request(request, env) - expect(env).to have_keys('CONTENT_TYPE' => be == 'text/plain') + expect(env).to have_keys("CONTENT_TYPE" => be == "text/plain") end end - with 'a app that returns nil' do + with "a app that returns nil" do include DisableConsoleContext let(:app) {->(env){nil}} - let(:request) {Protocol::HTTP::Request["GET", "/", {'content-type' => 'text/plain'}, nil]} + let(:request) {Protocol::HTTP::Request["GET", "/", {"content-type" => "text/plain"}, nil]} it "can generate a failure response" do response = adapter.call(request) diff --git a/test/protocol/rack/adapter/rack2.rb b/test/protocol/rack/adapter/rack2.rb index 9d2b448..0b9523e 100644 --- a/test/protocol/rack/adapter/rack2.rb +++ b/test/protocol/rack/adapter/rack2.rb @@ -1,46 +1,46 @@ # frozen_string_literal: true # Released under the MIT License. -# Copyright, 2022, by Samuel Williams. +# Copyright, 2022-2024, by Samuel Williams. -require 'disable_console_context' -require 'protocol/rack/adapter/rack2' +require "disable_console_context" +require "protocol/rack/adapter/rack2" describe Protocol::Rack::Adapter::Rack2 do let(:app) {->(env) {[200, {}, []]}} let(:adapter) {subject.new(app)} let(:body) {Protocol::HTTP::Body::Buffered.new} - let(:request) {Protocol::HTTP::Request.new('https', 'example.com', 'GET', '/', 'http/1.1', Protocol::HTTP::Headers[{'accept' => 'text/html'}], body)} + let(:request) {Protocol::HTTP::Request.new("https", "example.com", "GET", "/", "http/1.1", Protocol::HTTP::Headers[{"accept" => "text/html"}], body)} let(:response) {adapter.call(request)} - with 'set-cookie headers that has multiple values' do - let(:app) {->(env) {[200, {'set-cookie' => "a=b\nx=y"}, []]}} + with "set-cookie headers that has multiple values" do + let(:app) {->(env) {[200, {"set-cookie" => "a=b\nx=y"}, []]}} it "can make a response newline separated headers" do - expect(response.headers['set-cookie']).to be == ["a=b", "x=y"] + expect(response.headers["set-cookie"]).to be == ["a=b", "x=y"] end end - with 'content-length header' do - let(:app) {->(env) {[200, {'content-length' => '10'}, ["1234567890"]]}} + with "content-length header" do + let(:app) {->(env) {[200, {"content-length" => "10"}, ["1234567890"]]}} it "removes content-length header" do - expect(response.headers).not.to be(:include?, 'content-length') + expect(response.headers).not.to be(:include?, "content-length") end end - with 'connection: close header' do + with "connection: close header" do include DisableConsoleContext - let(:app) {->(env) {[200, {'connection' => 'close'}, []]}} + let(:app) {->(env) {[200, {"connection" => "close"}, []]}} it "removes content-length header" do - expect(response.headers).not.to be(:include?, 'connection') + expect(response.headers).not.to be(:include?, "connection") end end - with 'body that responds to #to_path' do + with "body that responds to #to_path" do let(:body) {Array.new} let(:app) {->(env) {[200, {}, body]}} @@ -50,7 +50,7 @@ expect(response.body).to be(:kind_of?, Protocol::HTTP::Body::File) end - with '206 partial response status' do + with "206 partial response status" do let(:app) {->(env) {[200, {}, body]}} it "should not modify partial responses" do @@ -59,9 +59,9 @@ end end - with 'a hijacked response' do + with "a hijacked response" do let(:callback) {->(stream){}} - let(:app) {->(env){[200, {'rack.hijack' => callback}, []]}} + let(:app) {->(env){[200, {"rack.hijack" => callback}, []]}} it "should support hijacking" do expect(response.body).to be(:kind_of?, Protocol::Rack::Body::Streaming) diff --git a/test/protocol/rack/adapter/rack3.rb b/test/protocol/rack/adapter/rack3.rb index 92df924..b9171a3 100644 --- a/test/protocol/rack/adapter/rack3.rb +++ b/test/protocol/rack/adapter/rack3.rb @@ -1,46 +1,46 @@ # frozen_string_literal: true # Released under the MIT License. -# Copyright, 2022, by Samuel Williams. +# Copyright, 2022-2024, by Samuel Williams. -require 'disable_console_context' -require 'protocol/http/request' -require 'protocol/rack/adapter/rack3' +require "disable_console_context" +require "protocol/http/request" +require "protocol/rack/adapter/rack3" describe Protocol::Rack::Adapter::Rack3 do let(:app) {->(env) {[200, {}, []]}} let(:adapter) {subject.new(app)} - let(:request) {Protocol::HTTP::Request.new('https', 'example.com', 'GET', '/', 'http/1.1', Protocol::HTTP::Headers[{'accept' => 'text/html'}], Protocol::HTTP::Body::Buffered.new)} + let(:request) {Protocol::HTTP::Request.new("https", "example.com", "GET", "/", "http/1.1", Protocol::HTTP::Headers[{"accept" => "text/html"}], Protocol::HTTP::Body::Buffered.new)} let(:response) {adapter.call(request)} - with 'set-cookie headers that has multiple values' do - let(:app) {->(env) {[200, {'set-cookie' => ['a=b', 'x=y']}, []]}} + with "set-cookie headers that has multiple values" do + let(:app) {->(env) {[200, {"set-cookie" => ["a=b", "x=y"]}, []]}} it "can make a response newline separated headers" do - expect(response.headers['set-cookie']).to be == ["a=b", "x=y"] + expect(response.headers["set-cookie"]).to be == ["a=b", "x=y"] end end - with 'content-length header' do - let(:app) {->(env) {[200, {'content-length' => '10'}, ["1234567890"]]}} + with "content-length header" do + let(:app) {->(env) {[200, {"content-length" => "10"}, ["1234567890"]]}} it "removes content-length header" do - expect(response.headers).not.to be(:include?, 'content-length') + expect(response.headers).not.to be(:include?, "content-length") end end - with 'connection: close header' do + with "connection: close header" do include DisableConsoleContext - let(:app) {->(env) {[200, {'connection' => 'close'}, []]}} + let(:app) {->(env) {[200, {"connection" => "close"}, []]}} it "removes content-length header" do - expect(response.headers).not.to be(:include?, 'connection') + expect(response.headers).not.to be(:include?, "connection") end end - with 'body that responds to #to_path' do + with "body that responds to #to_path" do let(:body) {Array.new} let(:app) {->(env) {[200, {}, body]}} @@ -50,7 +50,7 @@ expect(response.body).to be(:kind_of?, Protocol::HTTP::Body::File) end - with '206 partial response status' do + with "206 partial response status" do let(:app) {->(env) {[200, {}, body]}} it "should not modify partial responses" do @@ -59,9 +59,9 @@ end end - with 'a request that has response finished callbacks' do + with "a request that has response finished callbacks" do let(:callback) {->(env, status, headers, error){}} - let(:app) {->(env) {env['rack.response_finished'] << callback; [200, {}, ["Hello World!"]]}} + let(:app) {->(env) {env["rack.response_finished"] << callback; [200, {}, ["Hello World!"]]}} it "should call the callbacks" do expect(callback).to receive(:call) diff --git a/test/protocol/rack/body/enumerable.rb b/test/protocol/rack/body/enumerable.rb index 3db0ab6..4c4b0fc 100644 --- a/test/protocol/rack/body/enumerable.rb +++ b/test/protocol/rack/body/enumerable.rb @@ -1,12 +1,12 @@ # frozen_string_literal: true # Released under the MIT License. -# Copyright, 2022, by Samuel Williams. +# Copyright, 2022-2024, by Samuel Williams. -require 'protocol/rack/body/enumerable' +require "protocol/rack/body/enumerable" describe Protocol::Rack::Body::Enumerable do - with 'empty body' do + with "empty body" do let(:body) {subject.new([], nil)} it "should be empty?" do @@ -14,7 +14,7 @@ end end - with 'single string body' do + with "single string body" do let(:body) {subject.new(["Hello World"], nil)} it "should not be empty?" do diff --git a/test/protocol/rack/body/input_wrapper.rb b/test/protocol/rack/body/input_wrapper.rb index 400f25e..fc8d033 100644 --- a/test/protocol/rack/body/input_wrapper.rb +++ b/test/protocol/rack/body/input_wrapper.rb @@ -1,12 +1,12 @@ # frozen_string_literal: true # Released under the MIT License. -# Copyright, 2022, by Samuel Williams. +# Copyright, 2022-2024, by Samuel Williams. -require 'protocol/rack/body/input_wrapper' +require "protocol/rack/body/input_wrapper" describe Protocol::Rack::Body::InputWrapper do - with 'file' do + with "file" do let(:contents) {File.read(__FILE__)} let(:body) {subject.new(File.open(__FILE__, "r"), block_size: 128)} diff --git a/test/protocol/rack/body/streaming.rb b/test/protocol/rack/body/streaming.rb index b486d5a..814e427 100644 --- a/test/protocol/rack/body/streaming.rb +++ b/test/protocol/rack/body/streaming.rb @@ -3,9 +3,9 @@ # Released under the MIT License. # Copyright, 2022-2024, by Samuel Williams. -require 'protocol/rack/body/streaming' +require "protocol/rack/body/streaming" -require 'sus/fixtures/async/scheduler_context' +require "sus/fixtures/async/scheduler_context" describe Protocol::Rack::Body::Streaming do include Sus::Fixtures::Async::SchedulerContext @@ -13,7 +13,7 @@ let(:block) {proc{|stream| stream.write("Hello"); stream.write("World"); stream.close}} let(:body) {subject.new(block)} - with '#read' do + with "#read" do it "can read the body" do expect(body.read).to be == "Hello" expect(body.read).to be == "World" @@ -21,7 +21,7 @@ end end - with '#each' do + with "#each" do it "can read the body" do chunks = [] body.each{|chunk| chunks << chunk} @@ -29,7 +29,7 @@ end end - with '#call' do + with "#call" do it "can read the body" do stream = StringIO.new body.call(stream) diff --git a/test/protocol/rack/input.rb b/test/protocol/rack/input.rb index 2ac76c4..f1cc3a5 100644 --- a/test/protocol/rack/input.rb +++ b/test/protocol/rack/input.rb @@ -4,13 +4,13 @@ # Copyright, 2022-2024, by Samuel Williams. # Copyright, 2023, by Genki Takiuchi. -require 'protocol/rack/input' -require 'protocol/http/body/buffered' +require "protocol/rack/input" +require "protocol/http/body/buffered" describe Protocol::Rack::Input do let(:input) {subject.new(body)} - with 'body' do + with "body" do let(:sample_data) {%w{The quick brown fox jumped over the lazy dog}} let(:body) {Protocol::HTTP::Body::Buffered.new(sample_data)} @@ -21,7 +21,7 @@ expect(input).to be(:empty?) end - with '#read(length, buffer)' do + with "#read(length, buffer)" do let(:buffer) {String.new} let(:expected_output) {sample_data.join} @@ -42,7 +42,7 @@ end end - with '#read' do + with "#read" do it "can read all input" do expect(body).to receive(:close) @@ -97,7 +97,7 @@ end end - with '#gets' do + with "#gets" do let(:sample_data) {"The quick brown\nfox jumped over the lazy dog\n"} it "can read chunks" do @@ -116,7 +116,7 @@ end end - with '#each' do + with "#each" do it "can read chunks" do input.each.with_index do |chunk, index| expect(chunk).to be == sample_data[index] @@ -124,17 +124,17 @@ end end - with '#closed?' do + with "#closed?" do it "should not be at end of file" do expect(input).not.to be(:closed?) end end end - with 'no body' do + with "no body" do let(:input) {subject.new(nil)} - with '#read(length, buffer)' do + with "#read(length, buffer)" do let(:buffer) {String.new} it "can read no input" do @@ -158,7 +158,7 @@ end end - with '#each' do + with "#each" do it "can read no input" do expect(input.each.to_a).to be == [] end diff --git a/test/protocol/rack/request.rb b/test/protocol/rack/request.rb index 8c479b8..1746d2f 100644 --- a/test/protocol/rack/request.rb +++ b/test/protocol/rack/request.rb @@ -3,25 +3,25 @@ # Released under the MIT License. # Copyright, 2022-2024, by Samuel Williams. -require 'protocol/rack/request' -require 'protocol/rack/adapter' +require "protocol/rack/request" +require "protocol/rack/adapter" describe Protocol::Rack::Request do let(:app) {proc{|env| [200, {}, []]}} let(:adapter) {Protocol::Rack::Adapter.new(app)} - let(:headers) {Protocol::HTTP::Headers[{'accept' => 'text/html'}]} + let(:headers) {Protocol::HTTP::Headers[{"accept" => "text/html"}]} let(:body) {Protocol::HTTP::Body::Buffered.new} let(:request) {Protocol::HTTP::Request.new( - 'https', 'example.com', 'GET', '/', 'HTTP/1.1', headers, body + "https", "example.com", "GET", "/", "HTTP/1.1", headers, body )} let(:env) {adapter.make_environment(request)} let(:wrapped_request) {subject.new(env)} - with 'incoming rack env' do + with "incoming rack env" do it "can restore request from original request" do expect(subject[env]).to be == request end @@ -38,8 +38,8 @@ end end - with 'incoming rack env which includes HTTP upgrade' do - let(:headers) {Protocol::HTTP::Headers[{'upgrade' => 'websocket'}]} + with "incoming rack env which includes HTTP upgrade" do + let(:headers) {Protocol::HTTP::Headers[{"upgrade" => "websocket"}]} it "can extract upgrade request" do expect(wrapped_request).to have_attributes( @@ -48,14 +48,14 @@ end end - with 'incoming rack env which includes rack.protocol' do + with "incoming rack env which includes rack.protocol" do let(:request) {Protocol::HTTP::Request.new( - 'https', 'example.com', 'GET', '/', 'HTTP/1.1', headers, body, ["websocket"] + "https", "example.com", "GET", "/", "HTTP/1.1", headers, body, ["websocket"] )} it "can extract upgrade request" do expect(env).to have_keys( - 'HTTP_UPGRADE' => be == 'websocket', + "HTTP_UPGRADE" => be == "websocket", ) expect(wrapped_request).to have_attributes( diff --git a/test/protocol/rack/response.rb b/test/protocol/rack/response.rb index a0d5ff1..e002157 100644 --- a/test/protocol/rack/response.rb +++ b/test/protocol/rack/response.rb @@ -1,10 +1,10 @@ # frozen_string_literal: true # Released under the MIT License. -# Copyright, 2022, by Samuel Williams. +# Copyright, 2022-2024, by Samuel Williams. -require 'protocol/rack/response' -require 'disable_console_context' +require "protocol/rack/response" +require "disable_console_context" describe Protocol::Rack::Response do let(:env) {Hash.new} @@ -15,14 +15,14 @@ let(:response) {subject.wrap(env, status, Protocol::HTTP::Headers[headers], meta, body)} - with 'hop headers' do + with "hop headers" do include DisableConsoleContext - let(:headers) {{'connection' => 'keep-alive', 'keep-alive' => 'timeout=10, max=100'}} + let(:headers) {{"connection" => "keep-alive", "keep-alive" => "timeout=10, max=100"}} - it 'ignores hop headers' do - expect(response.headers).not.to be(:include?, 'connection') - expect(response.headers).not.to be(:include?, 'keep-alive') + it "ignores hop headers" do + expect(response.headers).not.to be(:include?, "connection") + expect(response.headers).not.to be(:include?, "keep-alive") expect(response.headers).to be(:empty?) end end