1
0
Fork 0
mirror of https://github.com/jnunemaker/httparty synced 2023-03-27 23:23:07 -04:00

Fix connection_adapter_spec

This commit is contained in:
Michael Stock 2014-12-06 17:07:05 -08:00
parent b72b212767
commit a783b3fd43
2 changed files with 60 additions and 12 deletions

View file

@ -65,7 +65,7 @@ RSpec.describe HTTParty::ConnectionAdapter do
let(:uri) { URI 'https://foobar.com' } let(:uri) { URI 'https://foobar.com' }
context "uses the system cert_store, by default" do context "uses the system cert_store, by default" do
let(:system_cert_store) do let!(:system_cert_store) do
system_cert_store = double('default_cert_store') system_cert_store = double('default_cert_store')
expect(system_cert_store).to receive(:set_default_paths) expect(system_cert_store).to receive(:set_default_paths)
expect(OpenSSL::X509::Store).to receive(:new).and_return(system_cert_store) expect(OpenSSL::X509::Store).to receive(:new).and_return(system_cert_store)
@ -122,7 +122,12 @@ RSpec.describe HTTParty::ConnectionAdapter do
context "when timeout is not set" do context "when timeout is not set" do
it "doesn't set the timeout" do it "doesn't set the timeout" do
http = double("http", null_object: true) http = double(
"http",
:null_object => true,
:use_ssl= => false,
:use_ssl? => false
)
expect(http).not_to receive(:open_timeout=) expect(http).not_to receive(:open_timeout=)
expect(http).not_to receive(:read_timeout=) expect(http).not_to receive(:read_timeout=)
allow(Net::HTTP).to receive_messages(new: http) allow(Net::HTTP).to receive_messages(new: http)
@ -150,7 +155,12 @@ RSpec.describe HTTParty::ConnectionAdapter do
let(:options) { {timeout: "five seconds"} } let(:options) { {timeout: "five seconds"} }
it "doesn't set the timeout" do it "doesn't set the timeout" do
http = double("http", null_object: true) http = double(
"http",
:null_object => true,
:use_ssl= => false,
:use_ssl? => false
)
expect(http).not_to receive(:open_timeout=) expect(http).not_to receive(:open_timeout=)
expect(http).not_to receive(:read_timeout=) expect(http).not_to receive(:read_timeout=)
allow(Net::HTTP).to receive_messages(new: http) allow(Net::HTTP).to receive_messages(new: http)
@ -169,7 +179,13 @@ RSpec.describe HTTParty::ConnectionAdapter do
end end
it "should not set the open_timeout" do it "should not set the open_timeout" do
http = double("http", null_object: true) http = double(
"http",
:null_object => true,
:use_ssl= => false,
:use_ssl? => false,
:read_timeout= => 0
)
expect(http).not_to receive(:open_timeout=) expect(http).not_to receive(:open_timeout=)
allow(Net::HTTP).to receive_messages(new: http) allow(Net::HTTP).to receive_messages(new: http)
adapter.connection adapter.connection
@ -190,7 +206,14 @@ RSpec.describe HTTParty::ConnectionAdapter do
end end
it "should override the timeout option" do it "should override the timeout option" do
http = double("http", null_object: true) http = double(
"http",
:null_object => true,
:use_ssl= => false,
:use_ssl? => false,
:read_timeout= => 0,
:open_timeout= => 0
)
expect(http).to receive(:open_timeout=) expect(http).to receive(:open_timeout=)
expect(http).to receive(:read_timeout=).twice expect(http).to receive(:read_timeout=).twice
allow(Net::HTTP).to receive_messages(new: http) allow(Net::HTTP).to receive_messages(new: http)
@ -207,7 +230,13 @@ RSpec.describe HTTParty::ConnectionAdapter do
end end
it "should not set the read_timeout" do it "should not set the read_timeout" do
http = double("http", null_object: true) http = double(
"http",
:null_object => true,
:use_ssl= => false,
:use_ssl? => false,
:open_timeout= => 0
)
expect(http).not_to receive(:read_timeout=) expect(http).not_to receive(:read_timeout=)
allow(Net::HTTP).to receive_messages(new: http) allow(Net::HTTP).to receive_messages(new: http)
adapter.connection adapter.connection
@ -228,7 +257,14 @@ RSpec.describe HTTParty::ConnectionAdapter do
end end
it "should override the timeout option" do it "should override the timeout option" do
http = double("http", null_object: true) http = double(
"http",
:null_object => true,
:use_ssl= => false,
:use_ssl? => false,
:read_timeout= => 0,
:open_timeout= => 0
)
expect(http).to receive(:open_timeout=).twice expect(http).to receive(:open_timeout=).twice
expect(http).to receive(:read_timeout=) expect(http).to receive(:read_timeout=)
allow(Net::HTTP).to receive_messages(new: http) allow(Net::HTTP).to receive_messages(new: http)
@ -337,10 +373,10 @@ RSpec.describe HTTParty::ConnectionAdapter do
it "will verify the certificate" do it "will verify the certificate" do
expect(subject.verify_mode).to eq(OpenSSL::SSL::VERIFY_PEER) expect(subject.verify_mode).to eq(OpenSSL::SSL::VERIFY_PEER)
end end
context "when options include verify_peer=false" do context "when options include verify_peer=false" do
let(:options) { {pem: pem, pem_password: "password", verify_peer: false} } let(:options) { {pem: pem, pem_password: "password", verify_peer: false} }
it "should not verify the certificate" do it "should not verify the certificate" do
expect(subject.verify_mode).to eq(OpenSSL::SSL::VERIFY_NONE) expect(subject.verify_mode).to eq(OpenSSL::SSL::VERIFY_NONE)
end end
@ -388,10 +424,10 @@ RSpec.describe HTTParty::ConnectionAdapter do
it "will verify the certificate" do it "will verify the certificate" do
expect(subject.verify_mode).to eq(OpenSSL::SSL::VERIFY_PEER) expect(subject.verify_mode).to eq(OpenSSL::SSL::VERIFY_PEER)
end end
context "when options include verify_peer=false" do context "when options include verify_peer=false" do
let(:options) { {p12: p12, p12_password: "password", verify_peer: false} } let(:options) { {p12: p12, p12_password: "password", verify_peer: false} }
it "should not verify the certificate" do it "should not verify the certificate" do
expect(subject.verify_mode).to eq(OpenSSL::SSL::VERIFY_NONE) expect(subject.verify_mode).to eq(OpenSSL::SSL::VERIFY_NONE)
end end
@ -404,7 +440,7 @@ RSpec.describe HTTParty::ConnectionAdapter do
before do before do
allow(Net::HTTP).to receive_messages(new: http) allow(Net::HTTP).to receive_messages(new: http)
expect(OpenSSL::PKCS12.new).not_to receive(:new).with(p12, "password") expect(OpenSSL::PKCS12).not_to receive(:new).with(p12, "password")
expect(http).not_to receive(:cert=) expect(http).not_to receive(:cert=)
expect(http).not_to receive(:key=) expect(http).not_to receive(:key=)
end end

View file

@ -48,3 +48,15 @@ RSpec.configure do |config|
Kernel.srand config.seed Kernel.srand config.seed
end end
RSpec::Matchers.define :use_ssl do
match do |connection|
connection.use_ssl?
end
end
RSpec::Matchers.define :use_cert_store do |cert_store|
match do |connection|
connection.cert_store == cert_store
end
end