net-http-spy 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,40 @@
1
+ require "rubygems"
2
+ require "rake/gempackagetask"
3
+ require "rake/rdoctask"
4
+
5
+ begin
6
+ require 'jeweler'
7
+ Jeweler::Tasks.new do |gemspec|
8
+ gemspec.name = "net-http-spy"
9
+ gemspec.summary = "Ever wondered what HTTP requests the Ruby gem you are using to connect to a third party API is making? Use HTTP Spy to see what is going on behind the scenes."
10
+ gemspec.email = "martin@beyondthetype.com"
11
+ gemspec.homepage = "http://github.com/martinbtt/net-http-spy"
12
+ gemspec.description = "Ever wondered what HTTP requests the Ruby gem you are using to connect to a third party API is making? Use HTTP Spy to see what is going on behind the scenes."
13
+ gemspec.authors = ["Martin Sadler"]
14
+ gemspec.files.include Dir["examples/*.rb"] + ["readme.markdown"]
15
+ end
16
+ rescue LoadError
17
+ puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
18
+ end
19
+
20
+ task :default => :spec
21
+
22
+ require "spec"
23
+ require "spec/rake/spectask"
24
+ Spec::Rake::SpecTask.new do |t|
25
+ t.spec_opts = %w(--format specdoc --colour)
26
+ t.libs = ["spec"]
27
+ end
28
+
29
+
30
+ # Generate documentation
31
+ Rake::RDocTask.new do |rd|
32
+ rd.main = "readme.markdown"
33
+ rd.rdoc_files.include("readme.markdown", "lib/**/*.rb")
34
+ rd.rdoc_dir = "rdoc"
35
+ end
36
+
37
+ desc 'Clear out RDoc and generated packages'
38
+ task :clean => [:clobber_rdoc, :clobber_package] do
39
+ rm "#{spec.name}.gemspec"
40
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.2.1
@@ -0,0 +1,14 @@
1
+ # Just showing an example of something other than Twitter.
2
+ require 'rubygems'
3
+ require 'fogbugz-api' # sudo gem install austinmoody-fogbugz-api
4
+ require File.expand_path(File.join(File.dirname(__FILE__),'..','lib','spy'))
5
+
6
+ config = {:username => "yourusername",
7
+ :password => "yourpass",
8
+ :domain => "yourdomain.fogbugz.com"
9
+ }
10
+
11
+ Net::HTTP.http_logger_options = {:body => true, :trace => false}
12
+
13
+ @fogbugz = FogBugz.new(config[:domain],true) # create instance
14
+ @fogbugz.logon(config[:username],config[:password]) # logs into FogBugz and sets *token*
@@ -0,0 +1,8 @@
1
+ # Just showing an example of something other than Twitter.
2
+ # Garb lets you access the Google Analytics API
3
+ require 'rubygems'
4
+ require 'garb'
5
+ require File.expand_path(File.join(File.dirname(__FILE__),'..','lib','net-http-spy'))
6
+
7
+ Net::HTTP.http_logger_options = {:body => false, :trace => false, :verbose => false}
8
+ Garb::Session.login('yourgoogleusername', 'yourpassword')
@@ -0,0 +1,7 @@
1
+ # See where the external API calls take place within the gem you are using with the :trace option
2
+ require 'rubygems'
3
+ require 'twitter'
4
+ require File.expand_path(File.join(File.dirname(__FILE__),'..','lib','net-http-spy'))
5
+
6
+ Net::HTTP.http_logger_options = {:trace => true}
7
+ Twitter::Search.new('httparty').each { |r| r }
@@ -0,0 +1,8 @@
1
+ # By default the logger outputs to STDOUT, it's easy to change this to a file if you like
2
+ # Great to capture and then use for testing with tools like FakeWeb when combined with :body => true
3
+ require 'rubygems'
4
+ require 'twitter'
5
+ require File.expand_path(File.join(File.dirname(__FILE__),'..','lib','net-http-spy'))
6
+
7
+ Net::HTTP.http_logger = Logger.new('twitter.log')
8
+ Twitter::Search.new('httparty').each { |r| r }
@@ -0,0 +1,6 @@
1
+ # Simplist example. All you need to do is include the spy lib for it to start doing the right thing
2
+ require 'rubygems'
3
+ require 'twitter'
4
+ require File.expand_path(File.join(File.dirname(__FILE__),'..','lib','net-http-spy'))
5
+
6
+ Twitter::Search.new('httparty').each { |r| r }
@@ -0,0 +1,7 @@
1
+ # Net::HTTP has it's own logging/debug functionality. Turn on :verbose to show the full raw HTTP communication
2
+ require 'rubygems'
3
+ require 'twitter'
4
+ require File.expand_path(File.join(File.dirname(__FILE__),'..','lib','net-http-spy'))
5
+
6
+ Net::HTTP.http_logger_options = {:verbose => true}
7
+ Twitter::Search.new('httparty').each { |r| r }
@@ -0,0 +1,7 @@
1
+ # Display the full response/request body. Usually just the response code is shown.
2
+ require 'rubygems'
3
+ require 'twitter'
4
+ require File.expand_path(File.join(File.dirname(__FILE__),'..','lib','net-http-spy'))
5
+
6
+ Net::HTTP.http_logger_options = {:body => true}
7
+ Twitter::Search.new('httparty').each { |r| r }
@@ -0,0 +1,52 @@
1
+ require 'net/https'
2
+ require 'logger'
3
+ require 'cgi'
4
+
5
+ # HTTP SPY
6
+ module Net
7
+ class HTTP
8
+ alias :old_initialize :initialize
9
+ alias :old_request :request
10
+
11
+ class << self
12
+ attr_accessor :http_logger
13
+ attr_accessor :http_logger_options
14
+ end
15
+
16
+ def initialize(*args, &block)
17
+ self.class.http_logger_options ||= {}
18
+ defaults = {:body => false, :trace => false, :verbose => false, :limit => -1}
19
+ self.class.http_logger_options = (self.class.http_logger_options == :default) ? defaults : self.class.http_logger_options
20
+ @logger_options = defaults.merge(self.class.http_logger_options)
21
+ @params_limit = @logger_options[:params_limit] || @logger_options[:limit]
22
+ @body_limit = @logger_options[:body_limit] || @logger_options[:limit]
23
+
24
+ self.class.http_logger.info "CONNECT: #{args.inspect}" if !@logger_options[:verbose]
25
+
26
+ old_initialize(*args, &block)
27
+ @debug_output = self.class.http_logger if @logger_options[:verbose]
28
+ end
29
+
30
+
31
+ def request(*args, &block)
32
+ unless started? || @logger_options[:verbose]
33
+ req = args[0].class::METHOD
34
+ self.class.http_logger.info "#{req} #{args[0].path}"
35
+ end
36
+
37
+ result = old_request(*args, &block)
38
+ unless started? || @logger_options[:verbose]
39
+
40
+ self.class.http_logger.info "PARAMS #{CGI.parse(args[0].body).inspect[0..@params_limit]} " if args[0].body && req != 'CONNECT'
41
+ self.class.http_logger.info "TRACE: #{caller.reverse}" if @logger_options[:trace]
42
+ self.class.http_logger.info "BODY: #{(@logger_options[:body] ? result.body : result.class.name)[0..@body_limit]}"
43
+ end
44
+ result
45
+ end
46
+
47
+
48
+ end
49
+
50
+ end
51
+
52
+ Net::HTTP.http_logger = Logger.new(STDOUT)
@@ -0,0 +1,59 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{net-http-spy}
8
+ s.version = "0.2.1"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Martin Sadler"]
12
+ s.date = %q{2009-12-05}
13
+ s.description = %q{Ever wondered what HTTP requests the Ruby gem you are using to connect to a third party API is making? Use HTTP Spy to see what is going on behind the scenes.}
14
+ s.email = %q{martin@beyondthetype.com}
15
+ s.files = [
16
+ "Rakefile",
17
+ "VERSION",
18
+ "examples/fogbugz.rb",
19
+ "examples/google.rb",
20
+ "examples/twitter-calltrace.rb",
21
+ "examples/twitter-customlog.rb",
22
+ "examples/twitter-simple.rb",
23
+ "examples/twitter-verbose.rb",
24
+ "examples/twitter-withbody.rb",
25
+ "lib/net-http-spy.rb",
26
+ "net-http-spy.gemspec",
27
+ "readme.markdown",
28
+ "spec/spec.opts",
29
+ "spec/spec_helper.rb",
30
+ "spec/spy_spec.rb"
31
+ ]
32
+ s.homepage = %q{http://github.com/martinbtt/net-http-spy}
33
+ s.rdoc_options = ["--charset=UTF-8"]
34
+ s.require_paths = ["lib"]
35
+ s.rubygems_version = %q{1.3.5}
36
+ s.summary = %q{Ever wondered what HTTP requests the Ruby gem you are using to connect to a third party API is making? Use HTTP Spy to see what is going on behind the scenes.}
37
+ s.test_files = [
38
+ "spec/spec_helper.rb",
39
+ "spec/spy_spec.rb",
40
+ "examples/fogbugz.rb",
41
+ "examples/google.rb",
42
+ "examples/twitter-calltrace.rb",
43
+ "examples/twitter-customlog.rb",
44
+ "examples/twitter-simple.rb",
45
+ "examples/twitter-verbose.rb",
46
+ "examples/twitter-withbody.rb"
47
+ ]
48
+
49
+ if s.respond_to? :specification_version then
50
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
51
+ s.specification_version = 3
52
+
53
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
54
+ else
55
+ end
56
+ else
57
+ end
58
+ end
59
+
@@ -0,0 +1,61 @@
1
+ ## About
2
+
3
+ Ever wondered what HTTP requests the Ruby gem you are using to connect to a third party
4
+ API is making? Use HTTP Spy to see what is going on behind the scenes.
5
+
6
+ ## Installation
7
+
8
+ sudo gem install martinbtt-net-http-spy
9
+
10
+ ## Example Usage
11
+
12
+ require 'rubygems'
13
+ require 'twitter'
14
+ gem 'net-http-spy'
15
+ require 'net-http-spy'
16
+
17
+ Twitter::Search.new('httparty').each { |r| r }
18
+ # Outputs...
19
+ -- : CONNECT: ["search.twitter.com", 80]
20
+ -- : GET /search.json?q=httparty
21
+ -- : BODY: Net::HTTPOK
22
+
23
+
24
+ See the examples folder for more.
25
+
26
+ ## Further Options
27
+
28
+ Show the call trace to the originating line of code in the third party gem
29
+
30
+ Net::HTTP.http_logger_options = {:trace => true}
31
+
32
+ Output the body of the request
33
+
34
+ Net::HTTP.http_logger_options = {:body => true}
35
+
36
+ Show the full raw HTTP output
37
+
38
+ Net::HTTP.http_logger_options = {:verbose => true}
39
+
40
+ Change the logger. By default HTTP spy logs to STDOUT
41
+
42
+ Net::HTTP.http_logger = Logger.new('twitter.log')
43
+
44
+ ## Bonus Points
45
+
46
+ Use it to grab sample data for FakeWeb = testing goodness.
47
+
48
+ ## Notes
49
+
50
+ This is a pretty early release. I'm sure there is plenty that can be done to improve compatibility
51
+ as several libraries call Net::HTTP in a slightly different way.
52
+ Feel free to fork and send in pull requests/patches.
53
+
54
+ ## Find Me
55
+
56
+ Martin Sadler (martin -- at -- beyondthetype.com)
57
+
58
+ * Blog: http://www.beyondthetype.com
59
+ * Follow: http://twitter.com/martinbtt
60
+ * Code: http://github.com/martinbtt
61
+ * Recommend: http://www.workingwithrails.com/person/5152-martin-sadler
@@ -0,0 +1,3 @@
1
+ --format
2
+ progress
3
+ --colour
@@ -0,0 +1,39 @@
1
+ require 'rubygems'
2
+ gem 'rspec'
3
+ require 'spec'
4
+ require 'net/http'
5
+ require 'open-uri'
6
+ require 'twitter'
7
+ require 'mechanize'
8
+
9
+ require 'webmock/rspec'
10
+ include WebMock
11
+
12
+ LIVE = ENV['LIVE']
13
+ WebMock.allow_net_connect! if LIVE
14
+
15
+
16
+ require File.join(File.dirname(__FILE__), '..', 'lib', 'net-http-spy')
17
+
18
+
19
+ class DummyLogger
20
+
21
+ attr_accessor :lines
22
+
23
+ def initialize
24
+ reset!
25
+ end
26
+
27
+ def <<(msg)
28
+ @lines << msg
29
+ end
30
+
31
+ def info(msg)
32
+ @lines << msg
33
+ end
34
+
35
+ def reset!
36
+ @lines = []
37
+ end
38
+
39
+ end
@@ -0,0 +1,84 @@
1
+ require File.join(File.dirname(__FILE__), 'spec_helper')
2
+
3
+ # Bare bones spec to make sure the core functionality is working
4
+ describe "Net:HTTP Spying on" do
5
+ before(:all) do
6
+ Net::HTTP.http_logger = DummyLogger.new()
7
+ end
8
+
9
+ describe "a get request with default options" do
10
+
11
+ before(:all) do
12
+ stub_request(:any, "search.twitter.com/search.json?q=httparty").to_return(:body => "\{\"results\"\: 1\}", :status => 200) unless LIVE
13
+ Net::HTTP.http_logger_options = :default
14
+ Twitter::Search.new('httparty').fetch
15
+ end
16
+
17
+ it "should give the connection" do
18
+ Net::HTTP.http_logger.lines.should include("CONNECT: [\"search.twitter.com\", 80]")
19
+ end
20
+
21
+ it "should give GET uri and query string" do
22
+ Net::HTTP.http_logger.lines.should include("GET /search.json?q=httparty")
23
+ end
24
+
25
+ it "should give the BODY response code" do
26
+ Net::HTTP.http_logger.lines.should include("BODY: Net::HTTPOK")
27
+ end
28
+ end
29
+
30
+
31
+ describe "a get request with body option set to true" do
32
+ before(:each) do
33
+ stub_request(:any, "search.twitter.com/search.json?q=httparty").to_return(:body => "\{\"results\"\: 1\}", :status => 200) unless LIVE
34
+ Net::HTTP.http_logger_options = {:body => true}
35
+ end
36
+
37
+ it "should give the body output" do
38
+ Twitter::Search.new('httparty').fetch
39
+ Net::HTTP.http_logger.lines
40
+ Net::HTTP.http_logger.lines.grep(/BODY: \{\"results\":/).should_not be_empty
41
+ end
42
+ end
43
+
44
+
45
+ describe "a get request with trace option set to true" do
46
+ before(:each) do
47
+ Net::HTTP.http_logger_options = {:trace => true}
48
+ stub_request(:any, "search.twitter.com/search.json?q=httparty").to_return(:body => "\{\"results\"\: 1\}", :status => 200) unless LIVE
49
+ end
50
+
51
+ it "should give the trace output" do
52
+ Twitter::Search.new('httparty').fetch
53
+ Net::HTTP.http_logger.lines.grep(/TRACE: /).should_not be_empty
54
+ end
55
+ end
56
+
57
+ describe "a post request with default options" do
58
+ before(:all) do
59
+ Net::HTTP.http_logger_options = {:verbose => false}
60
+ stub_request(:any, "search.twitter.com/search").to_return(:body => "\{\"results\"\: 1\}", :status => 200) unless LIVE
61
+ @connection = Net::HTTP.new('search.twitter.com')
62
+ @connection.post('/search','?q=hello')
63
+ end
64
+
65
+ it "should give the post url" do
66
+ Net::HTTP.http_logger.lines.should include("POST /search")
67
+ end
68
+
69
+
70
+ if LIVE # only works if real live request
71
+ it "should give the post params" do
72
+ Net::HTTP.http_logger.lines.should include("PARAMS {\"?q\"=>[\"hello\"]} ")
73
+ end
74
+ end
75
+
76
+ it "should give the BODY response code" do
77
+ Net::HTTP.http_logger.lines.should include("BODY: Net::HTTPOK")
78
+ end
79
+ end
80
+
81
+ after(:all) do
82
+ Net::HTTP.http_logger.reset!
83
+ end
84
+ end
metadata ADDED
@@ -0,0 +1,77 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: net-http-spy
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.1
5
+ platform: ruby
6
+ authors:
7
+ - Martin Sadler
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-12-05 00:00:00 +00:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: Ever wondered what HTTP requests the Ruby gem you are using to connect to a third party API is making? Use HTTP Spy to see what is going on behind the scenes.
17
+ email: martin@beyondthetype.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files: []
23
+
24
+ files:
25
+ - Rakefile
26
+ - VERSION
27
+ - examples/fogbugz.rb
28
+ - examples/google.rb
29
+ - examples/twitter-calltrace.rb
30
+ - examples/twitter-customlog.rb
31
+ - examples/twitter-simple.rb
32
+ - examples/twitter-verbose.rb
33
+ - examples/twitter-withbody.rb
34
+ - lib/net-http-spy.rb
35
+ - net-http-spy.gemspec
36
+ - readme.markdown
37
+ - spec/spec.opts
38
+ - spec/spec_helper.rb
39
+ - spec/spy_spec.rb
40
+ has_rdoc: true
41
+ homepage: http://github.com/martinbtt/net-http-spy
42
+ licenses: []
43
+
44
+ post_install_message:
45
+ rdoc_options:
46
+ - --charset=UTF-8
47
+ require_paths:
48
+ - lib
49
+ required_ruby_version: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: "0"
54
+ version:
55
+ required_rubygems_version: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ version: "0"
60
+ version:
61
+ requirements: []
62
+
63
+ rubyforge_project:
64
+ rubygems_version: 1.3.5
65
+ signing_key:
66
+ specification_version: 3
67
+ summary: Ever wondered what HTTP requests the Ruby gem you are using to connect to a third party API is making? Use HTTP Spy to see what is going on behind the scenes.
68
+ test_files:
69
+ - spec/spec_helper.rb
70
+ - spec/spy_spec.rb
71
+ - examples/fogbugz.rb
72
+ - examples/google.rb
73
+ - examples/twitter-calltrace.rb
74
+ - examples/twitter-customlog.rb
75
+ - examples/twitter-simple.rb
76
+ - examples/twitter-verbose.rb
77
+ - examples/twitter-withbody.rb