mirror of
https://github.com/jnunemaker/httparty
synced 2023-03-27 23:23:07 -04:00
refactor private request methods to make them better testable
Signed-off-by: John Nunemaker <nunemaker@gmail.com>
This commit is contained in:
parent
d05dab8855
commit
d7e5fccfe3
2 changed files with 21 additions and 17 deletions
|
@ -31,6 +31,7 @@ module HTTParty
|
||||||
|
|
||||||
def perform
|
def perform
|
||||||
validate!
|
validate!
|
||||||
|
setup_raw_request
|
||||||
handle_response!(get_response)
|
handle_response!(get_response)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -41,22 +42,28 @@ module HTTParty
|
||||||
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
||||||
http
|
http
|
||||||
end
|
end
|
||||||
|
|
||||||
def get_response #:nodoc:
|
def setup_raw_request
|
||||||
request = http_method.new(uri.request_uri)
|
@raw_request = http_method.new(uri.request_uri)
|
||||||
|
|
||||||
if post? && options[:query]
|
if post? && options[:query]
|
||||||
request.set_form_data(options[:query])
|
@raw_request.set_form_data(options[:query])
|
||||||
end
|
end
|
||||||
|
|
||||||
request.body = options[:body].is_a?(Hash) ? options[:body].to_params : options[:body] unless options[:body].blank?
|
@raw_request.body = options[:body].is_a?(Hash) ? options[:body].to_params : options[:body] unless options[:body].blank?
|
||||||
request.initialize_http_header options[:headers]
|
@raw_request.initialize_http_header options[:headers]
|
||||||
|
|
||||||
if options[:basic_auth]
|
if options[:basic_auth]
|
||||||
request.basic_auth(options[:basic_auth][:username], options[:basic_auth][:password])
|
@raw_request.basic_auth(options[:basic_auth][:username], options[:basic_auth][:password])
|
||||||
end
|
end
|
||||||
|
end
|
||||||
response = http(uri).request(request)
|
|
||||||
|
def perform_actual_request
|
||||||
|
http(uri).request(@raw_request)
|
||||||
|
end
|
||||||
|
|
||||||
|
def get_response #:nodoc:
|
||||||
|
response = perform_actual_request
|
||||||
options[:format] ||= format_from_mimetype(response['content-type'])
|
options[:format] ||= format_from_mimetype(response['content-type'])
|
||||||
response
|
response
|
||||||
end
|
end
|
||||||
|
|
|
@ -10,15 +10,12 @@ end
|
||||||
def stub_http_response_with(filename)
|
def stub_http_response_with(filename)
|
||||||
format = filename.split('.').last.intern
|
format = filename.split('.').last.intern
|
||||||
data = file_fixture(filename)
|
data = file_fixture(filename)
|
||||||
http = Net::HTTP.new('localhost', 80)
|
|
||||||
|
|
||||||
response = Net::HTTPOK.new("1.1", 200, "Content for you")
|
response = Net::HTTPOK.new("1.1", 200, "Content for you")
|
||||||
response.stub!(:body).and_return(data)
|
response.stub!(:body).and_return(data)
|
||||||
http.stub!(:request).and_return(response)
|
|
||||||
|
http_request = HTTParty::Request.new(Net::HTTP::Get, 'http://localhost', :format => format)
|
||||||
http_request = HTTParty::Request.new(Net::HTTP::Get, '')
|
http_request.stub!(:perform_actual_request).and_return(response)
|
||||||
http_request.stub!(:get_response).and_return(response)
|
|
||||||
http_request.stub!(:format).and_return(format)
|
|
||||||
|
|
||||||
HTTParty::Request.should_receive(:new).and_return(http_request)
|
HTTParty::Request.should_receive(:new).and_return(http_request)
|
||||||
end
|
end
|
Loading…
Add table
Reference in a new issue