2015-10-11 17:41:23 +08:00
|
|
|
require "rails_helper"
|
2013-10-16 13:39:18 +08:00
|
|
|
require_dependency "middleware/anonymous_cache"
|
|
|
|
|
|
|
|
describe Middleware::AnonymousCache::Helper do
|
|
|
|
|
2015-02-04 13:14:56 +08:00
|
|
|
def env(opts={})
|
|
|
|
{
|
2013-10-16 13:39:18 +08:00
|
|
|
"HTTP_HOST" => "http://test.com",
|
|
|
|
"REQUEST_URI" => "/path?bla=1",
|
2015-02-04 13:14:56 +08:00
|
|
|
"REQUEST_METHOD" => "GET",
|
|
|
|
"rack.input" => ""
|
|
|
|
}.merge(opts)
|
|
|
|
end
|
|
|
|
|
|
|
|
def new_helper(opts={})
|
|
|
|
Middleware::AnonymousCache::Helper.new(env(opts))
|
|
|
|
end
|
|
|
|
|
2013-10-16 13:39:18 +08:00
|
|
|
|
|
|
|
context "cachable?" do
|
|
|
|
it "true by default" do
|
2015-01-10 00:34:37 +08:00
|
|
|
expect(new_helper.cacheable?).to eq(true)
|
2013-10-16 13:39:18 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "is false for non GET" do
|
2015-01-10 00:34:37 +08:00
|
|
|
expect(new_helper("ANON_CACHE_DURATION" => 10, "REQUEST_METHOD" => "POST").cacheable?).to eq(false)
|
2013-10-16 13:39:18 +08:00
|
|
|
end
|
2013-10-17 07:37:06 +08:00
|
|
|
|
|
|
|
it "is false if it has an auth cookie" do
|
2015-01-10 00:34:37 +08:00
|
|
|
expect(new_helper("HTTP_COOKIE" => "jack=1; _t=#{"1"*32}; jill=2").cacheable?).to eq(false)
|
2013-10-17 07:37:06 +08:00
|
|
|
end
|
2013-10-16 13:39:18 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
context "cached" do
|
|
|
|
let!(:helper) do
|
|
|
|
new_helper("ANON_CACHE_DURATION" => 10)
|
|
|
|
end
|
|
|
|
|
2014-04-29 08:48:09 +08:00
|
|
|
let!(:crawler) do
|
|
|
|
new_helper("ANON_CACHE_DURATION" => 10, "HTTP_USER_AGENT" => "AdsBot-Google (+http://www.google.com/adsbot.html)")
|
|
|
|
end
|
|
|
|
|
2013-10-16 13:39:18 +08:00
|
|
|
after do
|
|
|
|
helper.clear_cache
|
2014-04-29 08:48:09 +08:00
|
|
|
crawler.clear_cache
|
2013-10-16 13:39:18 +08:00
|
|
|
end
|
|
|
|
|
2016-12-05 10:57:09 +08:00
|
|
|
it "handles brotli switching" do
|
|
|
|
helper.cache([200, {"HELLO" => "WORLD"}, ["hello ", "my world"]])
|
|
|
|
|
|
|
|
helper = new_helper("ANON_CACHE_DURATION" => 10)
|
|
|
|
expect(helper.cached).to eq([200, {"X-Discourse-Cached" => "true", "HELLO" => "WORLD"}, ["hello my world"]])
|
|
|
|
|
|
|
|
helper = new_helper("ANON_CACHE_DURATION" => 10, "HTTP_ACCEPT_ENCODING" => "gz, br")
|
|
|
|
expect(helper.cached).to eq(nil)
|
|
|
|
end
|
|
|
|
|
2013-10-16 13:39:18 +08:00
|
|
|
it "returns cached data for cached requests" do
|
2014-01-09 12:11:04 +08:00
|
|
|
helper.is_mobile = true
|
2015-01-10 00:34:37 +08:00
|
|
|
expect(helper.cached).to eq(nil)
|
2014-04-29 08:48:09 +08:00
|
|
|
helper.cache([200, {"HELLO" => "WORLD"}, ["hello ", "my world"]])
|
|
|
|
|
|
|
|
helper = new_helper("ANON_CACHE_DURATION" => 10)
|
|
|
|
helper.is_mobile = true
|
2015-06-16 08:53:28 +08:00
|
|
|
expect(helper.cached).to eq([200, {"X-Discourse-Cached" => "true", "HELLO" => "WORLD"}, ["hello my world"]])
|
2014-04-29 08:48:09 +08:00
|
|
|
|
2015-01-10 00:34:37 +08:00
|
|
|
expect(crawler.cached).to eq(nil)
|
2014-04-29 08:48:09 +08:00
|
|
|
crawler.cache([200, {"HELLO" => "WORLD"}, ["hello ", "world"]])
|
2015-06-16 08:53:28 +08:00
|
|
|
expect(crawler.cached).to eq([200, {"X-Discourse-Cached" => "true", "HELLO" => "WORLD"}, ["hello world"]])
|
2013-10-16 13:39:18 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|