Skip to content

Commit

Permalink
rack 3 support
Browse files Browse the repository at this point in the history
  • Loading branch information
pcai committed Feb 11, 2024
1 parent a67e021 commit 56cd67b
Show file tree
Hide file tree
Showing 9 changed files with 15 additions and 15 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/development.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ jobs:
experimental: true

steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4

- name: Install dependencies
run: sudo apt-get install libcurl4-openssl-dev
Expand Down
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
### Unreleased

* Add your changes here.
* `HTTPI::Request#headers` and `HTTPI::Response#headers` now return `Rack::Headers` instead of `Rack::Utils::HeaderHash`

### 3.0.2 (2024-02-10)

Expand Down
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ gem 'net-http-persistent', '~> 4.0', :require => false
gem 'http', :require => false

# adapter extensions
gem 'rack', '< 3'
gem 'rack'
gem 'socksify'

# coverage
Expand Down
4 changes: 2 additions & 2 deletions httpi.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Gem::Specification.new do |s|

s.license = 'MIT'

s.add_dependency 'rack', '< 3'
s.add_dependency 'rack', '>= 2.0', '< 3.1'
s.add_dependency 'nkf'
s.add_dependency 'base64'
s.add_dependency 'mutex_m'
Expand All @@ -30,7 +30,7 @@ Gem::Specification.new do |s|
s.add_development_dependency 'rake', '~> 13.0'
s.add_development_dependency 'rspec', '~> 3.5'
s.add_development_dependency 'mocha', '~> 0.13'
s.add_development_dependency 'puma', '~> 5.0'
s.add_development_dependency 'puma', '~> 6.0'
s.add_development_dependency 'webmock'

s.metadata["rubygems_mfa_required"] = "true"
Expand Down
4 changes: 2 additions & 2 deletions lib/httpi/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,12 @@ def ssl?

# Returns a Hash of HTTP headers. Defaults to return an empty Hash.
def headers
@headers ||= Rack::Utils::HeaderHash.new
@headers ||= Rack::Headers.new
end

# Sets the Hash of HTTP headers.
def headers=(headers)
@headers = Rack::Utils::HeaderHash.new(headers)
@headers = Rack::Headers.new.merge(headers)
end

This comment has been minimized.

Copy link
@frodsan

frodsan Feb 14, 2024

Is it possible to make this backward compatible like:

@headers = Rack.const_defined?(:Headers) ?  Rack::Headers.new.merge(headers) : Rack::Utils::HeaderHash.new(headers)

I can't use the latest savon on my Rails app:

NameError:
       uninitialized constant Rack::Headers
     # /usr/local/bundle/ruby/3.2.0/gems/httpi-4.0.0/lib/httpi/request.rb:67:in `headers'
     # /usr/local/bundle/ruby/3.2.0/gems/savon-2.15.0/lib/savon/request.rb:109:in `configure_headers'
     # /usr/local/bundle/ruby/3.2.0/gems/savon-2.15.0/lib/savon/request.rb:91:in `build'

This comment has been minimized.

Copy link
@pcai

pcai Feb 14, 2024

Author Member

tracked as #244


# Adds a header information to accept gzipped content.
Expand Down
2 changes: 1 addition & 1 deletion lib/httpi/response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class Response
# Initializer expects an HTTP response +code+, +headers+ and +body+.
def initialize(code, headers, body)
self.code = code.to_i
self.headers = Rack::Utils::HeaderHash.new(headers)
self.headers = Rack::Headers.new.merge(headers)
self.raw_body = body
end

Expand Down
8 changes: 4 additions & 4 deletions spec/httpi/request_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,11 @@
after { HTTPI.query_builder = :flat }

it "lets you specify query parameter as Hash" do
expect(request.url.to_s).to eq("http://example.com?q[]=nested&q[]=query")
expect(request.url.to_s).to eq("http://example.com?q%5B%5D=nested&q%5B%5D=query")
end

it "getter return String for query parameter as Hash" do
expect(request.query).to eq("q[]=nested&q[]=query")
expect(request.query).to eq("q%5B%5D=nested&q%5B%5D=query")
end
end
end
Expand Down Expand Up @@ -149,7 +149,7 @@
describe "#headers" do
it "lets you specify a Hash of HTTP request headers" do
request.headers = { "Accept-Encoding" => "gzip" }
expect(request.headers).to eq({ "Accept-Encoding" => "gzip" })
expect(request.headers).to eq Rack::Headers.new.merge({ "Accept-Encoding" => "gzip" })
end

it "defaults to return an empty Hash" do
Expand Down Expand Up @@ -235,7 +235,7 @@ def response_with_cookie(cookie)
end
it "request body using a Hash with Array" do
request.body = {:foo => :bar, :baz => [:foo, :tst]}
expect(request.body.split("&")).to match_array(["foo=bar", "baz[]=foo", "baz[]=tst"])
expect(request.body.split("&")).to match_array(["foo=bar", "baz%5B%5D=foo", "baz%5B%5D=tst"])
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions spec/httpi/response_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@

describe "#headers" do
it "returns the HTTP response headers" do
expect(response.headers).to eq({ "Content-Encoding" => "gzip" })
expect(response.headers).to eq Rack::Headers.new.merge({ "Content-Encoding" => "gzip" })
end
end

Expand Down Expand Up @@ -116,7 +116,7 @@

describe "#headers" do
it "returns the HTTP response headers" do
expect(response.headers).to eq({ "Content-Type" => "application/dime" })
expect(response.headers).to eq Rack::Headers.new.merge({ "Content-Type" => "application/dime" })
end
end

Expand Down
2 changes: 1 addition & 1 deletion spec/integration/support/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def stop
private

def events
Puma::Events.new($stdout, $stderr)
Puma::Events.new
end

def add_tcp_listener
Expand Down

0 comments on commit 56cd67b

Please sign in to comment.