Skip to content

Commit

Permalink
Merge b153673 into 25193da
Browse files Browse the repository at this point in the history
  • Loading branch information
samstarling committed Jul 11, 2018
2 parents 25193da + b153673 commit 4002153
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 43 deletions.
16 changes: 2 additions & 14 deletions lib/flexirest/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,18 +63,12 @@ def username(value = nil, &block)
value
end
else
if value.respond_to?(:call)
@username = value
else
value = CGI::escape(value) if value.present? && !value.include?("%")
@username = value
end
@username = value
end
end

def username=(value)
Flexirest::Logger.info "\033[1;4;32m#{name}\033[0m Username set to be #{value}"
value = CGI::escape(value) if value.present? && !value.include?("%")
@@username = value
end

Expand All @@ -94,18 +88,12 @@ def password(value = nil, &block)
value
end
else
if value.respond_to?(:call)
@password = value
else
value = CGI::escape(value) if value.present? && !value.include?("%")
@password = value
end
@password = value
end
end

def password=(value)
Flexirest::Logger.info "\033[1;4;32m#{name}\033[0m Password set..."
value = CGI::escape(value) if value.present? && !value.include?("%")
@@password = value
end

Expand Down
4 changes: 4 additions & 0 deletions lib/flexirest/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ def headers
@session.headers
end

def basic_auth(username, password)
@session.basic_auth(username, password)
end

def make_safe_request(path, &block)
block.call
rescue Faraday::Error::TimeoutError
Expand Down
6 changes: 4 additions & 2 deletions lib/flexirest/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,6 @@ def do_request(etag)
else
_, @base_url, @url = parts
end
base_url.gsub!(%r{//(.)}, "//#{username}:#{password}@\\1") if username && !base_url[%r{//[^/]*:[^/]*@}]
connection = Flexirest::ConnectionManager.get_connection(base_url)
end
else
Expand All @@ -459,7 +458,6 @@ def do_request(etag)
else
base_url = parts[0]
end
base_url.gsub!(%r{//(.)}, "//#{username}:#{password}@\\1") if username && !base_url[%r{//[^/]*:[^/]*@}]
connection = Flexirest::ConnectionManager.get_connection(base_url)
end
if @method[:options][:direct]
Expand All @@ -468,6 +466,10 @@ def do_request(etag)
Flexirest::Logger.info " \033[1;4;32m#{Flexirest.name}\033[0m #{@instrumentation_name} - Requesting #{connection.base_url}#{@url}"
end

if username
connection.basic_auth(username, password)
end

if verbose?
Flexirest::Logger.debug "Flexirest Verbose Log:"
Flexirest::Logger.debug " Request"
Expand Down
24 changes: 0 additions & 24 deletions spec/lib/configuration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,18 +81,6 @@ class ConfigurationExample2
expect(SubConfigurationExample.username).to eq("john")
end

it "should escape the username" do
Flexirest::Base.username = "bill@example.com"
expect(Flexirest::Base.username).to eq("bill%40example.com")
Flexirest::Base.username = nil
end

it "should not doubly escape the username" do
Flexirest::Base.username = "bill%40example.com"
expect(Flexirest::Base.username).to_not eq("bill%2540example.com")
Flexirest::Base.username = nil
end

it "should remember the set password" do
expect(ConfigurationExample.password).to eq("smith")
end
Expand All @@ -107,18 +95,6 @@ class ConfigurationExample2
expect(SubConfigurationExample.password).to eq("smith")
end

it "should escape the password" do
Flexirest::Base.password = "something@else"
expect(Flexirest::Base.password).to eq("something%40else")
Flexirest::Base.password = nil
end

it "should not doubly escape the password" do
Flexirest::Base.password = "something%40else"
expect(Flexirest::Base.password).to_not eq("something%2540else")
Flexirest::Base.password = nil
end

it "should default to a form_encoded request_body_type" do
expect(Flexirest::Base.request_body_type).to eq(:form_encoded)
end
Expand Down
24 changes: 21 additions & 3 deletions spec/lib/request_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,13 @@ class AuthenticatedExampleClient < Flexirest::Base
get :all, "/"
end

class AuthenticatedWithEmailExampleClient < Flexirest::Base
base_url "http://www.example.com"
username "john@smith.com"
password "smith"
get :all, "/"
end

class AuthenticatedProcExampleClient < Flexirest::Base
base_url "http://www.example.com"
username Proc.new { |obj| obj ? "bill-#{obj.id}" : "bill" }
Expand Down Expand Up @@ -175,21 +182,32 @@ class WhitelistedDateClient < Flexirest::Base

it "should get an HTTP connection with authentication when called" do
connection = double(Flexirest::Connection).as_null_object
expect(Flexirest::ConnectionManager).to receive(:get_connection).with("http://john:smith@www.example.com").and_return(connection)
expect(Flexirest::ConnectionManager).to receive(:get_connection).with("http://www.example.com").and_return(connection)
expect(connection).to receive(:basic_auth).with("john", "smith")
expect(connection).to receive(:get).with("/", an_instance_of(Hash)).and_return(::FaradayResponseMock.new(OpenStruct.new(body:'{"result":true}', response_headers:{})))
AuthenticatedExampleClient.all
end

it "should get an HTTP connection with e-mail authentication when called" do
connection = double(Flexirest::Connection).as_null_object
expect(Flexirest::ConnectionManager).to receive(:get_connection).with("http://www.example.com").and_return(connection)
expect(connection).to receive(:basic_auth).with("john@smith.com", "smith")
expect(connection).to receive(:get).with("/", an_instance_of(Hash)).and_return(::FaradayResponseMock.new(OpenStruct.new(body:'{"result":true}', response_headers:{})))
AuthenticatedWithEmailExampleClient.all
end

it "should get an HTTP connection with authentication using procs when called in a class context" do
connection = double(Flexirest::Connection).as_null_object
expect(Flexirest::ConnectionManager).to receive(:get_connection).with("http://bill:jones@www.example.com").and_return(connection)
expect(Flexirest::ConnectionManager).to receive(:get_connection).with("http://www.example.com").and_return(connection)
expect(connection).to receive(:basic_auth).with("bill", "jones")
expect(connection).to receive(:get).with("/", an_instance_of(Hash)).and_return(::FaradayResponseMock.new(OpenStruct.new(body:'{"result":true}', response_headers:{})))
AuthenticatedProcExampleClient.all
end

it "should get an HTTP connection with authentication using procs when called in an object context" do
connection = double(Flexirest::Connection).as_null_object
expect(Flexirest::ConnectionManager).to receive(:get_connection).with("http://bill-1:jones-1@www.example.com").and_return(connection)
expect(Flexirest::ConnectionManager).to receive(:get_connection).with("http://www.example.com").and_return(connection)
expect(connection).to receive(:basic_auth).with("bill-1", "jones-1")
expect(connection).to receive(:get).with("/?id=1", an_instance_of(Hash)).and_return(::FaradayResponseMock.new(OpenStruct.new(body:'{"result":true}', response_headers:{})))
obj = AuthenticatedProcExampleClient.new(id: 1)
obj.all
Expand Down

0 comments on commit 4002153

Please sign in to comment.