2019-04-30 08:27:42 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-07-28 10:27:38 +08:00
|
|
|
RSpec.describe WebCrawlerRequest do
|
2018-03-16 05:10:45 +08:00
|
|
|
before do
|
2022-02-23 00:45:25 +08:00
|
|
|
CachedCounting.reset
|
|
|
|
CachedCounting.enable
|
2018-03-16 05:10:45 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
after do
|
2022-02-23 00:45:25 +08:00
|
|
|
CachedCounting.disable
|
2018-03-16 05:10:45 +08:00
|
|
|
end
|
|
|
|
|
2022-02-23 00:45:25 +08:00
|
|
|
it "can log crawler requests" do
|
|
|
|
freeze_time
|
|
|
|
d1 = Time.now.utc.to_date
|
2018-03-16 05:10:45 +08:00
|
|
|
|
2022-02-23 00:45:25 +08:00
|
|
|
4.times do
|
|
|
|
WebCrawlerRequest.increment!("Googlebot")
|
|
|
|
end
|
2018-03-16 05:10:45 +08:00
|
|
|
|
2022-02-23 00:45:25 +08:00
|
|
|
WebCrawlerRequest.increment!("Bingbot")
|
2018-03-16 05:10:45 +08:00
|
|
|
|
2022-02-23 00:45:25 +08:00
|
|
|
freeze_time 1.day.from_now
|
|
|
|
d2 = Time.now.utc.to_date
|
2018-03-16 05:10:45 +08:00
|
|
|
|
2022-02-23 00:45:25 +08:00
|
|
|
WebCrawlerRequest.increment!("Googlebot")
|
|
|
|
WebCrawlerRequest.increment!("Superbot")
|
2018-03-16 05:10:45 +08:00
|
|
|
|
2022-02-23 00:45:25 +08:00
|
|
|
CachedCounting.flush
|
2018-03-16 05:10:45 +08:00
|
|
|
|
2022-02-23 00:45:25 +08:00
|
|
|
expect(WebCrawlerRequest.find_by(date: d2, user_agent: "Googlebot").count).to eq(1)
|
|
|
|
expect(WebCrawlerRequest.find_by(date: d2, user_agent: "Superbot").count).to eq(1)
|
2018-03-16 05:10:45 +08:00
|
|
|
|
2022-02-23 00:45:25 +08:00
|
|
|
expect(WebCrawlerRequest.find_by(date: d1, user_agent: "Googlebot").count).to eq(4)
|
|
|
|
expect(WebCrawlerRequest.find_by(date: d1, user_agent: "Bingbot").count).to eq(1)
|
2018-03-16 05:10:45 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|