Webmock: Registered mock not responding

Created on 8 Jun 2013  路  8Comments  路  Source: bblimke/webmock

Caveat, this could well be operator error.

Here's the error message I get when one of my tests fails:

     Failure/Error: visit root_path
     WebMock::NetConnectNotAllowedError:
       Real HTTP connections are disabled. Unregistered request: GET http://127.0.0.1:61411/__identify__ with headers {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent'=>'Ruby'}

       You can stub this request with the following snippet:

       stub_request(:get, "http://127.0.0.1:61411/__identify__").
         with(:headers => {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent'=>'Ruby'}).
         to_return(:status => 200, :body => "", :headers => {})

       registered request stubs:

       stub_request(:get, "http://127.0.0.1:61386/__identify__").
         with(:headers => {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent'=>'Ruby'})

Based upon the error message, it looks like my stubbed request should be working.

The exact snipped from error message above is in the "config.before :each" block of my rspec config in spec_helper.rb.

Any ideas what might be going on?

Most helpful comment

You have to allow connections to localhost if you use Poltergeist, Selenium or any other driver that requires localhost server to be started. Add WebMock.disable_net_connect!(:allow_localhost => true) to config.before

All 8 comments

The failure is coming on a test that uses the poltergeist driver for Capybara. We might be looking at an issue with the interplay of these systems.

You have to allow connections to localhost if you use Poltergeist, Selenium or any other driver that requires localhost server to be started. Add WebMock.disable_net_connect!(:allow_localhost => true) to config.before

@emarthinsen, you still having this issue? What version of Ruby are you using?

I was having a similar issue, I found removing the 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3' field fixed it.

Whats weird is that it would pass fine with that field in Ruby 2.0.0, but fails in other versions (1.8.7, 1.9.2 and 1.9.2), it must be the way different versions of Ruby handle HTTP requests or something?

@petems, I actually ended up changing my testing strategy to get around this issue.

@petems Yes, different Ruby versions usually add different Accept-Encoding headers. The suggested signature generated by WebMock shows all headers, as it has no idea which of them are "not important". I usually get rid of Accept-Encoding from my stub declarations.

I'm experiencing the same error even after removing the Accept-Encoding headers. You can see in the stack trace below that the first registered request stub is identical to the request stub suggested in the error message:

  You can stub this request with the following snippet:

  stub_request(:get, "https://graph.facebook.com/act_9999999/generatepreviews?access_token=thisistoken%20999999&creative=%7B%22link_url%22:%22http://www.test.com%22,%22title%22:%22Check%20out%20this%20new%20ad!%22,%22body%22:%22This%20product%20will%20change%20your%20life.%22,%22id%22:36,%22fb_ads_ad_group_id%22:36,%22creative_key%22:null,%22related_fan_page%22:null,%22draft%22:true,%22fb_ads_ad_image_id%22:36,%22preview_url%22:null,%22image_url%22:null,%22type%22:1%7D").
    with(:headers => {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent'=>'Ruby'}).
    to_return(:status => 200, :body => "", :headers => {})

  registered request stubs:

  stub_request(:get, "https://graph.facebook.com/act_9999999/generatepreviews?access_token=thisistoken%20999999&creative=%7B%22link_url%22:%22http://www.test.com%22,%22title%22:%22Check%20out%20this%20new%20ad!%22,%22body%22:%22This%20product%20will%20change%20your%20life.%22,%22id%22:15,%22fb_ads_ad_group_id%22:15,%22creative_key%22:null,%22related_fan_page%22:null,%22draft%22:true,%22fb_ads_ad_image_id%22:15,%22preview_url%22:null,%22image_url%22:null,%22type%22:1%7D").
    with(:headers => {'Accept'=>'*/*', 'User-Agent'=>'Ruby'})
  stub_request(:get, "https://hyfn8r-dev.s3.amazonaws.com/tmp/images/df6ea63dc6470d4a13544e8acd35a461.jpeg").
    with(:headers => {'Accept'=>'*/*', 'User-Agent'=>'Ruby'})
  stub_request(:get, "https://graph.facebook.com/act_9999999/reachestimate?access_token=thisistoken%20999999&currency=USD&targeting_spec=%7B%22countries%22:%5B%22US%22%5D,%22genders%22:%5B1%5D,%22relationship_statuses%22:%5B1,2,3,4,6%5D,%22broad_age%22:0,%22radius%22:10%7D").
    with(:headers => {'Accept'=>'*/*', 'User-Agent'=>'Ruby'})
  stub_request(:get, "https://graph.facebook.com/act_9999999/reachestimate?access_token=thisistoken%20999999&currency=USD&targeting_spec=%7B%22countries%22:%5B%22US%22%5D,%22relationship_statuses%22:%5B1,2,3,4,6%5D,%22broad_age%22:0%7D").
    with(:headers => {'Accept'=>'*/*', 'User-Agent'=>'Ruby'})
  stub_request(:get, "https://graph.facebook.com/act_9999999/adpreviewscss?access_token=thisistoken%20999999").
    with(:headers => {'Accept'=>'*/*', 'User-Agent'=>'Ruby'})
  stub_request(:get, "https://graph.facebook.com/act_9999999/generatepreviews?access_token=thisistoken%20999999&creative=%7B%22link_url%22:%22%22,%22title%22:%22%22,%22body%22:%22%22,%22image_url%22:%22%22,%22type%22:1%7D").
    with(:headers => {'Accept'=>'*/*', 'User-Agent'=>'Ruby'})

  ============================================================ (WebMock::NetConnectNotAllowedError)
  /Users/ycha28/hyfn8r/vendor/bundle/gems/webmock-1.9.0/lib/webmock/http_lib_adapters/net_http.rb:107:in `request'
  /Users/ycha28/.rbenv/versions/1.9.3-p448/lib/ruby/1.9.1/net/http.rb:1027:in `get'
  /Users/ycha28/hyfn8r/vendor/bundle/gems/faraday-0.8.4/lib/faraday/adapter/net_http.rb:72:in `perform_request'
  /Users/ycha28/hyfn8r/vendor/bundle/gems/faraday-0.8.4/lib/faraday/adapter/net_http.rb:37:in `call'
  /Users/ycha28/hyfn8r/vendor/bundle/gems/faraday-0.8.4/lib/faraday/request/url_encoded.rb:14:in `call'
  /Users/ycha28/hyfn8r/lib/faraday_middleware/response/facebook_usage_meter.rb:21:in `call'
  /Users/ycha28/hyfn8r/vendor/bundle/gems/faraday-0.8.4/lib/faraday/request/multipart.rb:13:in `call'
  /Users/ycha28/hyfn8r/vendor/bundle/gems/faraday-0.8.4/lib/faraday/connection.rb:226:in `run_request'
  /Users/ycha28/hyfn8r/vendor/bundle/gems/faraday-0.8.4/lib/faraday/connection.rb:87:in `get'
  /Users/ycha28/hyfn8r/vendor/bundle/gems/koala-1.6.0/lib/koala/http_service.rb:78:in `make_request'
  /Users/ycha28/hyfn8r/vendor/bundle/gems/koala-1.6.0/lib/koala.rb:51:in `make_request'
  /Users/ycha28/hyfn8r/vendor/bundle/gems/koala-1.6.0/lib/koala/api.rb:51:in `api'
  /Users/ycha28/hyfn8r/vendor/bundle/gems/koala-1.6.0/lib/koala/api/graph_api.rb:468:in `graph_call'
  /Users/ycha28/hyfn8r/vendor/bundle/gems/koala-1.6.0/lib/koala/api/graph_api.rb:117:in `get_connection'
  /Users/ycha28/hyfn8r/app/concerns/models/facebook_ads/ad_group_logic/fb_apis.rb:24:in `api_get_preview_html'
  /Users/ycha28/hyfn8r/app/concerns/models/facebook_ads/ad_group_logic/support.rb:29:in `get_preview_html'
  /Users/ycha28/hyfn8r/app/controllers/api/v1/ads/ad_groups_controller.rb:110:in `preview_html'
  /Users/ycha28/hyfn8r/vendor/bundle/gems/actionpack-3.2.12/lib/abstract_controller/base.rb:167:in `process_action'
  /Users/ycha28/hyfn8r/vendor/bundle/gems/rocket_pants-1.5.5/lib/rocket_pants/controller/instrumentation.rb:22:in `block in process_action'
  /Users/ycha28/hyfn8r/vendor/bundle/gems/activesupport-3.2.12/lib/active_support/notifications.rb:123:in `block in instrument'
  /Users/ycha28/hyfn8r/vendor/bundle/gems/activesupport-3.2.12/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
  /Users/ycha28/hyfn8r/vendor/bundle/gems/activesupport-3.2.12/lib/active_support/notifications.rb:123:in `instrument'
  /Users/ycha28/hyfn8r/vendor/bundle/gems/rocket_pants-1.5.5/lib/rocket_pants/controller/instrumentation.rb:21:in `process_action'
  /Users/ycha28/hyfn8r/vendor/bundle/gems/actionpack-3.2.12/lib/abstract_controller/callbacks.rb:18:in `block in process_action'
  /Users/ycha28/hyfn8r/vendor/bundle/gems/activesupport-3.2.12/lib/active_support/callbacks.rb:470:in `block in _run__1014806395370160471__process_action__3755195299212196675__callbacks'
  /Users/ycha28/hyfn8r/vendor/bundle/gems/activesupport-3.2.12/lib/active_support/callbacks.rb:215:in `block in _conditional_callback_around_3604'
  /Users/ycha28/hyfn8r/app/controllers/api/v1/api_base_controller.rb:32:in `exception_handler'
  /Users/ycha28/hyfn8r/vendor/bundle/gems/activesupport-3.2.12/lib/active_support/callbacks.rb:214:in `_conditional_callback_around_3604'
  /Users/ycha28/hyfn8r/vendor/bundle/gems/activesupport-3.2.12/lib/active_support/callbacks.rb:425:in `_run__1014806395370160471__process_action__3755195299212196675__callbacks'
  /Users/ycha28/hyfn8r/vendor/bundle/gems/activesupport-3.2.12/lib/active_support/callbacks.rb:405:in `__run_callback'
  /Users/ycha28/hyfn8r/vendor/bundle/gems/activesupport-3.2.12/lib/active_support/callbacks.rb:385:in `_run_process_action_callbacks'
  /Users/ycha28/hyfn8r/vendor/bundle/gems/activesupport-3.2.12/lib/active_support/callbacks.rb:81:in `run_callbacks'
  /Users/ycha28/hyfn8r/vendor/bundle/gems/actionpack-3.2.12/lib/abstract_controller/callbacks.rb:17:in `process_action'
  /Users/ycha28/hyfn8r/vendor/bundle/gems/actionpack-3.2.12/lib/action_controller/metal/rescue.rb:29:in `process_action'
  /Users/ycha28/hyfn8r/vendor/bundle/gems/rocket_pants-1.5.5/lib/rocket_pants/controller/rescuable.rb:53:in `process_action'
  /Users/ycha28/hyfn8r/vendor/bundle/gems/actionpack-3.2.12/lib/action_controller/metal/params_wrapper.rb:207:in `process_action'
  /Users/ycha28/hyfn8r/vendor/bundle/gems/actionpack-3.2.12/lib/abstract_controller/base.rb:121:in `process'
  /Users/ycha28/hyfn8r/vendor/bundle/gems/actionpack-3.2.12/lib/action_controller/metal.rb:203:in `dispatch'
  /Users/ycha28/hyfn8r/vendor/bundle/gems/actionpack-3.2.12/lib/action_controller/metal/rack_delegation.rb:14:in `dispatch'
  /Users/ycha28/hyfn8r/vendor/bundle/gems/actionpack-3.2.12/lib/action_controller/metal.rb:246:in `block in action'
  /Users/ycha28/hyfn8r/vendor/bundle/gems/actionpack-3.2.12/lib/action_dispatch/routing/route_set.rb:73:in `call'
  /Users/ycha28/hyfn8r/vendor/bundle/gems/actionpack-3.2.12/lib/action_dispatch/routing/route_set.rb:73:in `dispatch'
  /Users/ycha28/hyfn8r/vendor/bundle/gems/actionpack-3.2.12/lib/action_dispatch/routing/route_set.rb:36:in `call'
  /Users/ycha28/hyfn8r/vendor/bundle/gems/journey-1.0.4/lib/journey/router.rb:68:in `block in call'
  /Users/ycha28/hyfn8r/vendor/bundle/gems/journey-1.0.4/lib/journey/router.rb:56:in `each'
  /Users/ycha28/hyfn8r/vendor/bundle/gems/journey-1.0.4/lib/journey/router.rb:56:in `call'
  /Users/ycha28/hyfn8r/vendor/bundle/gems/actionpack-3.2.12/lib/action_dispatch/routing/route_set.rb:601:in `call'
  /Users/ycha28/hyfn8r/vendor/bundle/gems/omniauth-1.1.1/lib/omniauth/strategy.rb:177:in `call!'
  /Users/ycha28/hyfn8r/vendor/bundle/gems/omniauth-1.1.1/lib/omniauth/strategy.rb:157:in `call'
  /Users/ycha28/hyfn8r/vendor/bundle/gems/omniauth-1.1.1/lib/omniauth/strategy.rb:177:in `call!'
  /Users/ycha28/hyfn8r/vendor/bundle/gems/omniauth-1.1.1/lib/omniauth/strategy.rb:157:in `call'
  /Users/ycha28/hyfn8r/vendor/bundle/gems/omniauth-1.1.1/lib/omniauth/strategy.rb:177:in `call!'
  /Users/ycha28/hyfn8r/vendor/bundle/gems/omniauth-1.1.1/lib/omniauth/strategy.rb:157:in `call'
  /Users/ycha28/hyfn8r/vendor/bundle/gems/omniauth-1.1.1/lib/omniauth/strategy.rb:177:in `call!'
  /Users/ycha28/hyfn8r/vendor/bundle/gems/omniauth-1.1.1/lib/omniauth/strategy.rb:157:in `call'
  /Users/ycha28/hyfn8r/app/middleware/redirector.rb:17:in `call'
  /Users/ycha28/hyfn8r/vendor/bundle/gems/warden-1.2.3/lib/warden/manager.rb:35:in `block in call'
  /Users/ycha28/hyfn8r/vendor/bundle/gems/warden-1.2.3/lib/warden/manager.rb:34:in `catch'
  /Users/ycha28/hyfn8r/vendor/bundle/gems/warden-1.2.3/lib/warden/manager.rb:34:in `call'
  /Users/ycha28/hyfn8r/vendor/bundle/gems/actionpack-3.2.12/lib/action_dispatch/middleware/best_standards_support.rb:17:in `call'
  /Users/ycha28/hyfn8r/vendor/bundle/gems/rack-1.4.5/lib/rack/etag.rb:23:in `call'
  /Users/ycha28/hyfn8r/vendor/bundle/gems/rack-1.4.5/lib/rack/conditionalget.rb:35:in `call'
  /Users/ycha28/hyfn8r/vendor/bundle/gems/actionpack-3.2.12/lib/action_dispatch/middleware/head.rb:14:in `call'
  /Users/ycha28/hyfn8r/vendor/bundle/gems/actionpack-3.2.12/lib/action_dispatch/middleware/params_parser.rb:21:in `call'
  /Users/ycha28/hyfn8r/vendor/bundle/gems/actionpack-3.2.12/lib/action_dispatch/middleware/flash.rb:242:in `call'
  /Users/ycha28/hyfn8r/vendor/bundle/gems/rack-1.4.5/lib/rack/session/abstract/id.rb:210:in `context'
  /Users/ycha28/hyfn8r/vendor/bundle/gems/rack-1.4.5/lib/rack/session/abstract/id.rb:205:in `call'
  /Users/ycha28/hyfn8r/vendor/bundle/gems/actionpack-3.2.12/lib/action_dispatch/middleware/cookies.rb:341:in `call'
  /Users/ycha28/hyfn8r/vendor/bundle/gems/activerecord-3.2.12/lib/active_record/query_cache.rb:64:in `call'
  /Users/ycha28/hyfn8r/vendor/bundle/gems/activerecord-3.2.12/lib/active_record/connection_adapters/abstract/connection_pool.rb:479:in `call'
  /Users/ycha28/hyfn8r/vendor/bundle/gems/actionpack-3.2.12/lib/action_dispatch/middleware/callbacks.rb:28:in `block in call'
  /Users/ycha28/hyfn8r/vendor/bundle/gems/activesupport-3.2.12/lib/active_support/callbacks.rb:405:in `_run__3408883378271992367__call__1257581451470831248__callbacks'
  /Users/ycha28/hyfn8r/vendor/bundle/gems/activesupport-3.2.12/lib/active_support/callbacks.rb:405:in `__run_callback'
  /Users/ycha28/hyfn8r/vendor/bundle/gems/activesupport-3.2.12/lib/active_support/callbacks.rb:385:in `_run_call_callbacks'
  /Users/ycha28/hyfn8r/vendor/bundle/gems/activesupport-3.2.12/lib/active_support/callbacks.rb:81:in `run_callbacks'
  /Users/ycha28/hyfn8r/vendor/bundle/gems/actionpack-3.2.12/lib/action_dispatch/middleware/callbacks.rb:27:in `call'
  /Users/ycha28/hyfn8r/vendor/bundle/gems/actionpack-3.2.12/lib/action_dispatch/middleware/remote_ip.rb:31:in `call'
  /Users/ycha28/hyfn8r/vendor/bundle/gems/actionpack-3.2.12/lib/action_dispatch/middleware/debug_exceptions.rb:16:in `call'
  /Users/ycha28/hyfn8r/vendor/bundle/gems/actionpack-3.2.12/lib/action_dispatch/middleware/show_exceptions.rb:56:in `call'
  /Users/ycha28/hyfn8r/vendor/bundle/gems/cucumber-rails-1.3.1/lib/cucumber/rails/action_controller.rb:10:in `call'
  /Users/ycha28/hyfn8r/vendor/bundle/gems/railties-3.2.12/lib/rails/rack/logger.rb:32:in `call_app'
  /Users/ycha28/hyfn8r/vendor/bundle/gems/railties-3.2.12/lib/rails/rack/logger.rb:16:in `block in call'
  /Users/ycha28/hyfn8r/vendor/bundle/gems/activesupport-3.2.12/lib/active_support/tagged_logging.rb:22:in `tagged'
  /Users/ycha28/hyfn8r/vendor/bundle/gems/railties-3.2.12/lib/rails/rack/logger.rb:16:in `call'
  /Users/ycha28/hyfn8r/config/initializers/quite_assets.rb:6:in `call_with_quiet_assets'
  /Users/ycha28/hyfn8r/vendor/bundle/gems/actionpack-3.2.12/lib/action_dispatch/middleware/request_id.rb:22:in `call'
  /Users/ycha28/hyfn8r/vendor/bundle/gems/rack-1.4.5/lib/rack/methodoverride.rb:21:in `call'
  /Users/ycha28/hyfn8r/vendor/bundle/gems/rack-1.4.5/lib/rack/runtime.rb:17:in `call'
  /Users/ycha28/hyfn8r/vendor/bundle/gems/activesupport-3.2.12/lib/active_support/cache/strategy/local_cache.rb:72:in `call'
  /Users/ycha28/hyfn8r/vendor/bundle/gems/rack-1.4.5/lib/rack/lock.rb:15:in `call'
  /Users/ycha28/hyfn8r/vendor/bundle/gems/actionpack-3.2.12/lib/action_dispatch/middleware/static.rb:62:in `call'
  /Users/ycha28/hyfn8r/vendor/bundle/gems/railties-3.2.12/lib/rails/engine.rb:479:in `call'
  /Users/ycha28/hyfn8r/vendor/bundle/gems/railties-3.2.12/lib/rails/application.rb:223:in `call'
  /Users/ycha28/hyfn8r/vendor/bundle/gems/rack-1.4.5/lib/rack/builder.rb:134:in `call'
  /Users/ycha28/hyfn8r/vendor/bundle/gems/rack-1.4.5/lib/rack/urlmap.rb:64:in `block in call'
  /Users/ycha28/hyfn8r/vendor/bundle/gems/rack-1.4.5/lib/rack/urlmap.rb:49:in `each'
  /Users/ycha28/hyfn8r/vendor/bundle/gems/rack-1.4.5/lib/rack/urlmap.rb:49:in `call'
  /Users/ycha28/hyfn8r/vendor/bundle/gems/capybara-2.1.0/lib/capybara/server.rb:19:in `call'
  /Users/ycha28/hyfn8r/vendor/bundle/gems/rack-1.4.5/lib/rack/handler/webrick.rb:59:in `service'
  /Users/ycha28/.rbenv/versions/1.9.3-p448/lib/ruby/1.9.1/webrick/httpserver.rb:138:in `service'
  /Users/ycha28/.rbenv/versions/1.9.3-p448/lib/ruby/1.9.1/webrick/httpserver.rb:94:in `run'
  /Users/ycha28/.rbenv/versions/1.9.3-p448/lib/ruby/1.9.1/webrick/server.rb:191:in `block in start_thread'

I'm using Poltergeist as my driver. Is anyone else still experiencing this problem?

@ycha28 I just tried to reproduce the scenario you described, and it works fine:

stub_request(:get, "https://graph.facebook.com/act_9999999/generatepreviews?access_token=thisistoken%20999999&creative=%7B%22link_url%22:%22http://www.test.com%22,%22title%22:%22Check%20out%20this%20new%20ad!%22,%22body%22:%22This%20product%20will%20change%20your%20life.%22,%22id%22:15,%22fb_ads_ad_group_id%22:15,%22creative_key%22:null,%22related_fan_page%22:null,%22draft%22:true,%22fb_ads_ad_image_id%22:15,%22preview_url%22:null,%22image_url%22:null,%22type%22:1%7D")

uri = URI.parse("https://graph.facebook.com/act_9999999/generatepreviews?access_token=thisistoken%20999999&creative=%7B%22link_url%22:%22http://www.test.com%22,%22title%22:%22Check%20out%20this%20new%20ad!%22,%22body%22:%22This%20product%20will%20change%20your%20life.%22,%22id%22:15,%22fb_ads_ad_group_id%22:15,%22creative_key%22:null,%22related_fan_page%22:null,%22draft%22:true,%22fb_ads_ad_image_id%22:15,%22preview_url%22:null,%22image_url%22:null,%22type%22:1%7D")
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE 
http.get(uri.request_uri)

You will need to provide a failing test/code.

I was having a similar issue with the Capybara/Webdriver testing (via rspec). putting the below code inside of /config/environments/test.rb Rails.application.configure do fixed it.

  module Addressable
    class URI
      alias_method :orig_scheme=, :scheme=
      def scheme=(new_scheme)
        if new_scheme == "127.0.0.1"
          new_scheme = "http"
        end
        self.orig_scheme=new_scheme
      end
    end
  end
Was this page helpful?
0 / 5 - 0 ratings

Related issues

Integralist picture Integralist  路  5Comments

vangberg picture vangberg  路  3Comments

dnesteryuk picture dnesteryuk  路  6Comments

rurounijones picture rurounijones  路  4Comments

RKushnir picture RKushnir  路  14Comments