Permalink
Browse files

Use inspect in the http service logger for better output

  • Loading branch information...
1 parent eaee3c9 commit 715b29acf8ff147fc94a5b67a4cb8666fd4f4196 @arsduo committed Apr 1, 2012
Showing with 56 additions and 56 deletions.
  1. +26 −26 lib/koala/http_service.rb
  2. +30 −30 spec/cases/http_service_spec.rb
View
@@ -4,7 +4,7 @@
require 'koala/http_service/response'
module Koala
- module HTTPService
+ module HTTPService
class << self
# A customized stack of Faraday middleware that will be used to make each request.
attr_accessor :faraday_middleware
@@ -13,9 +13,9 @@ class << self
end
@http_options ||= {}
-
- # Koala's default middleware stack.
- # We encode requests in a Facebook-compatible multipart request,
+
+ # Koala's default middleware stack.
+ # We encode requests in a Facebook-compatible multipart request,
# and use whichever adapter has been configured for this application.
DEFAULT_MIDDLEWARE = Proc.new do |builder|
builder.use Koala::HTTPService::MultipartRequest
@@ -30,7 +30,7 @@ class << self
# @option options :video use the server designated for video uploads
# @option options :beta use the beta tier
# @option options :use_ssl force https, even if not needed
- #
+ #
# @return a complete server address with protocol
def self.server(options = {})
server = "#{options[:rest_api] ? Facebook::REST_SERVER : Facebook::GRAPH_SERVER}"
@@ -48,9 +48,9 @@ def self.server(options = {})
#
# @param path the server path for this request
# @param args (see Koala::Facebook::API#api)
- # @param verb the HTTP method to use.
+ # @param verb the HTTP method to use.
# If not get or post, this will be turned into a POST request with the appropriate :method
- # specified in the arguments.
+ # specified in the arguments.
# @param options (see Koala::Facebook::API#api)
#
# @raise an appropriate connection error if unable to make the request to Facebook
@@ -63,7 +63,7 @@ def self.make_request(path, args, verb, options = {})
# turn all the keys to strings (Faraday has issues with symbols under 1.8.7) and resolve UploadableIOs
params = args.inject({}) {|hash, kv| hash[kv.first.to_s] = kv.last.is_a?(UploadableIO) ? kv.last.to_upload_io : kv.last; hash}
- # figure out our options for this request
+ # figure out our options for this request
request_options = {:params => (verb == "get" ? params : {})}.merge(http_options || {}).merge(process_options(options))
request_options[:use_ssl] = true if args["access_token"] # require https if there's a token
@@ -72,17 +72,17 @@ def self.make_request(path, args, verb, options = {})
conn = Faraday.new(server(request_options), request_options, &(faraday_middleware || DEFAULT_MIDDLEWARE))
response = conn.send(verb, path, (verb == "post" ? params : {}))
-
+
# Log URL information
- Koala::Utils.debug "#{verb.upcase}: #{path} params: #{params}"
+ Koala::Utils.debug "#{verb.upcase}: #{path} params: #{params.inspect}"
Koala::HTTPService::Response.new(response.status.to_i, response.body, response.headers)
end
- # Encodes a given hash into a query string.
+ # Encodes a given hash into a query string.
# This is used mainly by the Batch API nowadays, since Faraday handles this for regular cases.
- #
+ #
# @param params_hash a hash of values to CGI-encode and appropriately join
- #
+ #
# @example
# Koala.http_service.encode_params({:a => 2, :b => "My String"})
# => "a=2&b=My+String"
@@ -94,10 +94,10 @@ def self.encode_params(param_hash)
"#{key_and_value[0].to_s}=#{CGI.escape key_and_value[1]}"
end).join("&")
end
-
+
# deprecations
# not elegant or compact code, but temporary
-
+
# @private
def self.always_use_ssl
Koala::Utils.deprecate("HTTPService.always_use_ssl is now HTTPService.http_options[:use_ssl]; always_use_ssl will be removed in a future version.")
@@ -109,7 +109,7 @@ def self.always_use_ssl=(value)
Koala::Utils.deprecate("HTTPService.always_use_ssl is now HTTPService.http_options[:use_ssl]; always_use_ssl will be removed in a future version.")
http_options[:use_ssl] = value
end
-
+
# @private
def self.timeout
Koala::Utils.deprecate("HTTPService.timeout is now HTTPService.http_options[:timeout]; .timeout will be removed in a future version.")
@@ -121,7 +121,7 @@ def self.timeout=(value)
Koala::Utils.deprecate("HTTPService.timeout is now HTTPService.http_options[:timeout]; .timeout will be removed in a future version.")
http_options[:timeout] = value
end
-
+
# @private
def self.timeout
Koala::Utils.deprecate("HTTPService.timeout is now HTTPService.http_options[:timeout]; .timeout will be removed in a future version.")
@@ -133,7 +133,7 @@ def self.timeout=(value)
Koala::Utils.deprecate("HTTPService.timeout is now HTTPService.http_options[:timeout]; .timeout will be removed in a future version.")
http_options[:timeout] = value
end
-
+
# @private
def self.proxy
Koala::Utils.deprecate("HTTPService.proxy is now HTTPService.http_options[:proxy]; .proxy will be removed in a future version.")
@@ -145,7 +145,7 @@ def self.proxy=(value)
Koala::Utils.deprecate("HTTPService.proxy is now HTTPService.http_options[:proxy]; .proxy will be removed in a future version.")
http_options[:proxy] = value
end
-
+
# @private
def self.ca_path
Koala::Utils.deprecate("HTTPService.ca_path is now (HTTPService.http_options[:ssl] ||= {})[:ca_path]; .ca_path will be removed in a future version.")
@@ -157,7 +157,7 @@ def self.ca_path=(value)
Koala::Utils.deprecate("HTTPService.ca_path is now (HTTPService.http_options[:ssl] ||= {})[:ca_path]; .ca_path will be removed in a future version.")
(http_options[:ssl] ||= {})[:ca_path] = value
end
-
+
# @private
def self.ca_file
Koala::Utils.deprecate("HTTPService.ca_file is now (HTTPService.http_options[:ssl] ||= {})[:ca_file]; .ca_file will be removed in a future version.")
@@ -182,14 +182,14 @@ def self.verify_mode=(value)
(http_options[:ssl] ||= {})[:verify_mode] = value
end
- private
-
+ private
+
def self.process_options(options)
if typhoeus_options = options.delete(:typhoeus_options)
Koala::Utils.deprecate("typhoeus_options should now be included directly in the http_options hash. Support for this key will be removed in a future version.")
options = options.merge(typhoeus_options)
end
-
+
if ca_file = options.delete(:ca_file)
Koala::Utils.deprecate("http_options[:ca_file] should now be passed inside (http_options[:ssl] = {}) -- that is, http_options[:ssl][:ca_file]. Support for this key will be removed in a future version.")
(options[:ssl] ||= {})[:ca_file] = ca_file
@@ -204,11 +204,11 @@ def self.process_options(options)
Koala::Utils.deprecate("http_options[:verify_mode] should now be passed inside (http_options[:ssl] = {}) -- that is, http_options[:ssl][:verify_mode]. Support for this key will be removed in a future version.")
(options[:ssl] ||= {})[:verify_mode] = verify_mode
end
-
+
options
- end
+ end
end
-
+
# @private
module TyphoeusService
def self.deprecated_interface
@@ -10,7 +10,7 @@
Koala::HTTPService.should respond_to(:http_options)
Koala::HTTPService.should respond_to(:http_options=)
end
-
+
it "sets http_options to {} by default" do
Koala::HTTPService.http_options.should == {}
end
@@ -123,10 +123,10 @@
val.should == CGI.escape(args[key])
end
end
-
+
it "encodes parameters in alphabetical order" do
args = {:b => '2', 'a' => '1'}
-
+
result = Koala::HTTPService.encode_params(args)
result.split('&').map{|key_val| key_val.split('=')[0]}.should == ['a', 'b']
end
@@ -184,33 +184,33 @@
Faraday.should_receive(:new).with(anything, hash_including(http_options.merge(options))).and_return(@mock_connection)
Koala::HTTPService.make_request("anything", {}, "get", options)
end
-
+
it "forces use_ssl to true if an access token is present" do
options = {:use_ssl => false}
Koala::HTTPService.stub(:http_options).and_return(:use_ssl => false)
Faraday.should_receive(:new).with(anything, hash_including(:use_ssl => true)).and_return(@mock_connection)
Koala::HTTPService.make_request("anything", {"access_token" => "foo"}, "get", options)
end
-
+
it "calls server with the composite options" do
options = {:a => 2, :c => "3"}
http_options = {:a => :a}
Koala::HTTPService.stub(:http_options).and_return(http_options)
Koala::HTTPService.should_receive(:server).with(hash_including(http_options.merge(options))).and_return("foo")
- Koala::HTTPService.make_request("anything", {}, "get", options)
+ Koala::HTTPService.make_request("anything", {}, "get", options)
end
it "uses the default builder block if HTTPService.faraday_middleware block is not defined" do
- Koala::HTTPService.stub(:faraday_middleware).and_return(nil)
+ Koala::HTTPService.stub(:faraday_middleware).and_return(nil)
Faraday.should_receive(:new).with(anything, anything, &Koala::HTTPService::DEFAULT_MIDDLEWARE).and_return(@mock_connection)
- Koala::HTTPService.make_request("anything", {}, "get")
+ Koala::HTTPService.make_request("anything", {}, "get")
end
-
+
it "uses the defined HTTPService.faraday_middleware block if defined" do
block = Proc.new { }
Koala::HTTPService.should_receive(:faraday_middleware).and_return(block)
Faraday.should_receive(:new).with(anything, anything, &block).and_return(@mock_connection)
- Koala::HTTPService.make_request("anything", {}, "get")
+ Koala::HTTPService.make_request("anything", {}, "get")
end
end
@@ -244,10 +244,10 @@
@mock_connection.should_receive(:get).with(anything, {}).and_return(@mock_http_response)
Koala::HTTPService.make_request("anything", args, "get")
end
-
+
it "logs verb, url and params to debug" do
args = {"a" => :b, "c" => 3}
- log_message = "GET: anything params: #{args}"
+ log_message = "GET: anything params: #{args.inspect}"
Koala::Utils.logger.should_receive(:debug).with(log_message)
Koala::HTTPService.make_request("anything", args, "get")
end
@@ -288,11 +288,11 @@
after :each do
Koala.http_service = @service
end
-
+
{
:timeout => :timeout,
:always_use_ssl => :use_ssl,
- :proxy => :proxy
+ :proxy => :proxy
}.each_pair do |deprecated_method, parameter|
describe ".#{deprecated_method}" do
context "read" do
@@ -301,29 +301,29 @@
Koala::HTTPService.http_options[parameter] = value
Koala::HTTPService.send(deprecated_method).should == value
end
-
+
it "generates a deprecation warning" do
Koala::Utils.should_receive(:deprecate)
Koala::HTTPService.send(deprecated_method)
end
end
-
+
context "write" do
it "writes to http_options[:#{parameter}]" do
Koala::HTTPService.http_options[parameter] = nil
value = "foo"
Koala::HTTPService.send(:"#{deprecated_method}=", value)
Koala::HTTPService.http_options[parameter].should == value
end
-
+
it "generates a deprecation warning" do
Koala::Utils.should_receive(:deprecate)
Koala::HTTPService.send(:"#{deprecated_method}=", 2)
end
end
end
end
-
+
# ssl options
[:ca_path, :ca_file, :verify_mode].each do |deprecated_method|
describe ".#{deprecated_method}" do
@@ -333,7 +333,7 @@
Koala::HTTPService.http_options[:ssl] = {deprecated_method => value}
Koala::HTTPService.send(deprecated_method).should == value
end
-
+
it "returns nil if http_options[:ssl] is not defined" do
Koala::HTTPService.send(deprecated_method).should be_nil
end
@@ -343,8 +343,8 @@
Koala::HTTPService.send(deprecated_method)
end
end
-
- context "write" do
+
+ context "write" do
it "defines http_options[:ssl] if not defined" do
Koala::HTTPService.http_options[:ssl] = nil
value = "foo"
@@ -358,22 +358,22 @@
Koala::HTTPService.http_options[:ssl].should
Koala::HTTPService.http_options[:ssl][deprecated_method].should == value
end
-
+
it "does not redefine http_options[:ssl] if already defined" do
hash = {:a => 2}
Koala::HTTPService.http_options[:ssl] = hash
Koala::HTTPService.send(:"#{deprecated_method}=", 3)
Koala::HTTPService.http_options[:ssl].should include(hash)
end
-
+
it "generates a deprecation warning" do
Koala::Utils.should_receive(:deprecate)
Koala::HTTPService.send(:"#{deprecated_method}=", 2)
end
end
end
end
-
+
describe "per-request options" do
before :each do
# Setup stubs for make_request to execute without exceptions
@@ -386,18 +386,18 @@
@mock_connection.stub(:post).and_return(@mock_http_response)
Faraday.stub(:new).and_return(@mock_connection)
end
-
+
describe ":typhoeus_options" do
it "merges any typhoeus_options into options" do
typhoeus_options = {:a => 2}
Faraday.should_receive(:new).with(anything, hash_including(typhoeus_options)).and_return(@mock_connection)
- Koala::HTTPService.make_request("anything", {}, "get", :typhoeus_options => typhoeus_options)
+ Koala::HTTPService.make_request("anything", {}, "get", :typhoeus_options => typhoeus_options)
end
-
+
it "deletes the typhoeus_options key" do
typhoeus_options = {:a => 2}
Faraday.should_receive(:new).with(anything, hash_not_including(:typhoeus_options => typhoeus_options)).and_return(@mock_connection)
- Koala::HTTPService.make_request("anything", {}, "get", :typhoeus_options => typhoeus_options)
+ Koala::HTTPService.make_request("anything", {}, "get", :typhoeus_options => typhoeus_options)
end
end
@@ -414,7 +414,7 @@
Koala::HTTPService.make_request("anything", {}, "get", :ca_path => ca_path)
end
end
-
+
describe ":ca_file" do
it "sets any ca_file into options[:ssl]" do
ca_file = :foo
@@ -428,7 +428,7 @@
Koala::HTTPService.make_request("anything", {}, "get", :ca_file => ca_file)
end
end
-
+
describe ":verify_mode" do
it "sets any verify_mode into options[:ssl]" do
verify_mode = :foo

0 comments on commit 715b29a

Please sign in to comment.