From c86b77f8c02b71d0bdbc32c521cda6161c700372 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Laszlo?= Date: Wed, 5 Jan 2022 19:10:54 +0100 Subject: [PATCH 1/5] Fix unitialized warnings --- lib/rack/proxy.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/rack/proxy.rb b/lib/rack/proxy.rb index 84462f0..a4cdeec 100644 --- a/lib/rack/proxy.rb +++ b/lib/rack/proxy.rb @@ -49,12 +49,12 @@ def initialize(app = nil, opts= {}) @streaming = opts.fetch(:streaming, true) @ssl_verify_none = opts.fetch(:ssl_verify_none, false) - @backend = URI(opts[:backend]) if opts[:backend] + @backend = opts[:backend] ? URI(opts[:backend]) : nil @read_timeout = opts.fetch(:read_timeout, 60) - @ssl_version = opts[:ssl_version] if opts[:ssl_version] + @ssl_version = opts[:ssl_version] - @username = opts[:username] if opts[:username] - @password = opts[:password] if opts[:password] + @username = opts[:username] + @password = opts[:password] @opts = opts end From 685e94df2450fd3ef56588cf630efdd65a5ce554 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Laszlo?= Date: Wed, 5 Jan 2022 19:11:26 +0100 Subject: [PATCH 2/5] Fix broken tests on master Use mockapi.io (just googled something neutral) instead of the hosts currently used. --- test/http_streaming_response_test.rb | 20 +++++++++++--------- test/rack_proxy_test.rb | 2 +- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/test/http_streaming_response_test.rb b/test/http_streaming_response_test.rb index 395bd95..4b03752 100644 --- a/test/http_streaming_response_test.rb +++ b/test/http_streaming_response_test.rb @@ -1,26 +1,28 @@ require "test_helper" require "rack/http_streaming_response" +require "pry" class HttpStreamingResponseTest < Test::Unit::TestCase def setup - host, req = "www.trix.pl", Net::HTTP::Get.new("/") - @response = Rack::HttpStreamingResponse.new(req, host) + host, req = "mockapi.io", Net::HTTP::Get.new("/") + @response = Rack::HttpStreamingResponse.new(req, host, 443) + @response.use_ssl = true end def test_streaming # Response status - assert @response.code == 200 - assert @response.status == 200 + assert_equal 200, @response.status + assert_equal 200, @response.status # Headers headers = @response.headers - assert headers.size > 0 + assert headers.size.positive? - assert headers["content-type"] == ["text/html;charset=utf-8"] - assert headers["CoNtEnT-TyPe"] == headers["content-type"] - assert headers["content-length"].first.to_i > 0 + assert_match %r{text/html; ?charset=utf-8}, headers["content-type"].first.downcase + assert_equal headers['content-type'], headers['CoNtEnT-TyPe'] + assert headers['content-length'].first.to_i.positive? # Body chunks = [] @@ -28,7 +30,7 @@ def test_streaming chunks << chunk end - assert chunks.size > 0 + assert chunks.size.positive? chunks.each do |chunk| assert chunk.is_a?(String) end diff --git a/test/rack_proxy_test.rb b/test/rack_proxy_test.rb index bce46f0..20e8e81 100644 --- a/test/rack_proxy_test.rb +++ b/test/rack_proxy_test.rb @@ -120,7 +120,7 @@ def test_handles_missing_content_length end def test_response_header_included_Hop_by_hop - app({:streaming => true}).host = 'auth.goeasyship.com' + app({:streaming => true}).host = 'mockapi.io' get 'https://example.com/oauth2/token/info?access_token=123' assert !last_response.headers.key?('transfer-encoding') end From a41e2a9df61e2e579de2f177cc767f4e9d97b3ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Laszlo?= Date: Wed, 5 Jan 2022 19:15:58 +0100 Subject: [PATCH 3/5] Bump rake and rack --- Gemfile.lock | 4 ++-- test/http_streaming_response_test.rb | 1 - 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index 83f6dd6..ff59eb7 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -8,10 +8,10 @@ GEM remote: https://rubygems.org/ specs: power_assert (0.2.6) - rack (2.0.3) + rack (2.2.3) rack-test (0.5.6) rack (>= 1.0) - rake (13.0.1) + rake (13.0.6) test-unit (3.1.5) power_assert diff --git a/test/http_streaming_response_test.rb b/test/http_streaming_response_test.rb index 4b03752..0ea3681 100644 --- a/test/http_streaming_response_test.rb +++ b/test/http_streaming_response_test.rb @@ -1,6 +1,5 @@ require "test_helper" require "rack/http_streaming_response" -require "pry" class HttpStreamingResponseTest < Test::Unit::TestCase From 8331680ca2e10943c03954b04c7bcdae1e23b5cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Laszlo?= Date: Wed, 5 Jan 2022 20:00:16 +0100 Subject: [PATCH 4/5] Change header normalization HTTP headers are case insensitive, but `Rack::MockResponse` is calling `headers.fetch` which isn't case insensitive even when `headers` is a `Utils::HeaderHash` instance. The easiest solution seemed to be to just change our normalized header format to whatever `Rack::MockResponse` expects. --- lib/rack/proxy.rb | 8 ++++++-- test/rack_proxy_test.rb | 4 ++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/lib/rack/proxy.rb b/lib/rack/proxy.rb index a4cdeec..a3417e7 100644 --- a/lib/rack/proxy.rb +++ b/lib/rack/proxy.rb @@ -26,7 +26,7 @@ def extract_http_request_headers(env) def normalize_headers(headers) mapped = headers.map do |k, v| - [k, if v.is_a? Array then v.join("\n") else v end] + [titleize(k), if v.is_a? Array then v.join("\n") else v end] end Utils::HeaderHash.new Hash[mapped] end @@ -34,7 +34,11 @@ def normalize_headers(headers) protected def reconstruct_header_name(name) - name.sub(/^HTTP_/, "").gsub("_", "-") + titleize(name.sub(/^HTTP_/, "").gsub("_", "-")) + end + + def titleize(str) + str.split("-").map(&:capitalize).join("-") end end diff --git a/test/rack_proxy_test.rb b/test/rack_proxy_test.rb index 20e8e81..7818bd0 100644 --- a/test/rack_proxy_test.rb +++ b/test/rack_proxy_test.rb @@ -78,10 +78,10 @@ def test_header_reconstruction proxy_class = Rack::Proxy header = proxy_class.send(:reconstruct_header_name, "HTTP_ABC") - assert header == "ABC" + assert header == "Abc" header = proxy_class.send(:reconstruct_header_name, "HTTP_ABC_D") - assert header == "ABC-D" + assert header == "Abc-D" end def test_extract_http_request_headers From 9d0465cadc5db876690d1e5f424bec2c6c43cfb3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Laszlo?= Date: Wed, 5 Jan 2022 20:06:18 +0100 Subject: [PATCH 5/5] Keep original quote style --- test/http_streaming_response_test.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/http_streaming_response_test.rb b/test/http_streaming_response_test.rb index 0ea3681..625ea00 100644 --- a/test/http_streaming_response_test.rb +++ b/test/http_streaming_response_test.rb @@ -20,8 +20,8 @@ def test_streaming assert headers.size.positive? assert_match %r{text/html; ?charset=utf-8}, headers["content-type"].first.downcase - assert_equal headers['content-type'], headers['CoNtEnT-TyPe'] - assert headers['content-length'].first.to_i.positive? + assert_equal headers["content-type"], headers["CoNtEnT-TyPe"] + assert headers["content-length"].first.to_i.positive? # Body chunks = []