-
Notifications
You must be signed in to change notification settings - Fork 9
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 #109 from alphagov/add-cache-control-header-to-s3-…
…object Add Cache-Control header to S3 object
- Loading branch information
Showing
14 changed files
with
101 additions
and
15 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
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,6 @@ | ||
require 'cache_control_configuration' | ||
|
||
AssetManager.cache_control = CacheControlConfiguration.new( | ||
max_age: 24.hours, | ||
public: true | ||
) |
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,22 @@ | ||
class CacheControlConfiguration | ||
def initialize(attributes = {}) | ||
@attributes = attributes | ||
end | ||
|
||
def max_age | ||
@attributes[:max_age] | ||
end | ||
|
||
def options | ||
@attributes.except(:max_age) | ||
end | ||
|
||
def header | ||
response = ActionDispatch::Response.new | ||
@attributes.each do |key, value| | ||
response.cache_control[key] = value | ||
end | ||
response.prepare! | ||
response['Cache-Control'] | ||
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
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,39 @@ | ||
require 'rails_helper' | ||
|
||
RSpec.describe CacheControlConfiguration do | ||
subject { described_class.new(attributes) } | ||
|
||
describe '#max_age' do | ||
let(:attributes) { { max_age: 1.hour } } | ||
|
||
it 'returns max_age attribute value' do | ||
expect(subject.max_age).to eq(1.hour) | ||
end | ||
end | ||
|
||
describe '#options' do | ||
let(:attributes) { { max_age: 1.hour, public: true } } | ||
|
||
it 'returns all attributes and their values except for max_age' do | ||
expect(subject.options).to eq(public: true) | ||
end | ||
end | ||
|
||
describe '#header' do | ||
context 'when attributes are supplied to constructor' do | ||
let(:attributes) { { max_age: 1.hour, public: true, must_revalidate: true } } | ||
|
||
it 'returns Cache-Control header value c.f. expires_in' do | ||
expect(subject.header).to eq('max-age=3600, public, must-revalidate') | ||
end | ||
end | ||
|
||
context 'when only max_age attribute is supplied to constructor' do | ||
let(:attributes) { { max_age: 1.hour } } | ||
|
||
it "returns Cache-Control header value with default visibility" do | ||
expect(subject.header).to include('private') | ||
end | ||
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
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