Permalink
Browse files

initial commit

  • Loading branch information...
0 parents commit 44185fec1a550447f4e4a56d587d805030677267 @trusche committed Jul 17, 2011
Showing with 311 additions and 0 deletions.
  1. +6 −0 .gitignore
  2. +12 −0 Gemfile
  3. +97 −0 Gemfile.lock
  4. +20 −0 MIT-LICENSE
  5. +3 −0 README.rdoc
  6. +34 −0 Rakefile
  7. +9 −0 httplog.gemspec
  8. +1 −0 lib/httplog.rb
  9. +45 −0 lib/httplog/http_log.rb
  10. +4 −0 lib/tasks/http_log_tasks.rake
  11. +60 −0 spec/http_log_spec.rb
  12. +20 −0 spec/spec_helper.rb
@@ -0,0 +1,6 @@
+.bundle/
+log/*.log
+pkg/
+test/dummy/db/*.sqlite3
+test/dummy/log/*.log
+test/dummy/tmp/
12 Gemfile
@@ -0,0 +1,12 @@
+source "http://rubygems.org"
+
+gem 'rails', '3.1.0.rc4'
+
+# Bundle edge Rails instead:
+# gem 'rails', :git => 'git://github.com/rails/rails.git'
+
+gem 'rspec'
+
+if RUBY_VERSION < '1.9'
+ gem "ruby-debug", ">= 0.10.3"
+end
@@ -0,0 +1,97 @@
+GEM
+ remote: http://rubygems.org/
+ specs:
+ actionmailer (3.1.0.rc4)
+ actionpack (= 3.1.0.rc4)
+ mail (~> 2.3.0)
+ actionpack (3.1.0.rc4)
+ activemodel (= 3.1.0.rc4)
+ activesupport (= 3.1.0.rc4)
+ builder (~> 3.0.0)
+ erubis (~> 2.7.0)
+ i18n (~> 0.6)
+ rack (~> 1.3.0)
+ rack-cache (~> 1.0.1)
+ rack-mount (~> 0.8.1)
+ rack-test (~> 0.6.0)
+ sprockets (~> 2.0.0.beta.10)
+ tzinfo (~> 0.3.27)
+ activemodel (3.1.0.rc4)
+ activesupport (= 3.1.0.rc4)
+ bcrypt-ruby (~> 2.1.4)
+ builder (~> 3.0.0)
+ i18n (~> 0.6)
+ activerecord (3.1.0.rc4)
+ activemodel (= 3.1.0.rc4)
+ activesupport (= 3.1.0.rc4)
+ arel (~> 2.1.1)
+ tzinfo (~> 0.3.27)
+ activeresource (3.1.0.rc4)
+ activemodel (= 3.1.0.rc4)
+ activesupport (= 3.1.0.rc4)
+ activesupport (3.1.0.rc4)
+ multi_json (~> 1.0)
+ arel (2.1.3)
+ bcrypt-ruby (2.1.4)
+ builder (3.0.0)
+ diff-lcs (1.1.2)
+ erubis (2.7.0)
+ hike (1.1.0)
+ i18n (0.6.0)
+ mail (2.3.0)
+ i18n (>= 0.4.0)
+ mime-types (~> 1.16)
+ treetop (~> 1.4.8)
+ mime-types (1.16)
+ multi_json (1.0.3)
+ polyglot (0.3.1)
+ rack (1.3.1)
+ rack-cache (1.0.2)
+ rack (>= 0.4)
+ rack-mount (0.8.1)
+ rack (>= 1.0.0)
+ rack-ssl (1.3.2)
+ rack
+ rack-test (0.6.0)
+ rack (>= 1.0)
+ rails (3.1.0.rc4)
+ actionmailer (= 3.1.0.rc4)
+ actionpack (= 3.1.0.rc4)
+ activerecord (= 3.1.0.rc4)
+ activeresource (= 3.1.0.rc4)
+ activesupport (= 3.1.0.rc4)
+ bundler (~> 1.0)
+ railties (= 3.1.0.rc4)
+ railties (3.1.0.rc4)
+ actionpack (= 3.1.0.rc4)
+ activesupport (= 3.1.0.rc4)
+ rack-ssl (~> 1.3.2)
+ rake (>= 0.8.7)
+ rdoc (~> 3.4)
+ thor (~> 0.14.6)
+ rake (0.9.2)
+ rdoc (3.8)
+ rspec (2.6.0)
+ rspec-core (~> 2.6.0)
+ rspec-expectations (~> 2.6.0)
+ rspec-mocks (~> 2.6.0)
+ rspec-core (2.6.4)
+ rspec-expectations (2.6.0)
+ diff-lcs (~> 1.1.2)
+ rspec-mocks (2.6.0)
+ sprockets (2.0.0.beta.10)
+ hike (~> 1.0)
+ rack (~> 1.0)
+ tilt (!= 1.3.0, ~> 1.1)
+ thor (0.14.6)
+ tilt (1.3.2)
+ treetop (1.4.9)
+ polyglot (>= 0.3.1)
+ tzinfo (0.3.29)
+
+PLATFORMS
+ ruby
+
+DEPENDENCIES
+ rails (= 3.1.0.rc4)
+ rspec
@@ -0,0 +1,20 @@
+Copyright 2011 Thilo Rusche
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,3 @@
+= HttpLog
+
+This project rocks and uses MIT-LICENSE.
@@ -0,0 +1,34 @@
+#!/usr/bin/env rake
+begin
+ require 'bundler/setup'
+rescue LoadError
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
+end
+begin
+ require 'rdoc/task'
+rescue LoadError
+ require 'rdoc/rdoc'
+ require 'rake/rdoctask'
+ RDoc::Task = Rake::RDocTask
+end
+
+RDoc::Task.new(:rdoc) do |rdoc|
+ rdoc.rdoc_dir = 'rdoc'
+ rdoc.title = 'HttpLog'
+ rdoc.options << '--line-numbers' << '--inline-source'
+ rdoc.rdoc_files.include('README.rdoc')
+ rdoc.rdoc_files.include('lib/**/*.rb')
+end
+
+
+require 'rake/testtask'
+
+Rake::TestTask.new(:test) do |t|
+ t.libs << 'lib'
+ t.libs << 'test'
+ t.pattern = 'test/**/*_test.rb'
+ t.verbose = false
+end
+
+
+task :default => :test
@@ -0,0 +1,9 @@
+# Provide a simple gemspec so you can easily use your
+# project in your rails apps through git.
+Gem::Specification.new do |s|
+ s.name = "httplog"
+ s.summary = "Logs outgoing Net::HTTP requests."
+ s.description = "Find out what those third party API gems are up to."
+ s.files = Dir["lib/**/*"] + ["MIT-LICENSE", "Rakefile", "README.rdoc"]
+ s.version = "0.0.1"
+end
@@ -0,0 +1 @@
+require "httplog/http_log"
@@ -0,0 +1,45 @@
+require "net/http"
+require "logger"
+
+module HttpLog
+
+ @@logger = nil
+ @@severity = nil
+
+ def self.logger=(logger)
+ @@logger = logger
+ end
+
+ def self.severity=(severity)
+ @@severity = severity
+ end
+
+ def self.logger
+ @@logger ||= Logger.new($stdout)
+ end
+
+ def self.severity
+ @@severity ||= Logger::Severity::DEBUG
+ end
+
+end
+
+module Net
+
+ class HTTP
+ alias_method(:orig_request, :request) unless method_defined?(:orig_request)
+ alias_method(:orig_connect, :connect) unless method_defined?(:orig_connect)
+
+ def request(req, body = nil, &block)
+ HttpLog.logger.add(HttpLog.severity) { "Sending: #{req.method} http://#{@address}:#{@port}#{req.path}" }
+ HttpLog.logger.add(HttpLog.severity) { "Body: #{req.body}" unless req.body.nil? }
+ orig_request(req, body, &block)
+ end
+
+ def connect
+ HttpLog.logger.add(HttpLog.severity) { "Connecting: #{@address}" }
+ orig_connect
+ end
+ end
+
+end
@@ -0,0 +1,4 @@
+# desc "Explaining what the task does"
+# task :httplog do
+# # Task goes here
+# end
@@ -0,0 +1,60 @@
+require 'spec_helper'
+
+describe HttpLog do
+
+ context "Misc" do
+
+ it "should log at DEBUG by default" do
+ Net::HTTP.get_response("localhost", "/", 3000)
+ log.should include("DEBUG")
+ end
+
+ it "should log at other levels" do
+ HttpLog.severity = Logger::Severity::INFO
+ Net::HTTP.get_response("localhost", "/", 3000)
+ log.should include("INFO")
+ end
+
+ end
+
+ context "GET" do
+ before(:each) do
+ @url = URI.parse('http://localhost:3000/')
+ @req = Net::HTTP::Get.new(@url.path)
+ @res = Net::HTTP.start(@url.host, @url.port) {|http|
+ http.request(@req)
+ }
+ end
+
+ it "should log the connection" do
+ log.should include("Connecting: #{@url.host}")
+ end
+
+ it "should log the request with method and path" do
+ log.should include("Sending: GET #{@url.scheme}://#{@url.host}:#{@url.port}#{@url.path}")
+ end
+
+ end
+
+ context "POST" do
+ before(:each) do
+ @url = URI.parse('http://www.example.com/todo.cgi')
+ req = Net::HTTP::Post.new(@url.path)
+ req.set_form_data({'from' => '2005-01-01', 'to' => '2005-03-31'}, ';')
+ res = Net::HTTP.new(@url.host, @url.port).start {|http| http.request(req) }
+ end
+
+ it "should log the connection" do
+ log.should include("Connecting: #{@url.host}")
+ end
+
+ it "should log the request with method and path" do
+ log.should include("Sending: POST #{@url.scheme}://#{@url.host}:#{@url.port}#{@url.path}")
+ end
+
+ it "should log the request body" do
+ log.should include("Body: from=2005-01-01;to=2005-03-31")
+ end
+ end
+
+end
@@ -0,0 +1,20 @@
+$:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
+$:.unshift(File.dirname(__FILE__))
+require 'rspec'
+require 'httplog'
+
+RSpec.configure do |config|
+ config.before(:each) do
+ require 'stringio'
+
+ @log = StringIO.new
+ @logger = Logger.new @log
+
+ HttpLog.class_variable_set :@@logger, @logger
+ end
+
+ def log
+ @log.string
+ end
+end
+

0 comments on commit 44185fe

Please sign in to comment.