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