Skip to content

Commit

Permalink
Fix up tests for non HTTP_PROXY supporting ruby.
Browse files Browse the repository at this point in the history
  • Loading branch information
ab committed Apr 24, 2015
1 parent d2ceac6 commit 5a3ff4c
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
4 changes: 4 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,8 @@

# add helpers
config.include Helpers, :include_helpers

config.mock_with :rspec do |mocks|
mocks.yield_receiver_to_any_instance_implementation_blocks = true
end
end
19 changes: 13 additions & 6 deletions spec/unit/request_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@

@net = double("net::http base")
@http = double("net::http connection")

Net::HTTP.stub(:new).and_return(@net)

@net.stub(:start).and_yield(@http)
@net.stub(:use_ssl=)
@net.stub(:verify_mode=)
Expand Down Expand Up @@ -414,7 +416,9 @@

describe "proxy" do
before do
allow(Net::HTTP).to receive(:new).and_call_original
# unstub Net::HTTP creation since we need to test it
Net::HTTP.unstub(:new)

@proxy_req = RestClient::Request.new(:method => :put, :url => 'http://some/resource', :payload => 'payload')
end

Expand All @@ -436,7 +440,6 @@
end

it "disables proxy on a per-request basis" do
allow(Net::HTTP).to receive(:new).and_call_original
RestClient.stub(:proxy).and_return('http://example.com')
RestClient.stub(:proxy_set?).and_return(true)
@proxy_req.net_http_object('host', 80).proxy?.should be true
Expand All @@ -446,15 +449,13 @@
end

it "sets proxy on a per-request basis" do
allow(Net::HTTP).to receive(:new).and_call_original
@proxy_req.net_http_object('some', 80).proxy?.should be_falsey

req = RestClient::Request.new(:method => :put, :url => 'http://some/resource', :payload => 'payload', :proxy => 'http://example.com')
req.net_http_object('host', 80).proxy?.should be true
end

it "overrides proxy from environment" do
allow(Net::HTTP).to receive(:new).and_call_original
it "overrides proxy from environment", if: RUBY_VERSION >= '2.0' do
allow(ENV).to receive(:[]).with("http_proxy").and_return("http://127.0.0.1")
allow(ENV).to receive(:[]).with("no_proxy").and_return(nil)
allow(ENV).to receive(:[]).with("NO_PROXY").and_return(nil)
Expand All @@ -463,18 +464,24 @@
obj.proxy?.should be true
obj.proxy_address.should eq '127.0.0.1'

# test original method .proxy?
req = RestClient::Request.new(:method => :put, :url => 'http://some/resource', :payload => 'payload', :proxy => nil)
obj = req.net_http_object('host', 80)
obj.proxy?.should be_falsey

# stub RestClient.proxy_set? to peek into implementation
RestClient.stub(:proxy_set?).and_return(true)
req = RestClient::Request.new(:method => :put, :url => 'http://some/resource', :payload => 'payload')
obj = req.net_http_object('host', 80)
obj.proxy?.should be_falsey

# test stubbed Net::HTTP.new
req = RestClient::Request.new(:method => :put, :url => 'http://some/resource', :payload => 'payload', :proxy => nil)
expect(Net::HTTP).to receive(:new).with('host', 80, nil, nil, nil, nil)
req.net_http_object('host', 80)
end

it "overrides global proxy with per-request proxy" do
allow(Net::HTTP).to receive(:new).and_call_original
RestClient.stub(:proxy).and_return('http://example.com')
RestClient.stub(:proxy_set?).and_return(true)
obj = @proxy_req.net_http_object('host', 80)
Expand Down

0 comments on commit 5a3ff4c

Please sign in to comment.