Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 2.1.0

* [FIXED] Reset HTTP client connection after unknown error responses to prevent subsequent requests from failing.

## 2.0.5

* [FIXED] Fix webhook content_type parsing for Rails 7.1+ using media_type
Expand Down
5 changes: 4 additions & 1 deletion lib/pusher/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,11 @@ def send_sync
end

body = response.body ? response.body.chomp : nil
code = response.code.to_i

return handle_response(response.code.to_i, body)
http.reset_all unless [200, 202, 400, 401, 403, 404, 407, 413].include?(code)

return handle_response(code, body)
end

def send_async
Expand Down
2 changes: 1 addition & 1 deletion lib/pusher/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Pusher
VERSION = '2.0.5'
VERSION = '2.1.0'
end
44 changes: 44 additions & 0 deletions spec/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,50 @@
:occupied => false,
})
end

it "should not reset the http client connection after a 400 error" do
api_path = %r{/apps/20/channels/mychannel}
stub_request(:get, api_path).to_return({ :status => 400, :body => "Bad request" })

http_client = @client.sync_http_client
expect(http_client).not_to receive(:reset_all)

expect { @client.channel_info('mychannel') }.to raise_error(Pusher::Error)
end

it "should reset the http client connection after an unknown error status" do
api_path = %r{/apps/20/channels/mychannel}
stub_request(:get, api_path).to_return({ :status => 500, :body => "Server error" })

http_client = @client.sync_http_client
expect(http_client).to receive(:reset_all).once

expect { @client.channel_info('mychannel') }.to raise_error(Pusher::Error)
end

it "should succeed on a subsequent request after an unknown error status" do
api_path = %r{/apps/20/channels/mychannel}
stub_request(:get, api_path)
.to_return({ :status => 500, :body => "Server error" })
.then
.to_return({ :status => 200, :body => MultiJson.encode({ 'occupied' => false }) })

expect { @client.channel_info('mychannel') }.to raise_error(Pusher::Error)
expect(@client.channel_info('mychannel')).to eq({ :occupied => false })
end

it "should not reset the http client connection after a successful request" do
api_path = %r{/apps/20/channels/mychannel}
stub_request(:get, api_path).to_return({
:status => 200,
:body => MultiJson.encode({ 'occupied' => false })
})

http_client = @client.sync_http_client
expect(http_client).not_to receive(:reset_all)

@client.channel_info('mychannel')
end
end

describe '#channel_users' do
Expand Down
Loading