Skip to content

Commit

Permalink
test: rack 2 & 3 compat
Browse files Browse the repository at this point in the history
  • Loading branch information
YOU54F committed Nov 29, 2024
1 parent 06f21fe commit 486c495
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 19 deletions.
22 changes: 22 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: Test

on: [push, pull_request]

jobs:
test:
strategy:
fail-fast: false
matrix:
ruby_version: ["2.7", "3.0", "3.1", "3.2", "3.3"]
os: ["ubuntu-latest","windows-latest","macos-latest"]
rack_version: ["2", "3"]
runs-on: ${{ matrix.os }}
env:
RACK_VERSION: ${{ matrix.rack_version }}
steps:
- uses: actions/checkout@v4
- uses: ruby/setup-ruby@v1
with:
ruby-version: ${{ matrix.ruby_version }}
- run: "bundle install"
- run: "bundle exec rake"
4 changes: 2 additions & 2 deletions lib/rack_reverse_proxy/middleware.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ class Middleware
}

def initialize(app = nil, &b)
@app = app || lambda { |_| [404, {}, []] }
@rules = []
@app = app || lambda { |_| [404, ENV['RACK_VERSION'] == '2' ? [] : {}, []] }
@rules = []
@global_options = DEFAULT_OPTIONS
instance_eval(&b) if block_given?
end
Expand Down
27 changes: 18 additions & 9 deletions lib/rack_reverse_proxy/roundtrip.rb
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,11 @@ def target_response
end

def response_headers
@_response_headers ||= build_response_headers.transform_keys(&:downcase)
@_response_headers ||= begin
headers = build_response_headers
headers = headers.transform_keys(&:downcase) unless ENV['RACK_VERSION'] == '2'
headers
end
end

def build_response_headers
Expand All @@ -163,25 +167,30 @@ def build_response_headers
end

def rack_response_headers
Rack::Headers.new.merge(
Rack::Proxy.normalize_headers(
format_headers(target_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
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?
rewrite_uri(response_location, source_request)
response_headers["location"] = response_location.to_s
response_headers[set_rack_version_specific_location_header] = response_location.to_s
end

def response_location
@_response_location ||= URI(response_headers["location"] || uri)
@_response_location ||= URI(response_headers[set_rack_version_specific_location_header] || uri)
end

def need_replace_location?
response_headers["location"] && options[:replace_response_host] && response_location.host
response_headers[set_rack_version_specific_location_header] && options[:replace_response_host] && response_location.host
end

def setup_request
Expand Down
15 changes: 10 additions & 5 deletions rack-reverse-proxy.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,15 @@ eos
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
spec.require_paths = ["lib"]

spec.add_dependency "rack", ">= 1.0.0"
spec.add_dependency "rack-proxy", "~> 0.7", ">= 0.7.0"

spec.add_development_dependency "bundler", "~> 2.5"
spec.add_development_dependency "rake", "~> 13.2"
if ENV['RACK_VERSION'] == '2'
spec.add_dependency 'rack', ">= 1.0.0", '< 3.0'
else
spec.add_dependency 'rack', '>= 3.0', '< 4.0'
spec.add_dependency 'rackup', '~> 2.0'
end
spec.add_dependency "rack-proxy", "~> 0.6", ">= 0.6.1"

spec.add_development_dependency "bundler", ">= 1.7", "< 3.0"
spec.add_development_dependency "rake", ">= 10.3"
end
# rubocop:enable
15 changes: 12 additions & 3 deletions spec/rack/reverse_proxy_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ def app
it "produces a response header of type Headers" do
stub_request(:get, "http://example.com/test")
get "/test"
expect(last_response.headers).to be_an_instance_of(Rack::Headers)
expected_class = ENV['RACK_VERSION'] == "2" ? Rack::Utils::HeaderHash : Rack::Headers
expect(last_response.headers).to be_an_instance_of(expected_class)
end

it "parses the headers as a Hash with values of type String" do
Expand Down Expand Up @@ -163,7 +164,11 @@ def app

headers = last_response.headers.to_hash
expect(headers["Date"]).to eq("Wed, 22 Jul 2015 11:27:21 GMT")
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
end

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

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

it "the response header includes content-length" do
Expand Down

0 comments on commit 486c495

Please sign in to comment.