The inclusion of the webmock gem breaks the peer_cert method for getting x509 Certificates. The following code does not work when webmock has been added to the Gemfile.
http.start do |conn|
cert = OpenSSL::X509::Certificate.new conn.peer_cert
@cert = cert
Sorry I havent had time to look through and propose a solution. Any suggestions would be welcome.
Am using Ruby 1.9.2
I can confirm it breaks it but I'll have to look more into it to figure out the solution.
I'm also not sure what should be the behaviour if net connect is disabled.
I investigated the issue. The problem is that webmock overwrites Net::HTTP#connect
and delays it's execution, until the real request is executed. This way it only connects, if a request is not stubbed and net connections are allowed. In your example there is no request executed yet.
The problem is that when Net::HTTP.start method is called, Net::HTTP makes connection to the remote server and webmock has no way of knowing what will be the request url yet, and whether the connection should be allowed or not. The requests are stubbed based on uri, not just host.
Obviously WebMock should not modify the default behaviour of Net::HTTP so I'll
have to change it to execute normal connect when real request is made.
I'm not sure how to do it yet, because at the time of start execution, webmock doesn't know yet if the request is real on stubbed.
Suggestions are welcome.
Would it be possible to add a force_web_connect attribute which would alter the behaviour of the Net::HTTP overwrite to always make the connection when start is called.
Making this a flag would mean that you could satisfy this edgecase and not add any fundamental changes?
The pull request I sent you works with the test but still didnt work when testing in my app. I solved the problem by using mocha to stub out the peer_cert method in my tests.