Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New release does not work with Net::HTTP subclass #767

Open
collimarco opened this issue Jun 21, 2018 · 8 comments
Open

New release does not work with Net::HTTP subclass #767

collimarco opened this issue Jun 21, 2018 · 8 comments

Comments

@collimarco
Copy link

collimarco commented Jun 21, 2018

I have upgraded WebMock from 3.1.1 to 3.4.2 (Ruby 2.5.1).

I have the following code that used to work. Now with the latest versions of WebMock it does not work anymore: the request is not seen by WebMock and is sent directly over the network. Any expectation will say that the HTTP request has not been made (but it was made!).

class MyModel
  def my_method
    # ...
    https = HTTPS.new(uri.host, uri.port)
    req = Net::HTTP::Post.new(uri.path, headers)
    req.body = body.to_json
    res = https.request(req) # not seen by WebMock
  end

  class HTTPS < Net::HTTP
    def initialize host, port
      super
      self.use_ssl = true
      # and other configuration, like timeouts...
    end
  end
end

The problem is with the subclass. If you use Net::HTTP directly, WebMock works:

class MyModel
  def my_method
    # ...
    https = Net::HTTP.new(uri.host, uri.port)
    https.use_ssl = true
    req = Net::HTTP::Post.new(uri.path, headers)
    req.body = body.to_json
    res = https.request(req) # seen by WebMock
  end
end
@bblimke
Copy link
Owner

bblimke commented Jun 21, 2018

Does that work woth Ruby 2.5.1 and WebMock 3.1.1?

@collimarco
Copy link
Author

@bblimke Yes, it works.

@bblimke
Copy link
Owner

bblimke commented Jun 22, 2018

@collimarco can you please check the last version that works for you? 3.4.1 perhaps?

@collimarco
Copy link
Author

@bblimke The last version that works for me is 3.1.1. The bug appears since version 3.2.0.

@bblimke
Copy link
Owner

bblimke commented Sep 22, 2018

@collimarco the difference between 3.2.0 and 3.1.1 just affect curb and enabling WebMock in Rspec.
Net::Http adapter hasn't been touched.

Does the problem exist when running Rspec or can you provide a filing code that doesn't use Rspec as well?

@collimarco
Copy link
Author

I am using Rspec...

This commit seems about SSL and it is in version 3.2.0:
03cc9df

@bblimke
Copy link
Owner

bblimke commented Sep 23, 2018

Would you mind checking if the previous commit, a6579a works fine?

@fcheung
Copy link
Contributor

fcheung commented Aug 8, 2019

It’s probably d9af3a5 . Prior to this commit webmock was enabled when you require webmock/rspec , it sounds like this was happening before the code you describe was loaded, so you subclass not from Net::HTTP but the class the webmock switches in. After this commit your class subclasses from the real net http, not from the webmock adapter and so sends real requests

Easiest thing is probably to roll your own “webmock/rspec” file

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants