Skip to content

Commit

Permalink
Bump rubocop; Bump min ruby version to 3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
ixti committed Oct 14, 2023
1 parent 1ba37c2 commit 4f6d6a4
Show file tree
Hide file tree
Showing 28 changed files with 163 additions and 120 deletions.
7 changes: 6 additions & 1 deletion .rubocop.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
require:
- rubocop-performance
- rubocop-rake
- rubocop-rspec

inherit_from:
- .rubocop_todo.yml
- .rubocop/layout.yml
Expand All @@ -8,4 +13,4 @@ AllCops:
DefaultFormatter: fuubar
DisplayCopNames: true
NewCops: enable
TargetRubyVersion: 2.6
TargetRubyVersion: 3.0
4 changes: 4 additions & 0 deletions .rubocop/style.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ Style/HashSyntax:
Style/OptionHash:
Enabled: true

# Using explicit `./` makes code more consistent
Style/RedundantCurrentDirectoryInPath:
Enabled: false

Style/RescueStandardError:
Enabled: true
EnforcedStyle: implicit
Expand Down
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ group :test do

gem "backports"

gem "rubocop", "~> 1.30.0"
gem "rubocop", "~> 1.57.0"
gem "rubocop-performance"
gem "rubocop-rake"
gem "rubocop-rspec"
Expand Down
4 changes: 1 addition & 3 deletions http.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,14 @@ Gem::Specification.new do |gem|
gem.require_paths = ["lib"]
gem.version = HTTP::VERSION

gem.required_ruby_version = ">= 2.6"
gem.required_ruby_version = ">= 3.0"

gem.add_runtime_dependency "addressable", "~> 2.8"
gem.add_runtime_dependency "base64", "~> 0.1"
gem.add_runtime_dependency "http-cookie", "~> 1.0"
gem.add_runtime_dependency "http-form_data", "~> 2.2"
gem.add_runtime_dependency "llhttp-ffi", "~> 0.5.0"

gem.add_development_dependency "bundler", "~> 2.0"

gem.metadata = {
"source_code_uri" => "https://github.com/httprb/http",
"wiki_uri" => "https://github.com/httprb/http/wiki",
Expand Down
4 changes: 2 additions & 2 deletions lib/http/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class Client
extend Forwardable
include Chainable

HTTP_OR_HTTPS_RE = %r{^https?://}i.freeze
HTTP_OR_HTTPS_RE = %r{^https?://}i

def initialize(default_options = {})
@default_options = HTTP::Options.new(default_options)
Expand Down Expand Up @@ -132,7 +132,7 @@ def verify_connection!(uri)

# If we get into a bad state (eg, Timeout.timeout ensure being killed)
# close the connection to prevent potential for mixed responses.
return close if @state == :dirty
close if @state == :dirty
end

# Merges query params if needed
Expand Down
4 changes: 2 additions & 2 deletions lib/http/content_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

module HTTP
class ContentType
MIME_TYPE_RE = %r{^([^/]+/[^;]+)(?:$|;)}.freeze
CHARSET_RE = /;\s*charset=([^;]+)/i.freeze
MIME_TYPE_RE = %r{^([^/]+/[^;]+)(?:$|;)}
CHARSET_RE = /;\s*charset=([^;]+)/i

attr_accessor :mime_type, :charset

Expand Down
8 changes: 4 additions & 4 deletions lib/http/headers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ class Headers
include Enumerable

# Matches HTTP header names when in "Canonical-Http-Format"
CANONICAL_NAME_RE = /\A[A-Z][a-z]*(?:-[A-Z][a-z]*)*\z/.freeze
CANONICAL_NAME_RE = /\A[A-Z][a-z]*(?:-[A-Z][a-z]*)*\z/

# Matches valid header field name according to RFC.
# @see http://tools.ietf.org/html/rfc7230#section-3.2
COMPLIANT_NAME_RE = /\A[A-Za-z0-9!#$%&'*+\-.^_`|~]+\z/.freeze
COMPLIANT_NAME_RE = /\A[A-Za-z0-9!#$%&'*+\-.^_`|~]+\z/

# Class constructor.
def initialize
Expand Down Expand Up @@ -226,11 +226,11 @@ def coerce(object)
# match {HEADER_NAME_RE}
# @return [String] canonical HTTP header name
def normalize_header(name)
return name if name =~ CANONICAL_NAME_RE
return name if CANONICAL_NAME_RE.match?(name)

normalized = name.split(/[\-_]/).each(&:capitalize!).join("-")

return normalized if normalized =~ COMPLIANT_NAME_RE
return normalized if COMPLIANT_NAME_RE.match?(normalized)

raise HeaderError, "Invalid HTTP header field name: #{name.inspect}"
end
Expand Down
2 changes: 1 addition & 1 deletion lib/http/uri.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class URI
HTTPS_SCHEME = "https"

# @private
PERCENT_ENCODE = /[^\x21-\x7E]+/.freeze
PERCENT_ENCODE = /[^\x21-\x7E]+/

# @private
NORMALIZER = lambda do |uri|
Expand Down
19 changes: 10 additions & 9 deletions spec/lib/http/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ def simple_response(body, status = 200)

describe "parsing params" do
let(:client) { HTTP::Client.new }

before { allow(client).to receive :perform }

it "accepts params within the provided URL" do
Expand Down Expand Up @@ -497,7 +498,7 @@ def wrap_response(res)

context "with HEAD request" do
it "does not iterates through body" do
expect_any_instance_of(HTTP::Connection).to_not receive(:readpartial)
expect_any_instance_of(HTTP::Connection).not_to receive(:readpartial)
client.head(dummy.endpoint)
end

Expand All @@ -512,7 +513,7 @@ def wrap_response(res)
socket_spy = double

chunks = [
<<-RESPONSE.gsub(/^\s*\| */, "").gsub(/\n/, "\r\n")
<<-RESPONSE.gsub(/^\s*\| */, "").gsub("\n", "\r\n")
| HTTP/1.1 200 OK
| Content-Type: text/html
| Server: WEBrick/1.3.1 (Ruby/1.9.3/2013-11-22)
Expand All @@ -524,8 +525,8 @@ def wrap_response(res)
RESPONSE
]

allow(socket_spy).to receive(:close) { nil }
allow(socket_spy).to receive(:closed?) { true }
allow(socket_spy).to receive(:close).and_return(nil)
allow(socket_spy).to receive(:closed?).and_return(true)
allow(socket_spy).to receive(:readpartial) { chunks.shift || :eof }
allow(socket_spy).to receive(:write) { chunks[0].length }

Expand All @@ -541,7 +542,7 @@ def wrap_response(res)
context "when uses chunked transfer encoding" do
let(:chunks) do
[
<<-RESPONSE.gsub(/^\s*\| */, "").gsub(/\n/, "\r\n") << body
<<-RESPONSE.gsub(/^\s*\| */, "").gsub("\n", "\r\n") << body
| HTTP/1.1 200 OK
| Content-Type: application/json
| Transfer-Encoding: chunked
Expand All @@ -551,7 +552,7 @@ def wrap_response(res)
]
end
let(:body) do
<<-BODY.gsub(/^\s*\| */, "").gsub(/\n/, "\r\n")
<<-BODY.gsub(/^\s*\| */, "").gsub("\n", "\r\n")
| 9
| {"state":
| 5
Expand All @@ -564,8 +565,8 @@ def wrap_response(res)
before do
socket_spy = double

allow(socket_spy).to receive(:close) { nil }
allow(socket_spy).to receive(:closed?) { true }
allow(socket_spy).to receive(:close).and_return(nil)
allow(socket_spy).to receive(:closed?).and_return(true)
allow(socket_spy).to receive(:readpartial) { chunks.shift || :eof }
allow(socket_spy).to receive(:write) { chunks[0].length }

Expand All @@ -579,7 +580,7 @@ def wrap_response(res)

context "with broken body (too early closed connection)" do
let(:body) do
<<-BODY.gsub(/^\s*\| */, "").gsub(/\n/, "\r\n")
<<-BODY.gsub(/^\s*\| */, "").gsub("\n", "\r\n")
| 9
| {"state":
BODY
Expand Down
22 changes: 11 additions & 11 deletions spec/lib/http/connection_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

before do
expect(socket).to receive(:start_tls).and_raise(HTTP::TimeoutError)
expect(socket).to receive(:closed?) { false }
expect(socket).to receive(:closed?).and_return(false)
expect(socket).to receive(:close)
end

Expand All @@ -37,7 +37,7 @@
before do
connection.instance_variable_set(:@pending_response, true)
expect(socket).to receive(:readpartial) do
<<-RESPONSE.gsub(/^\s*\| */, "").gsub(/\n/, "\r\n")
<<-RESPONSE.gsub(/^\s*\| */, "").gsub("\n", "\r\n")
| HTTP/1.1 200 OK
| Content-Type: text
| foo_bar: 123
Expand All @@ -58,20 +58,20 @@
before do
connection.instance_variable_set(:@pending_response, true)
expect(socket).to receive(:readpartial) do
<<-RESPONSE.gsub(/^\s*\| */, "").gsub(/\n/, "\r\n")
<<-RESPONSE.gsub(/^\s*\| */, "").gsub("\n", "\r\n")
| HTTP/1.1 200 OK
| Content-Type: text
|
RESPONSE
end
expect(socket).to receive(:readpartial) { "1" }
expect(socket).to receive(:readpartial) { "23" }
expect(socket).to receive(:readpartial) { "456" }
expect(socket).to receive(:readpartial) { "78" }
expect(socket).to receive(:readpartial) { "9" }
expect(socket).to receive(:readpartial) { "0" }
expect(socket).to receive(:readpartial) { :eof }
expect(socket).to receive(:closed?) { true }
expect(socket).to receive(:readpartial).and_return("1")
expect(socket).to receive(:readpartial).and_return("23")
expect(socket).to receive(:readpartial).and_return("456")
expect(socket).to receive(:readpartial).and_return("78")
expect(socket).to receive(:readpartial).and_return("9")
expect(socket).to receive(:readpartial).and_return("0")
expect(socket).to receive(:readpartial).and_return(:eof)
expect(socket).to receive(:closed?).and_return(true)
end

it "reads data in parts" do
Expand Down
7 changes: 7 additions & 0 deletions spec/lib/http/content_type_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,42 +4,49 @@
describe ".parse" do
context "with text/plain" do
subject { described_class.parse "text/plain" }

its(:mime_type) { is_expected.to eq "text/plain" }
its(:charset) { is_expected.to be_nil }
end

context "with tEXT/plaIN" do
subject { described_class.parse "tEXT/plaIN" }

its(:mime_type) { is_expected.to eq "text/plain" }
its(:charset) { is_expected.to be_nil }
end

context "with text/plain; charset=utf-8" do
subject { described_class.parse "text/plain; charset=utf-8" }

its(:mime_type) { is_expected.to eq "text/plain" }
its(:charset) { is_expected.to eq "utf-8" }
end

context 'with text/plain; charset="utf-8"' do
subject { described_class.parse 'text/plain; charset="utf-8"' }

its(:mime_type) { is_expected.to eq "text/plain" }
its(:charset) { is_expected.to eq "utf-8" }
end

context "with text/plain; charSET=utf-8" do
subject { described_class.parse "text/plain; charSET=utf-8" }

its(:mime_type) { is_expected.to eq "text/plain" }
its(:charset) { is_expected.to eq "utf-8" }
end

context "with text/plain; foo=bar; charset=utf-8" do
subject { described_class.parse "text/plain; foo=bar; charset=utf-8" }

its(:mime_type) { is_expected.to eq "text/plain" }
its(:charset) { is_expected.to eq "utf-8" }
end

context "with text/plain;charset=utf-8;foo=bar" do
subject { described_class.parse "text/plain;charset=utf-8;foo=bar" }

its(:mime_type) { is_expected.to eq "text/plain" }
its(:charset) { is_expected.to eq "utf-8" }
end
Expand Down
6 changes: 3 additions & 3 deletions spec/lib/http/features/instrumentation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def finish(_name, payload)
)
end

it "should log the request" do
it "logs the request" do
feature.wrap_request(request)

expect(instrumenter.output[:start]).to eq(:request => request)
Expand All @@ -53,7 +53,7 @@ def finish(_name, payload)
)
end

it "should log the response" do
it "logs the response" do
feature.wrap_response(response)

expect(instrumenter.output[:finish]).to eq(:response => response)
Expand All @@ -72,7 +72,7 @@ def finish(_name, payload)

let(:error) { HTTP::TimeoutError.new }

it "should log the error" do
it "logs the error" do
feature.on_error(request, error)

expect(instrumenter.output[:finish]).to eq(:request => request, :error => error)
Expand Down
4 changes: 2 additions & 2 deletions spec/lib/http/features/logging_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
)
end

it "should log the request" do
it "logs the request" do
feature.wrap_request(request)

expect(logdev.string).to eq <<~OUTPUT
Expand All @@ -49,7 +49,7 @@
)
end

it "should log the response" do
it "logs the response" do
feature.wrap_response(response)

expect(logdev.string).to eq <<~OUTPUT
Expand Down
Loading

0 comments on commit 4f6d6a4

Please sign in to comment.