oesmith / puffing-billy

A rewriting web proxy for testing interactions between your browser and external sites. Works with ruby + rspec.
MIT License
660 stars 168 forks source link

Puffing Billy not catching requests #213

Closed misner closed 6 years ago

misner commented 6 years ago

I have rspec tests and I feel there is a bug with puffing billy or maybe I am misunderstanding the use of whitelists in the settings.

Basically the test is about checking that, if a third party hosted image (on a service called Cloudinary) takes time to be downloaded by a page, then the user sees a loading spinner and after the image is finally loaded the spinner disappears and is not visible anymore.

I'm sure billy is the root cause as

Puffing billy settings

Capybara.configure do |config|
    # we must force the value of the capybara server port. We have to do that because puffing-billy
    # saves everything locally for a specific host and port. But, capybara starts your rack application
    # on a new port each time you run your tests. The consequence is puffing-billy
    # is not able to reuse the created cache files and tries to do all the API call again.
    # source - kevin.disneur.me/archives/2015-03-05-end-to-end-tests-in-javascript.html
    config.server_port = 60001
    # source: comments in coderwall.com/p/jsutlq, enable to load save_and_open page with css and js
    config.asset_host  = 'http://localhost:3000'
  end

require 'billy/capybara/rspec'
Billy.configure do |c|
  c.cache = true
  c.cache_request_headers = false
  c.persist_cache = true  
  c.non_successful_cache_disabled = true
  c.non_successful_error_level = :warn
  c.whitelist = ['localhost', '127.0.0.1', 'https://res.cloudinary.com'] 
  c.ignore_cache_port = true
  c.cache_path = "spec/support/http_cache/billy/"  
  # Only set non_whitelisted_requests_disabled **temporarily**
  # to false when first recording a 3rd party interaction. After
  # the recording has been stored to cache_path, then set
  # non_whitelisted_requests_disabled back to true.
  c.non_whitelisted_requests_disabled = true  
end

The rspec test:

 context "non signed in visitor", js: true, billy: true do

describe "Spinner shows while waiting then disappears" do               
  it "should work" do                    proxy.stub("https://res.cloudinary.com/demo/image/upload/sample.jpg").and_return(
   Proc.new { |params, headers, body|
     sleep 10
    {code: 200}
 }
   )

   visit actual_deal_page_path(deal)

# detect spinner
expect(page).to have_css('div#fullPageLoadingSpinner', visible: :visible)                   
# check subsequent image elements not yet visible               
expect(page).to have_no_css("img[src*='https://res.cloudinary.com/demo/image/upload/sample.jpg']")

  # then after 15 seconds, the cloudinary image finally is loaded and
 # the spinner disappears
sleep 15     
expect(page).to have_css('div#fullPageLoadingSpinner', visible: :hidden)                    
expect(page).to have_css("img[src*='https://res.cloudinary.com/demo/image/upload/sample.jpg']")

)

     end                    
  end           
end

The image in the view

<section  
    id="introImg"    
    <img      src="https://res.cloudinary.com/demo/image/upload/sample.jpg" class="cld-responsive deal-page-bckdImgCover"
  </section>

I have tried different variations on the settings, tried also to change https//res.cloudinary to res.cloudinary.com...nothing works

Finally, I think it matters, I keep seeing in my tests logs:

puffing-billy: CACHE KEY for 'https://res.cloudinary.com:443/demo/image/upload/sample.jpg' is 'get_res.cloudinary.com_2c4fefdac8978387ee341535c534e21e2588ed76'
puffing-billy: Connection to https://res.cloudinary.com:443/demo/image/upload/sample.jpg not cached and new http connections are disabled
puffing-billy: CACHE KEY for 'https://res.cloudinary.com:443/demo/image/upload/sample.jpg' is 'get_res.cloudinary.com_2c4fefdac8978387ee341535c534e21e2588ed76'
puffing-billy: Connection to https://res.cloudinary.com:443/demo/image/upload/sample.jpg not cached and new http connections are disabled
puffing-billy: CACHE KEY for 'https://res.cloudinary.com:443/demo/image/upload/sample.jpg' is 'get_res.cloudinary.com_2c4fefdac8978387ee341535c534e21e2588ed76'
puffing-billy: Connection to https://res.cloudinary.com:443/demo/image/upload/sample.jpg not cached and new http connections are disabled
puffing-billy: CACHE KEY for 'https://res.cloudinary.com:443/demo/image/upload/sample.jpg' is 'get_res.cloudinary.com_2c4fefdac8978387ee341535c534e21e2588ed76'
puffing-billy: Connection to https://res.cloudinary.com:443/demo/image/upload/sample.jpg not cached and new http connections are disabled

On these Test logs, first I don't quite understand why there are so many lines for the same resource and second, what does this message mean "not cached and new http connections are disabled". I tried other tickets with similar sounding issues such as https://github.com/oesmith/puffing-billy/issues/104 or https://github.com/oesmith/puffing-billy/issues/179 or, but my bug might be original...

misner commented 6 years ago

Not being sure it's the right place, as the line between a core bug and me failing as a dev:) is blurry here, I'll also post a Stackoverflow: https://stackoverflow.com/questions/48419015/rails-4-2-failing-to-stub-delayed-response-for-image-with-puffing-billy-imag

ronwsmith commented 6 years ago

Looking at the Connection to log messages, it's not finding your stubbed request. I'm assuming it's because it's looking for :443 after the domain whereas your stub doesn't have that.

misner commented 6 years ago

@ronwsmith Thanks for your reply. That's what I thought too but trying to add :443 but my test keep failing. However my logs changed and now I don't have the same log message anymore puffing-billy: Connection to https://res.cloudinary.com:443/demo/image/upload/sample.jpg not cached and new http connections are disabled I am investigating now but it most likely shows it's a bug outside puffing billy or loosely connected. Indeed, I still pass my tests as soon as I remove billy: true, that is to say I am able to see the image after the spinner when there is no billy: true.

misner commented 6 years ago

Hi, really appreciated the time you took to help me forward. I tracked down the bug in the "rabbit hole":) and I am opening a new issue as I now have closed down on a clear issue (different from this one), but which is pretty insane to me: https://github.com/oesmith/puffing-billy/issues/215

This ticket can be closed I think.