diff --git a/Gemfile b/Gemfile index 89f216c..bfb196c 100644 --- a/Gemfile +++ b/Gemfile @@ -10,4 +10,5 @@ group :development do gem 'minitest-reporters' gem 'rake' gem 'rubocop' + gem 'webmock' end diff --git a/test/ipinfo_test.rb b/test/ipinfo_test.rb index 5663d7c..3478c28 100644 --- a/test/ipinfo_test.rb +++ b/test/ipinfo_test.rb @@ -177,20 +177,41 @@ def test_lookup_ip4 end def test_resproxy - ipinfo = IPinfo.create(ENV['IPINFO_TOKEN']) + # Mock the resproxy API response (with any query params) + stub_request(:get, /https:\/\/ipinfo\.io\/resproxy\/#{TEST_RESPROXY_IP}/) + .to_return( + status: 200, + body: { + ip: TEST_RESPROXY_IP, + last_seen: '2025-01-20', + percent_days_seen: 0.85, + service: 'example_service' + }.to_json, + headers: { 'Content-Type' => 'application/json' } + ) + + ipinfo = IPinfo.create('test_token') # multiple checks for cache (0...5).each do |_| resp = ipinfo.resproxy(TEST_RESPROXY_IP) assert_equal(resp.ip, TEST_RESPROXY_IP) - refute_nil(resp.last_seen) - refute_nil(resp.percent_days_seen) - refute_nil(resp.service) + assert_equal(resp.last_seen, '2025-01-20') + assert_equal(resp.percent_days_seen, 0.85) + assert_equal(resp.service, 'example_service') end end def test_resproxy_empty - ipinfo = IPinfo.create(ENV['IPINFO_TOKEN']) + # Mock the resproxy API response for non-residential proxy IP (with any query params) + stub_request(:get, /https:\/\/ipinfo\.io\/resproxy\/#{TEST_IPV4}/) + .to_return( + status: 200, + body: '{}', + headers: { 'Content-Type' => 'application/json' } + ) + + ipinfo = IPinfo.create('test_token') resp = ipinfo.resproxy(TEST_IPV4) assert_equal(resp.all, {}) diff --git a/test/test_helper.rb b/test/test_helper.rb index 20c5fb4..0b7aaf3 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -8,6 +8,10 @@ require 'minitest/autorun' require 'minitest/reporters' +require 'webmock/minitest' + +# Allow real network connections by default, but tests can disable this +WebMock.allow_net_connect! Minitest::Reporters.use!( Minitest::Reporters::SpecReporter.new