From 59c9c8335776f943dff5b33f4f913c8a6411d451 Mon Sep 17 00:00:00 2001 From: Karl Baker Date: Thu, 27 Jun 2019 16:50:15 +0100 Subject: [PATCH] Change feature flag names to represent headers set at the CDN This commit changes the feature flag names used in `FeatureFlagNames` to represent headers as we set them at the CDN. This allows us to output response headers for the feature flags and in the `Vary` header which are consistent with others headers that we set. --- app/models/http_feature_flags.rb | 18 ++++++++++++------ config/initializers/feature_flags.rb | 2 +- .../content_items_controller_test.rb | 6 +++--- test/models/http_feature_flags_test.rb | 12 ++++++------ 4 files changed, 22 insertions(+), 16 deletions(-) diff --git a/app/models/http_feature_flags.rb b/app/models/http_feature_flags.rb index b58f0428d..d6799e5ae 100644 --- a/app/models/http_feature_flags.rb +++ b/app/models/http_feature_flags.rb @@ -3,19 +3,25 @@ def initialize @feature_flags = {} end - def add_http_feature_flag(header_name, val) - @feature_flags[header_name] = val + def add_http_feature_flag(feature_flag_name, val) + @feature_flags[get_header_name(feature_flag_name)] = val end - def get_feature_flag(header_name) - @feature_flags[header_name] + def get_feature_flag(feature_flag_name) + @feature_flags[get_header_name(feature_flag_name)] end - def feature_enabled?(header_name, request_headers) - @feature_flags.has_key?(header_name) && @feature_flags[header_name] == request_headers[header_name] + def feature_enabled?(feature_flag_name, request_headers) + get_feature_flag(feature_flag_name) == request_headers[get_header_name(feature_flag_name)] end def self.instance @instance ||= HttpFeatureFlags.new end + +private + + def get_header_name(feature_flag_name) + "HTTP_#{feature_flag_name.upcase.tr('-', '_')}" + end end diff --git a/config/initializers/feature_flags.rb b/config/initializers/feature_flags.rb index 64278180c..47ce11bec 100644 --- a/config/initializers/feature_flags.rb +++ b/config/initializers/feature_flags.rb @@ -1,6 +1,6 @@ module FeatureFlagNames def self.recommended_related_links - 'HTTP_GOVUK_USE_RECOMMENDED_RELATED_LINKS' + 'Govuk-Use-Recommended-Related-Links' end end diff --git a/test/controllers/content_items_controller_test.rb b/test/controllers/content_items_controller_test.rb index f39aea4bb..3274b6c67 100644 --- a/test/controllers/content_items_controller_test.rb +++ b/test/controllers/content_items_controller_test.rb @@ -141,7 +141,7 @@ class ContentItemsControllerTest < ActionController::TestCase test "gets item from content store and keep existing ordered_related_items when feature flag header is specified but links already exist" do HttpFeatureFlags.instance.add_http_feature_flag(FeatureFlagNames.recommended_related_links, 'true') - request.headers[FeatureFlagNames.recommended_related_links] = 'true' + request.headers["HTTP_GOVUK_USE_RECOMMENDED_RELATED_LINKS"] = 'true' content_item = content_store_has_schema_example('guide', 'guide') @@ -154,7 +154,7 @@ class ContentItemsControllerTest < ActionController::TestCase test "gets item from content store and keeps ordered_related_items when feature flag header is specified but recommended links turned off" do HttpFeatureFlags.instance.add_http_feature_flag(FeatureFlagNames.recommended_related_links, 'false') - request.headers[FeatureFlagNames.recommended_related_links] = 'true' + request.headers["HTTP_GOVUK_USE_RECOMMENDED_RELATED_LINKS"] = 'true' content_item = content_store_has_schema_example('case_study', 'case_study') @@ -167,7 +167,7 @@ class ContentItemsControllerTest < ActionController::TestCase test "gets item from content store and replaces ordered_related_items when feature flag header is specified and there are no existing links" do HttpFeatureFlags.instance.add_http_feature_flag(FeatureFlagNames.recommended_related_links, 'true') - request.headers[FeatureFlagNames.recommended_related_links] = 'true' + request.headers["HTTP_GOVUK_USE_RECOMMENDED_RELATED_LINKS"] = 'true' content_item = content_store_has_schema_example('case_study', 'case_study') diff --git a/test/models/http_feature_flags_test.rb b/test/models/http_feature_flags_test.rb index 17bd08ac7..df2b90abc 100644 --- a/test/models/http_feature_flags_test.rb +++ b/test/models/http_feature_flags_test.rb @@ -6,7 +6,7 @@ class HttpFeatureFlagsTest < ActiveSupport::TestCase instance.add_http_feature_flag('TEST_HEADER', 'show') new_instance = HttpFeatureFlags.instance - feature_enabled = new_instance.feature_enabled?('TEST_HEADER', 'TEST_HEADER' => 'show') + feature_enabled = new_instance.feature_enabled?('TEST_HEADER', 'HTTP_TEST_HEADER' => 'show') assert_equal(true, feature_enabled) end @@ -14,18 +14,18 @@ class HttpFeatureFlagsTest < ActiveSupport::TestCase test 'add_http_feature_flag should set a new feature flag' do instance = HttpFeatureFlags.new - feature_enabled = instance.feature_enabled?('USE_MAGIC', 'USE_MAGIC' => 'only_at_weekends') + feature_enabled = instance.feature_enabled?('USE_MAGIC', 'HTTP_USE_MAGIC' => 'only_at_weekends') assert_equal(false, feature_enabled) instance.add_http_feature_flag('USE_MAGIC', 'only_at_weekends') - feature_enabled = instance.feature_enabled?('USE_MAGIC', 'USE_MAGIC' => 'only_at_weekends') + feature_enabled = instance.feature_enabled?('USE_MAGIC', 'HTTP_USE_MAGIC' => 'only_at_weekends') assert_equal(true, feature_enabled) end test 'feature_enabled? should return false when feature flag has not been set' do instance = HttpFeatureFlags.new - feature_enabled = instance.feature_enabled?('USE_MAGIC', 'USE_MAGIC' => 'only_at_weekends') + feature_enabled = instance.feature_enabled?('USE_MAGIC', 'HTTP_USE_MAGIC' => 'only_at_weekends') assert_equal(false, feature_enabled) end @@ -41,7 +41,7 @@ class HttpFeatureFlagsTest < ActiveSupport::TestCase instance = HttpFeatureFlags.new instance.add_http_feature_flag('USE_MAGIC', 'only_at_weekends') - feature_enabled = instance.feature_enabled?('USE_MAGIC', 'USE_MAGIC' => 'all_the_time') + feature_enabled = instance.feature_enabled?('USE_MAGIC', 'HTTP_USE_MAGIC' => 'all_the_time') assert_equal(false, feature_enabled) end @@ -49,7 +49,7 @@ class HttpFeatureFlagsTest < ActiveSupport::TestCase instance = HttpFeatureFlags.new instance.add_http_feature_flag('USE_MAGIC', 'only_at_weekends') - feature_enabled = instance.feature_enabled?('USE_MAGIC', 'USE_MAGIC' => 'only_at_weekends') + feature_enabled = instance.feature_enabled?('USE_MAGIC', 'HTTP_USE_MAGIC' => 'only_at_weekends') assert_equal(true, feature_enabled) end