Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extract request building method #786

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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