require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper') describe "Cloudhdr::HTTP::NetHTTP" do describe "call options" do it "should request with timeout" do stub_request(:post, "https://example.com") Timeout.should_receive(:timeout).with(0.001) Cloudhdr::HTTP::NetHTTP.post('https://example.com', :timeout => 1) end it "should request without timeout" do stub_request(:post, "https://example.com") Timeout.stub(:timeout).and_raise(Exception) lambda { Cloudhdr::HTTP::NetHTTP.post('https://example.com', :timeout => nil) }.should_not raise_error(Exception) end it "should add params to the query string if passed" do stub_request(:post, "https://example.com/path?some=param") Cloudhdr::HTTP::NetHTTP.post('https://example.com/path', {:params => {:some => 'param' } }) end it "should add params to the existing query string if passed" do stub_request(:post,'https://example.com/path?original=param&some=param') Cloudhdr::HTTP::NetHTTP.post('https://example.com/path?original=param', {:params => {:some => 'param'}}) end it "should add headers" do stub_request(:post,'https://example.com/path').with(:headers => {'some' => 'header' }) Cloudhdr::HTTP::NetHTTP.post('https://example.com/path', {:headers => {:some => 'header' } }) end it "should add the body to the request" do stub_request(:post, 'https://example.com/path').with(:body => '{"some": "body"}') Cloudhdr::HTTP::NetHTTP.post('https://example.com/path', {:body => '{"some": "body"}'} ) end end describe "SSL verification" do it "should verify when the SSL directory is found" do http_stub = double(:use_ssl= => true, :ca_path= => true, :verify_depth= => true, :request => true) http_stub.should_receive(:verify_mode=).with(OpenSSL::SSL::VERIFY_PEER) ::Net::HTTP.should_receive(:new).and_return(http_stub) Cloudhdr::HTTP::NetHTTP.any_instance.should_receive(:locate_root_cert_path).and_return('/fake/path') Cloudhdr::HTTP::NetHTTP.post('https://example.com/path') end it "should not verify when set to skip ssl verification" do http_stub = double(:use_ssl= => true, :request => true) http_stub.should_receive(:verify_mode=).with(OpenSSL::SSL::VERIFY_NONE) ::Net::HTTP.should_receive(:new).and_return(http_stub) Cloudhdr::HTTP::NetHTTP.post('https://example.com/path', :skip_ssl_verify => true) end it "should not verify when the SSL directory is not found" do http_stub = double(:use_ssl= => true, :ca_path= => true, :verify_depth= => true, :request => true) http_stub.should_receive(:verify_mode=).with(OpenSSL::SSL::VERIFY_NONE) ::Net::HTTP.should_receive(:new).and_return(http_stub) Cloudhdr::HTTP::NetHTTP.any_instance.should_receive(:locate_root_cert_path).and_return(nil) Cloudhdr::HTTP::NetHTTP.post('https://example.com/path') end end describe ".post" do it "should POST to specified body to the specified path" do Cloudhdr::HTTP::NetHTTP.should_receive(:post). with('https://example.com',:body => '{}'). and_return(Cloudhdr::Response.new) Cloudhdr::HTTP::NetHTTP.post('https://example.com',:body => '{}').should_not be_nil end end describe ".put" do it "should PUT to specified body to the specified path" do Cloudhdr::HTTP::NetHTTP.should_receive(:put). with('https://example.com',:body => '{}'). and_return(Cloudhdr::Response.new) Cloudhdr::HTTP::NetHTTP.put('https://example.com', :body => '{}') end end describe ".get" do it "should GET to specified body to the specified path" do Cloudhdr::HTTP::NetHTTP.should_receive(:get). with('https://example.com'). and_return(Cloudhdr::Response.new) Cloudhdr::HTTP::NetHTTP.get('https://example.com') end end describe ".delete" do it "should DELETE to specified body to the specified path" do Cloudhdr::HTTP::NetHTTP.should_receive(:delete). with('https://example.com'). and_return(Cloudhdr::Response.new) Cloudhdr::HTTP::NetHTTP.delete('https://example.com') end end end