diff --git a/lib/http/features/auto_inflate.rb b/lib/http/features/auto_inflate.rb index 6b659560..e1d383ef 100644 --- a/lib/http/features/auto_inflate.rb +++ b/lib/http/features/auto_inflate.rb @@ -21,8 +21,6 @@ def wrap_response(response) :request => response.request } - options[:uri] = response.uri if response.uri - Response.new(options) end diff --git a/lib/http/response.rb b/lib/http/response.rb index 0d3cd48f..3194556f 100644 --- a/lib/http/response.rb +++ b/lib/http/response.rb @@ -40,10 +40,11 @@ class Response # @option opts [HTTP::Connection] :connection # @option opts [String] :encoding Encoding to use when reading body # @option opts [String] :body - # @option opts [HTTP::Request] request + # @option opts [HTTP::Request] request The request this is in response to. + # @option opts [String] :uri (DEPRECATED) used to populate a missing request def initialize(opts) @version = opts.fetch(:version) - @request = opts.fetch(:request) + @request = init_request(opts) @status = HTTP::Response::Status.new(opts.fetch(:status)) @headers = HTTP::Headers.coerce(opts[:headers] || {}) @proxy_headers = HTTP::Headers.coerce(opts[:proxy_headers] || {}) @@ -164,5 +165,22 @@ def parse(type = nil) def inspect "#<#{self.class}/#{@version} #{code} #{reason} #{headers.to_h.inspect}>" end + + private + + # Initialize an HTTP::Request from options. + # + # @return [HTTP::Request] + def init_request(opts) + raise ArgumentError, ":uri is for backwards compatibilty and conflicts with :request" \ + if opts[:request] && opts[:uri] + + # For backwards compatibilty + if opts[:uri] + HTTP::Request.new(:uri => opts[:uri], :verb => :get) + else + opts.fetch(:request) + end + end end end diff --git a/spec/lib/http/features/auto_inflate_spec.rb b/spec/lib/http/features/auto_inflate_spec.rb index 3350055f..f6c345d0 100644 --- a/spec/lib/http/features/auto_inflate_spec.rb +++ b/spec/lib/http/features/auto_inflate_spec.rb @@ -74,7 +74,6 @@ :status => 200, :headers => {:content_encoding => "gzip"}, :connection => connection, - :uri => "https://example.com", :request => HTTP::Request.new(:verb => :get, :uri => "https://example.com") ) end diff --git a/spec/lib/http/features/instrumentation_spec.rb b/spec/lib/http/features/instrumentation_spec.rb index 4e03d08c..2c733396 100644 --- a/spec/lib/http/features/instrumentation_spec.rb +++ b/spec/lib/http/features/instrumentation_spec.rb @@ -46,7 +46,6 @@ def finish(_name, payload) let(:response) do HTTP::Response.new( :version => "1.1", - :uri => "https://example.com", :status => 200, :headers => {:content_type => "application/json"}, :body => '{"success": true}', diff --git a/spec/lib/http/features/logging_spec.rb b/spec/lib/http/features/logging_spec.rb index 4b43adeb..c3518594 100644 --- a/spec/lib/http/features/logging_spec.rb +++ b/spec/lib/http/features/logging_spec.rb @@ -42,7 +42,6 @@ let(:response) do HTTP::Response.new( :version => "1.1", - :uri => "https://example.com", :status => 200, :headers => {:content_type => "application/json"}, :body => '{"success": true}', diff --git a/spec/lib/http/response_spec.rb b/spec/lib/http/response_spec.rb index eff67a5e..909f3ce2 100644 --- a/spec/lib/http/response_spec.rb +++ b/spec/lib/http/response_spec.rb @@ -4,6 +4,7 @@ let(:body) { "Hello world!" } let(:uri) { "http://example.com/" } let(:headers) { {} } + let(:request) { HTTP::Request.new(:verb => :get, :uri => uri) } subject(:response) do HTTP::Response.new( @@ -11,8 +12,7 @@ :version => "1.1", :headers => headers, :body => body, - :uri => uri, - :request => HTTP::Request.new(:verb => :get, :uri => "http://example.com") + :request => request ) end @@ -167,7 +167,7 @@ :version => "1.1", :status => 200, :connection => connection, - :request => HTTP::Request.new(:verb => :get, :uri => "http://example.com") + :request => request ) end @@ -184,4 +184,43 @@ end it { is_expected.not_to be_chunked } end + + describe "backwards compatibilty with :uri" do + context "with no :verb" do + subject(:response) do + HTTP::Response.new( + :status => 200, + :version => "1.1", + :headers => headers, + :body => body, + :uri => uri + ) + end + + it "defaults the uri to :uri" do + expect(response.request.uri.to_s).to eq uri + end + + it "defaults to the verb to :get" do + expect(response.request.verb).to eq :get + end + end + + context "with both a :request and :uri" do + subject(:response) do + HTTP::Response.new( + :status => 200, + :version => "1.1", + :headers => headers, + :body => body, + :uri => uri, + :request => request + ) + end + + it "raises ArgumentError" do + expect { response }.to raise_error(ArgumentError) + end + end + end end