diff --git a/lib/pact_broker/api/resources/badge_methods.rb b/lib/pact_broker/api/resources/badge_methods.rb index badd81654..11e1d02ab 100644 --- a/lib/pact_broker/api/resources/badge_methods.rb +++ b/lib/pact_broker/api/resources/badge_methods.rb @@ -32,7 +32,7 @@ def policy_name end def moved_temporarily? - response.headers["Cache-Control"] = "no-cache" + set_cache_control("no-cache") begin badge_url rescue StandardError => e @@ -46,6 +46,10 @@ def badge_url raise NotImplementedError end + def set_cache_control(cache_control_str) + response.headers["Cache-Control"] = cache_control_str + end + private def label diff --git a/lib/pact_broker/api/resources/can_i_merge_badge.rb b/lib/pact_broker/api/resources/can_i_merge_badge.rb index 964b74957..6214f0823 100644 --- a/lib/pact_broker/api/resources/can_i_merge_badge.rb +++ b/lib/pact_broker/api/resources/can_i_merge_badge.rb @@ -15,6 +15,7 @@ def badge_url # when there is no main branch version, we return an error badge url badge_service.error_badge_url("main branch version", "not found") else + set_cache_control("max-age=30") # override the cache control header to a default 30 seconds # we call badge_service to build the badge url badge_service.can_i_merge_badge_url(deployable: results) end diff --git a/spec/lib/pact_broker/api/resources/can_i_merge_badge_spec.rb b/spec/lib/pact_broker/api/resources/can_i_merge_badge_spec.rb index b0ebb8e4d..7522909bf 100644 --- a/spec/lib/pact_broker/api/resources/can_i_merge_badge_spec.rb +++ b/spec/lib/pact_broker/api/resources/can_i_merge_badge_spec.rb @@ -35,6 +35,7 @@ module Resources it "return the badge URL" do expect(badge_service). to receive(:can_i_merge_badge_url).with(deployable: true) expect(subject.headers["Location"]).to eq "http://badge_url" + expect(subject.headers["Cache-Control"]).to eq "max-age=30" end end @@ -44,6 +45,7 @@ module Resources it "returns an error badge URL" do expect(badge_service).to receive(:error_badge_url).with("pacticipant", "not found") expect(subject.headers["Location"]).to eq "http://error_badge_url" + expect(subject.headers["Cache-Control"]).to eq "no-cache" end end