Skip to content

Commit

Permalink
Merge pull request #1249 from itbm/master
Browse files Browse the repository at this point in the history
Allow setting unquoted or custom flags on add_headers
  • Loading branch information
bastelfreak authored Oct 20, 2018
2 parents bd18a19 + 3c9b424 commit 54a5056
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 21 deletions.
13 changes: 8 additions & 5 deletions spec/defines/resource_location_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -404,17 +404,20 @@
let(:params) do
default_params.merge(
'add_header' => {
'X-Frame-Options' => 'SAMEORIGIN',
'X-Powered-By' => 'Puppet'
'header 1' => 'test value 1',
'header 2' => { 'test value 2' => 'tv2' },
'header 3' => { '' => '\'test value 3\' tv3' }
}
)
end

it 'contains both add_header lines' do
it 'contains 3 add_header lines' do
is_expected.to contain_concat__fragment('server1-500-' + Digest::MD5.hexdigest('location')).
with_content(%r{^\s+add_header\s+"X-Frame-Options"\s+"SAMEORIGIN";$})
with_content(%r{^\s+add_header\s+"header 1"\s+"test value 1";$})
is_expected.to contain_concat__fragment('server1-500-' + Digest::MD5.hexdigest('location')).
with_content(%r{^\s+add_header\s+"X-Powered-By"\s+"Puppet";$})
with_content(%r{^\s+add_header\s+"header 2"\s+"test value 2" tv2;$})
is_expected.to contain_concat__fragment('server1-500-' + Digest::MD5.hexdigest('location')).
with_content(%r{^\s+add_header\s+"header 3"\s+'test value 3' tv3;$})
end
end
end
Expand Down
8 changes: 4 additions & 4 deletions spec/defines/resource_server_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1333,24 +1333,24 @@

context 'when add_header is set' do
let :params do
default_params.merge(add_header: { 'header3' => 'test value 3', 'header2' => 'test value 2', 'header1' => 'test value 1' })
default_params.merge(add_header: { 'header3' => { '' => '\'test value 3\' tv3' }, 'header2' => { 'test value 2' => 'tv2' }, 'header1' => 'test value 1' })
end

it 'has correctly ordered entries in the config' do
is_expected.to contain_concat__fragment("#{title}-header").with_content(%r{\s+add_header\s+"header1" "test value 1";\n\s+add_header\s+"header2" "test value 2";\n\s+add_header\s+"header3" "test value 3";\n})
is_expected.to contain_concat__fragment("#{title}-header").with_content(%r{\s+add_header\s+"header1" "test value 1";\n\s+add_header\s+"header2" "test value 2" tv2;\n\s+add_header\s+"header3" 'test value 3' tv3;\n})
end
end

context 'when add_header is set and ssl => true' do
let :params do
default_params.merge(add_header: { 'header3' => 'test value 3', 'header2' => 'test value 2', 'header1' => 'test value 1' },
default_params.merge(add_header: { 'header3' => { '' => '\'test value 3\' tv3' }, 'header2' => { 'test value 2' => 'tv2' }, 'header1' => 'test value 1' },
ssl: true,
ssl_key: 'dummy.key',
ssl_cert: 'dummy.cert')
end

it 'has correctly ordered entries in the config' do
is_expected.to contain_concat__fragment("#{title}-ssl-header").with_content(%r{\s+add_header\s+"header1" "test value 1";\n\s+add_header\s+"header2" "test value 2";\n\s+add_header\s+"header3" "test value 3";\n})
is_expected.to contain_concat__fragment("#{title}-ssl-header").with_content(%r{\s+add_header\s+"header1" "test value 1";\n\s+add_header\s+"header2" "test value 2" tv2;\n\s+add_header\s+"header3" 'test value 3' tv3;\n})
end
end
end
Expand Down
12 changes: 10 additions & 2 deletions templates/server/locations/headers.erb
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
<%- @add_header.keys.sort.each do |key| -%>
add_header "<%= key %>" "<%= @add_header[key] %>";
<%- @add_header.sort.each do |header, value| -%>
<%- if value -%>
<%- if value.is_a?(Hash) -%>
<%- value.each do |sk, sv| -%>
add_header "<%= header %>" <% if sk != '' %>"<%= sk %>"<% end %> <%= sv %>;
<%- end -%>
<%- else -%>
add_header "<%= header %>" "<%= value %>";
<%- end -%>
<%- end -%>
<%- end -%>
6 changes: 1 addition & 5 deletions templates/server/server_header.erb
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,7 @@ server {
<% if Array(@resolver).count > 0 -%>
resolver <% Array(@resolver).each do |r| %> <%= r %><% end %>;
<% end -%>
<% if @add_header -%>
<%- @add_header.keys.sort.each do |key| -%>
add_header "<%= key %>" "<%= @add_header[key] %>";
<%- end -%>
<% end -%>
<%= scope.function_template(["nginx/server/locations/headers.erb"]) %>
<% if @maintenance -%>
<%= @maintenance_value %>;
<% end -%>
Expand Down
6 changes: 1 addition & 5 deletions templates/server/server_ssl_header.erb
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,4 @@ server {
<% Array(@passenger_env_var).each do |key,value| -%>
passenger_env_var <%= key %> <%= value %>;
<% end -%>
<% if @add_header -%>
<%- @add_header.keys.sort.each do |key| -%>
add_header "<%= key %>" "<%= @add_header[key] %>";
<%- end -%>
<% end -%>
<%= scope.function_template(["nginx/server/locations/headers.erb"]) %>

0 comments on commit 54a5056

Please sign in to comment.