Skip to content

Commit dc6449c

Browse files
committed
Introduce InvalidTrailerError.
1 parent f4f463a commit dc6449c

File tree

3 files changed

+18
-3
lines changed

3 files changed

+18
-3
lines changed

lib/protocol/http/error.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,18 @@ def initialize(key)
2626
# @attribute [String] key The header key that was duplicated.
2727
attr :key
2828
end
29+
30+
# Raised when an invalid trailer header is encountered in headers.
31+
class InvalidTrailerError < Error
32+
include BadRequest
33+
34+
# @parameter key [String] The trailer key that is invalid.
35+
def initialize(key)
36+
super("Invalid trailer key: #{key.inspect}")
37+
end
38+
39+
# @attribute [String] key The trailer key that is invalid.
40+
attr :key
41+
end
2942
end
3043
end

lib/protocol/http/headers.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,7 @@ def delete(key)
407407
if policy = @policy[key]
408408
# Check if we're adding to trailers and this header is allowed:
409409
if trailer && !policy.trailer?
410-
return false
410+
raise InvalidTrailerError, key
411411
end
412412

413413
if current_value = hash[key]
@@ -422,7 +422,7 @@ def delete(key)
422422
else
423423
# By default, headers are not allowed in trailers:
424424
if trailer
425-
return false
425+
raise InvalidTrailerError, key
426426
end
427427

428428
if hash.key?(key)
@@ -435,6 +435,8 @@ def delete(key)
435435

436436
# Compute a hash table of headers, where the keys are normalized to lower case and the values are normalized according to the policy for that header.
437437
#
438+
# This will enforce policy rules, such as merging multiple headers into arrays, or raising errors for duplicate headers.
439+
#
438440
# @returns [Hash] A hash table of `{key, value}` pairs.
439441
def to_h
440442
unless @indexed

test/protocol/http/headers.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@
343343
it "can't add a #{key.inspect} header in the trailer", unique: key do
344344
trailer = headers.trailer!
345345
headers.add(key, "example")
346-
expect(headers).not.to be(:include?, key)
346+
expect{headers.to_h}.to raise_exception(Protocol::HTTP::InvalidTrailerError)
347347
end
348348
end
349349
end

0 commit comments

Comments
 (0)