Skip to content

Commit

Permalink
allow Applebot to masquerade as Googlebot [closes #6]
Browse files Browse the repository at this point in the history
  • Loading branch information
alaz committed May 20, 2018
1 parent d39e37d commit 2d03063
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 2 deletions.
4 changes: 4 additions & 0 deletions lib/legitbot/apple.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,9 @@ def valid?
end
end

class Apple_as_Google < Apple
end

rule Legitbot::Apple, %w(Applebot)
rule Legitbot::Apple_as_Google, %w(Googlebot)
end
7 changes: 5 additions & 2 deletions lib/legitbot/legitbot.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,15 @@ module Legitbot
# :yields: a found bot
#
def self.bot(userAgent, ip, resolver_config = nil)
bot =
bots =
@rules.select { |rule|
rule[:fragments].any? {|f| userAgent.index f}
}.map { |rule|
rule[:class].new(ip, resolver_config)
}.first
}

bot = bots.select { |bot| bot.valid? }.first if bots.size > 1
bot = bots.first if bot.nil?

if bot && block_given?
yield bot
Expand Down
22 changes: 22 additions & 0 deletions test/apple_as_google_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
require 'minitest/autorun'
require 'legitbot'

class AppleAsGoogleTest < Minitest::Test
def test_valid_ip
ip = "17.58.98.60"
match = Legitbot::Apple_as_Google.new(ip)
assert match.valid?, msg: "#{ip} is a valid Applebot IP"
end

def test_invalid_ip
ip = "127.0.0.1"
match = Legitbot::Apple_as_Google.new(ip)
assert match.fake?, msg: "#{ip} is a fake Applebot IP"
end

def test_user_agent
bot = Legitbot.bot("Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)", "17.58.98.60")
assert_equal :apple_as_google, bot.detected_as
assert bot.valid?, msg: "A valid Applebot User-agent and IP"
end
end

0 comments on commit 2d03063

Please sign in to comment.