Skip to content

Commit

Permalink
Extract request building method
Browse files Browse the repository at this point in the history
  • Loading branch information
aliismayilov committed Aug 19, 2023
1 parent 30f2ec1 commit a8b77b0
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
16 changes: 12 additions & 4 deletions lib/httparty.rb
Original file line number Diff line number Diff line change
Expand Up @@ -591,6 +591,13 @@ def unlock(path, options = {}, &block)
perform_request Net::HTTP::Unlock, path, options, &block
end

def build_request(http_method, path, options = {})
options = ModuleInheritableAttributes.hash_deep_dup(default_options).merge(options)
HeadersProcessor.new(headers, options).call
process_cookies(options)
Request.new(http_method, path, options)
end

attr_reader :default_options

private
Expand All @@ -606,10 +613,7 @@ def ensure_method_maintained_across_redirects(options)
end

def perform_request(http_method, path, options, &block) #:nodoc:
options = ModuleInheritableAttributes.hash_deep_dup(default_options).merge(options)
HeadersProcessor.new(headers, options).call
process_cookies(options)
Request.new(http_method, path, options).perform(&block)
build_request(http_method, path, options).perform(&block)
end

def process_cookies(options) #:nodoc:
Expand Down Expand Up @@ -676,6 +680,10 @@ def self.head(*args, &block)
def self.options(*args, &block)
Basement.options(*args, &block)
end

def self.build_request(*args, &block)
Basement.build_request(*args, &block)
end
end

require 'httparty/hash_conversions'
Expand Down
9 changes: 9 additions & 0 deletions spec/httparty_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -970,4 +970,13 @@ def self.inherited(subclass)
end.to raise_error(URI::InvalidURIError)
end
end

describe "#build_request" do
it "should build a request object" do
stub_http_response_with('example.html')
request = HTTParty.build_request(Net::HTTP::Get, "http://www.example.com")
expect(request).to be_a(HTTParty::Request)
expect(request.perform.parsed_response).to eq(file_fixture('example.html'))
end
end
end

0 comments on commit a8b77b0

Please sign in to comment.