Puffing Billy not working on fresh Rails install
I can't seem to get Puffing Billy to work.
After attempting it a few times on my app, I have set up a completely fresh Rails install plus RSpec, Capybara, and Billy, and it still doesn't work. Repo here: https://github.com/jdudley1123/puffing-billy-test
# spec/rails_helper.rb
# ...
# Default config
# ...
require 'capybara/rails'
require 'capybara/rspec'
require 'billy/capybara/rspec'
Capybara.javascript_driver = :selenium_billy
I have a very simple test, copied from the docs:
# spec/features/billy_spec.rb
require 'rails_helper'
feature "Test Puffing Billy", js: true do
it 'should stub google' do
proxy.stub('http://www.google.com').and_return(text: "I'm not Google!")
visit 'http://www.google.com'
expect(page).to have_content("I'm not Google!")
end
end
Google loads as normal and is not intercepted.
1) Test Puffing Billy should stub google
Failure/Error: expect(page).to have_content("I'm not Google!")
expected to find text "I'm not Google!" in "About\nStore\nGmailImages\nSign in\nUnited Kingdom\nAdvertising\nBusiness\nHow Search works\nOur third decade of climate action: join us\nPrivacy\nTerms\nSettings\nEN\nSign in\nBefore you continue to Google\nWe use cookies and data to\nDeliver and maintain Google services\nTrack outages and protect against spam, fraud and abuse\nMeasure audience engagement and site statistics to understand how our services are used and enhance the quality of those services\nIf you choose to 'Accept all', we will also use cookies and data to\nDevelop and improve new services\nDeliver and measure the effectiveness of ads\nShow personalised content, depending on your settings\nShow personalised ads, depending on your settings\nIf you choose to 'Reject all', we will not use cookies for these additional purposes.\nNon-personalised content is influenced by things like the content that you’re currently viewing, activity in your active Search session, and your location. Non-personalised ads are influenced by the content that you’re currently viewing and your general location. Personalised content and ads can also include more relevant results, recommendations and tailored ads based on past activity from this browser, like previous Google searches. We also use cookies and data to tailor the experience to be age-appropriate, if relevant.\nSelect 'More options' to see additional information, including details about managing your privacy settings. You can also visit g.co/privacytools at any time.\nReject all\nAccept all\nMore options\nPrivacy\n·\nTerms"
# ./spec/features/billy_spec.rb:8:in `block (2 levels) in <top (required)>'
Where am I going wrong?
puffing-billy intercepts requests from the browser via a proxy config in the browser itself. It's not meant to intercept sites that you are visiting directly. (Yes, I know your example is from the README but I've never attempted to use it that way).
Check for any activity in the logs to see if it's doing anything.
Okay, that makes sense. You can see why I was confused based on the example in the readme though? 😅
Could you provide a minimal example of a test that should pass? I would assume this should pass, but instead the request successfully reaches https://api.maptiler.com.
# app/views/pages/test.html.erb
<script>
window.addEventListener('load', function() {
const block = document.getElementById('test');
block.addEventListener('click', function() {
fetch('https://api.maptiler.com/geolocation/ip.json')
.then(function(response) {
return response.text();
})
.then(function(data) {
block.innerHTML = data;
})
});
});
</script>
<div id="test">Test</div>
# spec/features/billy_spec.rb
require 'rails_helper'
feature "Test Puffing Billy", js: true do
it 'should stub google' do
proxy.stub('https://api.maptiler.com/geolocation/ip.json').and_return(text: "I'm not Google!")
visit test_path
find('#test').click
expect(page).to have_content("I'm not Google!")
end
end