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
|
|
|
|
|
2023-05-06 05:15:33 +08:00
|
|
|
after do
|
|
|
|
CachedCounting.reset
|
|
|
|
CachedCounting.disable
|
|
|
|
end
|
2018-03-16 05:10:45 +08:00
|
|
|
|
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 { WebCrawlerRequest.increment!("Googlebot") }
|
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
|