diff --git a/.rubocop.yml b/.rubocop.yml index 545804e1..776debbb 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -7,6 +7,7 @@ inherit_from: - .rubocop_todo.yml - .rubocop/layout.yml - .rubocop/metrics.yml + - .rubocop/rspec.yml - .rubocop/style.yml AllCops: diff --git a/.rubocop/layout.yml b/.rubocop/layout.yml index f64f37a5..b3d9421a 100644 --- a/.rubocop/layout.yml +++ b/.rubocop/layout.yml @@ -2,6 +2,10 @@ Layout/DotPosition: Enabled: true EnforcedStyle: leading +Layout/FirstHashElementIndentation: + Enabled: true + EnforcedStyle: consistent + Layout/HashAlignment: Enabled: true EnforcedColonStyle: table diff --git a/.rubocop/rspec.yml b/.rubocop/rspec.yml new file mode 100644 index 00000000..51711080 --- /dev/null +++ b/.rubocop/rspec.yml @@ -0,0 +1,5 @@ +RSpec/ExampleLength: + CountAsOne: + - array + - heredoc + - method_call diff --git a/.rubocop/style.yml b/.rubocop/style.yml index 35e4c5f1..1609e042 100644 --- a/.rubocop/style.yml +++ b/.rubocop/style.yml @@ -12,7 +12,7 @@ Style/FormatStringToken: Style/HashSyntax: Enabled: true - EnforcedStyle: hash_rockets + EnforcedStyle: ruby19_no_mixed_keys Style/OptionHash: Enabled: true diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 6687b116..d6f36a5d 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -1,6 +1,6 @@ # This configuration was generated by # `rubocop --auto-gen-config --auto-gen-only-exclude --exclude-limit 100` -# on 2023-10-17 15:09:44 UTC using RuboCop version 1.57.1. +# on 2023-10-18 14:33:19 UTC using RuboCop version 1.57.1. # The point is for the user to remove these configuration records # one by one as the offenses are removed from the code base. # Note that changes in the inspected code, or installation of new @@ -208,20 +208,16 @@ RSpec/DescribedClass: - 'spec/lib/http/uri/normalizer_spec.rb' - 'spec/lib/http_spec.rb' -# Offense count: 55 +# Offense count: 41 # Configuration parameters: Max, CountAsOne. RSpec/ExampleLength: Exclude: - 'spec/lib/http/client_spec.rb' - 'spec/lib/http/connection_spec.rb' - 'spec/lib/http/features/auto_deflate_spec.rb' - - 'spec/lib/http/features/logging_spec.rb' - - 'spec/lib/http/options/features_spec.rb' - 'spec/lib/http/options/headers_spec.rb' - - 'spec/lib/http/options/merge_spec.rb' - 'spec/lib/http/redirector_spec.rb' - 'spec/lib/http/request/body_spec.rb' - - 'spec/lib/http/request/writer_spec.rb' - 'spec/lib/http/response/body_spec.rb' - 'spec/lib/http/uri_spec.rb' - 'spec/lib/http_spec.rb' diff --git a/Gemfile b/Gemfile index 712de1c0..f6af0bf1 100644 --- a/Gemfile +++ b/Gemfile @@ -10,12 +10,12 @@ gem "rake" gem "webrick" group :development do - gem "guard-rspec", :require => false - gem "nokogiri", :require => false - gem "pry", :require => false + gem "guard-rspec", require: false + gem "nokogiri", require: false + gem "pry", require: false # RSpec formatter - gem "fuubar", :require => false + gem "fuubar", require: false platform :mri do gem "pry-byebug" @@ -23,7 +23,7 @@ group :development do end group :test do - gem "certificate_authority", "~> 1.0", :require => false + gem "certificate_authority", "~> 1.0", require: false gem "backports" @@ -32,8 +32,8 @@ group :test do gem "rubocop-rake", "~> 0.6.0" gem "rubocop-rspec", "~> 2.24.1" - gem "simplecov", :require => false - gem "simplecov-lcov", :require => false + gem "simplecov", require: false + gem "simplecov-lcov", require: false gem "rspec", "~> 3.10" gem "rspec-its" diff --git a/Guardfile b/Guardfile index b9aea643..62c1ee1e 100644 --- a/Guardfile +++ b/Guardfile @@ -2,7 +2,7 @@ # More info at https://github.com/guard/guard#readme -guard :rspec, :cmd => "GUARD_RSPEC=1 bundle exec rspec --no-profile" do +guard :rspec, cmd: "GUARD_RSPEC=1 bundle exec rspec --no-profile" do require "guard/rspec/dsl" dsl = Guard::RSpec::Dsl.new(self) diff --git a/Rakefile b/Rakefile index ca32a40d..d05f8a16 100644 --- a/Rakefile +++ b/Rakefile @@ -61,4 +61,4 @@ task :generate_status_codes do end end -task :default => %i[spec rubocop verify_measurements] +task default: %i[spec rubocop verify_measurements] diff --git a/lib/http/chainable.rb b/lib/http/chainable.rb index de6affde..acd534f6 100644 --- a/lib/http/chainable.rb +++ b/lib/http/chainable.rb @@ -92,7 +92,7 @@ def build_request(*args) # @param [Numeric] global_timeout def timeout(options) klass, options = case options - when Numeric then [HTTP::Timeout::Global, {:global => options}] + when Numeric then [HTTP::Timeout::Global, {global: options}] when Hash then [HTTP::Timeout::PerOperation, options.dup] when :null then [HTTP::Timeout::Null, {}] else raise ArgumentError, "Use `.timeout(global_timeout_in_seconds)` or `.timeout(connect: x, write: y, read: z)`." @@ -106,8 +106,8 @@ def timeout(options) end branch default_options.merge( - :timeout_class => klass, - :timeout_options => options + timeout_class: klass, + timeout_options: options ) end @@ -142,7 +142,7 @@ def timeout(options) # @yieldparam [HTTP::Client] client Persistent client # @return [Object] result of last expression in the block def persistent(host, timeout: 5) - options = {:keep_alive_timeout => timeout} + options = {keep_alive_timeout: timeout} p_client = branch default_options.merge(options).with_persistent host return p_client unless block_given? diff --git a/lib/http/client.rb b/lib/http/client.rb index 494b2913..e256a7ec 100644 --- a/lib/http/client.rb +++ b/lib/http/client.rb @@ -43,14 +43,14 @@ def build_request(verb, uri, opts = {}) headers = make_request_headers(opts) body = make_request_body(opts, headers) - req = HTTP::Request.new( - :verb => verb, - :uri => uri, - :uri_normalizer => opts.feature(:normalize_uri)&.normalizer, - :proxy => opts.proxy, - :headers => headers, - :body => body - ) + req = HTTP::Request.new({ + verb: verb, + uri: uri, + uri_normalizer: opts.feature(:normalize_uri)&.normalizer, + proxy: opts.proxy, + headers: headers, + body: body + }) wrap_request(req, opts) end @@ -110,13 +110,13 @@ def wrap_request(req, opts) def build_response(req, options) Response.new( - :status => @connection.status_code, - :version => @connection.http_version, - :headers => @connection.headers, - :proxy_headers => @connection.proxy_response_headers, - :connection => @connection, - :encoding => options.encoding, - :request => req + status: @connection.status_code, + version: @connection.http_version, + headers: @connection.headers, + proxy_headers: @connection.proxy_response_headers, + connection: @connection, + encoding: options.encoding, + request: req ) end diff --git a/lib/http/feature.rb b/lib/http/feature.rb index 02e23187..b178becf 100644 --- a/lib/http/feature.rb +++ b/lib/http/feature.rb @@ -2,10 +2,6 @@ module HTTP class Feature - def initialize(opts = {}) - @opts = opts - end - def wrap_request(request) request end @@ -14,7 +10,7 @@ def wrap_response(response) response end - def on_error(request, error); end + def on_error(_request, _error); end end end diff --git a/lib/http/features/auto_deflate.rb b/lib/http/features/auto_deflate.rb index ff1b9259..478d26cf 100644 --- a/lib/http/features/auto_deflate.rb +++ b/lib/http/features/auto_deflate.rb @@ -1,21 +1,24 @@ # frozen_string_literal: true -require "zlib" +require "set" require "tempfile" +require "zlib" require "http/request/body" module HTTP module Features class AutoDeflate < Feature + VALID_METHODS = Set.new(%w[gzip deflate]).freeze + attr_reader :method - def initialize(**) - super + def initialize(method: "gzip") + super() - @method = @opts.key?(:method) ? @opts[:method].to_s : "gzip" + @method = method.to_s - raise Error, "Only gzip and deflate methods are supported" unless %w[gzip deflate].include?(@method) + raise Error, "Only gzip and deflate methods are supported" unless VALID_METHODS.include?(@method) end def wrap_request(request) @@ -27,13 +30,13 @@ def wrap_request(request) request.headers[Headers::CONTENT_ENCODING] = method Request.new( - :version => request.version, - :verb => request.verb, - :uri => request.uri, - :headers => request.headers, - :proxy => request.proxy, - :body => deflated_body(request.body), - :uri_normalizer => request.uri_normalizer + version: request.version, + verb: request.verb, + uri: request.uri, + headers: request.headers, + proxy: request.proxy, + body: deflated_body(request.body), + uri_normalizer: request.uri_normalizer ) end @@ -82,7 +85,7 @@ def compressed_each end def compress_all! - @compressed = Tempfile.new("http-compressed_body", :binmode => true) + @compressed = Tempfile.new("http-compressed_body", binmode: true) compress { |data| @compressed.write(data) } @compressed.rewind end diff --git a/lib/http/features/auto_inflate.rb b/lib/http/features/auto_inflate.rb index e1d383ef..36a04507 100644 --- a/lib/http/features/auto_inflate.rb +++ b/lib/http/features/auto_inflate.rb @@ -12,13 +12,13 @@ def wrap_response(response) return response unless supported_encoding?(response) options = { - :status => response.status, - :version => response.version, - :headers => response.headers, - :proxy_headers => response.proxy_headers, - :connection => response.connection, - :body => stream_for(response.connection), - :request => response.request + status: response.status, + version: response.version, + headers: response.headers, + proxy_headers: response.proxy_headers, + connection: response.connection, + body: stream_for(response.connection), + request: response.request } Response.new(options) diff --git a/lib/http/features/instrumentation.rb b/lib/http/features/instrumentation.rb index 44110159..a4dd923a 100644 --- a/lib/http/features/instrumentation.rb +++ b/lib/http/features/instrumentation.rb @@ -22,6 +22,7 @@ class Instrumentation < Feature attr_reader :instrumenter, :name, :error_name def initialize(instrumenter: NullInstrumenter.new, namespace: "http") + super() @instrumenter = instrumenter @name = "request.#{namespace}" @error_name = "error.#{namespace}" @@ -30,18 +31,18 @@ def initialize(instrumenter: NullInstrumenter.new, namespace: "http") def wrap_request(request) # Emit a separate "start" event, so a logger can print the request # being run without waiting for a response - instrumenter.instrument("start_#{name}", :request => request) - instrumenter.start(name, :request => request) + instrumenter.instrument("start_#{name}", request: request) + instrumenter.start(name, request: request) request end def wrap_response(response) - instrumenter.finish(name, :response => response) + instrumenter.finish(name, response: response) response end def on_error(request, error) - instrumenter.instrument(error_name, :request => request, :error => error) + instrumenter.instrument(error_name, request: request, error: error) end HTTP::Options.register_feature(:instrumentation, self) diff --git a/lib/http/features/logging.rb b/lib/http/features/logging.rb index d65241c1..c888763e 100644 --- a/lib/http/features/logging.rb +++ b/lib/http/features/logging.rb @@ -26,6 +26,7 @@ class NullLogger attr_reader :logger def initialize(logger: NullLogger.new) + super() @logger = logger end diff --git a/lib/http/features/normalize_uri.rb b/lib/http/features/normalize_uri.rb index c8ef768e..2aa179ff 100644 --- a/lib/http/features/normalize_uri.rb +++ b/lib/http/features/normalize_uri.rb @@ -8,6 +8,7 @@ class NormalizeUri < Feature attr_reader :normalizer def initialize(normalizer: HTTP::URI::NORMALIZER) + super() @normalizer = normalizer end diff --git a/lib/http/options.rb b/lib/http/options.rb index 753057ab..6f99e3d7 100644 --- a/lib/http/options.rb +++ b/lib/http/options.rb @@ -49,19 +49,19 @@ def def_option(name, reader_only: false, &interpreter) def initialize(options = {}) defaults = { - :response => :auto, - :proxy => {}, - :timeout_class => self.class.default_timeout_class, - :timeout_options => {}, - :socket_class => self.class.default_socket_class, - :nodelay => false, - :ssl_socket_class => self.class.default_ssl_socket_class, - :ssl => {}, - :keep_alive_timeout => 5, - :headers => {}, - :cookies => {}, - :encoding => nil, - :features => {} + response: :auto, + proxy: {}, + timeout_class: self.class.default_timeout_class, + timeout_options: {}, + socket_class: self.class.default_socket_class, + nodelay: false, + ssl_socket_class: self.class.default_ssl_socket_class, + ssl: {}, + keep_alive_timeout: 5, + headers: {}, + cookies: {}, + encoding: nil, + features: {} } opts_w_defaults = defaults.merge(options) @@ -84,7 +84,7 @@ def initialize(options = {}) self.encoding = Encoding.find(encoding) end - def_option :features, :reader_only => true do |new_features| + def_option :features, reader_only: true do |new_features| # Normalize features from: # # [{feature_one: {opt: 'val'}}, :feature_two] @@ -124,7 +124,7 @@ def features=(features) def_option method_name end - def_option :follow, :reader_only => true + def_option :follow, reader_only: true def follow=(value) @follow = @@ -136,7 +136,7 @@ def follow=(value) end end - def_option :persistent, :reader_only => true + def_option :persistent, reader_only: true def persistent=(value) @persistent = value ? HTTP::URI.parse(value).origin : nil diff --git a/lib/http/redirector.rb b/lib/http/redirector.rb index 921d7177..c51bbad9 100644 --- a/lib/http/redirector.rb +++ b/lib/http/redirector.rb @@ -93,7 +93,7 @@ def collect_cookies_from_request end cookies.each do |key, value| - cookie_jar.add(HTTP::Cookie.new(key, value, :path => @request.uri.path, :domain => @request.host)) + cookie_jar.add(HTTP::Cookie.new(key, value, path: @request.uri.path, domain: @request.host)) end end diff --git a/lib/http/request.rb b/lib/http/request.rb index b32e8ee3..ff2dbb3b 100644 --- a/lib/http/request.rb +++ b/lib/http/request.rb @@ -60,10 +60,10 @@ class UnsupportedSchemeError < RequestError; end # Default ports of supported schemes PORTS = { - :http => 80, - :https => 443, - :ws => 80, - :wss => 443 + http: 80, + https: 443, + ws: 80, + wss: 443 }.freeze # Method is given as a lowercase symbol e.g. :get, :post @@ -122,13 +122,13 @@ def redirect(uri, verb = @verb) end self.class.new( - :verb => verb, - :uri => @uri.join(uri), - :headers => headers, - :proxy => proxy, - :body => new_body, - :version => version, - :uri_normalizer => uri_normalizer + verb: verb, + uri: @uri.join(uri), + headers: headers, + proxy: proxy, + body: new_body, + version: version, + uri_normalizer: uri_normalizer ) end diff --git a/lib/http/response.rb b/lib/http/response.rb index 52f46686..0dbdbd36 100644 --- a/lib/http/response.rb +++ b/lib/http/response.rb @@ -55,7 +55,7 @@ def initialize(opts) connection = opts.fetch(:connection) encoding = opts[:encoding] || charset || default_encoding - @body = Response::Body.new(connection, :encoding => encoding) + @body = Response::Body.new(connection, encoding: encoding) end end @@ -183,7 +183,7 @@ def init_request(opts) # For backwards compatibilty if opts[:uri] - HTTP::Request.new(:uri => opts[:uri], :verb => :get) + HTTP::Request.new(uri: opts[:uri], verb: :get) else opts.fetch(:request) end diff --git a/lib/http/response/body.rb b/lib/http/response/body.rb index d4406a80..8148fd08 100644 --- a/lib/http/response/body.rb +++ b/lib/http/response/body.rb @@ -29,7 +29,7 @@ def readpartial(*args) stream! chunk = @stream.readpartial(*args) - String.new(chunk, :encoding => @encoding) if chunk + String.new(chunk, encoding: @encoding) if chunk end # Iterate over the body, allowing it to be enumerable @@ -47,10 +47,10 @@ def to_s begin @streaming = false - @contents = String.new("", :encoding => @encoding) + @contents = String.new("", encoding: @encoding) while (chunk = @stream.readpartial) - @contents << String.new(chunk, :encoding => @encoding) + @contents << String.new(chunk, encoding: @encoding) chunk = nil # deallocate string end rescue diff --git a/lib/http/response/parser.rb b/lib/http/response/parser.rb index 13e78efc..222f8a3d 100644 --- a/lib/http/response/parser.rb +++ b/lib/http/response/parser.rb @@ -10,7 +10,7 @@ class Parser def initialize @handler = Handler.new(self) - @parser = LLHttp::Parser.new(@handler, :type => :response) + @parser = LLHttp::Parser.new(@handler, type: :response) reset end diff --git a/lib/http/timeout/global.rb b/lib/http/timeout/global.rb index 1148aef9..350a2144 100644 --- a/lib/http/timeout/global.rb +++ b/lib/http/timeout/global.rb @@ -58,11 +58,11 @@ def write(data) private def read_nonblock(size, buffer = nil) - @socket.read_nonblock(size, buffer, :exception => false) + @socket.read_nonblock(size, buffer, exception: false) end def write_nonblock(data) - @socket.write_nonblock(data, :exception => false) + @socket.write_nonblock(data, exception: false) end # Perform the given I/O operation with the given argument diff --git a/lib/http/timeout/per_operation.rb b/lib/http/timeout/per_operation.rb index 9274325e..100ff66c 100644 --- a/lib/http/timeout/per_operation.rb +++ b/lib/http/timeout/per_operation.rb @@ -38,7 +38,7 @@ def connect_ssl def readpartial(size, buffer = nil) timeout = false loop do - result = @socket.read_nonblock(size, buffer, :exception => false) + result = @socket.read_nonblock(size, buffer, exception: false) return :eof if result.nil? return result if result != :wait_readable @@ -63,7 +63,7 @@ def readpartial(size, buffer = nil) def write(data) timeout = false loop do - result = @socket.write_nonblock(data, :exception => false) + result = @socket.write_nonblock(data, exception: false) return result unless result == :wait_writable raise TimeoutError, "Write timed out after #{@write_timeout} seconds" if timeout diff --git a/lib/http/uri.rb b/lib/http/uri.rb index b890777f..49ee025c 100644 --- a/lib/http/uri.rb +++ b/lib/http/uri.rb @@ -45,11 +45,11 @@ class URI uri = HTTP::URI.parse uri HTTP::URI.new( - :scheme => uri.normalized_scheme, - :authority => uri.normalized_authority, - :path => uri.path.empty? ? "/" : percent_encode(Addressable::URI.normalize_path(uri.path)), - :query => percent_encode(uri.query), - :fragment => uri.normalized_fragment + scheme: uri.normalized_scheme, + authority: uri.normalized_authority, + path: uri.path.empty? ? "/" : percent_encode(Addressable::URI.normalize_path(uri.path)), + query: percent_encode(uri.query), + fragment: uri.normalized_fragment ) end @@ -145,7 +145,7 @@ def hash # @param [String, #to_str] new_host The new host component. # @return [void] def host=(new_host) - @uri.host = process_ipv6_brackets(new_host, :brackets => true) + @uri.host = process_ipv6_brackets(new_host, brackets: true) @host = process_ipv6_brackets(@uri.host) @normalized_host = process_ipv6_brackets(@uri.normalized_host) diff --git a/spec/lib/http/client_spec.rb b/spec/lib/http/client_spec.rb index fa1790ff..dc86a2fd 100644 --- a/spec/lib/http/client_spec.rb +++ b/spec/lib/http/client_spec.rb @@ -34,11 +34,11 @@ def stub(stubs) def redirect_response(location, status = 302) lambda do |request| HTTP::Response.new( - :status => status, - :version => "1.1", - :headers => {"Location" => location}, - :body => "", - :request => request + status: status, + version: "1.1", + headers: {"Location" => location}, + body: "", + request: request ) end end @@ -46,10 +46,10 @@ def redirect_response(location, status = 302) def simple_response(body, status = 200) lambda do |request| HTTP::Response.new( - :status => status, - :version => "1.1", - :body => body, - :request => request + status: status, + version: "1.1", + body: body, + request: request ) end end @@ -59,7 +59,7 @@ def simple_response(body, status = 200) describe "following redirects" do it "returns response of new location" do - client = StubbedClient.new(:follow => true).stub( + client = StubbedClient.new(follow: true).stub( "http://example.com/" => redirect_response("http://example.com/blog"), "http://example.com/blog" => simple_response("OK") ) @@ -68,7 +68,7 @@ def simple_response(body, status = 200) end it "prepends previous request uri scheme and host if needed" do - client = StubbedClient.new(:follow => true).stub( + client = StubbedClient.new(follow: true).stub( "http://example.com/" => redirect_response("/index"), "http://example.com/index" => redirect_response("/index.html"), "http://example.com/index.html" => simple_response("OK") @@ -78,7 +78,7 @@ def simple_response(body, status = 200) end it "fails upon endless redirects" do - client = StubbedClient.new(:follow => true).stub( + client = StubbedClient.new(follow: true).stub( "http://example.com/" => redirect_response("/") ) @@ -87,7 +87,7 @@ def simple_response(body, status = 200) end it "fails if max amount of hops reached" do - client = StubbedClient.new(:follow => {:max_hops => 5}).stub( + client = StubbedClient.new(follow: {max_hops: 5}).stub( "http://example.com/" => redirect_response("/1"), "http://example.com/1" => redirect_response("/2"), "http://example.com/2" => redirect_response("/3"), @@ -103,7 +103,7 @@ def simple_response(body, status = 200) context "with non-ASCII URLs" do it "theoretically works like a charm" do - client = StubbedClient.new(:follow => true).stub( + client = StubbedClient.new(follow: true).stub( "http://example.com/" => redirect_response("/könig"), "http://example.com/könig" => simple_response("OK") ) @@ -129,7 +129,7 @@ def simple_response(body, status = 200) let(:logdev) { StringIO.new } it "logs all requests" do - client = StubbedClient.new(:follow => true, :features => { :logging => { :logger => logger } }).stub( + client = StubbedClient.new(follow: true, features: { logging: { logger: logger } }).stub( "http://example.com/" => redirect_response("/1"), "http://example.com/1" => redirect_response("/2"), "http://example.com/2" => redirect_response("/3"), @@ -169,7 +169,7 @@ def simple_response(body, status = 200) expect(CGI.parse(opts[:uri].query)).to eq("foo" => %w[bar], "baz" => %w[quux]) end - client.get("http://example.com/?foo=bar", :params => {:baz => "quux"}) + client.get("http://example.com/?foo=bar", params: {baz: "quux"}) end it "merges duplicate values" do @@ -177,7 +177,7 @@ def simple_response(body, status = 200) expect(opts[:uri].query).to match(/^(a=1&a=2|a=2&a=1)$/) end - client.get("http://example.com/?a=1", :params => {:a => 2}) + client.get("http://example.com/?a=1", params: {a: 2}) end it "does not modifies query part if no params were given" do @@ -193,7 +193,7 @@ def simple_response(body, status = 200) expect(CGI.parse(opts[:uri].query)).to eq "a[]" => %w[b c], "d" => %w[e] end - client.get("http://example.com/?a[]=b&a[]=c", :params => {:d => "e"}) + client.get("http://example.com/?a[]=b&a[]=c", params: {d: "e"}) end it "properly encodes colons" do @@ -201,7 +201,7 @@ def simple_response(body, status = 200) expect(opts[:uri].query).to eq "t=1970-01-01T00%3A00%3A00Z" end - client.get("http://example.com/", :params => {:t => "1970-01-01T00:00:00Z"}) + client.get("http://example.com/", params: {t: "1970-01-01T00:00:00Z"}) end it 'does not convert newlines into \r\n before encoding string values' do @@ -209,7 +209,7 @@ def simple_response(body, status = 200) expect(opts[:uri].query).to eq "foo=bar%0Abaz" end - client.get("http://example.com/", :params => {:foo => "bar\nbaz"}) + client.get("http://example.com/", params: {foo: "bar\nbaz"}) end end @@ -223,7 +223,7 @@ def simple_response(body, status = 200) expect(opts[:body].to_s).to eq "foo=bar" end - client.get("http://example.com/", :form => {:foo => "bar"}) + client.get("http://example.com/", form: {foo: "bar"}) end it "creates multipart form data object" do @@ -235,13 +235,13 @@ def simple_response(body, status = 200) expect(opts[:body].to_s).to include("content") end - client.get("http://example.com/", :form => {:foo => HTTP::FormData::Part.new("content")}) + client.get("http://example.com/", form: {foo: HTTP::FormData::Part.new("content")}) end context "when passing an HTTP::FormData object directly" do it "creates url encoded form data object" do client = HTTP::Client.new - form_data = HTTP::FormData::Multipart.new({ :foo => "bar" }) + form_data = HTTP::FormData::Multipart.new({ foo: "bar" }) allow(client).to receive(:perform) @@ -250,7 +250,7 @@ def simple_response(body, status = 200) expect(opts[:body].to_s).to match(/^Content-Disposition: form-data; name="foo"\r\n\r\nbar\r\n/m) end - client.get("http://example.com/", :form => form_data) + client.get("http://example.com/", form: form_data) end end end @@ -265,7 +265,7 @@ def simple_response(body, status = 200) expect(opts[:headers]["Content-Type"]).to eq "application/json; charset=utf-8" end - client.get("http://example.com/", :json => {:foo => :bar}) + client.get("http://example.com/", json: {foo: :bar}) end end @@ -285,7 +285,7 @@ def simple_response(body, status = 200) context "with explicitly given `Host` header" do let(:headers) { {"Host" => "another.example.com"} } - let(:client) { described_class.new :headers => headers } + let(:client) { described_class.new headers: headers } it "keeps `Host` header as is" do expect(client).to receive(:perform) do |req, _| @@ -298,7 +298,7 @@ def simple_response(body, status = 200) context "when :auto_deflate was specified" do let(:headers) { {"Content-Length" => "12"} } - let(:client) { described_class.new :headers => headers, :features => {:auto_deflate => {}}, :body => "foo" } + let(:client) { described_class.new headers: headers, features: {auto_deflate: {}}, body: "foo" } it "deletes Content-Length header" do expect(client).to receive(:perform) do |req, _| @@ -317,7 +317,7 @@ def simple_response(body, status = 200) end context "and there is no body" do - let(:client) { described_class.new :headers => headers, :features => {:auto_deflate => {}} } + let(:client) { described_class.new headers: headers, features: {auto_deflate: {}} } it "doesn't set Content-Encoding header" do expect(client).to receive(:perform) do |req, _| @@ -352,7 +352,7 @@ def on_error(request, error) it "is given a chance to wrap the Request" do feature_instance = feature_class.new - response = client.use(:test_feature => feature_instance). + response = client.use(test_feature: feature_instance). request(:get, dummy.endpoint) expect(response.code).to eq(200) @@ -363,7 +363,7 @@ def on_error(request, error) it "is given a chance to wrap the Response" do feature_instance = feature_class.new - response = client.use(:test_feature => feature_instance). + response = client.use(test_feature: feature_instance). request(:get, dummy.endpoint) expect(feature_instance.captured_response).to eq(response) @@ -374,7 +374,7 @@ def on_error(request, error) feature_instance = feature_class.new expect do - client.use(:test_feature => feature_instance). + client.use(test_feature: feature_instance). timeout(0.2). request(:post, sleep_url) end.to raise_error(HTTP::TimeoutError) @@ -390,7 +390,7 @@ def on_error(request, error) feature_instance = feature_class.new expect do - client.use(:test_feature => feature_instance). + client.use(test_feature: feature_instance). timeout(0.001). request(:post, sleep_url) end.to raise_error(HTTP::ConnectTimeoutError) @@ -420,14 +420,14 @@ def wrap_response(res) res end end - feature_instance_a = feature_class_order.new(:id => "a") - feature_instance_b = feature_class_order.new(:id => "b") - feature_instance_c = feature_class_order.new(:id => "c") + feature_instance_a = feature_class_order.new(id: "a") + feature_instance_b = feature_class_order.new(id: "b") + feature_instance_c = feature_class_order.new(id: "c") client.use( - :test_feature_a => feature_instance_a, - :test_feature_b => feature_instance_b, - :test_feature_c => feature_instance_c + test_feature_a: feature_instance_a, + test_feature_b: feature_instance_b, + test_feature_c: feature_instance_c ).request(:get, dummy.endpoint) expect(feature_class_order.order).to eq( @@ -446,12 +446,12 @@ def wrap_response(res) # TODO: https://github.com/httprb/http/issues/627 xdescribe "working with SSL" do - run_server(:dummy_ssl) { DummyServer.new(:ssl => true) } + run_server(:dummy_ssl) { DummyServer.new(ssl: true) } let(:extra_options) { {} } let(:client) do - described_class.new options.merge(:ssl_context => SSLHelper.client_context).merge(extra_options) + described_class.new options.merge(ssl_context: SSLHelper.client_context).merge(extra_options) end include_context "HTTP handling" do @@ -470,7 +470,7 @@ def wrap_response(res) context "with SSL options instead of a context" do let(:client) do - described_class.new options.merge :ssl => SSLHelper.client_params + described_class.new options.merge ssl: SSLHelper.client_params end it "just works" do diff --git a/spec/lib/http/connection_spec.rb b/spec/lib/http/connection_spec.rb index 7cd3a6b9..fbe5dd10 100644 --- a/spec/lib/http/connection_spec.rb +++ b/spec/lib/http/connection_spec.rb @@ -3,22 +3,22 @@ RSpec.describe HTTP::Connection do let(:req) do HTTP::Request.new( - :verb => :get, - :uri => "http://example.com/", - :headers => {} + verb: :get, + uri: "http://example.com/", + headers: {} ) end - let(:socket) { double(:connect => nil, :close => nil) } - let(:timeout_class) { double(:new => socket) } - let(:opts) { HTTP::Options.new(:timeout_class => timeout_class) } + let(:socket) { double(connect: nil, close: nil) } + let(:timeout_class) { double(new: socket) } + let(:opts) { HTTP::Options.new(timeout_class: timeout_class) } let(:connection) { HTTP::Connection.new(req, opts) } describe "#initialize times out" do let(:req) do HTTP::Request.new( - :verb => :get, - :uri => "https://example.com/", - :headers => {} + verb: :get, + uri: "https://example.com/", + headers: {} ) end diff --git a/spec/lib/http/features/auto_deflate_spec.rb b/spec/lib/http/features/auto_deflate_spec.rb index aa1e2371..e7a681bf 100644 --- a/spec/lib/http/features/auto_deflate_spec.rb +++ b/spec/lib/http/features/auto_deflate_spec.rb @@ -4,22 +4,22 @@ subject { HTTP::Features::AutoDeflate.new } it "raises error for wrong type" do - expect { HTTP::Features::AutoDeflate.new(:method => :wrong) }. + expect { HTTP::Features::AutoDeflate.new(method: :wrong) }. to raise_error(HTTP::Error) { |error| expect(error.message).to eq("Only gzip and deflate methods are supported") } end it "accepts gzip method" do - expect(HTTP::Features::AutoDeflate.new(:method => :gzip).method).to eq "gzip" + expect(HTTP::Features::AutoDeflate.new(method: :gzip).method).to eq "gzip" end it "accepts deflate method" do - expect(HTTP::Features::AutoDeflate.new(:method => :deflate).method).to eq "deflate" + expect(HTTP::Features::AutoDeflate.new(method: :deflate).method).to eq "deflate" end it "accepts string as method" do - expect(HTTP::Features::AutoDeflate.new(:method => "gzip").method).to eq "gzip" + expect(HTTP::Features::AutoDeflate.new(method: "gzip").method).to eq "gzip" end it "uses gzip by default" do @@ -31,7 +31,7 @@ let(:deflated_body) { subject.deflated_body(body) } context "when method is gzip" do - subject { HTTP::Features::AutoDeflate.new(:method => :gzip) } + subject { HTTP::Features::AutoDeflate.new(method: :gzip) } it "returns object which yields gzipped content of the given body" do io = StringIO.new @@ -58,7 +58,7 @@ end context "when method is deflate" do - subject { HTTP::Features::AutoDeflate.new(:method => :deflate) } + subject { HTTP::Features::AutoDeflate.new(method: :deflate) } it "returns object which yields deflated content of the given body" do deflated = Zlib::Deflate.deflate("beescows") diff --git a/spec/lib/http/features/auto_inflate_spec.rb b/spec/lib/http/features/auto_inflate_spec.rb index f6c345d0..8345708c 100644 --- a/spec/lib/http/features/auto_inflate_spec.rb +++ b/spec/lib/http/features/auto_inflate_spec.rb @@ -8,11 +8,11 @@ let(:response) do HTTP::Response.new( - :version => "1.1", - :status => 200, - :headers => headers, - :connection => connection, - :request => HTTP::Request.new(:verb => :get, :uri => "http://example.com") + version: "1.1", + status: 200, + headers: headers, + connection: connection, + request: HTTP::Request.new(verb: :get, uri: "http://example.com") ) end @@ -26,7 +26,7 @@ end context "for identity Content-Encoding header" do - let(:headers) { {:content_encoding => "identity"} } + let(:headers) { {content_encoding: "identity"} } it "returns original request" do expect(result).to be response @@ -34,7 +34,7 @@ end context "for unknown Content-Encoding header" do - let(:headers) { {:content_encoding => "not-supported"} } + let(:headers) { {content_encoding: "not-supported"} } it "returns original request" do expect(result).to be response @@ -42,7 +42,7 @@ end context "for deflate Content-Encoding header" do - let(:headers) { {:content_encoding => "deflate"} } + let(:headers) { {content_encoding: "deflate"} } it "returns a HTTP::Response wrapping the inflated response body" do expect(result.body).to be_instance_of HTTP::Response::Body @@ -50,7 +50,7 @@ end context "for gzip Content-Encoding header" do - let(:headers) { {:content_encoding => "gzip"} } + let(:headers) { {content_encoding: "gzip"} } it "returns a HTTP::Response wrapping the inflated response body" do expect(result.body).to be_instance_of HTTP::Response::Body @@ -58,7 +58,7 @@ end context "for x-gzip Content-Encoding header" do - let(:headers) { {:content_encoding => "x-gzip"} } + let(:headers) { {content_encoding: "x-gzip"} } it "returns a HTTP::Response wrapping the inflated response body" do expect(result.body).to be_instance_of HTTP::Response::Body @@ -70,11 +70,11 @@ context "when response has uri" do let(:response) do HTTP::Response.new( - :version => "1.1", - :status => 200, - :headers => {:content_encoding => "gzip"}, - :connection => connection, - :request => HTTP::Request.new(:verb => :get, :uri => "https://example.com") + version: "1.1", + status: 200, + headers: {content_encoding: "gzip"}, + connection: connection, + request: HTTP::Request.new(verb: :get, uri: "https://example.com") ) end diff --git a/spec/lib/http/features/instrumentation_spec.rb b/spec/lib/http/features/instrumentation_spec.rb index ddbb6666..4743bf6e 100644 --- a/spec/lib/http/features/instrumentation_spec.rb +++ b/spec/lib/http/features/instrumentation_spec.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true RSpec.describe HTTP::Features::Instrumentation do - subject(:feature) { HTTP::Features::Instrumentation.new(:instrumenter => instrumenter) } + subject(:feature) { HTTP::Features::Instrumentation.new(instrumenter: instrumenter) } let(:instrumenter) { TestInstrumenter.new } @@ -28,45 +28,45 @@ def finish(_name, payload) describe "logging the request" do let(:request) do HTTP::Request.new( - :verb => :post, - :uri => "https://example.com/", - :headers => {:accept => "application/json"}, - :body => '{"hello": "world!"}' + verb: :post, + uri: "https://example.com/", + headers: {accept: "application/json"}, + body: '{"hello": "world!"}' ) end it "logs the request" do feature.wrap_request(request) - expect(instrumenter.output[:start]).to eq(:request => request) + expect(instrumenter.output[:start]).to eq(request: request) end end describe "logging the response" do let(:response) do HTTP::Response.new( - :version => "1.1", - :status => 200, - :headers => {:content_type => "application/json"}, - :body => '{"success": true}', - :request => HTTP::Request.new(:verb => :get, :uri => "https://example.com") + version: "1.1", + status: 200, + headers: {content_type: "application/json"}, + body: '{"success": true}', + request: HTTP::Request.new(verb: :get, uri: "https://example.com") ) end it "logs the response" do feature.wrap_response(response) - expect(instrumenter.output[:finish]).to eq(:response => response) + expect(instrumenter.output[:finish]).to eq(response: response) end end describe "logging errors" do let(:request) do HTTP::Request.new( - :verb => :post, - :uri => "https://example.com/", - :headers => {:accept => "application/json"}, - :body => '{"hello": "world!"}' + verb: :post, + uri: "https://example.com/", + headers: {accept: "application/json"}, + body: '{"hello": "world!"}' ) end @@ -75,7 +75,7 @@ def finish(_name, payload) it "logs the error" do feature.on_error(request, error) - expect(instrumenter.output[:finish]).to eq(:request => request, :error => error) + expect(instrumenter.output[:finish]).to eq(request: request, error: error) end end end diff --git a/spec/lib/http/features/logging_spec.rb b/spec/lib/http/features/logging_spec.rb index 5eeae06c..c55015a0 100644 --- a/spec/lib/http/features/logging_spec.rb +++ b/spec/lib/http/features/logging_spec.rb @@ -7,7 +7,7 @@ logger = Logger.new(logdev) logger.formatter = ->(severity, _, _, message) { format("** %s **\n%s\n", severity, message) } - described_class.new(:logger => logger) + described_class.new(logger: logger) end let(:logdev) { StringIO.new } @@ -15,10 +15,10 @@ describe "logging the request" do let(:request) do HTTP::Request.new( - :verb => :post, - :uri => "https://example.com/", - :headers => {:accept => "application/json"}, - :body => '{"hello": "world!"}' + verb: :post, + uri: "https://example.com/", + headers: {accept: "application/json"}, + body: '{"hello": "world!"}' ) end @@ -41,11 +41,11 @@ describe "logging the response" do let(:response) do HTTP::Response.new( - :version => "1.1", - :status => 200, - :headers => {:content_type => "application/json"}, - :body => '{"success": true}', - :request => HTTP::Request.new(:verb => :get, :uri => "https://example.com") + version: "1.1", + status: 200, + headers: {content_type: "application/json"}, + body: '{"success": true}', + request: HTTP::Request.new(verb: :get, uri: "https://example.com") ) end diff --git a/spec/lib/http/headers_spec.rb b/spec/lib/http/headers_spec.rb index 9225a508..e4ac774d 100644 --- a/spec/lib/http/headers_spec.rb +++ b/spec/lib/http/headers_spec.rb @@ -439,7 +439,7 @@ before do headers.set :host, "example.com" headers.set :accept, "application/json" - headers.merge! :accept => "plain/text", :cookie => %w[hoo=ray woo=hoo] + headers.merge! accept: "plain/text", cookie: %w[hoo=ray woo=hoo] end it "leaves headers not presented in other as is" do @@ -457,7 +457,7 @@ describe "#merge" do subject(:merged) do - headers.merge :accept => "plain/text", :cookie => %w[hoo=ray woo=hoo] + headers.merge accept: "plain/text", cookie: %w[hoo=ray woo=hoo] end before do @@ -489,17 +489,17 @@ let(:dummyClass) { Class.new { def respond_to?(*); end } } it "accepts any object that respond to #to_hash" do - hashie = double :to_hash => {"accept" => "json"} + hashie = double to_hash: {"accept" => "json"} expect(described_class.coerce(hashie)["accept"]).to eq "json" end it "accepts any object that respond to #to_h" do - hashie = double :to_h => {"accept" => "json"} + hashie = double to_h: {"accept" => "json"} expect(described_class.coerce(hashie)["accept"]).to eq "json" end it "accepts any object that respond to #to_a" do - hashie = double :to_a => [%w[accept json]] + hashie = double to_a: [%w[accept json]] expect(described_class.coerce(hashie)["accept"]).to eq "json" end diff --git a/spec/lib/http/options/features_spec.rb b/spec/lib/http/options/features_spec.rb index 488d0634..01916455 100644 --- a/spec/lib/http/options/features_spec.rb +++ b/spec/lib/http/options/features_spec.rb @@ -16,7 +16,7 @@ end it "accepts feature name with its options in array" do - opts2 = opts.with_features([{:auto_deflate => {:method => :deflate}}]) + opts2 = opts.with_features([{auto_deflate: {method: :deflate}}]) expect(opts.features).to be_empty expect(opts2.features.keys).to eq([:auto_deflate]) expect(opts2.features[:auto_deflate]). diff --git a/spec/lib/http/options/form_spec.rb b/spec/lib/http/options/form_spec.rb index 46332279..23ed1a38 100644 --- a/spec/lib/http/options/form_spec.rb +++ b/spec/lib/http/options/form_spec.rb @@ -8,8 +8,8 @@ end it "may be specified with with_form_data" do - opts2 = opts.with_form(:foo => 42) + opts2 = opts.with_form(foo: 42) expect(opts.form).to be_nil - expect(opts2.form).to eq(:foo => 42) + expect(opts2.form).to eq(foo: 42) end end diff --git a/spec/lib/http/options/headers_spec.rb b/spec/lib/http/options/headers_spec.rb index d7bb7fc6..2ee145b2 100644 --- a/spec/lib/http/options/headers_spec.rb +++ b/spec/lib/http/options/headers_spec.rb @@ -8,14 +8,14 @@ end it "may be specified with with_headers" do - opts2 = opts.with_headers(:accept => "json") + opts2 = opts.with_headers(accept: "json") expect(opts.headers).to be_empty expect(opts2.headers).to eq([%w[Accept json]]) end it "accepts any object that respond to :to_hash" do x = if RUBY_VERSION >= "3.2.0" - Data.define(:to_hash).new(:to_hash => { "accept" => "json" }) + Data.define(:to_hash).new(to_hash: { "accept" => "json" }) else Struct.new(:to_hash).new({ "accept" => "json" }) end diff --git a/spec/lib/http/options/json_spec.rb b/spec/lib/http/options/json_spec.rb index 6afd57da..139a16d0 100644 --- a/spec/lib/http/options/json_spec.rb +++ b/spec/lib/http/options/json_spec.rb @@ -8,8 +8,8 @@ end it "may be specified with with_json data" do - opts2 = opts.with_json(:foo => 42) + opts2 = opts.with_json(foo: 42) expect(opts.json).to be_nil - expect(opts2.json).to eq(:foo => 42) + expect(opts2.json).to eq(foo: 42) end end diff --git a/spec/lib/http/options/merge_spec.rb b/spec/lib/http/options/merge_spec.rb index ea09a443..69830306 100644 --- a/spec/lib/http/options/merge_spec.rb +++ b/spec/lib/http/options/merge_spec.rb @@ -5,12 +5,12 @@ it "supports a Hash" do old_response = opts.response - expect(opts.merge(:response => :body).response).to eq(:body) + expect(opts.merge(response: :body).response).to eq(:body) expect(opts.response).to eq(old_response) end it "supports another Options" do - merged = opts.merge(HTTP::Options.new(:response => :body)) + merged = opts.merge(HTTP::Options.new(response: :body)) expect(merged.response).to eq(:body) end @@ -18,51 +18,51 @@ # FIXME: yuck :( foo = HTTP::Options.new( - :response => :body, - :params => {:baz => "bar"}, - :form => {:foo => "foo"}, - :body => "body-foo", - :json => {:foo => "foo"}, - :headers => {:accept => "json", :foo => "foo"}, - :proxy => {}, - :features => {} + response: :body, + params: {baz: "bar"}, + form: {foo: "foo"}, + body: "body-foo", + json: {foo: "foo"}, + headers: {accept: "json", foo: "foo"}, + proxy: {}, + features: {} ) bar = HTTP::Options.new( - :response => :parsed_body, - :persistent => "https://www.googe.com", - :params => {:plop => "plip"}, - :form => {:bar => "bar"}, - :body => "body-bar", - :json => {:bar => "bar"}, - :keep_alive_timeout => 10, - :headers => {:accept => "xml", :bar => "bar"}, - :timeout_options => {:foo => :bar}, - :ssl => {:foo => "bar"}, - :proxy => {:proxy_address => "127.0.0.1", :proxy_port => 8080} + response: :parsed_body, + persistent: "https://www.googe.com", + params: {plop: "plip"}, + form: {bar: "bar"}, + body: "body-bar", + json: {bar: "bar"}, + keep_alive_timeout: 10, + headers: {accept: "xml", bar: "bar"}, + timeout_options: {foo: :bar}, + ssl: {foo: "bar"}, + proxy: {proxy_address: "127.0.0.1", proxy_port: 8080} ) expect(foo.merge(bar).to_hash).to eq( - :response => :parsed_body, - :timeout_class => described_class.default_timeout_class, - :timeout_options => {:foo => :bar}, - :params => {:plop => "plip"}, - :form => {:bar => "bar"}, - :body => "body-bar", - :json => {:bar => "bar"}, - :persistent => "https://www.googe.com", - :keep_alive_timeout => 10, - :ssl => {:foo => "bar"}, - :headers => {"Foo" => "foo", "Accept" => "xml", "Bar" => "bar"}, - :proxy => {:proxy_address => "127.0.0.1", :proxy_port => 8080}, - :follow => nil, - :socket_class => described_class.default_socket_class, - :nodelay => false, - :ssl_socket_class => described_class.default_ssl_socket_class, - :ssl_context => nil, - :cookies => {}, - :encoding => nil, - :features => {} + response: :parsed_body, + timeout_class: described_class.default_timeout_class, + timeout_options: {foo: :bar}, + params: {plop: "plip"}, + form: {bar: "bar"}, + body: "body-bar", + json: {bar: "bar"}, + persistent: "https://www.googe.com", + keep_alive_timeout: 10, + ssl: {foo: "bar"}, + headers: {"Foo" => "foo", "Accept" => "xml", "Bar" => "bar"}, + proxy: {proxy_address: "127.0.0.1", proxy_port: 8080}, + follow: nil, + socket_class: described_class.default_socket_class, + nodelay: false, + ssl_socket_class: described_class.default_ssl_socket_class, + ssl_context: nil, + cookies: {}, + encoding: nil, + features: {} ) end end diff --git a/spec/lib/http/options/new_spec.rb b/spec/lib/http/options/new_spec.rb index 4e4df666..eba08751 100644 --- a/spec/lib/http/options/new_spec.rb +++ b/spec/lib/http/options/new_spec.rb @@ -8,23 +8,23 @@ context "with a Hash" do it "coerces :response correctly" do - opts = HTTP::Options.new(:response => :object) + opts = HTTP::Options.new(response: :object) expect(opts.response).to eq(:object) end it "coerces :headers correctly" do - opts = HTTP::Options.new(:headers => {:accept => "json"}) + opts = HTTP::Options.new(headers: {accept: "json"}) expect(opts.headers).to eq([%w[Accept json]]) end it "coerces :proxy correctly" do - opts = HTTP::Options.new(:proxy => {:proxy_address => "127.0.0.1", :proxy_port => 8080}) - expect(opts.proxy).to eq(:proxy_address => "127.0.0.1", :proxy_port => 8080) + opts = HTTP::Options.new(proxy: {proxy_address: "127.0.0.1", proxy_port: 8080}) + expect(opts.proxy).to eq(proxy_address: "127.0.0.1", proxy_port: 8080) end it "coerces :form correctly" do - opts = HTTP::Options.new(:form => {:foo => 42}) - expect(opts.form).to eq(:foo => 42) + opts = HTTP::Options.new(form: {foo: 42}) + expect(opts.form).to eq(foo: 42) end end end diff --git a/spec/lib/http/options/proxy_spec.rb b/spec/lib/http/options/proxy_spec.rb index 6a47a496..89eff062 100644 --- a/spec/lib/http/options/proxy_spec.rb +++ b/spec/lib/http/options/proxy_spec.rb @@ -8,13 +8,13 @@ end it "may be specified with with_proxy" do - opts2 = opts.with_proxy(:proxy_address => "127.0.0.1", :proxy_port => 8080) + opts2 = opts.with_proxy(proxy_address: "127.0.0.1", proxy_port: 8080) expect(opts.proxy).to eq({}) - expect(opts2.proxy).to eq(:proxy_address => "127.0.0.1", :proxy_port => 8080) + expect(opts2.proxy).to eq(proxy_address: "127.0.0.1", proxy_port: 8080) end it "accepts proxy address, port, username, and password" do - opts2 = opts.with_proxy(:proxy_address => "127.0.0.1", :proxy_port => 8080, :proxy_username => "username", :proxy_password => "password") - expect(opts2.proxy).to eq(:proxy_address => "127.0.0.1", :proxy_port => 8080, :proxy_username => "username", :proxy_password => "password") + opts2 = opts.with_proxy(proxy_address: "127.0.0.1", proxy_port: 8080, proxy_username: "username", proxy_password: "password") + expect(opts2.proxy).to eq(proxy_address: "127.0.0.1", proxy_port: 8080, proxy_username: "username", proxy_password: "password") end end diff --git a/spec/lib/http/options_spec.rb b/spec/lib/http/options_spec.rb index 7c81df0c..8e6512c9 100644 --- a/spec/lib/http/options_spec.rb +++ b/spec/lib/http/options_spec.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true RSpec.describe HTTP::Options do - subject { described_class.new(:response => :body) } + subject { described_class.new(response: :body) } it "has reader methods for attributes" do expect(subject.response).to eq(:body) diff --git a/spec/lib/http/redirector_spec.rb b/spec/lib/http/redirector_spec.rb index f61170fb..e182129a 100644 --- a/spec/lib/http/redirector_spec.rb +++ b/spec/lib/http/redirector_spec.rb @@ -3,11 +3,11 @@ RSpec.describe HTTP::Redirector do def simple_response(status, body = "", headers = {}) HTTP::Response.new( - :status => status, - :version => "1.1", - :headers => headers, - :body => body, - :request => HTTP::Request.new(:verb => :get, :uri => "http://example.com") + status: status, + version: "1.1", + headers: headers, + body: body, + request: HTTP::Request.new(verb: :get, uri: "http://example.com") ) end @@ -44,7 +44,7 @@ def redirect_response(status, location, set_cookie = {}) let(:redirector) { described_class.new options } it "fails with TooManyRedirectsError if max hops reached" do - req = HTTP::Request.new :verb => :head, :uri => "http://example.com" + req = HTTP::Request.new verb: :head, uri: "http://example.com" res = proc { |prev_req| redirect_response(301, "#{prev_req.uri}/1") } expect { redirector.perform(req, res.call(req), &res) }. @@ -52,7 +52,7 @@ def redirect_response(status, location, set_cookie = {}) end it "fails with EndlessRedirectError if endless loop detected" do - req = HTTP::Request.new :verb => :head, :uri => "http://example.com" + req = HTTP::Request.new verb: :head, uri: "http://example.com" res = redirect_response(301, req.uri) expect { redirector.perform(req, res) { res } }. @@ -60,7 +60,7 @@ def redirect_response(status, location, set_cookie = {}) end it "fails with StateError if there were no Location header" do - req = HTTP::Request.new :verb => :head, :uri => "http://example.com" + req = HTTP::Request.new verb: :head, uri: "http://example.com" res = simple_response(301) expect { |b| redirector.perform(req, res, &b) }. @@ -68,7 +68,7 @@ def redirect_response(status, location, set_cookie = {}) end it "returns first non-redirect response" do - req = HTTP::Request.new :verb => :head, :uri => "http://example.com" + req = HTTP::Request.new verb: :head, uri: "http://example.com" hops = [ redirect_response(301, "http://example.com/1"), redirect_response(301, "http://example.com/2"), @@ -83,7 +83,7 @@ def redirect_response(status, location, set_cookie = {}) end it "concatenates multiple Location headers" do - req = HTTP::Request.new :verb => :head, :uri => "http://example.com" + req = HTTP::Request.new verb: :head, uri: "http://example.com" headers = HTTP::Headers.new %w[http://example.com /123].each { |loc| headers.add("Location", loc) } @@ -96,7 +96,7 @@ def redirect_response(status, location, set_cookie = {}) end it "returns cookies in response" do - req = HTTP::Request.new :verb => :head, :uri => "http://example.com" + req = HTTP::Request.new verb: :head, uri: "http://example.com" hops = [ redirect_response(301, "http://example.com/1", {"foo" => "42"}), redirect_response(301, "http://example.com/2", {"bar" => "53", "deleted" => "foo"}), @@ -126,7 +126,7 @@ def redirect_response(status, location, set_cookie = {}) end it "returns original cookies in response" do - req = HTTP::Request.new :verb => :head, :uri => "http://example.com" + req = HTTP::Request.new verb: :head, uri: "http://example.com" req.headers.set("Cookie", "foo=42; deleted=baz") hops = [ redirect_response(301, "http://example.com/1", {"bar" => "64", "deleted" => ""}), @@ -153,7 +153,7 @@ def redirect_response(status, location, set_cookie = {}) context "with on_redirect callback" do let(:options) do { - :on_redirect => proc do |response, location| + on_redirect: proc do |response, location| @redirect_response = response @redirect_location = location end @@ -161,7 +161,7 @@ def redirect_response(status, location, set_cookie = {}) end it "calls on_redirect" do - req = HTTP::Request.new :verb => :head, :uri => "http://example.com" + req = HTTP::Request.new verb: :head, uri: "http://example.com" hops = [ redirect_response(301, "http://example.com/1"), redirect_response(301, "http://example.com/2"), @@ -178,10 +178,10 @@ def redirect_response(status, location, set_cookie = {}) context "following 300 redirect" do context "with strict mode" do - let(:options) { {:strict => true} } + let(:options) { {strict: true} } it "follows with original verb if it's safe" do - req = HTTP::Request.new :verb => :head, :uri => "http://example.com" + req = HTTP::Request.new verb: :head, uri: "http://example.com" res = redirect_response 300, "http://example.com/1" redirector.perform(req, res) do |prev_req, _| @@ -191,7 +191,7 @@ def redirect_response(status, location, set_cookie = {}) end it "raises StateError if original request was PUT" do - req = HTTP::Request.new :verb => :put, :uri => "http://example.com" + req = HTTP::Request.new verb: :put, uri: "http://example.com" res = redirect_response 300, "http://example.com/1" expect { redirector.perform(req, res) { simple_response 200 } }. @@ -199,7 +199,7 @@ def redirect_response(status, location, set_cookie = {}) end it "raises StateError if original request was POST" do - req = HTTP::Request.new :verb => :post, :uri => "http://example.com" + req = HTTP::Request.new verb: :post, uri: "http://example.com" res = redirect_response 300, "http://example.com/1" expect { redirector.perform(req, res) { simple_response 200 } }. @@ -207,7 +207,7 @@ def redirect_response(status, location, set_cookie = {}) end it "raises StateError if original request was DELETE" do - req = HTTP::Request.new :verb => :delete, :uri => "http://example.com" + req = HTTP::Request.new verb: :delete, uri: "http://example.com" res = redirect_response 300, "http://example.com/1" expect { redirector.perform(req, res) { simple_response 200 } }. @@ -216,10 +216,10 @@ def redirect_response(status, location, set_cookie = {}) end context "with non-strict mode" do - let(:options) { {:strict => false} } + let(:options) { {strict: false} } it "follows with original verb if it's safe" do - req = HTTP::Request.new :verb => :head, :uri => "http://example.com" + req = HTTP::Request.new verb: :head, uri: "http://example.com" res = redirect_response 300, "http://example.com/1" redirector.perform(req, res) do |prev_req, _| @@ -229,7 +229,7 @@ def redirect_response(status, location, set_cookie = {}) end it "follows with GET if original request was PUT" do - req = HTTP::Request.new :verb => :put, :uri => "http://example.com" + req = HTTP::Request.new verb: :put, uri: "http://example.com" res = redirect_response 300, "http://example.com/1" redirector.perform(req, res) do |prev_req, _| @@ -239,7 +239,7 @@ def redirect_response(status, location, set_cookie = {}) end it "follows with GET if original request was POST" do - req = HTTP::Request.new :verb => :post, :uri => "http://example.com" + req = HTTP::Request.new verb: :post, uri: "http://example.com" res = redirect_response 300, "http://example.com/1" redirector.perform(req, res) do |prev_req, _| @@ -249,7 +249,7 @@ def redirect_response(status, location, set_cookie = {}) end it "follows with GET if original request was DELETE" do - req = HTTP::Request.new :verb => :delete, :uri => "http://example.com" + req = HTTP::Request.new verb: :delete, uri: "http://example.com" res = redirect_response 300, "http://example.com/1" redirector.perform(req, res) do |prev_req, _| @@ -262,10 +262,10 @@ def redirect_response(status, location, set_cookie = {}) context "following 301 redirect" do context "with strict mode" do - let(:options) { {:strict => true} } + let(:options) { {strict: true} } it "follows with original verb if it's safe" do - req = HTTP::Request.new :verb => :head, :uri => "http://example.com" + req = HTTP::Request.new verb: :head, uri: "http://example.com" res = redirect_response 301, "http://example.com/1" redirector.perform(req, res) do |prev_req, _| @@ -275,7 +275,7 @@ def redirect_response(status, location, set_cookie = {}) end it "raises StateError if original request was PUT" do - req = HTTP::Request.new :verb => :put, :uri => "http://example.com" + req = HTTP::Request.new verb: :put, uri: "http://example.com" res = redirect_response 301, "http://example.com/1" expect { redirector.perform(req, res) { simple_response 200 } }. @@ -283,7 +283,7 @@ def redirect_response(status, location, set_cookie = {}) end it "raises StateError if original request was POST" do - req = HTTP::Request.new :verb => :post, :uri => "http://example.com" + req = HTTP::Request.new verb: :post, uri: "http://example.com" res = redirect_response 301, "http://example.com/1" expect { redirector.perform(req, res) { simple_response 200 } }. @@ -291,7 +291,7 @@ def redirect_response(status, location, set_cookie = {}) end it "raises StateError if original request was DELETE" do - req = HTTP::Request.new :verb => :delete, :uri => "http://example.com" + req = HTTP::Request.new verb: :delete, uri: "http://example.com" res = redirect_response 301, "http://example.com/1" expect { redirector.perform(req, res) { simple_response 200 } }. @@ -300,10 +300,10 @@ def redirect_response(status, location, set_cookie = {}) end context "with non-strict mode" do - let(:options) { {:strict => false} } + let(:options) { {strict: false} } it "follows with original verb if it's safe" do - req = HTTP::Request.new :verb => :head, :uri => "http://example.com" + req = HTTP::Request.new verb: :head, uri: "http://example.com" res = redirect_response 301, "http://example.com/1" redirector.perform(req, res) do |prev_req, _| @@ -313,7 +313,7 @@ def redirect_response(status, location, set_cookie = {}) end it "follows with GET if original request was PUT" do - req = HTTP::Request.new :verb => :put, :uri => "http://example.com" + req = HTTP::Request.new verb: :put, uri: "http://example.com" res = redirect_response 301, "http://example.com/1" redirector.perform(req, res) do |prev_req, _| @@ -323,7 +323,7 @@ def redirect_response(status, location, set_cookie = {}) end it "follows with GET if original request was POST" do - req = HTTP::Request.new :verb => :post, :uri => "http://example.com" + req = HTTP::Request.new verb: :post, uri: "http://example.com" res = redirect_response 301, "http://example.com/1" redirector.perform(req, res) do |prev_req, _| @@ -333,7 +333,7 @@ def redirect_response(status, location, set_cookie = {}) end it "follows with GET if original request was DELETE" do - req = HTTP::Request.new :verb => :delete, :uri => "http://example.com" + req = HTTP::Request.new verb: :delete, uri: "http://example.com" res = redirect_response 301, "http://example.com/1" redirector.perform(req, res) do |prev_req, _| @@ -346,10 +346,10 @@ def redirect_response(status, location, set_cookie = {}) context "following 302 redirect" do context "with strict mode" do - let(:options) { {:strict => true} } + let(:options) { {strict: true} } it "follows with original verb if it's safe" do - req = HTTP::Request.new :verb => :head, :uri => "http://example.com" + req = HTTP::Request.new verb: :head, uri: "http://example.com" res = redirect_response 302, "http://example.com/1" redirector.perform(req, res) do |prev_req, _| @@ -359,7 +359,7 @@ def redirect_response(status, location, set_cookie = {}) end it "raises StateError if original request was PUT" do - req = HTTP::Request.new :verb => :put, :uri => "http://example.com" + req = HTTP::Request.new verb: :put, uri: "http://example.com" res = redirect_response 302, "http://example.com/1" expect { redirector.perform(req, res) { simple_response 200 } }. @@ -367,7 +367,7 @@ def redirect_response(status, location, set_cookie = {}) end it "raises StateError if original request was POST" do - req = HTTP::Request.new :verb => :post, :uri => "http://example.com" + req = HTTP::Request.new verb: :post, uri: "http://example.com" res = redirect_response 302, "http://example.com/1" expect { redirector.perform(req, res) { simple_response 200 } }. @@ -375,7 +375,7 @@ def redirect_response(status, location, set_cookie = {}) end it "raises StateError if original request was DELETE" do - req = HTTP::Request.new :verb => :delete, :uri => "http://example.com" + req = HTTP::Request.new verb: :delete, uri: "http://example.com" res = redirect_response 302, "http://example.com/1" expect { redirector.perform(req, res) { simple_response 200 } }. @@ -384,10 +384,10 @@ def redirect_response(status, location, set_cookie = {}) end context "with non-strict mode" do - let(:options) { {:strict => false} } + let(:options) { {strict: false} } it "follows with original verb if it's safe" do - req = HTTP::Request.new :verb => :head, :uri => "http://example.com" + req = HTTP::Request.new verb: :head, uri: "http://example.com" res = redirect_response 302, "http://example.com/1" redirector.perform(req, res) do |prev_req, _| @@ -397,7 +397,7 @@ def redirect_response(status, location, set_cookie = {}) end it "follows with GET if original request was PUT" do - req = HTTP::Request.new :verb => :put, :uri => "http://example.com" + req = HTTP::Request.new verb: :put, uri: "http://example.com" res = redirect_response 302, "http://example.com/1" redirector.perform(req, res) do |prev_req, _| @@ -407,7 +407,7 @@ def redirect_response(status, location, set_cookie = {}) end it "follows with GET if original request was POST" do - req = HTTP::Request.new :verb => :post, :uri => "http://example.com" + req = HTTP::Request.new verb: :post, uri: "http://example.com" res = redirect_response 302, "http://example.com/1" redirector.perform(req, res) do |prev_req, _| @@ -417,7 +417,7 @@ def redirect_response(status, location, set_cookie = {}) end it "follows with GET if original request was DELETE" do - req = HTTP::Request.new :verb => :delete, :uri => "http://example.com" + req = HTTP::Request.new verb: :delete, uri: "http://example.com" res = redirect_response 302, "http://example.com/1" redirector.perform(req, res) do |prev_req, _| @@ -430,7 +430,7 @@ def redirect_response(status, location, set_cookie = {}) context "following 303 redirect" do it "follows with HEAD if original request was HEAD" do - req = HTTP::Request.new :verb => :head, :uri => "http://example.com" + req = HTTP::Request.new verb: :head, uri: "http://example.com" res = redirect_response 303, "http://example.com/1" redirector.perform(req, res) do |prev_req, _| @@ -440,7 +440,7 @@ def redirect_response(status, location, set_cookie = {}) end it "follows with GET if original request was GET" do - req = HTTP::Request.new :verb => :get, :uri => "http://example.com" + req = HTTP::Request.new verb: :get, uri: "http://example.com" res = redirect_response 303, "http://example.com/1" redirector.perform(req, res) do |prev_req, _| @@ -450,7 +450,7 @@ def redirect_response(status, location, set_cookie = {}) end it "follows with GET if original request was neither GET nor HEAD" do - req = HTTP::Request.new :verb => :post, :uri => "http://example.com" + req = HTTP::Request.new verb: :post, uri: "http://example.com" res = redirect_response 303, "http://example.com/1" redirector.perform(req, res) do |prev_req, _| @@ -462,7 +462,7 @@ def redirect_response(status, location, set_cookie = {}) context "following 307 redirect" do it "follows with original request's verb" do - req = HTTP::Request.new :verb => :post, :uri => "http://example.com" + req = HTTP::Request.new verb: :post, uri: "http://example.com" res = redirect_response 307, "http://example.com/1" redirector.perform(req, res) do |prev_req, _| @@ -474,7 +474,7 @@ def redirect_response(status, location, set_cookie = {}) context "following 308 redirect" do it "follows with original request's verb" do - req = HTTP::Request.new :verb => :post, :uri => "http://example.com" + req = HTTP::Request.new verb: :post, uri: "http://example.com" res = redirect_response 308, "http://example.com/1" redirector.perform(req, res) do |prev_req, _| @@ -485,14 +485,14 @@ def redirect_response(status, location, set_cookie = {}) end describe "changing verbs during redirects" do - let(:options) { {:strict => false} } + let(:options) { {strict: false} } let(:post_body) { HTTP::Request::Body.new("i might be way longer in real life") } let(:cookie) { "dont=eat my cookies" } def a_dangerous_request(verb) HTTP::Request.new( - :verb => verb, :uri => "http://example.com", - :body => post_body, :headers => { + verb: verb, uri: "http://example.com", + body: post_body, headers: { "Content-Type" => "meme", "Cookie" => cookie } diff --git a/spec/lib/http/request_spec.rb b/spec/lib/http/request_spec.rb index 75226f94..558b54e7 100644 --- a/spec/lib/http/request_spec.rb +++ b/spec/lib/http/request_spec.rb @@ -4,15 +4,15 @@ RSpec.describe HTTP::Request do subject :request do HTTP::Request.new( - :verb => :get, - :uri => request_uri, - :headers => headers, - :proxy => proxy + verb: :get, + uri: request_uri, + headers: headers, + proxy: proxy ) end let(:proxy) { {} } - let(:headers) { {:accept => "text/html"} } + let(:headers) { {accept: "text/html"} } let(:request_uri) { "http://example.com/foo?bar=baz" } it "includes HTTP::Headers::Mixin" do @@ -20,7 +20,7 @@ end it "requires URI to have scheme part" do - expect { HTTP::Request.new(:verb => :get, :uri => "example.com/") }.to \ + expect { HTTP::Request.new(verb: :get, uri: "example.com/") }.to \ raise_error(HTTP::Request::UnsupportedSchemeError) end @@ -73,17 +73,17 @@ describe "#redirect" do subject(:redirected) { request.redirect "http://blog.example.com/" } - let(:headers) { {:accept => "text/html"} } - let(:proxy) { {:proxy_username => "douglas", :proxy_password => "adams"} } + let(:headers) { {accept: "text/html"} } + let(:proxy) { {proxy_username: "douglas", proxy_password: "adams"} } let(:body) { "The Ultimate Question" } let :request do HTTP::Request.new( - :verb => :post, - :uri => "http://example.com/", - :headers => headers, - :proxy => proxy, - :body => body + verb: :post, + uri: "http://example.com/", + headers: headers, + proxy: proxy, + body: body ) end @@ -141,11 +141,11 @@ context "with original URI having non-standard port" do let :request do HTTP::Request.new( - :verb => :post, - :uri => "http://example.com:8080/", - :headers => headers, - :proxy => proxy, - :body => body + verb: :post, + uri: "http://example.com:8080/", + headers: headers, + proxy: proxy, + body: body ) end @@ -169,11 +169,11 @@ context "with original URI having non-standard port" do let :request do HTTP::Request.new( - :verb => :post, - :uri => "http://example.com:8080/", - :headers => headers, - :proxy => proxy, - :body => body + verb: :post, + uri: "http://example.com:8080/", + headers: headers, + proxy: proxy, + body: body ) end @@ -219,7 +219,7 @@ end context "with proxy" do - let(:proxy) { {:user => "user", :pass => "pass"} } + let(:proxy) { {user: "user", pass: "pass"} } it { is_expected.to eq "GET http://example.com/foo?bar=baz HTTP/1.1" } diff --git a/spec/lib/http/response/body_spec.rb b/spec/lib/http/response/body_spec.rb index e976c94e..6bcc1673 100644 --- a/spec/lib/http/response/body_spec.rb +++ b/spec/lib/http/response/body_spec.rb @@ -1,9 +1,9 @@ # frozen_string_literal: true RSpec.describe HTTP::Response::Body do - subject(:body) { described_class.new(connection, :encoding => Encoding::UTF_8) } + subject(:body) { described_class.new(connection, encoding: Encoding::UTF_8) } - let(:connection) { double(:sequence_id => 0) } + let(:connection) { double(sequence_id: 0) } let(:chunks) { ["Hello, ", "World!"] } before do @@ -45,12 +45,12 @@ it "returns content in specified encoding" do body = described_class.new(connection) expect(connection).to receive(:readpartial). - and_return(String.new("content", :encoding => Encoding::UTF_8)) + and_return(String.new("content", encoding: Encoding::UTF_8)) expect(body.readpartial.encoding).to eq Encoding::BINARY - body = described_class.new(connection, :encoding => Encoding::UTF_8) + body = described_class.new(connection, encoding: Encoding::UTF_8) expect(connection).to receive(:readpartial). - and_return(String.new("content", :encoding => Encoding::BINARY)) + and_return(String.new("content", encoding: Encoding::BINARY)) expect(body.readpartial.encoding).to eq Encoding::UTF_8 end end @@ -58,7 +58,7 @@ context "when body is gzipped" do subject(:body) do inflater = HTTP::Response::Inflater.new(connection) - described_class.new(inflater, :encoding => Encoding::UTF_8) + described_class.new(inflater, encoding: Encoding::UTF_8) end let(:chunks) do diff --git a/spec/lib/http/response/status_spec.rb b/spec/lib/http/response/status_spec.rb index 6aded9af..d0a6b75e 100644 --- a/spec/lib/http/response/status_spec.rb +++ b/spec/lib/http/response/status_spec.rb @@ -7,7 +7,7 @@ end it "accepts any object that responds to #to_i" do - expect { described_class.new double :to_i => 200 }.not_to raise_error + expect { described_class.new double to_i: 200 }.not_to raise_error end end diff --git a/spec/lib/http/response_spec.rb b/spec/lib/http/response_spec.rb index d5846792..322e25b3 100644 --- a/spec/lib/http/response_spec.rb +++ b/spec/lib/http/response_spec.rb @@ -3,18 +3,18 @@ RSpec.describe HTTP::Response do subject(:response) do HTTP::Response.new( - :status => 200, - :version => "1.1", - :headers => headers, - :body => body, - :request => request + status: 200, + version: "1.1", + headers: headers, + body: body, + request: request ) end let(:body) { "Hello world!" } let(:uri) { "http://example.com/" } let(:headers) { {} } - let(:request) { HTTP::Request.new(:verb => :get, :uri => uri) } + let(:request) { HTTP::Request.new(verb: :get, uri: uri) } it "includes HTTP::Headers::Mixin" do expect(described_class).to include HTTP::Headers::Mixin @@ -128,7 +128,7 @@ end describe "#flush" do - let(:body) { double :to_s => "" } + let(:body) { double to_s: "" } it "returns response self-reference" do expect(response.flush).to be response @@ -143,8 +143,8 @@ describe "#inspect" do subject { response.inspect } - let(:headers) { {:content_type => "text/plain"} } - let(:body) { double :to_s => "foobar" } + let(:headers) { {content_type: "text/plain"} } + let(:body) { double to_s: "foobar" } it { is_expected.to eq '#"text/plain"}>' } end @@ -173,10 +173,10 @@ describe "#connection" do subject(:response) do HTTP::Response.new( - :version => "1.1", - :status => 200, - :connection => connection, - :request => request + version: "1.1", + status: 200, + connection: connection, + request: request ) end @@ -203,11 +203,11 @@ context "with no :verb" do subject(:response) do HTTP::Response.new( - :status => 200, - :version => "1.1", - :headers => headers, - :body => body, - :uri => uri + status: 200, + version: "1.1", + headers: headers, + body: body, + uri: uri ) end @@ -223,12 +223,12 @@ context "with both a :request and :uri" do subject(:response) do HTTP::Response.new( - :status => 200, - :version => "1.1", - :headers => headers, - :body => body, - :uri => uri, - :request => request + status: 200, + version: "1.1", + headers: headers, + body: body, + uri: uri, + request: request ) end @@ -241,15 +241,15 @@ describe "#body" do subject(:response) do HTTP::Response.new( - :status => 200, - :version => "1.1", - :headers => headers, - :request => request, - :connection => connection + status: 200, + version: "1.1", + headers: headers, + request: request, + connection: connection ) end - let(:connection) { double(:sequence_id => 0) } + let(:connection) { double(sequence_id: 0) } let(:chunks) { ["Hello, ", "World!"] } before do diff --git a/spec/lib/http_spec.rb b/spec/lib/http_spec.rb index 4bf848e8..05fd6efd 100644 --- a/spec/lib/http_spec.rb +++ b/spec/lib/http_spec.rb @@ -8,10 +8,10 @@ RSpec.describe HTTP do run_server(:dummy) { DummyServer.new } - run_server(:dummy_ssl) { DummyServer.new(:ssl => true) } + run_server(:dummy_ssl) { DummyServer.new(ssl: true) } let(:ssl_client) do - HTTP::Client.new :ssl_context => SSLHelper.client_context + HTTP::Client.new ssl_context: SSLHelper.client_context end context "getting resources" do @@ -29,14 +29,14 @@ context "with query string parameters" do it "is easy" do - response = HTTP.get "#{dummy.endpoint}/params", :params => {:foo => "bar"} + response = HTTP.get "#{dummy.endpoint}/params", params: {foo: "bar"} expect(response.to_s).to match(/Params!/) end end context "with query string parameters in the URI and opts hash" do it "includes both" do - response = HTTP.get "#{dummy.endpoint}/multiple-params?foo=bar", :params => {:baz => "quux"} + response = HTTP.get "#{dummy.endpoint}/multiple-params?foo=bar", params: {baz: "quux"} expect(response.to_s).to match(/More Params!/) end end @@ -57,12 +57,12 @@ context "with a large request body" do let(:request_body) { "“" * 1_000_000 } # use multi-byte character - [:null, 6, {:read => 2, :write => 2, :connect => 2}].each do |timeout| + [:null, 6, {read: 2, write: 2, connect: 2}].each do |timeout| context "with `.timeout(#{timeout.inspect})`" do let(:client) { HTTP.timeout(timeout) } it "writes the whole body" do - response = client.post "#{dummy.endpoint}/echo-body", :body => request_body + response = client.post "#{dummy.endpoint}/echo-body", body: request_body expect(response.body.to_s).to eq(request_body.b) expect(response.headers["Content-Length"].to_i).to eq request_body.bytesize @@ -154,7 +154,7 @@ context "posting forms to resources" do it "is easy" do - response = HTTP.post "#{dummy.endpoint}/form", :form => {:example => "testing-form"} + response = HTTP.post "#{dummy.endpoint}/form", form: {example: "testing-form"} expect(response.to_s).to eq("passed :)") end end @@ -175,7 +175,7 @@ context "with encoding option" do it "respects option" do - response = HTTP.get "#{dummy.endpoint}/iso-8859-1", :encoding => Encoding::BINARY + response = HTTP.get "#{dummy.endpoint}/iso-8859-1", encoding: Encoding::BINARY expect(response.to_s.encoding).to eq(Encoding::BINARY) end end @@ -183,7 +183,7 @@ context "passing a string encoding type" do it "finds encoding" do - response = HTTP.get dummy.endpoint, :encoding => "ascii" + response = HTTP.get dummy.endpoint, encoding: "ascii" expect(response.to_s.encoding).to eq(Encoding::ASCII) end end @@ -197,7 +197,7 @@ context "posting with an explicit body" do it "is easy" do - response = HTTP.post "#{dummy.endpoint}/body", :body => "testing-body" + response = HTTP.post "#{dummy.endpoint}/body", body: "testing-body" expect(response.to_s).to eq("passed :)") end end @@ -229,7 +229,7 @@ end it "accepts any #to_s object" do - client = HTTP.auth double :to_s => "abc" + client = HTTP.auth double to_s: "abc" expect(client.default_options.headers[:authorization]).to eq "abc" end end @@ -240,15 +240,15 @@ end it "fails when :pass is not given" do - expect { HTTP.basic_auth :user => "[USER]" }.to raise_error(KeyError) + expect { HTTP.basic_auth user: "[USER]" }.to raise_error(KeyError) end it "fails when :user is not given" do - expect { HTTP.basic_auth :pass => "[PASS]" }.to raise_error(KeyError) + expect { HTTP.basic_auth pass: "[PASS]" }.to raise_error(KeyError) end it "sets Authorization header with proper BasicAuth value" do - client = HTTP.basic_auth :user => "foo", :pass => "bar" + client = HTTP.basic_auth user: "foo", pass: "bar" expect(client.default_options.headers[:authorization]). to match(%r{^Basic [A-Za-z0-9+/]+=*$}) end @@ -278,7 +278,7 @@ end context "with timeout specified" do - subject(:client) { HTTP.persistent host, :timeout => 100 } + subject(:client) { HTTP.persistent host, timeout: 100 } it "sets keep_alive_timeout" do options = client.default_options @@ -298,7 +298,7 @@ end context "specifying per operation timeouts" do - subject(:client) { HTTP.timeout :read => 123 } + subject(:client) { HTTP.timeout read: 123 } it "sets timeout_class to PerOperation" do expect(client.default_options.timeout_class). @@ -307,14 +307,14 @@ it "sets given timeout options" do expect(client.default_options.timeout_options). - to eq :read_timeout => 123 + to eq read_timeout: 123 end end context "specifying per operation timeouts as frozen hash" do subject(:client) { HTTP.timeout(frozen_options) } - let(:frozen_options) { {:read => 123}.freeze } + let(:frozen_options) { {read: 123}.freeze } it "does not raise an error" do expect { client }.not_to raise_error @@ -331,7 +331,7 @@ it "sets given timeout option" do expect(client.default_options.timeout_options). - to eq :global_timeout => 123 + to eq global_timeout: 123 end end end @@ -340,7 +340,7 @@ let(:endpoint) { "#{dummy.endpoint}/cookies" } it "passes correct `Cookie` header" do - expect(HTTP.cookies(:abc => :def).get(endpoint).to_s). + expect(HTTP.cookies(abc: :def).get(endpoint).to_s). to eq "abc: def" end @@ -353,20 +353,20 @@ it "properly merges cookies" do res = HTTP.get(endpoint).flush - client = HTTP.cookies(:foo => 123, :bar => 321).cookies(res.cookies) + client = HTTP.cookies(foo: 123, bar: 321).cookies(res.cookies) expect(client.get(endpoint).to_s).to eq "foo: bar\nbar: 321" end it "properly merges Cookie headers and cookies" do - client = HTTP.headers("Cookie" => "foo=bar").cookies(:baz => :moo) + client = HTTP.headers("Cookie" => "foo=bar").cookies(baz: :moo) expect(client.get(endpoint).to_s).to eq "foo: bar\nbaz: moo" end end describe ".nodelay" do before do - HTTP.default_options = {:socket_class => socket_spy_class} + HTTP.default_options = {socket_class: socket_spy_class} end after do @@ -404,16 +404,16 @@ def setsockopt(*args) it "sends gzipped body" do client = HTTP.use :auto_deflate body = "Hello!" - response = client.post("#{dummy.endpoint}/echo-body", :body => body) + response = client.post("#{dummy.endpoint}/echo-body", body: body) encoded = response.to_s expect(Zlib::GzipReader.new(StringIO.new(encoded)).read).to eq body end it "sends deflated body" do - client = HTTP.use :auto_deflate => {:method => "deflate"} + client = HTTP.use auto_deflate: {method: "deflate"} body = "Hello!" - response = client.post("#{dummy.endpoint}/echo-body", :body => body) + response = client.post("#{dummy.endpoint}/echo-body", body: body) encoded = response.to_s expect(Zlib::Inflate.inflate(encoded)).to eq body @@ -424,14 +424,14 @@ def setsockopt(*args) it "returns raw body when Content-Encoding type is missing" do client = HTTP.use :auto_inflate body = "Hello!" - response = client.post("#{dummy.endpoint}/encoded-body", :body => body) + response = client.post("#{dummy.endpoint}/encoded-body", body: body) expect(response.to_s).to eq("#{body}-raw") end it "returns decoded body" do client = HTTP.use(:auto_inflate).headers("Accept-Encoding" => "gzip") body = "Hello!" - response = client.post("#{dummy.endpoint}/encoded-body", :body => body) + response = client.post("#{dummy.endpoint}/encoded-body", body: body) expect(response.to_s).to eq("#{body}-gzipped") end @@ -439,7 +439,7 @@ def setsockopt(*args) it "returns deflated body" do client = HTTP.use(:auto_inflate).headers("Accept-Encoding" => "deflate") body = "Hello!" - response = client.post("#{dummy.endpoint}/encoded-body", :body => body) + response = client.post("#{dummy.endpoint}/encoded-body", body: body) expect(response.to_s).to eq("#{body}-deflated") end @@ -447,7 +447,7 @@ def setsockopt(*args) it "returns empty body for no content response where Content-Encoding is gzip" do client = HTTP.use(:auto_inflate).headers("Accept-Encoding" => "gzip") body = "Hello!" - response = client.post("#{dummy.endpoint}/no-content-204", :body => body) + response = client.post("#{dummy.endpoint}/no-content-204", body: body) expect(response.to_s).to eq("") end @@ -455,7 +455,7 @@ def setsockopt(*args) it "returns empty body for no content response where Content-Encoding is deflate" do client = HTTP.use(:auto_inflate).headers("Accept-Encoding" => "deflate") body = "Hello!" - response = client.post("#{dummy.endpoint}/no-content-204", :body => body) + response = client.post("#{dummy.endpoint}/no-content-204", body: body) expect(response.to_s).to eq("") end @@ -468,13 +468,13 @@ def setsockopt(*args) end it "uses the custom URI Normalizer method" do - client = HTTP.use(:normalize_uri => {:normalizer => :itself.to_proc}) + client = HTTP.use(normalize_uri: {normalizer: :itself.to_proc}) response = client.get("#{dummy.endpoint}/héllö-wörld") expect(response.status).to eq(400) end it "raises if custom URI Normalizer returns invalid path" do - client = HTTP.use(:normalize_uri => {:normalizer => :itself.to_proc}) + client = HTTP.use(normalize_uri: {normalizer: :itself.to_proc}) expect { client.get("#{dummy.endpoint}/hello\nworld") }. to raise_error HTTP::RequestError, 'Invalid request URI: "/hello\nworld"' end @@ -485,7 +485,7 @@ def setsockopt(*args) uri.instance_variable_set(:@host, "example\ncom") uri end - client = HTTP.use(:normalize_uri => {:normalizer => normalizer}) + client = HTTP.use(normalize_uri: {normalizer: normalizer}) expect { client.get(dummy.endpoint) }. to raise_error HTTP::RequestError, 'Invalid host: "example\ncom"' end diff --git a/spec/support/dummy_server.rb b/spec/support/dummy_server.rb index bcbde771..58ed8648 100644 --- a/spec/support/dummy_server.rb +++ b/spec/support/dummy_server.rb @@ -13,15 +13,15 @@ class DummyServer < WEBrick::HTTPServer include ServerConfig CONFIG = { - :BindAddress => "127.0.0.1", - :Port => 0, - :AccessLog => BlackHole, - :Logger => BlackHole + BindAddress: "127.0.0.1", + Port: 0, + AccessLog: BlackHole, + Logger: BlackHole }.freeze SSL_CONFIG = CONFIG.merge( - :SSLEnable => true, - :SSLStartImmediately => true + SSLEnable: true, + SSLStartImmediately: true ).freeze def initialize(options = {}) diff --git a/spec/support/http_handling_shared.rb b/spec/support/http_handling_shared.rb index 8bbdf47a..2f28179d 100644 --- a/spec/support/http_handling_shared.rb +++ b/spec/support/http_handling_shared.rb @@ -2,7 +2,7 @@ RSpec.shared_context "HTTP handling" do context "without timeouts" do - let(:options) { {:timeout_class => HTTP::Timeout::Null, :timeout_options => {}} } + let(:options) { {timeout_class: HTTP::Timeout::Null, timeout_options: {}} } it "works" do expect(client.get(server.endpoint).body.to_s).to eq("") @@ -14,11 +14,11 @@ let(:options) do { - :timeout_class => HTTP::Timeout::PerOperation, - :timeout_options => { - :connect_timeout => conn_timeout, - :read_timeout => read_timeout, - :write_timeout => write_timeout + timeout_class: HTTP::Timeout::PerOperation, + timeout_options: { + connect_timeout: conn_timeout, + read_timeout: read_timeout, + write_timeout: write_timeout } } end @@ -62,9 +62,9 @@ context "with a global timeout" do let(:options) do { - :timeout_class => HTTP::Timeout::Global, - :timeout_options => { - :global_timeout => global_timeout + timeout_class: HTTP::Timeout::Global, + timeout_options: { + global_timeout: global_timeout } } end @@ -86,7 +86,7 @@ end context "it resets state when reusing connections" do - let(:extra_options) { {:persistent => server.endpoint} } + let(:extra_options) { {persistent: server.endpoint} } let(:global_timeout) { 2.5 } @@ -106,7 +106,7 @@ end context "when enabled" do - let(:options) { {:persistent => server.endpoint} } + let(:options) { {persistent: server.endpoint} } context "without a host" do it "infers host from persistent config" do diff --git a/spec/support/proxy_server.rb b/spec/support/proxy_server.rb index 94560233..36239879 100644 --- a/spec/support/proxy_server.rb +++ b/spec/support/proxy_server.rb @@ -10,11 +10,11 @@ class ProxyServer < WEBrick::HTTPProxyServer include ServerConfig CONFIG = { - :BindAddress => "127.0.0.1", - :Port => 0, - :AccessLog => BlackHole, - :Logger => BlackHole, - :RequestCallback => proc { |_, res| res["X-PROXIED"] = true } + BindAddress: "127.0.0.1", + Port: 0, + AccessLog: BlackHole, + Logger: BlackHole, + RequestCallback: proc { |_, res| res["X-PROXIED"] = true } }.freeze def initialize @@ -31,7 +31,7 @@ class AuthProxyServer < WEBrick::HTTPProxyServer end end - CONFIG = ProxyServer::CONFIG.merge :ProxyAuthProc => AUTHENTICATOR + CONFIG = ProxyServer::CONFIG.merge ProxyAuthProc: AUTHENTICATOR def initialize super CONFIG diff --git a/spec/support/ssl_helper.rb b/spec/support/ssl_helper.rb index d3bb7418..47f7bfb0 100644 --- a/spec/support/ssl_helper.rb +++ b/spec/support/ssl_helper.rb @@ -83,9 +83,9 @@ def client_context def client_params { - :key => client_cert.key, - :cert => client_cert.cert, - :ca_file => ca.file + key: client_cert.key, + cert: client_cert.cert, + ca_file: ca.file } end