Skip to content

Commit

Permalink
feat(deps): update to faraday 2
Browse files Browse the repository at this point in the history
  • Loading branch information
ksol committed Dec 26, 2023
1 parent edd5a06 commit 75c04f4
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 19 deletions.
13 changes: 2 additions & 11 deletions lib/scalingo/api/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,7 @@ def authenticated_connection
@connection = Faraday.new(connection_options) { |conn|
conn.response :json, content_type: /\bjson$/, parser_options: {symbolize_names: true}
conn.request :json

if token_holder.token&.value
auth_header = Faraday::Request::Authorization.header "Bearer", token_holder.token.value
conn.headers[Faraday::Request::Authorization::KEY] = auth_header
end
conn.request :authorization, "Bearer", -> { token_holder.token&.value }

conn.adapter(config.faraday_adapter) if config.faraday_adapter
}
Expand All @@ -123,12 +119,7 @@ def database_connection(database_id)
@database_connections[database_id] ||= Faraday.new(connection_options) { |conn|
conn.response :json, content_type: /\bjson$/, parser_options: {symbolize_names: true}
conn.request :json

bearer_token = token_holder.database_tokens[database_id]&.value
if bearer_token
auth_header = Faraday::Request::Authorization.header "Bearer", bearer_token
conn.headers[Faraday::Request::Authorization::KEY] = auth_header
end
conn.request :authorization, "Bearer", -> { token_holder.database_tokens[database_id]&.value }

conn.adapter(config.faraday_adapter) if config.faraday_adapter
}
Expand Down
2 changes: 1 addition & 1 deletion lib/scalingo/auth/tokens.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class Auth::Tokens < API::Endpoint
def exchange(token, headers = nil, &block)
data = nil

authorization = Faraday::Request::BasicAuthentication.header("", token)
authorization = Faraday::Utils.basic_header_from("", token)

request_headers = {
Faraday::Request::Authorization::KEY => authorization
Expand Down
1 change: 0 additions & 1 deletion lib/scalingo/core_client.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
require "forwardable"
require "faraday"
require "faraday_middleware"
require "scalingo/token_holder"
require "scalingo/errors"

Expand Down
5 changes: 2 additions & 3 deletions scalingo.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ Gem::Specification.new do |s|
s.require_paths = ["lib"]

s.add_dependency "activesupport", [">= 5", "< 8"]
s.add_dependency "faraday", "~> 1.0"
s.add_dependency "faraday_middleware", "~> 1.0"
s.add_dependency "faraday", "~> 2.0"
s.add_dependency "multi_json", ">= 1.0.3", "~> 1.0"

s.add_development_dependency "bundler", "~> 2.0"
Expand All @@ -43,5 +42,5 @@ Gem::Specification.new do |s|
s.add_development_dependency "standard", "~> 1.32"
s.add_development_dependency "rubocop-rspec"
s.add_development_dependency "pry", "~> 0.14.1"
s.add_development_dependency "webmock", "~> 3.12.2"
s.add_development_dependency "webmock", "~> 3.19"
end
17 changes: 14 additions & 3 deletions spec/scalingo/api/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,13 @@
end

context "with bearer token" do
before { stub_request(:any, "localhost") }

it "has an authentication header set with a bearer scheme" do
expect(subject.connection.headers["Authorization"]).to eq "Bearer #{subject.token_holder.token.value}"
request_headers = subject.connection.get("/").env.request_headers
expected = "Bearer #{subject.token_holder.token.value}"

expect(request_headers["Authorization"]).to eq(expected)
end
end
end
Expand All @@ -184,14 +189,20 @@
end

context "with bearer token" do
before { stub_request(:any, "localhost") }

it "has an authentication header set with a bearer scheme" do
scalingo.authenticate_database_with_bearer_token(
database_id,
"1234",
expires_at: Time.now + 1.hour,
raise_on_expired_token: false,
raise_on_expired_token: false
)
expect(subject.database_connection(database_id).headers["Authorization"]).to eq "Bearer #{subject.token_holder.database_tokens[database_id].value}"

request_headers = subject.database_connection(database_id).get("/").env.request_headers
expected = "Bearer #{subject.token_holder.database_tokens[database_id].value}"

expect(request_headers["Authorization"]).to eq(expected)
end
end

Expand Down
1 change: 1 addition & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require "bundler/setup"
require "scalingo"
require "active_support/all"

pattern = File.join(File.expand_path(__dir__), "support", "**", "*.rb")

Expand Down

0 comments on commit 75c04f4

Please sign in to comment.