garufa 0.0.1.beta.0 → 0.0.1.beta.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +1 -1
- data/lib/garufa/connection.rb +1 -1
- data/lib/garufa/message.rb +2 -2
- data/lib/garufa/subscriptions.rb +20 -10
- data/lib/garufa/version.rb +1 -1
- data/test/connection.rb +75 -0
- data/test/helper.rb +10 -0
- data/test/message.rb +41 -19
- metadata +3 -3
- data/bin/garufa.pid +0 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ed1fb1125ed40c4f3cc0a19dcae61dff59691948
|
4
|
+
data.tar.gz: 30e670e78a6ecdf3c678a8a32523bce7ad768ee9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c92cfa90de87f3256e6b9e7888438d6ae2c014458ea4fd630a9f44e516861dece549eb9a47ee08c3b9384d7f1b962a7734bf8a26b3bb90dacb031b123790c567
|
7
|
+
data.tar.gz: f294190202d0cc16e2a7bd95e9d34d213ffaa003bcc473c67800e166ae6a9d409d91331ead19d5c02e77889e5f891250266520e2ece44291999897282f8db2ba
|
data/README.md
CHANGED
@@ -5,7 +5,7 @@ Garufa
|
|
5
5
|
|
6
6
|
An open source server implementation of the [Pusher][pusher] protocol.
|
7
7
|
|
8
|
-
**IMPORTANT:** Garufa is currently in
|
8
|
+
**IMPORTANT:** Garufa is currently in beta version, which means it is not
|
9
9
|
production ready, but you are free to use it and test it. Any feedback is
|
10
10
|
welcome.
|
11
11
|
|
data/lib/garufa/connection.rb
CHANGED
@@ -84,7 +84,7 @@ module Garufa
|
|
84
84
|
|
85
85
|
if subscription.success?
|
86
86
|
@subscriptions[subscription.channel] = subscription
|
87
|
-
send_subscription_succeeded(subscription)
|
87
|
+
send_subscription_succeeded(subscription)
|
88
88
|
else
|
89
89
|
error(subscription.error.code, subscription.error.message)
|
90
90
|
end
|
data/lib/garufa/message.rb
CHANGED
@@ -32,11 +32,11 @@ module Garufa
|
|
32
32
|
end
|
33
33
|
|
34
34
|
def self.subscription_succeeded(channel)
|
35
|
-
new(event: 'pusher_internal:subscription_succeeded', channel: channel)
|
35
|
+
new(event: 'pusher_internal:subscription_succeeded', channel: channel, data: {})
|
36
36
|
end
|
37
37
|
|
38
38
|
def self.pong
|
39
|
-
new(event: 'pusher:pong')
|
39
|
+
new(event: 'pusher:pong', data: {})
|
40
40
|
end
|
41
41
|
|
42
42
|
def self.error(code, message)
|
data/lib/garufa/subscriptions.rb
CHANGED
@@ -52,10 +52,12 @@ module Garufa
|
|
52
52
|
|
53
53
|
def subscribe
|
54
54
|
case true
|
55
|
-
when
|
56
|
-
set_error(nil, 'Invalid
|
57
|
-
when
|
55
|
+
when !valid_channel?
|
56
|
+
set_error(nil, 'Invalid channel')
|
57
|
+
when !public_channel? && !valid_signature?
|
58
58
|
set_error(nil, 'Invalid signature')
|
59
|
+
when !public_channel? && !valid_app_key?
|
60
|
+
set_error(nil, 'Invalid key')
|
59
61
|
when already_subscribed?
|
60
62
|
set_error(nil, "Already subscribed to channel: #{channel}")
|
61
63
|
else
|
@@ -97,17 +99,17 @@ module Garufa
|
|
97
99
|
|
98
100
|
private
|
99
101
|
|
100
|
-
def
|
101
|
-
|
102
|
+
def valid_channel?
|
103
|
+
channel.is_a?(String) && !channel.empty?
|
102
104
|
end
|
103
105
|
|
104
|
-
def
|
105
|
-
|
106
|
+
def valid_app_key?
|
107
|
+
app_key && app_key == Config[:app_key]
|
108
|
+
end
|
106
109
|
|
107
|
-
|
110
|
+
def valid_signature?
|
108
111
|
string_to_sign = [@connection.socket_id, channel].compact.join(':')
|
109
|
-
|
110
|
-
app_key != Config[:app_key] || token(string_to_sign) != signature
|
112
|
+
token(string_to_sign) == signature
|
111
113
|
end
|
112
114
|
|
113
115
|
def token(string_to_sign)
|
@@ -118,6 +120,14 @@ module Garufa
|
|
118
120
|
def already_subscribed?
|
119
121
|
Subscriptions.include? self
|
120
122
|
end
|
123
|
+
|
124
|
+
def app_key
|
125
|
+
@data['auth'].split(':').first if @data['auth']
|
126
|
+
end
|
127
|
+
|
128
|
+
def signature
|
129
|
+
@data['auth'].split(':').last if @data['auth']
|
130
|
+
end
|
121
131
|
end
|
122
132
|
|
123
133
|
class SubscriptionError < Struct.new(:code, :message)
|
data/lib/garufa/version.rb
CHANGED
data/test/connection.rb
ADDED
@@ -0,0 +1,75 @@
|
|
1
|
+
require File.expand_path("helper", File.dirname(__FILE__))
|
2
|
+
require 'logger'
|
3
|
+
|
4
|
+
module Garufa
|
5
|
+
describe Connection do
|
6
|
+
|
7
|
+
before do
|
8
|
+
@socket = MiniTest::Mock.new
|
9
|
+
@connection = Connection.new(@socket, Logger.new('/dev/null'))
|
10
|
+
end
|
11
|
+
|
12
|
+
describe '.handle_incomming_data' do
|
13
|
+
|
14
|
+
describe 'pusher:subscribe' do
|
15
|
+
|
16
|
+
let(:data) { { event: 'pusher:subscribe', data: { channel: 'ch1' } } }
|
17
|
+
|
18
|
+
it 'should add a new Subscription to Subscriptions' do
|
19
|
+
@socket.expect :send, true, [String]
|
20
|
+
@connection.handle_incomming_data data.to_json
|
21
|
+
Subscriptions.all['ch1'].first.class.must_equal Subscription
|
22
|
+
Subscriptions.all['ch1'].count.must_equal 1
|
23
|
+
end
|
24
|
+
|
25
|
+
describe 'public channels' do
|
26
|
+
it 'should response with subscription_succeeded' do
|
27
|
+
message = Message.subscription_succeeded(data[:data][:channel])
|
28
|
+
@socket.expect :send, true, [message.to_json]
|
29
|
+
@connection.handle_incomming_data data.to_json
|
30
|
+
@socket.verify
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
describe 'private channels' do
|
35
|
+
|
36
|
+
let(:channel) { 'private-ch1' }
|
37
|
+
let(:app_key) { Config[:app_key] }
|
38
|
+
let(:signature) { sign(@connection.socket_id, channel) }
|
39
|
+
let(:data) { { event: 'pusher:subscribe', data: { channel: channel, auth: "#{app_key}:#{signature}" } } }
|
40
|
+
|
41
|
+
it 'should response with subscription_succeeded' do
|
42
|
+
message = Message.subscription_succeeded(channel)
|
43
|
+
@socket.expect :send, true, [message.to_json]
|
44
|
+
@connection.handle_incomming_data data.to_json
|
45
|
+
@socket.verify
|
46
|
+
end
|
47
|
+
|
48
|
+
it 'should response with invalid channel' do
|
49
|
+
data[:data][:channel] = ''
|
50
|
+
message = Message.error(nil, 'Invalid channel')
|
51
|
+
@socket.expect :send, true, [message.to_json]
|
52
|
+
@connection.handle_incomming_data data.to_json
|
53
|
+
@socket.verify
|
54
|
+
end
|
55
|
+
|
56
|
+
it 'should response with invalid signature' do
|
57
|
+
data[:data][:auth] = "#{app_key}:invalid-signature"
|
58
|
+
message = Message.error(nil, 'Invalid signature')
|
59
|
+
@socket.expect :send, true, [message.to_json]
|
60
|
+
@connection.handle_incomming_data data.to_json
|
61
|
+
@socket.verify
|
62
|
+
end
|
63
|
+
|
64
|
+
it 'should response with invalid key' do
|
65
|
+
data[:data][:auth] = "invalid-app-key:#{signature}"
|
66
|
+
message = Message.error(nil, 'Invalid key')
|
67
|
+
@socket.expect :send, true, [message.to_json]
|
68
|
+
@connection.handle_incomming_data data.to_json
|
69
|
+
@socket.verify
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
data/test/helper.rb
CHANGED
@@ -2,3 +2,13 @@ $:.unshift(File.expand_path("../lib", File.dirname(__FILE__)))
|
|
2
2
|
|
3
3
|
require "garufa"
|
4
4
|
require 'minitest/autorun'
|
5
|
+
require 'minitest/mock'
|
6
|
+
|
7
|
+
Garufa::Config[:app_key] = '123123-123123'
|
8
|
+
Garufa::Config[:secret] = '456456-456456'
|
9
|
+
|
10
|
+
def sign(socket_id, custom_string = nil)
|
11
|
+
string_to_sign = [socket_id, custom_string].join(':')
|
12
|
+
digest = OpenSSL::Digest::SHA256.new
|
13
|
+
return OpenSSL::HMAC.hexdigest(digest, Garufa::Config[:secret], string_to_sign)
|
14
|
+
end
|
data/test/message.rb
CHANGED
@@ -3,9 +3,11 @@ require File.expand_path("helper", File.dirname(__FILE__))
|
|
3
3
|
module Garufa
|
4
4
|
describe Message do
|
5
5
|
describe '.connection_established' do
|
6
|
+
|
7
|
+
let(:socket_id) { '123123-123123' }
|
8
|
+
|
6
9
|
before do
|
7
|
-
@
|
8
|
-
@message = Message.connection_established @socket_id
|
10
|
+
@message = Message.connection_established socket_id
|
9
11
|
end
|
10
12
|
|
11
13
|
it 'should response with data attribute as string' do
|
@@ -18,36 +20,40 @@ module Garufa
|
|
18
20
|
|
19
21
|
it 'should response with expected data' do
|
20
22
|
data = JSON.parse(@message.data)
|
21
|
-
data["socket_id"].must_equal
|
23
|
+
data["socket_id"].must_equal socket_id
|
22
24
|
data["activity_timeout"].must_equal 120
|
23
25
|
end
|
24
26
|
end
|
25
27
|
|
26
28
|
describe '.channel_event' do
|
29
|
+
|
30
|
+
let(:channel) { 'channel-123' }
|
31
|
+
let(:event) { 'my-event' }
|
32
|
+
let(:data) { { itemId: 1, value: 'Sample Item' } }
|
33
|
+
|
27
34
|
before do
|
28
|
-
@
|
29
|
-
@event = 'my-event'
|
30
|
-
@data = { itemId: 1, value: 'Sample Item' }
|
31
|
-
@message = Message.channel_event @channel, @event, @data
|
35
|
+
@message = Message.channel_event channel, event, data
|
32
36
|
end
|
33
37
|
|
34
38
|
it 'should response with expected event' do
|
35
|
-
@message.event.must_equal
|
39
|
+
@message.event.must_equal event
|
36
40
|
end
|
37
41
|
|
38
42
|
it 'should response with expected data' do
|
39
|
-
@message.data.must_equal
|
43
|
+
@message.data.must_equal data
|
40
44
|
end
|
41
45
|
|
42
46
|
it 'should response with expected channel' do
|
43
|
-
@message.channel.must_equal
|
47
|
+
@message.channel.must_equal channel
|
44
48
|
end
|
45
49
|
end
|
46
50
|
|
47
51
|
describe '.subscription_succeeded' do
|
52
|
+
|
53
|
+
let(:channel) { 'channel-123' }
|
54
|
+
|
48
55
|
before do
|
49
|
-
@
|
50
|
-
@message = Message.subscription_succeeded @channel
|
56
|
+
@message = Message.subscription_succeeded channel
|
51
57
|
end
|
52
58
|
|
53
59
|
it 'should response with expected event' do
|
@@ -55,20 +61,36 @@ module Garufa
|
|
55
61
|
end
|
56
62
|
|
57
63
|
it 'should response with expected channel' do
|
58
|
-
@message.channel.must_equal
|
64
|
+
@message.channel.must_equal channel
|
65
|
+
end
|
66
|
+
|
67
|
+
it 'should response with empty data' do
|
68
|
+
@message.data.must_equal({})
|
59
69
|
end
|
60
70
|
end
|
61
71
|
|
62
72
|
describe '.pong' do
|
63
|
-
|
64
|
-
|
73
|
+
|
74
|
+
before do
|
75
|
+
@message = Message.pong
|
76
|
+
end
|
77
|
+
|
78
|
+
it 'should response with expected event' do
|
79
|
+
@message.event.must_equal 'pusher:pong'
|
80
|
+
end
|
81
|
+
|
82
|
+
it 'should response with empty data' do
|
83
|
+
@message.data.must_equal({})
|
65
84
|
end
|
66
85
|
end
|
67
86
|
|
68
87
|
describe '.error' do
|
88
|
+
|
89
|
+
let(:code) { 4000 }
|
90
|
+
let(:error_message) { 'There was an error!' }
|
91
|
+
|
69
92
|
before do
|
70
|
-
@
|
71
|
-
@message = Message.error @code, @error_message
|
93
|
+
@message = Message.error code, error_message
|
72
94
|
end
|
73
95
|
|
74
96
|
it 'should response with expected event' do
|
@@ -77,8 +99,8 @@ module Garufa
|
|
77
99
|
|
78
100
|
it 'should response with expected data' do
|
79
101
|
data = JSON.parse(@message.data)
|
80
|
-
data["code"].must_equal
|
81
|
-
data["message"].must_equal
|
102
|
+
data["code"].must_equal code
|
103
|
+
data["message"].must_equal error_message
|
82
104
|
end
|
83
105
|
end
|
84
106
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: garufa
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.1.beta.
|
4
|
+
version: 0.0.1.beta.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Juan Manuel Cuello
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-03-16 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: goliath
|
@@ -103,11 +103,11 @@ files:
|
|
103
103
|
- lib/garufa/garufa_app.rb
|
104
104
|
- lib/garufa/connection.rb
|
105
105
|
- lib/garufa.rb
|
106
|
-
- bin/garufa.pid
|
107
106
|
- bin/garufa
|
108
107
|
- garufa.gemspec
|
109
108
|
- test/helper.rb
|
110
109
|
- test/message.rb
|
110
|
+
- test/connection.rb
|
111
111
|
homepage: http://github.com/Juanmcuello/garufa
|
112
112
|
licenses: []
|
113
113
|
metadata: {}
|
data/bin/garufa.pid
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
16262
|