Search code examples
regexrspeccapybarawebmock

Make a webmock with port range


I'm using rspec and capybara with js: true in the describe but I have one problem :

     Failure/Error: visit '/users/sign_in'
     WebMock::NetConnectNotAllowedError:
       Real HTTP connections are disabled. Unregistered request: GET http://127.0.0.1:62453/__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:62453/__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 => {})

If I stub it like they said, next port is different :

  1) the signin process signs me in
     Failure/Error: visit '/users/sign_in'
     WebMock::NetConnectNotAllowedError:
       Real HTTP connections are disabled. Unregistered request: GET http://127.0.0.1:62453/__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:62453/__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:61772/__identify__").
         with(:headers => {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent'=>'Ruby'})

I've try to add regex to the port and it failed all the time.


Solution

  • You can't use webmock and js: true at the same time. When js: true is set Capybara is using a separate browser that makes the requests so any attempt to intercept connections in the test thread will have no effect.
    The connection attempt you're seeing shown in your output is actually Capybara attempting to start it's server to run the app in, and verify the server is started. Blocking that will prevent everything from working. If you really need to mock some requests while using capybara with js: true you should probably be looking at the puffing-billy gem which implements a proxy that intergrates with capybara drivers and allows for that sort of thing.