mirror of
https://github.com/discourse/discourse.git
synced 2025-05-24 14:12:10 +08:00
DEV: Refactor anonymouse cache spec.
Mainly to properly categorize `Middleware::AnonymousCache` vs `Middleware::AnonymousCache::Helper` specs.
This commit is contained in:
@ -2,17 +2,14 @@
|
|||||||
|
|
||||||
require "rails_helper"
|
require "rails_helper"
|
||||||
|
|
||||||
describe Middleware::AnonymousCache::Helper do
|
describe Middleware::AnonymousCache do
|
||||||
|
let(:middleware) { Middleware::AnonymousCache.new(lambda { |_| [200, {}, []] }) }
|
||||||
|
|
||||||
def env(opts = {})
|
def env(opts = {})
|
||||||
{
|
Rack::MockRequest.env_for("http://test.com/path?bla=1").merge(opts)
|
||||||
"HTTP_HOST" => "http://test.com",
|
|
||||||
"REQUEST_URI" => "/path?bla=1",
|
|
||||||
"REQUEST_METHOD" => "GET",
|
|
||||||
"rack.input" => ""
|
|
||||||
}.merge(opts)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe Middleware::AnonymousCache::Helper do
|
||||||
def new_helper(opts = {})
|
def new_helper(opts = {})
|
||||||
Middleware::AnonymousCache::Helper.new(env(opts))
|
Middleware::AnonymousCache::Helper.new(env(opts))
|
||||||
end
|
end
|
||||||
@ -72,6 +69,64 @@ describe Middleware::AnonymousCache::Helper do
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
context "cached" do
|
||||||
|
let!(:helper) do
|
||||||
|
new_helper("ANON_CACHE_DURATION" => 10)
|
||||||
|
end
|
||||||
|
|
||||||
|
let!(:crawler) do
|
||||||
|
new_helper("ANON_CACHE_DURATION" => 10, "HTTP_USER_AGENT" => "AdsBot-Google (+http://www.google.com/adsbot.html)")
|
||||||
|
end
|
||||||
|
|
||||||
|
after do
|
||||||
|
helper.clear_cache
|
||||||
|
crawler.clear_cache
|
||||||
|
end
|
||||||
|
|
||||||
|
before do
|
||||||
|
global_setting :anon_cache_store_threshold, 1
|
||||||
|
end
|
||||||
|
|
||||||
|
it "compresses body on demand" do
|
||||||
|
global_setting :compress_anon_cache, true
|
||||||
|
|
||||||
|
payload = "x" * 1000
|
||||||
|
helper.cache([200, { "HELLO" => "WORLD" }, [payload]])
|
||||||
|
|
||||||
|
helper = new_helper("ANON_CACHE_DURATION" => 10)
|
||||||
|
expect(helper.cached).to eq([200, { "X-Discourse-Cached" => "true", "HELLO" => "WORLD" }, [payload]])
|
||||||
|
|
||||||
|
# depends on i7z implementation, but lets assume it is stable unless we discover
|
||||||
|
# otherwise
|
||||||
|
expect(Discourse.redis.get(helper.cache_key_body).length).to eq(16)
|
||||||
|
end
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
|
it "returns cached data for cached requests" do
|
||||||
|
helper.is_mobile = true
|
||||||
|
expect(helper.cached).to eq(nil)
|
||||||
|
helper.cache([200, { "HELLO" => "WORLD" }, ["hello ", "my world"]])
|
||||||
|
|
||||||
|
helper = new_helper("ANON_CACHE_DURATION" => 10)
|
||||||
|
helper.is_mobile = true
|
||||||
|
expect(helper.cached).to eq([200, { "X-Discourse-Cached" => "true", "HELLO" => "WORLD" }, ["hello my world"]])
|
||||||
|
|
||||||
|
expect(crawler.cached).to eq(nil)
|
||||||
|
crawler.cache([200, { "HELLO" => "WORLD" }, ["hello ", "world"]])
|
||||||
|
expect(crawler.cached).to eq([200, { "X-Discourse-Cached" => "true", "HELLO" => "WORLD" }, ["hello world"]])
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
context 'force_anonymous!' do
|
context 'force_anonymous!' do
|
||||||
before do
|
before do
|
||||||
RateLimiter.enable
|
RateLimiter.enable
|
||||||
@ -140,63 +195,6 @@ describe Middleware::AnonymousCache::Helper do
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
context "cached" do
|
|
||||||
let!(:helper) do
|
|
||||||
new_helper("ANON_CACHE_DURATION" => 10)
|
|
||||||
end
|
|
||||||
|
|
||||||
let!(:crawler) do
|
|
||||||
new_helper("ANON_CACHE_DURATION" => 10, "HTTP_USER_AGENT" => "AdsBot-Google (+http://www.google.com/adsbot.html)")
|
|
||||||
end
|
|
||||||
|
|
||||||
after do
|
|
||||||
helper.clear_cache
|
|
||||||
crawler.clear_cache
|
|
||||||
end
|
|
||||||
|
|
||||||
before do
|
|
||||||
global_setting :anon_cache_store_threshold, 1
|
|
||||||
end
|
|
||||||
|
|
||||||
it "compresses body on demand" do
|
|
||||||
global_setting :compress_anon_cache, true
|
|
||||||
|
|
||||||
payload = "x" * 1000
|
|
||||||
helper.cache([200, { "HELLO" => "WORLD" }, [payload]])
|
|
||||||
|
|
||||||
helper = new_helper("ANON_CACHE_DURATION" => 10)
|
|
||||||
expect(helper.cached).to eq([200, { "X-Discourse-Cached" => "true", "HELLO" => "WORLD" }, [payload]])
|
|
||||||
|
|
||||||
# depends on i7z implementation, but lets assume it is stable unless we discover
|
|
||||||
# otherwise
|
|
||||||
expect(Discourse.redis.get(helper.cache_key_body).length).to eq(16)
|
|
||||||
end
|
|
||||||
|
|
||||||
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
|
|
||||||
|
|
||||||
it "returns cached data for cached requests" do
|
|
||||||
helper.is_mobile = true
|
|
||||||
expect(helper.cached).to eq(nil)
|
|
||||||
helper.cache([200, { "HELLO" => "WORLD" }, ["hello ", "my world"]])
|
|
||||||
|
|
||||||
helper = new_helper("ANON_CACHE_DURATION" => 10)
|
|
||||||
helper.is_mobile = true
|
|
||||||
expect(helper.cached).to eq([200, { "X-Discourse-Cached" => "true", "HELLO" => "WORLD" }, ["hello my world"]])
|
|
||||||
|
|
||||||
expect(crawler.cached).to eq(nil)
|
|
||||||
crawler.cache([200, { "HELLO" => "WORLD" }, ["hello ", "world"]])
|
|
||||||
expect(crawler.cached).to eq([200, { "X-Discourse-Cached" => "true", "HELLO" => "WORLD" }, ["hello world"]])
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
context "crawler blocking" do
|
context "crawler blocking" do
|
||||||
let :non_crawler do
|
let :non_crawler do
|
||||||
{
|
{
|
||||||
@ -206,7 +204,6 @@ describe Middleware::AnonymousCache::Helper do
|
|||||||
end
|
end
|
||||||
|
|
||||||
def get(path, options)
|
def get(path, options)
|
||||||
middleware = Middleware::AnonymousCache.new(lambda { |_| [200, {}, []] })
|
|
||||||
@env = env({
|
@env = env({
|
||||||
"REQUEST_URI" => path,
|
"REQUEST_URI" => path,
|
||||||
"PATH_INFO" => path,
|
"PATH_INFO" => path,
|
||||||
|
Reference in New Issue
Block a user