Skip to content

Commit 7b24c6e

Browse files
authored
Merge pull request #201 from pusher/fix/reset-http-client-on-unknown-response
Reset HTTP client connection after unknown error responses
2 parents b4f8cb8 + c602611 commit 7b24c6e

4 files changed

Lines changed: 53 additions & 2 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## 2.1.0
4+
5+
* [FIXED] Reset HTTP client connection after unknown error responses to prevent subsequent requests from failing.
6+
37
## 2.0.5
48

59
* [FIXED] Fix webhook content_type parsing for Rails 7.1+ using media_type

lib/pusher/request.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,11 @@ def send_sync
3636
end
3737

3838
body = response.body ? response.body.chomp : nil
39+
code = response.code.to_i
3940

40-
return handle_response(response.code.to_i, body)
41+
http.reset_all unless [200, 202, 400, 401, 403, 404, 407, 413].include?(code)
42+
43+
return handle_response(code, body)
4144
end
4245

4346
def send_async

lib/pusher/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module Pusher
2-
VERSION = '2.0.5'
2+
VERSION = '2.1.0'
33
end

spec/client_spec.rb

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,50 @@
262262
:occupied => false,
263263
})
264264
end
265+
266+
it "should not reset the http client connection after a 400 error" do
267+
api_path = %r{/apps/20/channels/mychannel}
268+
stub_request(:get, api_path).to_return({ :status => 400, :body => "Bad request" })
269+
270+
http_client = @client.sync_http_client
271+
expect(http_client).not_to receive(:reset_all)
272+
273+
expect { @client.channel_info('mychannel') }.to raise_error(Pusher::Error)
274+
end
275+
276+
it "should reset the http client connection after an unknown error status" do
277+
api_path = %r{/apps/20/channels/mychannel}
278+
stub_request(:get, api_path).to_return({ :status => 500, :body => "Server error" })
279+
280+
http_client = @client.sync_http_client
281+
expect(http_client).to receive(:reset_all).once
282+
283+
expect { @client.channel_info('mychannel') }.to raise_error(Pusher::Error)
284+
end
285+
286+
it "should succeed on a subsequent request after an unknown error status" do
287+
api_path = %r{/apps/20/channels/mychannel}
288+
stub_request(:get, api_path)
289+
.to_return({ :status => 500, :body => "Server error" })
290+
.then
291+
.to_return({ :status => 200, :body => MultiJson.encode({ 'occupied' => false }) })
292+
293+
expect { @client.channel_info('mychannel') }.to raise_error(Pusher::Error)
294+
expect(@client.channel_info('mychannel')).to eq({ :occupied => false })
295+
end
296+
297+
it "should not reset the http client connection after a successful request" do
298+
api_path = %r{/apps/20/channels/mychannel}
299+
stub_request(:get, api_path).to_return({
300+
:status => 200,
301+
:body => MultiJson.encode({ 'occupied' => false })
302+
})
303+
304+
http_client = @client.sync_http_client
305+
expect(http_client).not_to receive(:reset_all)
306+
307+
@client.channel_info('mychannel')
308+
end
265309
end
266310

267311
describe '#channel_users' do

0 commit comments

Comments
 (0)