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/lib/rack/proxy.rb b/lib/rack/proxy.rb index 84462f0..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 @@ -49,12 +53,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 diff --git a/test/http_streaming_response_test.rb b/test/http_streaming_response_test.rb index 395bd95..625ea00 100644 --- a/test/http_streaming_response_test.rb +++ b/test/http_streaming_response_test.rb @@ -4,23 +4,24 @@ 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 +29,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..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 @@ -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