Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7df47f8
commit 46fb1e6
Showing
3 changed files
with
46 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,7 +34,10 @@ def initialize(base_url, name, client = Pusher) | |
# | ||
def trigger_async(event_name, data, socket_id = nil) | ||
params = {} | ||
params[:socket_id] = socket_id if socket_id | ||
if socket_id | ||
validate_socket_id(socket_id) | ||
params[:socket_id] = socket_id | ||
This comment has been minimized.
Sorry, something went wrong. |
||
end | ||
@client.trigger_async(name, event_name, data, params) | ||
end | ||
|
||
|
@@ -60,7 +63,10 @@ def trigger_async(event_name, data, socket_id = nil) | |
# | ||
def trigger!(event_name, data, socket_id = nil) | ||
params = {} | ||
params[:socket_id] = socket_id if socket_id | ||
if socket_id | ||
validate_socket_id(socket_id) | ||
params[:socket_id] = socket_id | ||
This comment has been minimized.
Sorry, something went wrong.
zimbatm
Contributor
|
||
end | ||
@client.trigger(name, event_name, data, params) | ||
end | ||
|
||
|
@@ -115,9 +121,7 @@ def users | |
# @return [String] | ||
# | ||
def authentication_string(socket_id, custom_string = nil) | ||
if socket_id.nil? || socket_id.empty? | ||
raise Error, "Invalid socket_id #{socket_id}" | ||
end | ||
validate_socket_id(socket_id) | ||
|
||
unless custom_string.nil? || custom_string.kind_of?(String) | ||
raise Error, 'Custom argument must be a string' | ||
|
@@ -162,5 +166,13 @@ def authenticate(socket_id, custom_data = nil) | |
r[:channel_data] = custom_data if custom_data | ||
r | ||
end | ||
|
||
private | ||
|
||
def validate_socket_id(socket_id) | ||
unless socket_id && /\d+\.\d+$/.match(socket_id) | ||
raise Pusher::Error, "Invalid socket ID #{socket_id}" | ||
This comment has been minimized.
Sorry, something went wrong.
zimbatm
Contributor
|
||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -99,9 +99,9 @@ def authentication_string(*data) | |
end | ||
|
||
it "should return an authentication string given a socket id" do | ||
auth = @channel.authentication_string('socketid') | ||
auth = @channel.authentication_string('1.1') | ||
|
||
auth.should == '12345678900000001:827076f551e22451357939e4c7bb1200de29f921d5bf80b40d71668f9cd61c40' | ||
auth.should == '12345678900000001:02259dff9a2a3f71ea8ab29ac0c0c0ef7996c8f3fd3702be5533f30da7d7fed4' | ||
end | ||
|
||
it "should raise error if authentication is invalid" do | ||
|
@@ -112,17 +112,17 @@ def authentication_string(*data) | |
|
||
describe 'with extra string argument' do | ||
it 'should be a string or nil' do | ||
authentication_string('socketid', 123).should raise_error Pusher::Error | ||
authentication_string('socketid', {}).should raise_error Pusher::Error | ||
authentication_string('1.1', 123).should raise_error Pusher::Error | ||
authentication_string('1.1', {}).should raise_error Pusher::Error | ||
|
||
authentication_string('socketid', 'boom').should_not raise_error | ||
authentication_string('socketid', nil).should_not raise_error | ||
authentication_string('1.1', 'boom').should_not raise_error | ||
authentication_string('1.1', nil).should_not raise_error | ||
end | ||
|
||
it "should return an authentication string given a socket id and custom args" do | ||
auth = @channel.authentication_string('socketid', 'foobar') | ||
auth = @channel.authentication_string('1.1', 'foobar') | ||
|
||
auth.should == "12345678900000001:#{hmac(@client.secret, "socketid:test_channel:foobar")}" | ||
auth.should == "12345678900000001:#{hmac(@client.secret, "1.1:test_channel:foobar")}" | ||
end | ||
end | ||
end | ||
|
@@ -135,12 +135,26 @@ def authentication_string(*data) | |
it 'should return a hash with signature including custom data and data as json string' do | ||
MultiJson.stub(:encode).with(@custom_data).and_return 'a json string' | ||
|
||
response = @channel.authenticate('socketid', @custom_data) | ||
response = @channel.authenticate('1.1', @custom_data) | ||
|
||
response.should == { | ||
:auth => "12345678900000001:#{hmac(@client.secret, "socketid:test_channel:a json string")}", | ||
:auth => "12345678900000001:#{hmac(@client.secret, "1.1:test_channel:a json string")}", | ||
:channel_data => 'a json string' | ||
} | ||
end | ||
|
||
it 'should fail on invalid socket_ids' do | ||
lambda { | ||
@channel.authenticate('1.1:') | ||
}.should raise_error Pusher::Error | ||
|
||
lambda { | ||
@channel.authenticate('1.1foo', 'channel') | ||
}.should raise_error Pusher::Error | ||
|
||
lambda { | ||
@channel.authenticate('foo', 'channel') | ||
}.should raise_error Pusher::Error | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
danielwaterworth
Author
Contributor
|
||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
If
validate_socket_id
was returning the socket_id both of these lines could be merged