Skip to content

Commit

Permalink
Merge pull request #1400 from alphagov/related-links-header-response
Browse files Browse the repository at this point in the history
Use request header for related links to determine response header
  • Loading branch information
Karl Baker authored Jul 1, 2019
2 parents 7e3be42 + fdc2adc commit 8d0a6ea
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 14 deletions.
5 changes: 4 additions & 1 deletion app/controllers/content_items_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,10 @@ def set_access_control_allow_origin_header

def set_use_recommended_related_links_header
response.headers['Vary'] = [response.headers['Vary'], FeatureFlagNames.recommended_related_links].compact.join(', ')
response.headers[FeatureFlagNames.recommended_related_links] = Services.feature_toggler.feature_flags.get_feature_flag(FeatureFlagNames.recommended_related_links)

related_links_request_header = RequestHelper.get_header(FeatureFlagNames.recommended_related_links, request.headers)
required_header_value = Services.feature_toggler.feature_flags.get_feature_flag(FeatureFlagNames.recommended_related_links)
response.headers[FeatureFlagNames.recommended_related_links] = (related_links_request_header == required_header_value).to_s
end

def set_expiry
Expand Down
9 changes: 9 additions & 0 deletions app/lib/request_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module RequestHelper
def self.get_header(header_name, request_headers)
request_headers[headerise(header_name)]
end

def self.headerise(header_name)
"HTTP_#{header_name.upcase.tr('-', '_')}"
end
end
12 changes: 3 additions & 9 deletions app/models/http_feature_flags.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,18 @@ def initialize
end

def add_http_feature_flag(feature_flag_name, val)
@feature_flags[get_header_name(feature_flag_name)] = val
@feature_flags[RequestHelper.headerise(feature_flag_name)] = val
end

def get_feature_flag(feature_flag_name)
@feature_flags[get_header_name(feature_flag_name)]
@feature_flags[RequestHelper.headerise(feature_flag_name)]
end

def feature_enabled?(feature_flag_name, request_headers)
get_feature_flag(feature_flag_name) == request_headers[get_header_name(feature_flag_name)]
get_feature_flag(feature_flag_name) == RequestHelper.get_header(feature_flag_name, request_headers)
end

def self.instance
@instance ||= HttpFeatureFlags.new
end

private

def get_header_name(feature_flag_name)
"HTTP_#{feature_flag_name.upcase.tr('-', '_')}"
end
end
20 changes: 16 additions & 4 deletions test/controllers/content_items_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -178,18 +178,19 @@ class ContentItemsControllerTest < ActionController::TestCase
assert_equal assigns[:content_item].content_item['links']['ordered_related_items'], content_item['links']['suggested_ordered_related_items']
end

test "sets the Govuk-Use-Recommended-Links header to true when using recommended links" do
test "sets the Govuk-Use-Recommended-Links response header to false when request header is not set" do
HttpFeatureFlags.instance.add_http_feature_flag(FeatureFlagNames.recommended_related_links, 'true')
content_item = content_store_has_schema_example('case_study', 'case_study')

get :show, params: { path: path_for(content_item) }

assert_includes response.headers['Vary'], FeatureFlagNames.recommended_related_links
assert_equal 'true', response.headers[FeatureFlagNames.recommended_related_links]
assert_equal 'false', response.headers[FeatureFlagNames.recommended_related_links]
end

test "sets the Govuk-Use-Recommended-Links header to false when not using recommended links" do
HttpFeatureFlags.instance.add_http_feature_flag(FeatureFlagNames.recommended_related_links, 'false')
test "sets the Govuk-Use-Recommended-Links response header to false when request header is set to false" do
HttpFeatureFlags.instance.add_http_feature_flag(FeatureFlagNames.recommended_related_links, 'true')
request.headers["HTTP_GOVUK_USE_RECOMMENDED_RELATED_LINKS"] = 'false'
content_item = content_store_has_schema_example('case_study', 'case_study')

get :show, params: { path: path_for(content_item) }
Expand All @@ -198,6 +199,17 @@ class ContentItemsControllerTest < ActionController::TestCase
assert_equal 'false', response.headers[FeatureFlagNames.recommended_related_links]
end

test "sets the Govuk-Use-Recommended-Links response header to true when request header is set to true" do
HttpFeatureFlags.instance.add_http_feature_flag(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')

get :show, params: { path: path_for(content_item) }

assert_includes response.headers['Vary'], FeatureFlagNames.recommended_related_links
assert_equal 'true', response.headers[FeatureFlagNames.recommended_related_links]
end

test "sets the expiry as sent by content-store" do
content_item = content_store_has_schema_example('coming_soon', 'coming_soon')
content_store_has_item(content_item['base_path'], content_item, max_age: 20)
Expand Down
23 changes: 23 additions & 0 deletions test/services/request_helper_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
require 'test_helper'

class RequestHelperTest < ActiveSupport::TestCase
test 'get_header returns nil when header does not exist' do
header_val = RequestHelper.get_header('Govuk-Example-Header', {})

assert_equal nil, header_val
end

test 'get_header returns header when header exists' do
header_val = RequestHelper.get_header('Govuk-Example-Header', 'HTTP_GOVUK_EXAMPLE_HEADER' => 'this_is_a_test')

assert_equal 'this_is_a_test', header_val
end

test 'headerise returns header with HTTP prefix, no dashes and all uppercase' do
header_name = 'Govuk-Example-Header'

transformed_header_name = RequestHelper.headerise(header_name)

assert_equal 'HTTP_GOVUK_EXAMPLE_HEADER', transformed_header_name
end
end

0 comments on commit 8d0a6ea

Please sign in to comment.