diff --git a/lib/pact_broker/feature_toggle.rb b/lib/pact_broker/feature_toggle.rb index 85d5736dd..6bf5765cc 100644 --- a/lib/pact_broker/feature_toggle.rb +++ b/lib/pact_broker/feature_toggle.rb @@ -1,10 +1,20 @@ module PactBroker class FeatureToggle - def self.enabled?(feature) - ENV['RACK_ENV'] != 'production' || (ENV['PACT_BROKER_FEATURES'] || "").include?(feature.to_s) + not_production? || feature_in_env_var?(feature) + end + + def self.not_production? + ENV['RACK_ENV'] != 'production' end + def self.feature_in_env_var?(feature) + (features =~ /\b#{feature}\b/i) != nil + end + + def self.features + ENV['PACT_BROKER_FEATURES'] || "" + end end def self.feature_enabled?(feature) diff --git a/spec/lib/pact_broker/feature_toggle_spec.rb b/spec/lib/pact_broker/feature_toggle_spec.rb index 8fd2d3241..7da5cc022 100644 --- a/spec/lib/pact_broker/feature_toggle_spec.rb +++ b/spec/lib/pact_broker/feature_toggle_spec.rb @@ -10,7 +10,6 @@ module PactBroker subject { FeatureToggle.enabled?(:foo) } context "when RACK_ENV is not production" do - before do allow(ENV).to receive(:[]).with('RACK_ENV').and_return('development') end @@ -23,13 +22,13 @@ module PactBroker it { is_expected.to be true } end - context "when PACT_BROKER_FEATURES does not include the given string" do - before do - allow(ENV).to receive(:[]).with('PACT_BROKER_FEATURES').and_return(nil) - end + context "when PACT_BROKER_FEATURES does not include the given string" do + before do + allow(ENV).to receive(:[]).with('PACT_BROKER_FEATURES').and_return(nil) + end - it { is_expected.to be true } - end + it { is_expected.to be true } + end end context "when RACK_ENV is production" do @@ -45,6 +44,23 @@ module PactBroker it { is_expected.to be true } end + context "when PACT_BROKER_FEATURES includes the given string inside another word" do + before do + allow(ENV).to receive(:[]).with('PACT_BROKER_FEATURES').and_return('foowiffle bar') + end + + it { is_expected.to be false } + end + + context "when PACT_BROKER_FEATURES includes the given string but the case doesn't match" do + before do + allow(ENV).to receive(:[]).with('PACT_BROKER_FEATURES').and_return('FOO bar') + end + + it { is_expected.to be true } + end + + context "when PACT_BROKER_FEATURES does not include the given string" do before do allow(ENV).to receive(:[]).with('PACT_BROKER_FEATURES').and_return(nil)