@@ -191,7 +191,7 @@ def empty?
191191 # @parameter key [String] The header key.
192192 # @parameter value [String] The raw header value.
193193 def each ( &block )
194- @fields . each ( &block )
194+ self . to_h . each ( &block )
195195 end
196196
197197 # @returns [Boolean] Whether the headers include the specified key.
@@ -279,7 +279,7 @@ def []=(key, value)
279279 # @parameter key [String] The header key.
280280 # @returns [String | Array | Object] The header value.
281281 def [] key
282- to_h [ key ]
282+ self . to_h [ key ]
283283 end
284284
285285 # Merge the headers into this instance.
@@ -403,11 +403,12 @@ def delete(key)
403403 # @parameter hash [Hash] The hash to merge into.
404404 # @parameter key [String] The header key.
405405 # @parameter value [String] The raw header value.
406+ # @parameter trailer [Boolean] Whether this header is in the trailer section.
406407 protected def merge_into ( hash , key , value , trailer = @tail )
407408 if policy = @policy [ key ]
408409 # Check if we're adding to trailers and this header is allowed:
409410 if trailer && !policy . trailer?
410- return false
411+ raise InvalidTrailerError , key
411412 end
412413
413414 if current_value = hash [ key ]
@@ -418,7 +419,7 @@ def delete(key)
418419 else
419420 # By default, headers are not allowed in trailers:
420421 if trailer
421- return false
422+ raise InvalidTrailerError , key
422423 end
423424
424425 if hash . key? ( key )
@@ -431,6 +432,8 @@ def delete(key)
431432
432433 # 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.
433434 #
435+ # This will enforce policy rules, such as merging multiple headers into arrays, or raising errors for duplicate headers.
436+ #
434437 # @returns [Hash] A hash table of `{key, value}` pairs.
435438 def to_h
436439 unless @indexed
@@ -461,7 +464,7 @@ def inspect
461464 def == other
462465 case other
463466 when Hash
464- to_h == other
467+ self . to_h == other
465468 when Headers
466469 @fields == other . fields
467470 else
0 commit comments