Skip to content

Commit

Permalink
chore: use Rack.release rather than RACK_VERSION env
Browse files Browse the repository at this point in the history
  • Loading branch information
YOU54F committed Nov 29, 2024
1 parent 58f74fa commit de22c7a
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 23 deletions.
6 changes: 5 additions & 1 deletion lib/rack_reverse_proxy/middleware.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class Middleware
}

def initialize(app = nil, &b)
@app = app || lambda { |_| [404, ENV['RACK_VERSION'] == '2' ? [] : {}, []] }
@app = app || lambda { |_| [404, rack_version_less_than_three ? [] : {}, []] }
@rules = []
@global_options = DEFAULT_OPTIONS
instance_eval(&b) if block_given?
Expand All @@ -29,6 +29,10 @@ def call(env)

private

def rack_version_less_than_three
Rack.release.split('.').first.to_i < 3
end

def reverse_proxy_options(options)
@global_options = @global_options.merge(options)
end
Expand Down
19 changes: 10 additions & 9 deletions lib/rack_reverse_proxy/roundtrip.rb
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ def target_response
def response_headers
@_response_headers ||= begin
headers = build_response_headers
headers = headers.transform_keys(&:downcase) unless ENV['RACK_VERSION'] == '2'
headers = headers.transform_keys(&:downcase) unless rack_version_less_than_three
headers
end
end
Expand All @@ -168,16 +168,9 @@ def build_response_headers

def rack_response_headers
headers = Rack::Proxy.normalize_headers(format_headers(target_response.headers))
if ENV['RACK_VERSION'] == '2'
Rack::Utils::HeaderHash.new(headers)
else
Rack::Headers.new.merge(headers)
end
rack_version_less_than_three ? Rack::Utils::HeaderHash.new(headers) : Rack::Headers.new.merge(headers)
end

def set_rack_version_specific_location_header
location_header = ENV['RACK_VERSION'] == '2' ? 'Location' : 'location'
end

def replace_location_header
return unless need_replace_location?
Expand Down Expand Up @@ -281,5 +274,13 @@ def non_ambiguous_match
def ambiguous_match?
matches.length > 1 && global_options[:matching] != :first
end

def rack_version_less_than_three
Rack.release.split('.').first.to_i < 3
end

def set_rack_version_specific_location_header
rack_version_less_than_three ? 'Location' : 'location'
end
end
end
2 changes: 1 addition & 1 deletion rack-reverse-proxy.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ eos
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
spec.require_paths = ["lib"]

if ENV['RACK_VERSION'] == '2'
if ENV["RACK_VERSION"] = "2"
spec.add_dependency 'rack', ">= 1.0.0", '< 3.0'
else
spec.add_dependency 'rack', '>= 3.0', '< 4.0'
Expand Down
16 changes: 4 additions & 12 deletions spec/rack/reverse_proxy_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def app
it "produces a response header of type Headers" do
stub_request(:get, "http://example.com/test")
get "/test"
expected_class = ENV['RACK_VERSION'] == "2" ? Rack::Utils::HeaderHash : Rack::Headers
expected_class = rack_version_less_than_three ? Rack::Utils::HeaderHash : Rack::Headers
expect(last_response.headers).to be_an_instance_of(expected_class)
end

Expand Down Expand Up @@ -164,11 +164,7 @@ def app

headers = last_response.headers.to_hash
expect(headers["Date"]).to eq("Wed, 22 Jul 2015 11:27:21 GMT")
if ENV['RACK_VERSION'] == "2"
expect(headers["date"]).to be_nil
else
expect(headers["date"]).to eq("Wed, 22 Jul 2015 11:27:21 GMT")
end
expect(headers["date"]).to rack_version_less_than_three ? be_nil : eq("Wed, 22 Jul 2015 11:27:21 GMT")
end

it "formats the headers with dashes correctly" do
Expand All @@ -180,11 +176,7 @@ def app
get "/2test"

headers = last_response.headers.to_hash
if ENV['RACK_VERSION'] == "2"
expect(headers["x-additional-info"]).to be_nil
else
expect(headers["x-additional-info"]).to eq("something")
end
expect(headers["x-additional-info"]).to eq(rack_version_less_than_three ? nil : "something")
end

it "the response header includes content-length" do
Expand Down Expand Up @@ -767,7 +759,7 @@ def app
end
end

describe "as a rack app", skip: ENV['RACK_VERSION'] == "2" do
describe "as a rack app", skip: rack_version_less_than_three do
it "responds with 404 when the path is not matched" do
get "/"
expect(last_response).to be_not_found
Expand Down
4 changes: 4 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,8 @@

# User-defined configuration
WebMock.disable_net_connect!

def rack_version_less_than_three
Rack.release.split('.').first.to_i < 3
end
end

0 comments on commit de22c7a

Please sign in to comment.