Skip to content

Commit 67cbfbd

Browse files
authored
Merge pull request #786 from aliismayilov/extract-request-building-method
Extract request building method
2 parents 30f2ec1 + a8b77b0 commit 67cbfbd

File tree

2 files changed

+21
-4
lines changed

2 files changed

+21
-4
lines changed

lib/httparty.rb

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -591,6 +591,13 @@ def unlock(path, options = {}, &block)
591591
perform_request Net::HTTP::Unlock, path, options, &block
592592
end
593593

594+
def build_request(http_method, path, options = {})
595+
options = ModuleInheritableAttributes.hash_deep_dup(default_options).merge(options)
596+
HeadersProcessor.new(headers, options).call
597+
process_cookies(options)
598+
Request.new(http_method, path, options)
599+
end
600+
594601
attr_reader :default_options
595602

596603
private
@@ -606,10 +613,7 @@ def ensure_method_maintained_across_redirects(options)
606613
end
607614

608615
def perform_request(http_method, path, options, &block) #:nodoc:
609-
options = ModuleInheritableAttributes.hash_deep_dup(default_options).merge(options)
610-
HeadersProcessor.new(headers, options).call
611-
process_cookies(options)
612-
Request.new(http_method, path, options).perform(&block)
616+
build_request(http_method, path, options).perform(&block)
613617
end
614618

615619
def process_cookies(options) #:nodoc:
@@ -676,6 +680,10 @@ def self.head(*args, &block)
676680
def self.options(*args, &block)
677681
Basement.options(*args, &block)
678682
end
683+
684+
def self.build_request(*args, &block)
685+
Basement.build_request(*args, &block)
686+
end
679687
end
680688

681689
require 'httparty/hash_conversions'

spec/httparty_spec.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -970,4 +970,13 @@ def self.inherited(subclass)
970970
end.to raise_error(URI::InvalidURIError)
971971
end
972972
end
973+
974+
describe "#build_request" do
975+
it "should build a request object" do
976+
stub_http_response_with('example.html')
977+
request = HTTParty.build_request(Net::HTTP::Get, "http://www.example.com")
978+
expect(request).to be_a(HTTParty::Request)
979+
expect(request.perform.parsed_response).to eq(file_fixture('example.html'))
980+
end
981+
end
973982
end

0 commit comments

Comments
 (0)