Skip to content

Commit

Permalink
Merge pull request #370 from httprb/improve/headers-class
Browse files Browse the repository at this point in the history
Add Headers#include?
  • Loading branch information
ixti authored Aug 23, 2016
2 parents 46c14e9 + 99e55c4 commit 32e9ed3
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
8 changes: 8 additions & 0 deletions lib/http/headers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
20 changes: 20 additions & 0 deletions spec/lib/http/headers_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 32e9ed3

Please sign in to comment.