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

WebMock and puffing-billy #44

Closed
dezmathio opened this Issue Mar 25, 2014 · 19 comments

Comments

Projects
None yet
7 participants
@dezmathio

dezmathio commented Mar 25, 2014

I'm trying to set up puffing-billy on my local repo, and I'm running into a few issues. I am basically registering the webkit driver like this:

Capybara.register_driver :webkit do |app|
Capybara::Webkit::Driver.new(app)
end

And then I am adding the @billy tag to my scenarios, and I've got a Before hook just like the one you guys have in the readme, which sets the current_driver and javascript_drivers to be :webkit_billy

Now, my issue is that if I visit a page when the driver is just :webkit
I take a screenshot, and everything looks proper, all the assets are there, it looks good.
However; When I run it tagged with @billy, or if I set the current_driver and javascript_driver's to be :webkit_billy, all of the assets are not rendered...

I suspect it might be related to nginx, since the asset host is http://localhost:8089

I added this into a billy.rb file which is in features/support/

server = Capybara.current_session.server
Billy.config.whitelist = ["#{server.host}:#{server.port}/", 'http://localhost:8089']

Doesn't seem to help with my issue though. Should I add anything else beyond that? Am I missing some config settings to get this running properly with capybara-webkit?

just noticed @oesmith was active 20 min ago so I'm hoping to get someone's attention!

@dezmathio

This comment has been minimized.

dezmathio commented Mar 25, 2014

Just realized in other issues below that there's no need to re-register the :webkit driver. That still hasn't resolved the issue I was seeing, though.

@oesmith

This comment has been minimized.

Owner

oesmith commented Mar 25, 2014

Have you tried using localhost:8089 (i.e., no http:// prefix) in your whitelist entry? AFAIK, the whitelist only matches host:port, not full urls.

@dezmathio

This comment has been minimized.

dezmathio commented Mar 25, 2014

Yeah I added that in there too, and a bunch of other variations like this:

Billy.config.whitelist = [/.*/,"#{server.host}:#{server.port}", 'http://localhost:8089', '//localhost:8089/static', 'localhost:8089', 'localhost', server.host]

the /.*/ was attempting to whitelist everything. Is there an option like that?

Basically at this point I want it to only stub external requests if I want it to.

if I do a visit 'http://www.google.com'
in selenium_billy, it tells me that the proxy server is refusing connections.
"Firefox is configured to use a proxy server that is refusing connections."

could that be related also? Do you guys happen to have an IRC channel or something you hang out on? I feel like it's quite difficult to get a hold of you or @ronwsmith unless I'm lucky!

@ronwsmith

This comment has been minimized.

Collaborator

ronwsmith commented Mar 26, 2014

Hmm, are you using another proxy server for anything? It sounds like your setup is quite unique.

Try tailing your logs, via tail -f log/* | grep puffing-billy, and see what it outputs for both the assets and your google test. It will help track down where it's being stopped, if it's even getting to the Billy proxy.

What does your Billy.config block look like? Also, do you have any other driver configuration settings in your spec_helper,rb?

Lastly, I don't think you'll get real-time support for any gems :) A day or two response is typical for ones under active development.

@dezmathio

This comment has been minimized.

dezmathio commented Mar 26, 2014

this is the output i'm getting atm:

puffing-billy: Proxy listening on http://localhost:54085
puffing-billy: STUB GET for 'http://www.google.com/'
puffing-billy: CACHE KEY for 'http://127.0.0.1:54086/' is 'get_127.0.0.1_62fe3d14ea426b905bd8e3e397d976261006bbe8'
puffing-billy: PROXY GET for 'http://127.0.0.1:54086/'
puffing-billy: Received response status code 200 for http://127.0.0.1:54086/
puffing-billy: CACHE KEY for 'http://127.0.0.1:54086/assets/wedding.css' is 'get_127.0.0.1_0cba30f27f30253eb08410bd2920111d18cbc34b'
puffing-billy: PROXY GET for 'http://127.0.0.1:54086/assets/wedding.css'
puffing-billy: CACHE KEY for 'http://127.0.0.1:54086/assets/application.js' is 'get_127.0.0.1_94d99b6a3944dceb9304887e5da3634d7d46bdd6'
puffing-billy: PROXY GET for 'http://127.0.0.1:54086/assets/application.js'
puffing-billy: CACHE KEY for 'http://cdn.optimizely.com/js/29863440.js' is 'get_cdn.optimizely.com_1174e753bd6e7cd3ff0c65340db12368d05234e3'
puffing-billy: PROXY GET for 'http://cdn.optimizely.com/js/29863440.js'
puffing-billy: Request failed: http://127.0.0.1:54086/assets/wedding.css
puffing-billy: Request failed: http://127.0.0.1:54086/assets/application.js

I created a billy.rb file in the features/support/ folder that gets required when I run cucumber.
this is what's in it:

require_relative '../support/webmock' ### I require this because we use WebMock and it doesnt like one of these calls.

server = Capybara.current_session.server

Billy.configure do |c|
c.cache = false
c.cache_request_headers = false
c.ignore_params = ["http://#{server.host}:#{server.port}/assets/application.js", "http://#{server.host}:#{server.port}/assets/wedding.css"]
c.persist_cache = false
c.non_successful_cache_disabled = false
c.non_successful_error_level = :warn
c.non_whitelisted_requests_disabled = false
c.whitelist = ["#{server.host}:#{server.port}", 'localhost:8089']
end

So... right now it seems like the problem lies with these two requests:
puffing-billy: Request failed: http://127.0.0.1:54086/assets/wedding.css
puffing-billy: Request failed: http://127.0.0.1:54086/assets/application.js

as you can see in the config, I tried adding in those links to the c.ignore_params array, but this is still happening.

I did notice however that the log says "Listening on proxy" and the port is different than my capybara server.

Is that just the EM ip?

Any ideas on how to further troubleshoot this? Thx for the quick replies!

@ronwsmith

This comment has been minimized.

Collaborator

ronwsmith commented Mar 26, 2014

Yes the different IP is EM and is expected.

I think the root of the issue is the "Firefox is configured to use a proxy server that is refusing connections" error, which is likely outside of puffing-billy. Do you have or require any other proxy servers to hit your web site outside of puffing-billy? It potentially is also webmock blocking requests. I've had to set allow_net_connect! in my spec_helper to get "external" requests to process correctly.

@dezmathio

This comment has been minimized.

dezmathio commented Mar 26, 2014

Yeah, essentially my require_relative '../support/webmock' basically requires a file which does this:

WebMock.disable_net_connect!(:allow_localhost => true)

Which... afaik shouldn't affect those assets from being loaded.
I don't believe we require any other proxy servers to hit the website, or any proxy settings within the cucumber setup in general, besides webmock / puffing-billy.

Is there any other data I can look into to better troubleshoot this?

@oesmith

This comment has been minimized.

Owner

oesmith commented Mar 26, 2014

AFAIK, WebMock doesn't play well with Billy. It hooks into the http client
library that billy uses to do upstream proxy requests. Have you tried
disabling WebMock completely to see what effect that has?

Olly

On Wednesday, March 26, 2014, Josiah Anjos notifications@github.com wrote:

Yeah, essentially my require_relative '../support/webmock' basically
requires a file which does this:

WebMock.disable_net_connect!(:allow_localhost => true)

Which... afaik shouldn't affect those assets from being loaded.
I don't believe we require any other proxy servers to hit the website, or
any proxy settings within the cucumber setup in general, besides webmock /
puffing-billy.

Is there any other data I can look into to better troubleshoot this?

Reply to this email directly or view it on GitHubhttps://github.com//issues/44#issuecomment-38741703
.

@dezmathio

This comment has been minimized.

dezmathio commented Mar 27, 2014

Ah you guys are right. I did WebMock.allow_net_connect! and everything did render... I would like to keep both though... Do you guys have any experience with using both? Having WebMock allow puffing-billy to do all its magic?

Edit: Also, changed the title to reflect the actual issue!

@dezmathio dezmathio changed the title from capybara-webkit and webkit_billy to WebMock and puffing-billy Mar 27, 2014

@ronwsmith

This comment has been minimized.

Collaborator

ronwsmith commented Mar 27, 2014

Great, glad we narrowed it down! Unfortunately, I don't think they will ever work together nicely as they are sort of different solutions for the same issue -- both are intercepting requests and performing some action. Perhaps look into using the stubbing feature of puffing-billy to eliminate whatever need you have for webmock.

@dezmathio

This comment has been minimized.

dezmathio commented Mar 27, 2014

Well, correct me if I'm wrong, but isn't puffing-billy more browser centric, and webmock is more about API calls your app makes?

Can puffing-billy do what webmock does for the API?

@ronwsmith

This comment has been minimized.

Collaborator

ronwsmith commented Mar 27, 2014

You are correct but it's not only API calls for webmock, it's anything in the same thread (whereas browsers are running in a different thread). What I do is limit webmock to unit tests and puffing-billy to feature tests and I turn webmock off and on in before and after blocks for type: :feature.

@dezmathio

This comment has been minimized.

dezmathio commented Mar 27, 2014

Hmm... Gotcha. I was hoping to make puffing-billy a general thing for our cucumber tests, but it looks like it's going to have to be something more localized/specific for some scenarios. We have some cases where we use webmock to return specific data for our api calls during acceptance/feature tests, so if we can't use both, I may have to come up with a custom way of setting up this data. Maybe even in a Before do, checking if the current driver is :webkit_billy, then to do a WebMock.allow_net_connect! or something of the sorts.
Well, I think this helps me figure all of this out, and will allow me to make an informed decision about which tools to use. Thanks for getting back to me so quickly again, guys.

@dezmathio dezmathio closed this Mar 27, 2014

@aivils

This comment has been minimized.

aivils commented Oct 20, 2014

Suggestion does not work in my case. Looks like Webmock kills billy proxy server. I made proxy start/stop for each rspec. proxy runs with fixed TCP port.

module Billy
  class Config
    attr_accessor :port
  end
end

module Billy
  class Proxy
    def stop
      EM.stop_server(@signature) if @signature
      @signature = nil
    end

    def port
      Billy.config.port
    end

    protected

    def main_loop
      EM.run do
        EM.error_handler do |e|
          puts e.class.name, e
          puts e.backtrace.join("\n")
        end

        @signature = EM.start_server('127.0.0.1', port, ProxyConnection) do |p|
          p.handler = self
          p.cache = @cache
        end

        Billy.log(:info, "puffing-billy: Proxy listening on #{url}")
      end
    end
  end
end

Billy.configure do |c|
  c.ignore_cache_port = false
  c.cache = true
  c.cache_path = 'spec/fixtures/billy/'
  c.persist_cache = true
  c.port = 51111
end

def billy_whitelist_capybara
  if server = Capybara.current_session.server
    server_url = "#{server.host}:#{server.port}"
    unless Billy.config.whitelist.include?(server_url)
      Billy.configure do |c|
        c.whitelist << server_url
      end
    end
  end
end

RSpec.configure do |config|
  config.before(:each, billy: true) do
    WebMock.allow_net_connect!
    billy_whitelist_capybara
    proxy.start
    Capybara.current_driver = :selenium_billy
  end

  config.after(:each, billy: true) do
    Capybara.use_default_driver
    Capybara.javascript_driver = :poltergeist
    proxy.stop
    proxy.cache.use_default_scope
    WebMock.disable_net_connect!(allow_localhost: true)
  end
end

@ronwsmith

This comment has been minimized.

Collaborator

ronwsmith commented Dec 17, 2014

@aivils, what exactly are you trying to do? Your setup looks overly complicated so my initial assumption is that your test is trying to do too much. Can you provide a (scrubbed) example spec?

@aivils

This comment has been minimized.

aivils commented Dec 18, 2014

I try to run multiple tests from a single *.rb file

feature 'as a renter' do
  context 'choose cars from results' do
    scenario 'i can add car to a cart', billy: true do
      proxy.cache.scope_to 'search-empty'
      visit search_page_path(type: 'car-rental')
    end
    scenario 'i can delete car from cart', billy: true  do
      proxy.cache.scope_to 'search-empty'
      visit search_page_path(type: 'car-rental')
    end
  end
end

When i use pure puffing-billy, then both tests run fine. When i use puffing-billy and webmock, then 1st test is ok but 2nd test failed because browser reported "cannot connect to proxy server". Anyway each single test runs fine.
Sorry, i cannot recall what i debug in the webmock, but finaly i decide to start/stop billy proxy server for each test as a simplest solution. I suppose webmock override libraries used by puffing-billy.
Billy configuration read in post above.

@jethroo

This comment has been minimized.

jethroo commented Jul 9, 2015

I was able to keep my existing Webmock setting and just enabled puffing billy for a test there i needed it as follows:

context 'with proxy', driver: :poltergeist_billy do
  scenario 'the user can share the link via facebook', billy: true do
    #test logic   
  end 
end

my Rspec config

RSpec.configure do |config|  
  config.around(:each, billy: true) do |example|
    WebMock.allow_net_connect!
    example.run
    WebMock.disable_net_connect!(allow_localhost: true)
  end
end

hope this might help others with same issues ;)

@korbin

This comment has been minimized.

korbin commented Apr 29, 2016

Few years late, but just stumbled across this:

If you're not using EventMachine (or WebMock for any other EM requests):

WebMock::HttpLibAdapters::EmHttpRequestAdapter.disable!

in spec_helper.rb will disable WebMock's interception of EM HTTP requests, allowing upstream proxy requests to go through.

Pretty blunt, but it works.

@nullobject

This comment has been minimized.

nullobject commented Mar 2, 2018

You can also do:

WebMock.enable!(except: :em_http_request)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment