Skip to content

Commit 9db73a2

Browse files
committed
Merge pull request rails#21838 from jeremy/response-add-header
Response#add_header for adding to multi-valued headers like Vary
2 parents dd57f60 + 28a1a39 commit 9db73a2

File tree

2 files changed

+79
-0
lines changed

2 files changed

+79
-0
lines changed

actionpack/lib/action_dispatch/http/response.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,26 @@ def get_header(key); headers[key]; end
161161
def set_header(key, v); headers[key] = v; end
162162
def delete_header(key); headers.delete key; end
163163

164+
# Add a header that may have multiple values.
165+
#
166+
# Example:
167+
# response.add_header 'Vary', 'Accept'
168+
# response.add_header 'Vary', 'Accept-Encoding'
169+
# response.add_header 'Vary', 'Cookie'
170+
#
171+
# assert_equal 'Accept,Accept-Encoding,Cookie', response.get_header 'Vary'
172+
#
173+
# http://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html#sec4.2
174+
def add_header(key, v)
175+
if v.nil?
176+
get_header key
177+
elsif have_header? key
178+
set_header key, "#{get_header key},#{v}"
179+
else
180+
set_header key, v
181+
end
182+
end
183+
164184
def await_commit
165185
synchronize do
166186
@cv.wait_until { @committed }

actionpack/test/dispatch/response_test.rb

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,65 @@ def test_only_set_charset_still_defaults_to_text_html
293293
end
294294
end
295295

296+
class ResponseHeadersTest < ActiveSupport::TestCase
297+
def setup
298+
@response = ActionDispatch::Response.create
299+
@response.set_header 'Foo', '1'
300+
end
301+
302+
test 'have_header?' do
303+
assert @response.have_header? 'Foo'
304+
assert_not @response.have_header? 'foo'
305+
assert_not @response.have_header? nil
306+
end
307+
308+
test 'get_header' do
309+
assert_equal '1', @response.get_header('Foo')
310+
assert_nil @response.get_header('foo')
311+
assert_nil @response.get_header(nil)
312+
end
313+
314+
test 'set_header' do
315+
assert_equal '2', @response.set_header('Foo', '2')
316+
assert @response.have_header?('Foo')
317+
assert_equal '2', @response.get_header('Foo')
318+
319+
assert_nil @response.set_header('Foo', nil)
320+
assert @response.have_header?('Foo')
321+
assert_nil @response.get_header('Foo')
322+
end
323+
324+
test 'delete_header' do
325+
assert_nil @response.delete_header(nil)
326+
327+
assert_nil @response.delete_header('foo')
328+
assert @response.have_header?('Foo')
329+
330+
assert_equal '1', @response.delete_header('Foo')
331+
assert_not @response.have_header?('Foo')
332+
end
333+
334+
test 'add_header' do
335+
# Add a value to an existing header
336+
assert_equal '1,2', @response.add_header('Foo', '2')
337+
assert_equal '1,2', @response.get_header('Foo')
338+
339+
# Add nil to an existing header
340+
assert_equal '1,2', @response.add_header('Foo', nil)
341+
assert_equal '1,2', @response.get_header('Foo')
342+
343+
# Add nil to a nonexistent header
344+
assert_nil @response.add_header('Bar', nil)
345+
assert_not @response.have_header?('Bar')
346+
assert_nil @response.get_header('Bar')
347+
348+
# Add a value to a nonexistent header
349+
assert_equal '1', @response.add_header('Bar', '1')
350+
assert @response.have_header?('Bar')
351+
assert_equal '1', @response.get_header('Bar')
352+
end
353+
end
354+
296355
class ResponseIntegrationTest < ActionDispatch::IntegrationTest
297356
test "response cache control from railsish app" do
298357
@app = lambda { |env|

0 commit comments

Comments
 (0)