Skip to content

Commit

Permalink
Merge pull request #1069 from ioquatix/patch-1
Browse files Browse the repository at this point in the history
Add default implementation of `Middleware#close`.
  • Loading branch information
technoweenie committed Nov 13, 2019
2 parents d5eaa4f + 4d49b03 commit 69f3078
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 1 deletion.
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

0 comments on commit 69f3078

Please sign in to comment.