GithubHelp home page GithubHelp logo

fakeweb's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

fakeweb's Issues

URI encoding causing an issue

Hiya,

I have a URI like this:

http:://test.host/resource?date=2010-07-28T14:19:47Z

When Net::HTTP sends the request, it has turned it into:

http:://test.host/resource?date=2010-07-28T14%3A19%3A47Z

Which then doesn't get matched by FakeWeb.

If I put the parameter through CGI.escape then that solves the problem for me, but it'd be nice if FakeWeb worked that out itself.

Cheers

Test failures?

┌──────────────────────────────────────────────────────────────────────────────┐
│ Run tests for ruby2.5 from debian/ruby-tests.rb                              │
└──────────────────────────────────────────────────────────────────────────────┘

RUBYLIB=/<<BUILDDIR>>/ruby-fakeweb-1.3.0+git20170806+dfsg1/debian/ruby-fakeweb/usr/lib/ruby/vendor_ruby:. GEM_PATH=debian/ruby-fakeweb/usr/share/rubygems-integration/all:/var/lib/gems/2.5.0:/usr/lib/x86_64-linux-gnu/rubygems-integration/2.5.0:/usr/share/rubygems-integration/2.5.0:/usr/share/rubygems-integration/all ruby2.5 debian/ruby-tests.rb
Loaded suite debian/ruby-tests
Started
...................................................F
===============================================================================
Failure: test_real_https_request(TestFakeWeb):
  not all expectations were satisfied
  unsatisfied expectations:
  - expected at least once, not yet invoked: #<Mock:SSLSocket>.session()
  satisfied expectations:
  - expected at least once, invoked once: #<Mock:SSLSocket>.read_nonblock(any_parameters)
  - expected exactly once, invoked once: #<Mock:SSLSocket>.write(all_of(includes("GET /main/rss/hotnews/hotnews.rss HTTP/1.1"), includes("Host: images.apple.com")))
  - allowed any number of times, invoked twice: #<Mock:SSLSocket>.close(any_parameters)
  - allowed any number of times, invoked 3 times: #<Mock:SSLSocket>.closed?(any_parameters)
  - allowed any number of times, invoked twice: #<Mock:SSLSocket>.setsockopt(any_parameters)
  - expected at least once, invoked twice: #<Mock:SSLSocket>.connect_nonblock(any_parameters)
  - allowed any number of times, invoked twice: #<Mock:SSLSocket>.sync_close=(any_parameters)
  - expected at least once, invoked twice: OpenSSL::SSL::SSLSocket.new(#<Mock:SSLSocket>, instance_of(OpenSSL::SSL::SSLContext))
  - expected at least once, invoked twice: OpenSSL::SSL::SSLSocket.===(#<Mock:SSLSocket>)
  - expected at least once, invoked twice: TCPSocket.open("images.apple.com", 443, nil, nil)
/<<BUILDDIR>>/ruby-fakeweb-1.3.0+git20170806+dfsg1/test/test_helper.rb:123:in `setup_expectations_for_real_request'
/<<BUILDDIR>>/ruby-fakeweb-1.3.0+git20170806+dfsg1/test/test_helper.rb:175:in `setup_expectations_for_real_apple_hot_news_request'
/<<BUILDDIR>>/ruby-fakeweb-1.3.0+git20170806+dfsg1/test/test_fake_web.rb:410:in `test_real_https_request'
     407: 
     408:   def test_real_https_request
     409:     FakeWeb.allow_net_connect = true
  => 410:     setup_expectations_for_real_apple_hot_news_request(:port => 443)
     411: 
     412:     http = Net::HTTP.new('images.apple.com', 443)
     413:     http.use_ssl = true
===============================================================================
...............................................................................
.............................................................
Finished in 0.254606772 seconds.
-------------------------------------------------------------------------------
192 tests, 353 assertions, 1 failures, 0 errors, 0 pendings, 0 omissions, 0 notifications
99.4792% passed
-------------------------------------------------------------------------------

NetConnectNotAllowedError reports the URI different than what is acually being matched.

The problem is that prior to matching, the URI is passed through normalize_uri which mangles it.

For example:

> require 'fakeweb'
> FakeWeb.allow_net_connect = false
> FakeWeb.register_uri(:get, /http:\/\/good\.com\/\?a=1\&b=2/, :body => "good")
> FakeWeb.register_uri(:get, /http:\/\/bad\.com\/\?b=1\&a=2/, :body => "bad")

> Net::HTTP.get('good.com', '/?a=1&b=2')
"good"
> Net::HTTP.get('bad.com', '/?b=1&a=2')
FakeWeb::NetConnectNotAllowedError: Real HTTP connections are disabled. Unregistered request: GET http://bad.com/?b=1&a=2

which is incredibly confusing, given that error message contains exactly the same URL that I passed in, while in reality match happens against the URL with normalized parameters (see https://github.com/chrisk/fakeweb/blob/master/lib/fake_web/registry.rb#L58)

Unregister single URIs

It would be great if we had the ability to unregister a single URI. The use case for this would be in specs, where we would ideally want fake URIs to only last the duration of each example, to decrease dependency on the order in which tests are run. For example:

context "with a FakeWeb URI" do
  before do
    FakeWeb.register_uri :any, %r{somerequest.com} # ...
  end

  after do
    FakeWeb.unregister_uri %r{somerequest.com}
  end

  scenario "responds to somerequest.com" do
    expect(Net::HTTP).to receive(:get).with(URI.parse "http://somerequest.com/").and_return(anything)
  end
end

context "without a FakeWeb URI" do
  scenario "doesn't respond to somerequest.com" do
    expect(Net::HTTP).to receive(:get).with(URI.parse "http://somerequest.com/").and_return(nil) # or whatever
  end
end

"FakeWeb.allow_net_connect = true" doesn't work with PORO/ElasticSearch

In my Rails project I have a utility class (plain old ruby object) which does a request to GitHub api and saves Project model. Just a moment ago I have started using ElasticSearch and my Project model has some stuff indexed. Each time I save this model a request to ElasticSearch is made on port 9200. Majority of my tests started to fail (because of the extra requests) with a message:

 FakeWeb::NetConnectNotAllowedError:
       Real HTTP connections are disabled. Unregistered request: PUT http://localhost:9200/karma_tracker_test/project/236

Now when I do

FakeWeb.allow_net_connect = true
#or 
FakeWeb.allow_net_connect = %r[^https?://localhost:9200]

explicitly in a test, or in a before(:each) block in spec_helper the test still fails. But if I put the same line in my utility class just before save, the test passes

Whole code is online at https://github.com/amberbit/KarmaTracker/tree/feature/59079242_elastic_search - failing test spec/model/pivotal_tracker_projects_fetcher_spec.rb:16, failing class app/services/pivotal_tracker_projects_fetcher.rb line 18

Adding a test-related logic inside app logic feels really bad. Any way to make it more clean or is it a bug?

Google Group Subscription

I would like to join the Google group for Fakeweb however I am afraid that, due to company policies, I cannot get a Google account.

Would it be possible to enable email subscription to the group without a google account.

(I am specifically interested in the request body post as I have a need to test a SOAP api where, naturally, the request body content is pivotal)

Final release in the 1.1.x series that prints warnings about the gem rename

According to the stats on rubyforge and gemcutter, it looks like a few users are still installing the old FakeWeb versions of the gem.

We could probably ask to have them removed outright, since the pre-1.2.0 gem versions are still available as "fakeweb" gems.

I think a nicer solution would be to do a final release in the 1.1.x series that uses RubyGems's post-install hook to print out a note about the FakeWeb -> fakeweb change. Maybe add a warning when that version is required, too. It might be annoying, but the exact same versions of the gems exist under the fakeweb name, so switching should be pretty easy for anyone still using the old gems.

1.9.2 compatibility

Tests are failing on 1.9.2-preview1. Looks related to changes in Net::HTTP, possibly the #read_nonblock method.

Regexp with query params fail without trailing slash after host

require 'fakeweb'
FakeWeb.allow_net_connect = false

reg = /http:\/\/a\.com\?a=1/
uri=URI("http://a.com?a=1")
FakeWeb.register_uri(:get, reg, body: 'ok')
Net::HTTP.get(uri)

results in a FakeWeb::NetConnectNotAllowedError (Real HTTP connections are disabled. Unregistered request: GET http://a.com/?a=1)

But uri.to_s.match reg gives a match

Registering the same url without regular expression also works

FakeWeb.clean_registry
uri=URI("http://a.com?a=1")
FakeWeb.register_uri(:get, uri.to_s, body: 'ok')
Net::HTTP.get(uri)

With a regular expression you need to add the trailing slash after the hostname to get a good result

FakeWeb.clean_registry
reg = /http:\/\/a\.com\/\?a=1/
uri=URI("http://a.com?a=1")
FakeWeb.register_uri(:get, reg, body: 'ok')
Net::HTTP.get(uri)

Seems inconsistent to me

Too many connections error

Hi,

In a project where we use fakeweb I ran into an issue when I tried to connect to Neo4j (a graph database). The gem I used to connect to it (neo4jrb) uses net-http to connect to the database and it turns out that fakeweb interfered with this in development.

I described my issues in detail here:
neo4jrb/activegraph#555 (comment)

After some back and forth between ruby versions and disabling various gems I found out that fakeweb was, in the end, the culprit in my spotty connection with the database. Since removing fakeweb from my Gemfile I haven't had any issues.

The error I received was:

Net::HTTP::Persistent::Error: too many connection resets (due to closed stream - IOError) after 0 requests on 2237191000, last used 4.27705 seconds ago
from /Users/achilleas/.rbenv/versions/2.3.0/lib/ruby/2.3.0/net/protocol.rb:211:in `write'

Please let me know if you need any more information.

Better API for matching query params

Right now, the query params of the incoming request are sorted by key before we try to match against all the registered Regexps and URI objects. This works fine for URIs, because we can sort those query params too, so the user doesn't even know.

For Regexps, though, it seems impossible to figure out what part of the Regexp represents the query params and sort it; so the user has to be aware that their Regexp must be written to match against sorted params. That can be kind of hard sometimes. For example, a registration might look something like:

/example\.com\?[z\d]{2}=1&(opt1=a|opt2=b)$/

To match this URI:

http://example.com?9z=1&opt2=b

But also this URI:

http://example.com?zz=1&opt1=a

So you can see that if we sorted the second URI's params by key, it wouldn't match the Regexp. Instead, the user would have to register a Regexp that matches either order (or do two register_uri calls), which is pretty ugly.

We tried getting around this by calculating all the possible orders of the params and matching them, but that's O(n!) and therefore intractable for even pretty reasonable query params counts.

So the idea is that we could add a new API entirely, and get the query params out of the Regexp. Maybe an additional argument to register_uri with a hash of params that must in the request?

(Forked from issue #5.)

Different Responses to the same URI based on the passed data

I am working on a project using fakeweb. Found it to be useful. :)

I have a scenario in which I need to return different responses based on the data passed in the request on the same URI. Example below:

FakeWeb.register_uri(:post,
                           %r{http:\/\/api.flickr.com\/services\/rest\/},
                           parameters: {method: 'flickr.reflection.getMethods', format: 'json', nojsoncallback: "1"},
                           body: "First Content")

# Should return First Content

FakeWeb.register_uri(:post,
                           %r{http:\/\/api.flickr.com\/services\/rest\/},
                           parameters: {method: 'flickr.photos.getRecent', format: 'json', nojsoncallback: "1"},
                           body: "Second Content")

# Should return Second Content

In the case above, the URI is the same, however, the values of the parameters are different.

How this can be achieved?

support setting content_type of resonse

I'd like to stub out a response to an image file and have the FakeWeb response return 'image/jpeg' as the content type.

i'd like it to work something like this:
FakeWeb.register_uri(:get, 'http://mysite.com/image.jpeg', :file => fixture_path + '/files/image.jpeg', :content_type => 'image/jpeg')

FakeWeb doesn't mimic Net::HTTP's check "#read_body called twice (IOError)"

This should raise Net::HTTPOK#read_body called twice (IOError) (or whatever the appropriate Net::HTTPResponse class is, there) whether or not the request hits FakeWeb:

%w(net/http rubygems fakeweb).each { |it| require it }
FakeWeb.register_uri(:get, "http://example.com", body: "example")
Net::HTTP.new("example.com").request_get("/") do |response|
  2.times { response.read_body { |part| p part } }
end

I found this with 1.9.3 p392; need to check the behavior of all the other implementations. I also see some other things in there we're probably not handling correctly, like #read_body's deprecated-but-still-working dest parameter—need to file issues for those.

1.3.0 does not recognize registered uri for post method

There is an issue with fakeweb 1.3.0 not being able to recognize a registered uri. I've made a patch on my local system but would love to see you incorporate the fix into a released version.

My application is using koala 0.8.0 to talk to Facebook's graph api. My unit test is basically trying to send a delete request to Facebook, but I'm trying to use fakeweb to intercept the uri.

Here's the test code:

setup fakeweb and register the facebook graph uri

FakeWeb.allow_net_connect = false
FakeWeb.register_uri(:post, %r{https://graph.facebook.com/[0-9,_]+}, :status => 200, :body => "")

create a koala client, then delete the object.

fb_graph_client_session = Koala::Facebook::GraphAPI.new(oauth_token)
fb_graph_client_session.delete_object("#{fb_id}")
...

Fakeweb 1.3.0 fails to recognize the uri. I tracked it down to the function request_uri_as_string, which builds an incorrect url.

Failing uri generated by utility.rb, line 35:
https://graph.facebook.com:44398765_7

The uri should be this:
https://graph.facebook.com:443/98765_7

My fix was to insert '/' before the path, unless it's already there.

fakeweb-1.3.0/lib/fake_web/utility.rb:

def self.request_uri_as_string(net_http, request)
...
#added by lkang - to make it work with gem koala (0.8.0) delete_object( )
#path.insert(0, '/') unless path.start_with?('/')

uri = "#{protocol}://#{userinfo}#{net_http.address}:#{net_http.port}#{path}"
puts "****** #{FILE} uri: #{uri}"
uri
end

FYI, fakeweb appears to work with Koala for get methods, but post is a problem. Looking at the Koala code, it is using Net::HTTP#post to create the http post request (line 35)

koala-0.8.0/lib/koala/http_services.rb:

...
25 server = options[:rest_api] ? Facebook::REST_SERVER : Facebook::GRAPH_SERVER
26 http = Net::HTTP.new(server, 443)
27 http.use_ssl = true
28 # we turn off certificate validation to avoid the
29 # "warning: peer certificate won't be verified in this SSL session" warning
30 # not sure if this is the right way to handle it
31 # see http://redcorundum.blogspot.com/2008/03/ssl-certificates-and-nethttps.html
32 http.verify_mode = OpenSSL::SSL::VERIFY_NONE
33
34 result = http.start { |http|
35 response, body = (verb == "post" ? http.post(path, encode_params(args)) : http.get("#{path}?#{encode_params(args)}"))
36 Koala::Response.new(response.code.to_i, body, response)
37 }
...

Make it easier to access submitted data

Right now, if I want to verify that I'm POST'ing the right data, I need to do something like this:

FakeWeb.register_uri(:post, my_url, body: 'i-dont-care-about-the-response-only-the-request')
do_something_that_hits_my_url
request_data = CGI.parse FakeWeb.last_request.body_stream.instance_variable_get('@stream').string
expect(request_data).to ...

Line 3 of this example should be encapsulated. For instance, it could be exposed as FakeWeb.last_request.form_data

FakeWeb doesn't play well together with Cucumber+Capybara+Culerity

If one turns off all HTTP requests (ie. FakeWeb.allow_net_connect = false) then it's not possible to run Cucumber features with Culerity through Capybara since all HTTP connections are disabled.

Theat means that necessary requests done in the background by Capybara are not possible, like this one:

GET http://localhost:9887/__identify__ 

Optimally FakeWeb would make an exceptions for the needed URL or maybe http://localhost all together.

permutation method for uri matching fails for uris with many query params

I ran into an issue tonight with version 1.2.6 of fakeweb when testing a request with 12 query params. One of my features was hanging, and I traced it back to the new method for uri matching in registry.rb. This code generates a list of possible matching URIs by generating the permutations of all the query params. This probably works very well when there are only a few params, but beyond that, the number of possibilities approaches infinity a little too quickly for my taste.

I'm not sure what the affect of rolling back the change would have on Regexp support or performance, but perhaps sorting the keys or doing a key-by-key check would be better for larger arrays.

using an https url

We were trying to register an https url (just to match the actual one used) and got this error:

FakeWeb.register_uri(method, "https://example.com/api/v1/account/balance", body: body.to_json)

 Failure/Error: Net::HTTP.get(URI.parse("https://example.com/api/v1/account/balance"))
 SocketError:
   getaddrinfo: nodename nor servname provided, or not known

If we remove the https, it works. Not a big deal but was just curious if there was an easy way to add https or why it didn't work. Thanks for the great gem!

Support net/ftp

There are a lot of situations when we need to stub FTP calls. It would be awesome to implement such functionality for that gem. I could help you with that.

Pathname support when a filename is expected

$ irb -rubygems -rfakeweb -rpathname
>> FakeWeb.register_uri(:get, "http://example.com", :body => Pathname.new("/example.txt"))
=> [#<FakeWeb::Responder:0x10147ba38 @uri="http://example.com", @method=:get, @options={:body=>#<Pathname:/example.txt>}, @times=1>]
>> Net::HTTP.get(URI.parse("http://example.com"))
NoMethodError: undefined method `include?' for #<Pathname:/example.txt>
    from /Library/Ruby/Gems/1.8/gems/fakeweb-1.2.7/lib/fake_web/responder.rb:50:in `body'

This comes up a lot because Rails.root is a Pathname.

FakeWeb stops @javascript (capybara & selenium) cukes from running.

fakeweb 1.3.0
cucumber-rails 0.3.2
capybara 0.4.1.1

When I allow Bundler to require the fakeweb gem, any of the @javascript cukes will hang at certain (seemingly random) steps. Some steps just hang for a long time and then continue. I'm not sure what the catalyst is, but it's really disappointing. I really want to move from Artifice to FakeWeb or WebMock, but both gems do the same thing to our selenium scenarios.

I wonder if anyone else has run into this.

This is the stacktrace when I Ctrl-c after it hangs indefinitely:

/Users/sfsekaran/.rvm/rubies/ree- 1.8.7-2011.01/lib/ruby/1.8/net/http.rb:560:in `initialize': Connection refused - connect(2) (Errno::ECONNREFUSED)
from /Users/sfsekaran/.rvm/rubies/ree-1.8.7-2011.01/lib/ruby/1.8/net/http.rb:560:in `open'
from /Users/sfsekaran/.rvm/rubies/ree-1.8.7-2011.01/lib/ruby/1.8/net/http.rb:560:in `connect_without_fakeweb'
from /Users/sfsekaran/.rvm/rubies/ree-1.8.7-2011.01/lib/ruby/1.8/timeout.rb:53:in `timeout'
from /Users/sfsekaran/.rvm/rubies/ree-1.8.7-2011.01/lib/ruby/1.8/timeout.rb:101:in `timeout'
from /Users/sfsekaran/.rvm/rubies/ree-1.8.7-2011.01/lib/ruby/1.8/net/http.rb:560:in `connect_without_fakeweb'
from /Users/sfsekaran/.rvm/gems/ree-1.8.7-2011.01/gems/fakeweb-1.3.0/lib/fake_web/ext/net_http.rb:49:in `request'
from /Users/sfsekaran/.rvm/gems/ree-1.8.7-2011.01/gems/selenium-webdriver-0.1.2/lib/selenium/webdriver/remote/http/default.rb:36:in `request'
from /Users/sfsekaran/.rvm/gems/ree-1.8.7-2011.01/gems/selenium-webdriver-0.1.2/lib/selenium/webdriver/remote/http/common.rb:36:in `call'
from /Users/sfsekaran/.rvm/gems/ree-1.8.7-2011.01/gems/selenium-webdriver-0.1.2/lib/selenium/webdriver/remote/bridge.rb:429:in `raw_execute'
from /Users/sfsekaran/.rvm/gems/ree-1.8.7-2011.01/gems/selenium-webdriver-0.1.2/lib/selenium/webdriver/remote/bridge.rb:407:in `execute'
from /Users/sfsekaran/.rvm/gems/ree-1.8.7-2011.01/gems/selenium-webdriver-0.1.2/lib/selenium/webdriver/remote/bridge.rb:170:in `quit'
from /Users/sfsekaran/.rvm/gems/ree-1.8.7-2011.01/gems/selenium-webdriver-0.1.2/lib/selenium/webdriver/firefox/bridge.rb:42:in `quit'
from /Users/sfsekaran/.rvm/gems/ree-1.8.7-2011.01/gems/selenium-webdriver-0.1.2/lib/selenium/webdriver/common/driver.rb:156:in `quit'
from /Users/sfsekaran/.rvm/gems/ree-1.8.7-2011.01/gems/capybara-0.4.1.1/lib/capybara/driver/selenium_driver.rb:96:in `browser'
from bin/cucumber:16

FakeWeb registering a url but ignore it afterwards

Registered URI:

[#<FakeWeb::Responder:0x106d219f8 @uri=/http:\/\/ws\.audioscrobbler\.com\/([^\/&]+)\/\?artist=([^\/&]+)&method=album.getinfo&api_key=([^\/&]+)&album=([^\/&]+)/, @method=:any, @times=1, @options={:body=>"<lfm status=\"ok\">trimmed</lfm>"}>]

Does not match

http://ws.audioscrobbler.com/2.0/?artist=Cher&method=album.getinfo&api_key=edited&album=Prisoner

Error:
Real HTTP connections are disabled. Unregistered request: GET http://ws.audioscrobbler.com/2.0/?artist=Cher&method=album.getinfo&api_key=edited&album=Prisoner

Have a look at:
http://rubular.com/regexes/13511

consider switching to Mocha 1.0

Hi!

A new major version of Mocha has been published for some time. Your tests pass if replacing just

require "mocha"

by

require "mocha/setup"

Please considering updating your test/test_helper and gemspec for this new version.

Cheers,

Cédric

I want to allow some URIs to not be faked

I like to have a failsafe setup where net_connects are disallowed by default.
I've implemented a method for specifying some URIs which are whitelisted.

Please let me know if I need more test coverage, or if the implementation could be more obvious.

The branch "passthrough" is on my fork.
halorgium/fakeweb@15f5eb2

Release is desperately needed

The last version 1.3.0 was released in year 2010, 6 years ago!
People needed to pull down changes which were made since that.

Please please please bump up and release a new version.

FakeWeb doesn't match non alphabetized query strings

I have a a registered URI regex of FileType=png&FileType=doc which are not in alphabetical order.

When I do a request for that I get an error that the URI is not registered, but flipping the png and doc around works.

I have built a custom query string normalizer called "non sorting normalizer" that is designed to remove the alphabetical order of array arguments. Can fakeweb be made to work with this?

FakeWeb post method and check the request.body

In the method FakeWeb.register_uri there is a :body option but it is related to the response.body.

How can I define a request.body in the way the mocked request has to have this body in order to accomplish the mock?

This is for a post method.

Example in README for Regexp register_uri

I didn't realize fakeweb had this feature until I searched the Google Groups list (well, I would have realized had I read the issues here).

I propose adding an example of this to the README, in addition to the brief mention it gets. I've done this over in akahn/fakeweb@d6076cf.

Let me know your thoughts on this addition.

Ruby 2 stops receiving security updates at the end of March

Ruby 2 is reaching end-of-life and will no longer receive security updates after 31 March 2023. Most production Ruby code, therefore, must be running on Ruby 3 no later than 1st April onwards.

I've had a quick look and found the attached failures and errors when running the tests for fakeweb under Ruby 3.

fakeweb test failures.txt

For the software I care about today, I'll probably just break the dependency chain further up, so that we no longer depend on fakeweb, but I figured I might as well post this somewhere.

FakeWeb changes the Net::HTTP behavior of a request with a block that calls #read_body on the response

Given this ruby script:

require 'net/http'
require 'rubygems'

def perform_test
  response = Net::HTTP.new('example.com', 80).request_get('/') { |r| r.read_body { } }
  puts "  " + response.body.class.to_s
end

puts "Without FakeWeb loaded: "
perform_test

require 'fakeweb'
FakeWeb.allow_net_connect = true
puts "With FakeWeb loaded, and http connections allowed: "
perform_test

FakeWeb.register_uri(:get, 'http://example.com/', :body => 'body')
puts "With a stubbed request: "
perform_test

I get this output:

$ ruby fakeweb_example.rb 
Without FakeWeb loaded: 
  Net::ReadAdapter
With FakeWeb loaded, and http connections allowed: 
  Net::ReadAdapter
With a stubbed request: 
  String

As you can see, Net::HTTP causes the body of a response for a request with a block that calls #read_body to be a Net::ReadAdapter, but FakeWeb modifies this behavior when stubbing a request. FakeWeb causes it to be a String.

I've got a fix for this bug here.

Compatibility Issue with Ruby 2.4?

I installed fakeweb (1.3.0) in Ruby 2.4.1 and Rails 5.1.4 and when tried to use it I started to get a this error.

Failure/Error: Net::HTTP.get(url)
NoMethodError:  undefined method `<<' for {:read_timeout=>60, :continue_timeout=>nil, :debug_output=>nil}:Hash

And this one, whenever I tried to make and HTTP call using Net::HTTP.

Failure/Error: puts Net::HTTP.get(@uri)
NoMethodError:  undefined method `close' for #<FakeWeb::StubSocket:0x007fc72ea8f230>

I searched the web and found that many other testing frameworks are affected by this compatibility issue and after reading many posts for WebMock and VCR, I finally came across a solution. It suggested doing this and oddly enough it worked.

gem 'fakeweb', :git => 'https://github.com/chrisk/fakeweb.git'

Is it time to make a new gem release?
Why does this work around actually work but the release gem fails?

I hope this helps.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.