From 5706ab666cfbf3bb70f96e9a02f7e4bf17b386bf Mon Sep 17 00:00:00 2001 From: Oleg Pudeyev Date: Sat, 12 Sep 2020 04:43:10 -0400 Subject: [PATCH] Require username and password to be not nil --- .rubocop_todo.yml | 2 +- lib/faraday/request/digestauth.rb | 7 ++++++ spec/faraday/request/digest_auth_spec.rb | 30 ++++++++++++++++++++++++ 3 files changed, 38 insertions(+), 1 deletion(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 75b705e..58ff072 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -10,7 +10,7 @@ # Configuration parameters: CountComments, ExcludedMethods. # ExcludedMethods: refine Metrics/BlockLength: - Max: 47 + Max: 99 # Offense count: 1 # Cop supports --auto-correct. diff --git a/lib/faraday/request/digestauth.rb b/lib/faraday/request/digestauth.rb index 8dd7bb8..34231dc 100644 --- a/lib/faraday/request/digestauth.rb +++ b/lib/faraday/request/digestauth.rb @@ -31,6 +31,13 @@ class DigestAuth < Faraday::Middleware # - keep_body_on_handshake: if set to truthy, will also send # the original request body def initialize(app, user, password, opts = {}) + if user.nil? + raise ArgumentError, 'Username cannot be nil' + end + if password.nil? + raise ArgumentError, 'Password cannot be nil' + end + super(app) @user = user @password = password diff --git a/spec/faraday/request/digest_auth_spec.rb b/spec/faraday/request/digest_auth_spec.rb index e732e8b..de03403 100644 --- a/spec/faraday/request/digest_auth_spec.rb +++ b/spec/faraday/request/digest_auth_spec.rb @@ -56,4 +56,34 @@ expect(response.body).to be_empty end end + + context 'when the username is nil' do + let(:connection) do + Faraday.new('http://api.example.org/') do |builder| + builder.request :digest, nil, 'PASS' + builder.adapter :net_http + end + end + + it 'raises ArgumentError during construction' do + expect do + connection.get('http://api.example.com') + end.to raise_error(ArgumentError, /Username cannot be nil/) + end + end + + context 'when the password is nil' do + let(:connection) do + Faraday.new('http://api.example.org/') do |builder| + builder.request :digest, 'USER', nil + builder.adapter :net_http + end + end + + it 'raises ArgumentError during construction' do + expect do + connection.get('http://api.example.com') + end.to raise_error(ArgumentError, /Password cannot be nil/) + end + end end