1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00
ruby--ruby/spec/ruby/library/net/http/httpheader/fetch_spec.rb
eregon 401b64c4e8 Update to ruby/spec@c1b568b
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@62656 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-03-04 15:09:32 +00:00

68 lines
2.3 KiB
Ruby

require_relative '../../../../spec_helper'
require 'net/http'
require_relative 'fixtures/classes'
describe "Net::HTTPHeader#fetch" do
before :each do
@headers = NetHTTPHeaderSpecs::Example.new
end
describe "when passed key" do
it "returns the header entry for the passed key" do
@headers["My-Header"] = "test"
@headers.fetch("My-Header").should == "test"
@headers.add_field("My-Other-Header", "a")
@headers.add_field("My-Other-Header", "b")
@headers.add_field("My-Other-Header", "c")
@headers.fetch("My-Other-Header").should == "a, b, c"
end
it "is case-insensitive" do
@headers["My-Header"] = "test"
@headers.fetch("my-header").should == "test"
@headers.fetch("MY-HEADER").should == "test"
end
it "returns nil when there is no entry for the passed key" do
lambda { @headers.fetch("my-header") }.should raise_error(IndexError)
end
end
describe "when passed key, default" do
it "returns the header entry for the passed key" do
@headers["My-Header"] = "test"
@headers.fetch("My-Header", "bla").should == "test"
@headers.add_field("My-Other-Header", "a")
@headers.add_field("My-Other-Header", "b")
@headers.add_field("My-Other-Header", "c")
@headers.fetch("My-Other-Header", "bla").should == "a, b, c"
end
# TODO: This raises a NoMethodError: undefined method `join' for "bla":String
it "returns the default value when there is no entry for the passed key" do
@headers.fetch("My-Header", "bla").should == "bla"
end
end
describe "when passed key and block" do
it "returns the header entry for the passed key" do
@headers["My-Header"] = "test"
@headers.fetch("My-Header") {}.should == "test"
@headers.add_field("My-Other-Header", "a")
@headers.add_field("My-Other-Header", "b")
@headers.add_field("My-Other-Header", "c")
-> {
@result = @headers.fetch("My-Other-Header", "bla") {}
}.should complain(/block supersedes default value argument/)
@result.should == "a, b, c"
end
# TODO: This raises a NoMethodError: undefined method `join' for "redaeh-ym":String
it "yieldsand returns the block's return value when there is no entry for the passed key" do
@headers.fetch("My-Header") { |key| key.reverse }.should == "redaeh-ym"
end
end
end