Permalink
Browse files

Add support for patron

  • Loading branch information...
1 parent a15d9e7 commit ba28a5ab7b343ac1120e30ba5812e24658e4b0a9 Doug Johnston committed Dec 4, 2012
Showing with 71 additions and 0 deletions.
  1. +2 −0 Gemfile.lock
  2. +1 −0 httplog.gemspec
  3. +1 −0 lib/httplog.rb
  4. +21 −0 lib/httplog/adapters/patron.rb
  5. +12 −0 spec/adapters/patron_adapter.rb
  6. +33 −0 spec/http_log_spec.rb
  7. +1 −0 spec/spec_helper.rb
View
@@ -24,6 +24,7 @@ GEM
multi_json (1.3.7)
multi_xml (0.5.1)
multipart-post (1.1.5)
+ patron (0.4.18)
rack (1.3.3)
rspec (2.6.0)
rspec-core (~> 2.6.0)
@@ -50,6 +51,7 @@ DEPENDENCIES
httparty
httpclient
httplog!
+ patron
rack
rspec
thin
View
@@ -25,4 +25,5 @@ Gem::Specification.new do |s|
s.add_development_dependency "excon"
s.add_development_dependency "typhoeus"
s.add_development_dependency "ethon"
+ s.add_development_dependency "patron"
end
View
@@ -4,3 +4,4 @@
require "httplog/adapters/httpclient"
require "httplog/adapters/excon"
require "httplog/adapters/ethon"
+require "httplog/adapters/patron"
@@ -0,0 +1,21 @@
+if defined?(Patron)
+ module Patron
+ class Session
+ alias_method :orig_request, :request
+ def request(action_name, url, headers, options = {})
+ HttpLog.log_request(action_name, url)
+ HttpLog.log_headers(headers)
+ HttpLog.log_data(options[:data]) if action_name == :post
+
+ bm = Benchmark.realtime do
+ @response = orig_request(action_name, url, headers, options)
+ end
+
+ HttpLog.log_compact(action_name, url, @response.status, bm)
+ HttpLog.log_status(@response.status)
+ HttpLog.log_benchmark(bm)
+ HttpLog.log_body(@response.body)
+ end
+ end
+ end
+end
@@ -0,0 +1,12 @@
+require "patron"
+class PatronAdapter < HTTPBaseAdapter
+ def send_get_request
+ session = Patron::Session.new
+ session.get(parse_uri.to_s, @headers)
+ end
+
+ def send_post_request
+ session = Patron::Session.new
+ session.post(parse_uri.to_s, @data, @headers)
+ end
+end
View
@@ -335,4 +335,37 @@
end
end
end
+
+ context "Patron" do
+ let(:adapter) { PatronAdapter.new(@host, @port, @path) }
+ context "with all options" do
+ before { HttpLog.options[:log_headers] = true }
+
+ it "should log GET requests" do
+ adapter.send_get_request
+ log.should_not include("[httplog] Connecting: #{@host}:#{@port}")
+ log.should include("[httplog] Sending: GET http://#{@host}:#{@port}#{@path}")
+ log.should include("[httplog] Header: accept: */*")
+ log.should include("[httplog] Header: foo: bar")
+ log.should_not include("[httplog] Data:")
+ log.should include("[httplog] Status: 200")
+ log.should include("[httplog] Benchmark: ")
+ log.should include("[httplog] Response:")
+ log.should include("<html>")
+ end
+
+ it "should log POST requests" do
+ adapter.send_post_request
+ log.should_not include("[httplog] Connecting: #{@host}:#{@port}")
+ log.should include("[httplog] Sending: POST http://#{@host}:#{@port}#{@path}")
+ log.should include("[httplog] Header: accept: */*")
+ log.should include("[httplog] Header: foo: bar")
+ log.should include("[httplog] Data: #{@data}")
+ log.should include("[httplog] Status: 200")
+ log.should include("[httplog] Benchmark: ")
+ log.should include("[httplog] Response:")
+ log.should include("<html>")
+ end
+ end
+ end
end
View
@@ -5,6 +5,7 @@
require 'excon'
require 'typhoeus'
require 'ethon'
+require 'patron'
require 'httplog'
require 'adapters/http_base_adapter'

0 comments on commit ba28a5a

Please sign in to comment.