Skip to content

Commit

Permalink
Change feature flag names to represent headers set at the CDN
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Karl Baker committed Jun 27, 2019
1 parent a15c693 commit 59c9c83
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 16 deletions.
18 changes: 12 additions & 6 deletions app/models/http_feature_flags.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion config/initializers/feature_flags.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module FeatureFlagNames
def self.recommended_related_links
'HTTP_GOVUK_USE_RECOMMENDED_RELATED_LINKS'
'Govuk-Use-Recommended-Related-Links'
end
end

Expand Down
6 changes: 3 additions & 3 deletions test/controllers/content_items_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand All @@ -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')

Expand All @@ -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')

Expand Down
12 changes: 6 additions & 6 deletions test/models/http_feature_flags_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,26 @@ 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

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

Expand All @@ -41,15 +41,15 @@ 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

test 'feature_enabled? should return true when headers has been set and matches specified value' do
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

Expand Down

0 comments on commit 59c9c83

Please sign in to comment.