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
|
|
|
|
|
2017-07-28 09:20:09 +08:00
|
|
|
def env(opts = {})
|
2015-02-04 13:14:56 +08:00
|
|
|
{
|
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
|
|
|
|
|
2017-07-28 09:20:09 +08:00
|
|
|
def new_helper(opts = {})
|
2015-02-04 13:14:56 +08:00
|
|
|
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
|
2017-07-28 09:20:09 +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
|
|
|
|
|
2017-06-15 21:36:27 +08:00
|
|
|
context "per theme cache" do
|
|
|
|
it "handles theme keys" do
|
|
|
|
theme = Theme.create(name: "test", user_id: -1, user_selectable: true)
|
|
|
|
|
|
|
|
with_bad_theme_key = new_helper("HTTP_COOKIE" => "theme_key=abc").cache_key
|
|
|
|
with_no_theme_key = new_helper().cache_key
|
|
|
|
|
|
|
|
expect(with_bad_theme_key).to eq(with_no_theme_key)
|
|
|
|
|
|
|
|
with_good_theme_key = new_helper("HTTP_COOKIE" => "theme_key=#{theme.key}").cache_key
|
|
|
|
|
|
|
|
expect(with_good_theme_key).not_to eq(with_no_theme_key)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-10-16 13:39:18 +08:00
|
|
|
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
|
2017-07-28 09:20:09 +08:00
|
|
|
helper.cache([200, { "HELLO" => "WORLD" }, ["hello ", "my world"]])
|
2016-12-05 10:57:09 +08:00
|
|
|
|
|
|
|
helper = new_helper("ANON_CACHE_DURATION" => 10)
|
2017-07-28 09:20:09 +08:00
|
|
|
expect(helper.cached).to eq([200, { "X-Discourse-Cached" => "true", "HELLO" => "WORLD" }, ["hello my world"]])
|
2016-12-05 10:57:09 +08:00
|
|
|
|
|
|
|
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)
|
2017-07-28 09:20:09 +08:00
|
|
|
helper.cache([200, { "HELLO" => "WORLD" }, ["hello ", "my world"]])
|
2014-04-29 08:48:09 +08:00
|
|
|
|
|
|
|
helper = new_helper("ANON_CACHE_DURATION" => 10)
|
|
|
|
helper.is_mobile = true
|
2017-07-28 09:20:09 +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)
|
2017-07-28 09:20:09 +08:00
|
|
|
crawler.cache([200, { "HELLO" => "WORLD" }, ["hello ", "world"]])
|
|
|
|
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
|