Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

only require ntlm if ntlm auth was requested. #187

Merged
merged 8 commits into from
Oct 3, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,6 @@ rvm:
- 2.2.4
- 2.3.0
- jruby
env:
global:
- JRUBY_OPTS="--2.0"
3 changes: 3 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ gemspec

gem 'jruby-openssl', :platforms => :jruby

# compatibility restrictions for http clients under existing travis test environments
gem 'public_suffix', '~> 2.0' # or remove rubies < 2.1 from travis.yml

# http clients
gem 'httpclient', '~> 2.3', :require => false
gem 'curb', '~> 0.8', :require => false, :platforms => :ruby
Expand Down
8 changes: 5 additions & 3 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ RSpec::Core::RakeTask.new "spec_integration" do |t|
t.pattern = "spec/integration/*_spec.rb"
end

task :default => :spec

desc "Run RSpec code and integration examples"
task :ci => [:spec, :spec_integration]
RSpec::Core::RakeTask.new "ci" do |t|
t.pattern = "spec/{httpi,integration}/**/*_spec.rb"
end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👌 🎯


task :default => :spec
8 changes: 6 additions & 2 deletions lib/httpi/adapter/net_http.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class NetHTTP < Base

register :net_http, :deps => %w(net/https)
def initialize(request)
check_net_ntlm_version!
check_net_ntlm_version! if request.auth.ntlm?
@request = request
@client = create_client
end
Expand Down Expand Up @@ -55,11 +55,15 @@ def request(method)
end

private
def ntlm_version
Net::NTLM::VERSION::STRING
end

def check_net_ntlm_version!
begin
require 'net/ntlm'
require 'net/ntlm/version' unless Net::NTLM.const_defined?(:VERSION, false)
unless Net::NTLM::VERSION::STRING >= '0.3.2'
unless ntlm_version >= '0.3.2'
raise ArgumentError, 'Invalid version of rubyntlm. Please use v0.3.2+.'
end
rescue LoadError
Expand Down
27 changes: 27 additions & 0 deletions spec/httpi/adapter/net_http_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,33 @@
to raise_error(HTTPI::NotSupportedError, /Net::NTLM is not available/)
end

it 'does not require ntlm when ntlm authenication is not requested' do
HTTPI::Adapter::NetHTTP.any_instance.stubs(:check_net_ntlm_version!).raises(RuntimeError)
request = HTTPI::Request.new(@server.url)
expect(request.auth.ntlm?).to be false

# make sure a request doesn't call ntlm check if we don't ask for it.
expect { HTTPI.get(request, adapter) }.not_to raise_error
HTTPI::Adapter::NetHTTP.any_instance.unstub(:check_net_ntlm_version!)
end

it 'does check ntlm when ntlm authentication is requested' do
request = HTTPI::Request.new(@server.url + "ntlm-auth")
request.auth.ntlm("tester", "vReqSoafRe5O")

expect { HTTPI.get(request, adapter) }.not_to raise_error

# the check should also verify that the version of ntlm is supported and still fail if it isn't
HTTPI::Adapter::NetHTTP.any_instance.stubs(:ntlm_version).returns("0.1.1")

request = HTTPI::Request.new(@server.url + "ntlm-auth")
request.auth.ntlm("tester", "vReqSoafRe5O")

expect { HTTPI.get(request, adapter) }.to raise_error(ArgumentError, /Invalid version/)

HTTPI::Adapter::NetHTTP.any_instance.unstub(:ntlm_version)
end

it "does not crash when authenticate header is missing (on second request)" do
request = HTTPI::Request.new(@server.url + 'ntlm-auth')
request.auth.ntlm("tester", "vReqSoafRe5O")
Expand Down
4 changes: 3 additions & 1 deletion spec/integration/net_http_persistent_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@
end

it "executes POST requests" do
response = HTTPI.post(@server.url, "<some>xml</some>", adapter)
request = HTTPI::Request.new(url: @server.url, open_timeout: 1, read_timeout: 1, body: "<some>xml</some>")

response = HTTPI.post(request, adapter)
expect(response.body).to eq("post")
expect(response.headers["Content-Type"]).to eq("text/plain")
end
Expand Down