diff --git a/lib/http/headers.rb b/lib/http/headers.rb index fce7027f..0d6291dd 100644 --- a/lib/http/headers.rb +++ b/lib/http/headers.rb @@ -75,6 +75,14 @@ def [](name) end end + # Tells whenever header with given `name` is set or not. + # + # @return [Boolean] + def include?(name) + name = normalize_header name.to_s + @pile.any? { |k, _| k == name } + end + # Returns Rack-compatible headers Hash # # @return [Hash] diff --git a/spec/lib/http/headers_spec.rb b/spec/lib/http/headers_spec.rb index ab6fccd7..863e0316 100644 --- a/spec/lib/http/headers_spec.rb +++ b/spec/lib/http/headers_spec.rb @@ -185,6 +185,26 @@ end end + describe "#include?" do + before do + headers.add :content_type, "application/json" + headers.add :set_cookie, "hoo=ray" + headers.add :set_cookie, "woo=hoo" + end + + it "tells whenever given headers is set or not" do + expect(headers.include?("Content-Type")).to be true + expect(headers.include?("Set-Cookie")).to be true + expect(headers.include?("Accept")).to be false + end + + it "normalizes given header name" do + expect(headers.include?(:content_type)).to be true + expect(headers.include?(:set_cookie)).to be true + expect(headers.include?(:accept)).to be false + end + end + describe "#to_h" do before do headers.add :content_type, "application/json"