-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2112 from tf/additional-headers
Allow registering additional headers for published entries
- Loading branch information
Showing
6 changed files
with
167 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
module Pageflow | ||
# Register additional response headers for published entries. | ||
class AdditionalHeaders | ||
# @api private | ||
def initialize | ||
@headers = [] | ||
end | ||
|
||
# Either a hash of name values pair or a callable taking a | ||
# {PublishedEntry} record and an {ActionDispatch::Request} object | ||
# and returns a hash. | ||
def register(headers) | ||
@headers << headers | ||
end | ||
|
||
# @api private | ||
def for(entry, request) | ||
@headers.map { |headers| | ||
if headers.respond_to?(:call) | ||
headers.call(entry, request) | ||
else | ||
headers | ||
end | ||
}.reduce({}, :merge) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
require 'spec_helper' | ||
|
||
module Pageflow | ||
describe AdditionalHeaders do | ||
it 'returns empty hash by default' do | ||
additional_headers = AdditionalHeaders.new | ||
entry = create(:published_entry) | ||
|
||
result = additional_headers.for(entry, request) | ||
|
||
expect(result).to eq({}) | ||
end | ||
|
||
it 'merges multiple registered hashes of headers' do | ||
additional_headers = AdditionalHeaders.new | ||
entry = create(:published_entry) | ||
|
||
additional_headers.register('Some' => 'value') | ||
additional_headers.register('Other' => 'header') | ||
additional_headers.register(proc { {'Dynamic' => 'header'} }) | ||
result = additional_headers.for(entry, request) | ||
|
||
expect(result).to eq('Some' => 'value', | ||
'Other' => 'header', | ||
'Dynamic' => 'header') | ||
end | ||
|
||
def request(uri = 'https://example.com') | ||
ActionDispatch::Request.new(Rack::MockRequest.env_for(uri)) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters