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

Add default implementation of Middleware#close. #1069

Merged
merged 5 commits into from Nov 13, 2019
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion .rubocop_todo.yml
Expand Up @@ -13,7 +13,7 @@ Metrics/AbcSize:
# Offense count: 4
# Configuration parameters: CountComments.
Metrics/ClassLength:
Max: 233
Max: 234

# Offense count: 17
Metrics/CyclomaticComplexity:
Expand Down
7 changes: 7 additions & 0 deletions lib/faraday/connection.rb
Expand Up @@ -117,6 +117,13 @@ def headers=(hash)

def_delegators :builder, :build, :use, :request, :response, :adapter, :app

# Closes the underlying resources and/or connections. In the case of
# persistent connections, this closes all currently open connections
# but does not prevent new connections from being made.
def close
app.close
end

# @!method get(url = nil, params = nil, headers = nil)
# Makes a GET HTTP request without a body.
# @!scope class
Expand Down
8 changes: 8 additions & 0 deletions lib/faraday/middleware.rb
Expand Up @@ -9,5 +9,13 @@ class Middleware
def initialize(app = nil)
@app = app
end

def close
if @app.respond_to?(:close)
@app.close
else
warn "#{@app} does not implement \#close!"
end
end
end
end
7 changes: 7 additions & 0 deletions spec/faraday/connection_spec.rb
Expand Up @@ -127,6 +127,13 @@
end
end

describe '#close' do
it 'can close underlying app' do
expect(conn.app).to receive(:close)
conn.close
end
end

describe 'basic_auth' do
subject { conn }

Expand Down
26 changes: 26 additions & 0 deletions spec/faraday/middleware_spec.rb
@@ -0,0 +1,26 @@
# frozen_string_literal: true

RSpec.describe Faraday::Middleware do
subject { described_class.new(app) }

describe '#close' do
context "with app that doesn't support \#close" do
let(:app) { double }

it 'should issue warning' do
expect(subject).to receive(:warn)
subject.close
end
end

context "with app that supports \#close" do
let(:app) { double }

it 'should issue warning' do
expect(app).to receive(:close)
expect(subject).to_not receive(:warn)
subject.close
end
end
end
end