Skip to content

Webmock breaks peer_cert method #50

Closed
pyrat opened this Issue Oct 14, 2010 · 4 comments

2 participants

@pyrat
pyrat commented Oct 14, 2010

Hi,

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
 end

Sorry I havent had time to look through and propose a solution. Any suggestions would be welcome.

Am using Ruby 1.9.2

Cheers,
Alastair

@bblimke
Owner
bblimke commented Oct 16, 2010

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.

@bblimke
Owner
bblimke commented Oct 17, 2010

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.

@pyrat
pyrat commented Oct 18, 2010

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?

@pyrat
pyrat commented Oct 18, 2010

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.

Cheers,
Alastair

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.