/
channel_spec.rb
195 lines (163 loc) · 6.03 KB
/
channel_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
require 'spec_helper'
describe Pusher::Channel do
before do
@client = Pusher::Client.new({
:app_id => '20',
:key => '12345678900000001',
:secret => '12345678900000001',
:host => 'api.pusherapp.com',
:port => 80,
})
@channel = @client['test_channel']
WebMock.reset!
WebMock.disable_net_connect!
end
let(:pusher_url_regexp) { %r{/apps/20/events} }
def stub_post(status, body = nil)
options = {:status => status}
options.merge!({:body => body}) if body
WebMock.stub_request(:post, pusher_url_regexp).to_return(options)
end
def stub_post_to_raise(e)
WebMock.stub_request(:post, pusher_url_regexp).to_raise(e)
end
describe 'trigger!' do
before :each do
stub_post 202
end
it 'should POST hashes by encoding as JSON in the request body' do
@channel.trigger!('new_event', {
:name => 'Pusher',
:last_name => 'App'
})
WebMock.should have_requested(:post, pusher_url_regexp).with { |req|
query_hash = req.uri.query_values
query_hash["auth_key"].should == @client.key
query_hash["auth_timestamp"].should_not be_nil
parsed = MultiJson.decode(req.body)
parsed["name"].should == 'new_event'
MultiJson.decode(parsed["data"]).should == {
"name" => 'Pusher',
"last_name" => 'App'
}
req.headers['Content-Type'].should == 'application/json'
}
end
[{:proxy => true}, {:proxy => false}].each do |c|
context "#{c[:proxy] ? 'with' : 'without'} http proxy" do
before do
@client.http_proxy = 'http://someuser:somepassword@proxy.host.com:8080' if c[:proxy]
end
it "should POST string data unmodified in request body" do
string = "foo\nbar\""
@channel.trigger!('new_event', string)
WebMock.should have_requested(:post, pusher_url_regexp).with { |req|
parsed = MultiJson.decode(req.body)
parsed['data'].should == "foo\nbar\""
}
end
end
end
end
describe '#trigger' do
it "should log failure if error raised in Net::HTTP call" do
stub_post_to_raise(Net::HTTPBadResponse)
Pusher.logger.should_receive(:error).with("Exception from WebMock (Net::HTTPBadResponse) (Pusher::HTTPError)")
Pusher.logger.should_receive(:debug) #backtrace
channel = Pusher::Channel.new(@client.url, 'test_channel', @client)
channel.trigger('new_event', 'Some data')
end
it "should log failure if Pusher returns an error response" do
stub_post 401
Pusher.logger.should_receive(:error).with("Pusher::AuthenticationError (Pusher::AuthenticationError)")
Pusher.logger.should_receive(:debug) #backtrace
channel = Pusher::Channel.new(@client.url, 'test_channel', @client)
channel.trigger('new_event', 'Some data')
end
end
describe "trigger_async" do
[{:proxy => true}, {:proxy => false}].each do |c|
context "#{c[:proxy] ? 'with' : 'without'} http proxy" do
before do
@client.http_proxy = 'http://someuser:somepassword@proxy.host.com:8080' if c[:proxy]
end
it "should by default POST to http api" do
EM.run {
stub_post 202
@channel.trigger_async('new_event', 'Some data').callback {
WebMock.should have_requested(:post, %r{http://api.pusherapp.com})
EM.stop
}
}
end
end
end
it "should return a deferrable which succeeds in success case" do
stub_post 202
EM.run {
d = @channel.trigger_async('new_event', 'Some data')
d.callback {
WebMock.should have_requested(:post, pusher_url_regexp)
EM.stop
}
d.errback {
fail
EM.stop
}
}
end
end
describe '#info' do
it "should call the Client#channel_info" do
@client.should_receive(:get).with("/channels/mychannel", anything)
@channel = @client['mychannel']
@channel.info
end
it "should assemble the requested attribes into the info option" do
@client.should_receive(:get).with(anything, {
:info => "user_count,connection_count"
})
@channel = @client['presence-foo']
@channel.info(%w{user_count connection_count})
end
end
describe "#authentication_string" do
def authentication_string(*data)
lambda { @channel.authentication_string(*data) }
end
it "should return an authentication string given a socket id" do
auth = @channel.authentication_string('socketid')
auth.should == '12345678900000001:827076f551e22451357939e4c7bb1200de29f921d5bf80b40d71668f9cd61c40'
end
it "should raise error if authentication is invalid" do
[nil, ''].each do |invalid|
authentication_string(invalid).should raise_error Pusher::Error
end
end
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('socketid', 'boom').should_not raise_error
authentication_string('socketid', 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.should == "12345678900000001:#{hmac(@client.secret, "socketid:test_channel:foobar")}"
end
end
end
describe '#authenticate' do
before :each do
@custom_data = {:uid => 123, :info => {:name => 'Foo'}}
end
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.should == {
:auth => "12345678900000001:#{hmac(@client.secret, "socketid:test_channel:a json string")}",
:channel_data => 'a json string'
}
end
end
end