Error when using with mechanize
An error is raised when using the spy on mechanize:
$ irb -f
>> require 'net-http-spy'
>> require 'mechanize'
>> a = Mechanize.new { |agent| agent.user_agent_alias = 'Mac Safari' }
>> a.get "http://heise.de"
NoMethodError: undefined method `info' for nil:NilClass
from .rvm/gems/ruby-1.9.3-p0/gems/net-http-spy-0.2.1/lib/net-http-spy.rb:24:in `initialize'
from .rvm/gems/ruby-1.9.3-p0/gems/net-http-persistent-2.5.2/lib/net/http/persistent/ssl_reuse.rb:19:in `initialize'
from .rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/net/http.rb:569:in `new'
from .rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/net/http.rb:569:in `new'
from .rvm/gems/ruby-1.9.3-p0/gems/net-http-persistent-2.5.2/lib/net/http/persistent.rb:495:in `connection_for'
from .rvm/gems/ruby-1.9.3-p0/gems/net-http-persistent-2.5.2/lib/net/http/persistent.rb:788:in `request'
from .rvm/gems/ruby-1.9.3-p0/gems/mechanize-2.2.1/lib/mechanize/http/agent.rb:228:in `fetch'
from .rvm/gems/ruby-1.9.3-p0/gems/mechanize-2.2.1/lib/mechanize.rb:403:in `get'
from (irb):6
from .rvm/rubies/ruby-1.9.3-p0/bin/irb:16:in `<main>'
Works fine without the spy.
I get the same error when using just net-http-persistent with this gem. If I figure out the root cause I'll let you know.
Looks like net-http-persistent subclasses Net::HTTP, and the http_logger* class variables that net-http-spy uses are not inherited.
You can work around this by re-setting the http_logger class variable on the subclass.
Net::HTTP::Persistent::SSLReuse.http_logger = Logger.new(STDOUT)
You also modify the options on that class:
Net::HTTP::Persistent::SSLReuse.http_logger_options = {:body => true, :verbose => true, :trace => true}
Unfortunately the class in question has a big warning comment on the top:
# DO NOT DEPEND UPON THIS CLASS
#
# This class is an implementation detail and is subject to change or removal
# at any time.
lib/net/http/persistent/ssl_reuse.rb
It seems a little weird that this subclass comes into play even when you're not using SSL. Perhaps @drbrain has a comment on that. Another thing to consider would be if there's a way to implement this that doesn't rely on class variables. Either way, the workaround does indeed work around for now.
My goal is to have Net::HTTP in ruby automatically reuse SSL sessions to speed up reconnection to SSL servers, so I don't want users or library implementers to depend on the use of this class.
I have hooks in net-http-persistent to disable the subclass for tools like these (originally added for mocking libraries), so I can add one for net-http-spy as well.