updated rubocop to 1.0.0 on possible rubies, making the necessary changes

This commit is contained in:
HoneyryderChuck 2020-11-07 02:00:20 +00:00
parent d900225f2e
commit 1b26977d16
34 changed files with 191 additions and 202 deletions

View File

@ -1 +1,7 @@
inherit_from: .rubocop-2.3.yml
Style/OptionalBooleanParameter:
Enabled: false
Gemspec/RequiredRubyVersion:
Enabled: false

View File

@ -1 +1 @@
inherit_from: .rubocop-2.3.yml
inherit_from: .rubocop-2.4.yml

View File

@ -1 +1 @@
inherit_from: .rubocop-2.3.yml
inherit_from: .rubocop-2.4.yml

View File

@ -1 +1 @@
inherit_from: .rubocop-2.3.yml
inherit_from: .rubocop-2.4.yml

View File

@ -1 +1 @@
inherit_from: .rubocop-2.3.yml
inherit_from: .rubocop-2.4.yml

View File

@ -1,7 +1,7 @@
inherit_from: .rubocop_todo.yml
AllCops:
TargetRubyVersion: 2.3
TargetRubyVersion: 2.4
DisplayCopNames: true
Include:
- lib/**/*.rb

View File

@ -37,3 +37,8 @@ Style/Documentation:
Naming/AccessorMethodName:
Enabled: false
Lint/UriEscapeUnescape:
Enabled: false
Gemspec/RequiredRubyVersion:
Enabled: false

View File

@ -16,8 +16,10 @@ group :test do
gem "net-ssh", "~> 4.2.0"
elsif RUBY_VERSION < "2.3"
gem "rubocop", "~> 0.68.1"
elsif RUBY_VERSION < "2.3"
gem "rubocop", "~> 0.81.1"
else
gem "rubocop", "~> 0.80.0"
gem "rubocop", "~> 1.0.0"
gem "rubocop-performance", "~> 1.5.2"
end

View File

@ -121,7 +121,7 @@ module Faraday
end
def respond_to_missing?(meth)
@env.respond_to?(meth)
@env.respond_to?(meth) || super
end
def method_missing(meth, *args, &blk)

View File

@ -142,7 +142,7 @@ module HTTPX
end
end
def purge_pending
def purge_pending(&block)
pendings = []
if @parser
@inflight -= @parser.pending.size
@ -150,9 +150,7 @@ module HTTPX
end
pendings << @pending
pendings.each do |pending|
pending.reject! do |request|
yield request
end
pending.reject!(&block)
end
end
@ -460,7 +458,6 @@ module HTTPX
throw(:jump_tick)
rescue Errno::ECONNREFUSED,
Errno::EADDRNOTAVAIL,
Errno::EHOSTUNREACH,
OpenSSL::SSL::SSLError => e
# connect errors, exit gracefully
handle_error(e)

View File

@ -181,7 +181,7 @@ module HTTPX
def manage_connection(response)
connection = response.headers["connection"]
case connection
when /keep\-alive/i
when /keep-alive/i
keep_alive = response.headers["keep-alive"]
return unless keep_alive

View File

@ -28,6 +28,7 @@ module HTTPX
NativeResolveError = Class.new(ResolveError) do
attr_reader :connection, :host
def initialize(connection, host, message = "Can't resolve #{host}")
@connection = connection
@host = host

View File

@ -54,6 +54,18 @@ module HTTPX
Numeric.__send__(:include, NegMethods)
end
module RegexpExtensions
# If you wonder why this is there: the oauth feature uses a refinement to enhance the
# Regexp class locally with #match? , but this is never tested, because ActiveSupport
# monkey-patches the same method... Please ActiveSupport, stop being so intrusive!
# :nocov:
refine(Regexp) do
def match?(*args)
!match(*args).nil?
end
end
end
module URIExtensions
refine URI::Generic do
def non_ascii_hostname

View File

@ -7,11 +7,7 @@ module HTTPX
class TCP
include Loggable
attr_reader :ip, :port
attr_reader :addresses
attr_reader :state
attr_reader :ip, :port, :addresses, :state
alias_method :host, :ip

View File

@ -8,6 +8,7 @@ module HTTPX
def_delegator :@uri, :port, :scheme
# rubocop:disable Lint/MissingSuper
def initialize(uri, addresses, options)
@uri = uri
@addresses = addresses
@ -29,6 +30,7 @@ module HTTPX
end
@io ||= build_socket
end
# rubocop:enable Lint/MissingSuper
def hostname
@uri.host

View File

@ -57,7 +57,7 @@ module HTTPX
idx = @buffer.index("\n")
return unless idx
(m = %r{\AHTTP(?:\/(\d+\.\d+))?\s+(\d\d\d)(?:\s+(.*))?}in.match(@buffer)) ||
(m = %r{\AHTTP(?:/(\d+\.\d+))?\s+(\d\d\d)(?:\s+(.*))?}in.match(@buffer)) ||
raise(Error, "wrong head line format")
version, code, _ = m.captures
raise(Error, "unsupported HTTP version (HTTP/#{version})") unless VERSIONS.include?(version)

View File

@ -46,11 +46,8 @@ module HTTPX
super
return if @body.nil?
if (threshold = options.compression_threshold_size)
unless unbounded_body?
return if @body.bytesize < threshold
end
end
threshold = options.compression_threshold_size
return if threshold && !unbounded_body? && @body.bytesize < threshold
@headers.get("content-encoding").each do |encoding|
next if encoding == "identity"

View File

@ -11,13 +11,7 @@ module HTTPX
# least)
MAX_LENGTH = 4096
attr_reader :domain
attr_reader :path
attr_reader :name, :value
attr_reader :created_at
attr_reader :domain, :path, :name, :value, :created_at
def path=(path)
path = String(path)

View File

@ -3,151 +3,139 @@
require "strscan"
require "time"
module HTTPX::Plugins::Cookies
module SetCookieParser
unless Regexp.method_defined?(:match?)
# If you wonder why this is there: the oauth feature uses a refinement to enhance the
# Regexp class locally with #match? , but this is never tested, because ActiveSupport
# monkey-patches the same method... Please ActiveSupport, stop being so intrusive!
# :nocov:
module RegexpExtensions
refine(Regexp) do
def match?(*args)
!match(*args).nil?
end
end
end
using(RegexpExtensions)
# :nocov:
end
module HTTPX
module Plugins::Cookies
module SetCookieParser
using(RegexpExtensions) unless Regexp.method_defined?(:match?)
# Whitespace.
RE_WSP = /[ \t]+/.freeze
# Whitespace.
RE_WSP = /[ \t]+/.freeze
# A pattern that matches a cookie name or attribute name which may
# be empty, capturing trailing whitespace.
RE_NAME = /(?!#{RE_WSP})[^,;\\"=]*/.freeze
# A pattern that matches a cookie name or attribute name which may
# be empty, capturing trailing whitespace.
RE_NAME = /(?!#{RE_WSP})[^,;\\"=]*/.freeze
RE_BAD_CHAR = /([\x00-\x20\x7F",;\\])/.freeze
RE_BAD_CHAR = /([\x00-\x20\x7F",;\\])/.freeze
# A pattern that matches the comma in a (typically date) value.
RE_COOKIE_COMMA = /,(?=#{RE_WSP}?#{RE_NAME}=)/.freeze
# A pattern that matches the comma in a (typically date) value.
RE_COOKIE_COMMA = /,(?=#{RE_WSP}?#{RE_NAME}=)/.freeze
module_function
module_function
def scan_dquoted(scanner)
s = +""
until scanner.eos?
break if scanner.skip(/"/)
if scanner.skip(/\\/)
s << scanner.getch
elsif scanner.scan(/[^"\\]+/)
s << scanner.matched
end
end
s
end
def scan_value(scanner, comma_as_separator = false)
value = +""
until scanner.eos?
if scanner.scan(/[^,;"]+/)
value << scanner.matched
elsif scanner.skip(/"/)
# RFC 6265 2.2
# A cookie-value may be DQUOTE'd.
value << scan_dquoted(scanner)
elsif scanner.check(/;/)
break
elsif comma_as_separator && scanner.check(RE_COOKIE_COMMA)
break
else
value << scanner.getch
end
end
value.rstrip!
value
end
def scan_name_value(scanner, comma_as_separator = false)
name = scanner.scan(RE_NAME)
name.rstrip! if name
if scanner.skip(/\=/)
value = scan_value(scanner, comma_as_separator)
else
scan_value(scanner, comma_as_separator)
value = nil
end
[name, value]
end
def call(set_cookie)
scanner = StringScanner.new(set_cookie)
# RFC 6265 4.1.1 & 5.2
until scanner.eos?
start = scanner.pos
len = nil
scanner.skip(RE_WSP)
name, value = scan_name_value(scanner, true)
value = nil if name.empty?
attrs = {}
def scan_dquoted(scanner)
s = +""
until scanner.eos?
if scanner.skip(/,/)
# The comma is used as separator for concatenating multiple
# values of a header.
len = (scanner.pos - 1) - start
break
elsif scanner.skip(/;/)
scanner.skip(RE_WSP)
break if scanner.skip(/"/)
aname, avalue = scan_name_value(scanner, true)
next if aname.empty? || value.nil?
aname.downcase!
case aname
when "expires"
# RFC 6265 5.2.1
(avalue &&= Time.httpdate(avalue)) || next
when "max-age"
# RFC 6265 5.2.2
next unless /\A-?\d+\z/.match?(avalue)
avalue = Integer(avalue)
when "domain"
# RFC 6265 5.2.3
# An empty value SHOULD be ignored.
next if avalue.nil? || avalue.empty?
when "path"
# RFC 6265 5.2.4
# A relative path must be ignored rather than normalizing it
# to "/".
next unless avalue.start_with?("/")
when "secure", "httponly"
# RFC 6265 5.2.5, 5.2.6
avalue = true
end
attrs[aname] = avalue
if scanner.skip(/\\/)
s << scanner.getch
elsif scanner.scan(/[^"\\]+/)
s << scanner.matched
end
end
len ||= scanner.pos - start
s
end
next if len > Cookie::MAX_LENGTH
def scan_value(scanner, comma_as_separator = false)
value = +""
yield(name, value, attrs) if name && !name.empty? && value
until scanner.eos?
if scanner.scan(/[^,;"]+/)
value << scanner.matched
elsif scanner.skip(/"/)
# RFC 6265 2.2
# A cookie-value may be DQUOTE'd.
value << scan_dquoted(scanner)
elsif scanner.check(/;/)
break
elsif comma_as_separator && scanner.check(RE_COOKIE_COMMA)
break
else
value << scanner.getch
end
end
value.rstrip!
value
end
def scan_name_value(scanner, comma_as_separator = false)
name = scanner.scan(RE_NAME)
name.rstrip! if name
if scanner.skip(/=/)
value = scan_value(scanner, comma_as_separator)
else
scan_value(scanner, comma_as_separator)
value = nil
end
[name, value]
end
def call(set_cookie)
scanner = StringScanner.new(set_cookie)
# RFC 6265 4.1.1 & 5.2
until scanner.eos?
start = scanner.pos
len = nil
scanner.skip(RE_WSP)
name, value = scan_name_value(scanner, true)
value = nil if name.empty?
attrs = {}
until scanner.eos?
if scanner.skip(/,/)
# The comma is used as separator for concatenating multiple
# values of a header.
len = (scanner.pos - 1) - start
break
elsif scanner.skip(/;/)
scanner.skip(RE_WSP)
aname, avalue = scan_name_value(scanner, true)
next if aname.empty? || value.nil?
aname.downcase!
case aname
when "expires"
# RFC 6265 5.2.1
(avalue &&= Time.httpdate(avalue)) || next
when "max-age"
# RFC 6265 5.2.2
next unless /\A-?\d+\z/.match?(avalue)
avalue = Integer(avalue)
when "domain"
# RFC 6265 5.2.3
# An empty value SHOULD be ignored.
next if avalue.nil? || avalue.empty?
when "path"
# RFC 6265 5.2.4
# A relative path must be ignored rather than normalizing it
# to "/".
next unless avalue.start_with?("/")
when "secure", "httponly"
# RFC 6265 5.2.5, 5.2.6
avalue = true
end
attrs[aname] = avalue
end
end
len ||= scanner.pos - start
next if len > Cookie::MAX_LENGTH
yield(name, value, attrs) if name && !name.empty? && value
end
end
end
end

View File

@ -81,7 +81,7 @@ module HTTPX
uri = request.path
params = Hash[auth_info.split(/ *, */)
params = Hash[auth_info.split(/ *, */) # rubocop:disable Style/HashTransformValues
.map { |val| val.split("=") }
.map { |k, v| [k, v.delete("\"")] }]
nonce = params["nonce"]

View File

@ -33,11 +33,8 @@ module HTTPX
super
return if @body.nil?
if (threshold = options.expect_threshold_size)
unless unbounded_body?
return if @body.bytesize < threshold
end
end
threshold = options.expect_threshold_size
return if threshold && !unbounded_body? && @body.bytesize < threshold
@headers["expect"] = "100-continue"
end

View File

@ -91,6 +91,8 @@ module HTTPX
end
module Packet
using(RegexpExtensions) unless Regexp.method_defined?(:match?)
module_function
def connect(parameters, uri)
@ -101,7 +103,7 @@ module HTTPX
packet << [ip.to_i].pack("N")
rescue IPAddr::InvalidAddressError
if parameters.uri.scheme =~ /^socks4a?$/
if /^socks4a?$/.match?(parameters.uri.scheme)
# resolv defaults to IPv4, and socks4 doesn't support IPv6 otherwise
ip = IPAddr.new(Resolv.getaddress(uri.host))
packet << [ip.to_i].pack("N")

View File

@ -166,7 +166,6 @@ module HTTPX
resolver.on(:error, &method(:on_resolver_error))
resolver.on(:close) { on_resolver_close(resolver) }
resolver
# rubocop: disable Layout/RescueEnsureAlignment
rescue ArgumentError
# this block is here because of an error which happens on CI from time to time
warn "tried resolver: #{resolver_type}"
@ -174,7 +173,6 @@ module HTTPX
warn "new: #{resolver_type.method(:new).source_location}"
raise
end
# rubocop: enable Layout/RescueEnsureAlignment
end
end
end

View File

@ -33,9 +33,7 @@ module HTTPX
USER_AGENT = "httpx.rb/#{VERSION}"
attr_reader :verb, :uri, :headers, :body, :state
attr_reader :options, :response
attr_reader :verb, :uri, :headers, :body, :state, :options, :response
def_delegator :@body, :empty?

View File

@ -213,7 +213,7 @@ module HTTPX
case response.headers["content-type"]
when "application/dns-json",
"application/json",
%r{^application\/x\-javascript} # because google...
%r{^application/x-javascript} # because google...
payload = JSON.parse(response.to_s)
payload["Answer"]
when "application/dns-udpwireformat",

View File

@ -75,11 +75,11 @@ module HTTPX
@status == 204 ||
@status == 205 ||
@status == 304 || begin
content_length = @headers["content-length"]
return false if content_length.nil?
content_length = @headers["content-length"]
return false if content_length.nil?
content_length == "0"
end
content_length == "0"
end
end
class Body

View File

@ -51,7 +51,7 @@ class HTTPX::Selector
READ_INTERESTS = %i[r rw].freeze
WRITE_INTERESTS = %i[w rw].freeze
def select_many(interval)
def select_many(interval, &block)
selectables, r, w = nil
# first, we group IOs based on interest type. On call to #interests however,
@ -102,9 +102,7 @@ class HTTPX::Selector
writers.delete(io)
end if readers
writers.each do |io|
yield io
end if writers
writers.each(&block) if writers
end
def select_one(interval)

View File

@ -190,15 +190,13 @@ module HTTPX
begin
# guarantee ordered responses
loop do
begin
request = requests.first
pool.next_tick until (response = fetch_response(request, connections, request_options))
request = requests.first
pool.next_tick until (response = fetch_response(request, connections, request_options))
responses << response
requests.shift
responses << response
requests.shift
break if requests.empty? || pool.empty?
end
break if requests.empty? || pool.empty?
end
responses
ensure

View File

@ -91,10 +91,8 @@ module ProfilerHelpers
end
end
def memory_profile
def memory_profile(&block)
require "memory_profiler"
MemoryProfiler.report(allow_files: ["lib/httpx"]) do
yield
end.pretty_print
MemoryProfiler.report(allow_files: ["lib/httpx"], &block).pretty_print
end
end

View File

@ -40,8 +40,8 @@ class HTTPTest < Minitest::Test
assert log_output.match(/HEADER: Connection: close/)
# assert response headers
assert log_output.match(%r{HEADLINE: 200 HTTP/1\.1})
assert log_output.match(/HEADER: content\-type: \w+/)
assert log_output.match(/HEADER: content\-length: \d+/)
assert log_output.match(/HEADER: content-type: \w+/)
assert log_output.match(/HEADER: content-length: \d+/)
end
private

View File

@ -70,8 +70,8 @@ class HTTPSTest < Minitest::Test
assert log_output.match(%r{HEADER: accept: */*})
# assert response headers
assert log_output.match(/HEADER: :status: 200/)
assert log_output.match(/HEADER: content\-type: \w+/)
assert log_output.match(/HEADER: content\-length: \d+/)
assert log_output.match(/HEADER: content-type: \w+/)
assert log_output.match(/HEADER: content-length: \d+/)
end
private

View File

@ -192,7 +192,7 @@ module Requests
end
def cookies_set_uri(cookies)
build_uri("/cookies/set?" + URI.encode_www_form(cookies))
build_uri("/cookies/set?#{URI.encode_www_form(cookies)}")
end
def verify_cookies(jar, cookies)

View File

@ -67,7 +67,7 @@ module Requests
private
def redirect_uri(redirect_uri = redirect_location)
build_uri("/redirect-to?url=" + redirect_uri)
build_uri("/redirect-to?url=#{redirect_uri}")
end
def max_redirect_uri(n)

View File

@ -88,8 +88,8 @@ module Requests
define_method :"test_#{meth}_multiple_params" do
uri = build_uri("/#{meth}")
response1, response2 = HTTPX.request([
[meth, uri, body: "data"],
[meth, uri, form: { "foo" => "bar" }],
[meth, uri, { body: "data" }],
[meth, uri, { form: { "foo" => "bar" } }],
], max_concurrent_requests: 1) # because httpbin sucks and can't handle pipeline requests
verify_status(response1, 200)