slack-ruby-client-bhe 0.5.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +17 -0
- data/.gitignore +4 -0
- data/.gitmodules +3 -0
- data/.rspec +2 -0
- data/.rubocop.yml +6 -0
- data/.rubocop_todo.yml +78 -0
- data/.travis.yml +26 -0
- data/CHANGELOG.md +79 -0
- data/CONTRIBUTING.md +157 -0
- data/Gemfile +5 -0
- data/LICENSE.md +22 -0
- data/README.md +385 -0
- data/RELEASING.md +69 -0
- data/Rakefile +19 -0
- data/UPGRADING.md +26 -0
- data/bin/commands.rb +21 -0
- data/bin/commands/api.rb +14 -0
- data/bin/commands/auth.rb +12 -0
- data/bin/commands/channels.rb +149 -0
- data/bin/commands/chat.rb +47 -0
- data/bin/commands/dnd.rb +47 -0
- data/bin/commands/emoji.rb +12 -0
- data/bin/commands/files.rb +72 -0
- data/bin/commands/groups.rb +167 -0
- data/bin/commands/im.rb +53 -0
- data/bin/commands/mpim.rb +53 -0
- data/bin/commands/oauth.rb +16 -0
- data/bin/commands/pins.rb +37 -0
- data/bin/commands/reactions.rb +53 -0
- data/bin/commands/rtm.rb +15 -0
- data/bin/commands/search.rb +40 -0
- data/bin/commands/stars.rb +37 -0
- data/bin/commands/team.rb +32 -0
- data/bin/commands/usergroups.rb +73 -0
- data/bin/commands/users.rb +57 -0
- data/bin/slack +50 -0
- data/examples/hi_real_time/Gemfile +5 -0
- data/examples/hi_real_time/hi.gif +0 -0
- data/examples/hi_real_time/hi.rb +32 -0
- data/examples/hi_real_time_and_web/Gemfile +5 -0
- data/examples/hi_real_time_and_web/hi.gif +0 -0
- data/examples/hi_real_time_and_web/hi.rb +24 -0
- data/examples/hi_real_time_async/Gemfile +5 -0
- data/examples/hi_real_time_async/hi.rb +29 -0
- data/examples/hi_web/Gemfile +3 -0
- data/examples/hi_web/hi.gif +0 -0
- data/examples/hi_web/hi.rb +12 -0
- data/examples/new_ticket/Gemfile +3 -0
- data/examples/new_ticket/new_ticket.rb +25 -0
- data/lib/slack-ruby-client.rb +29 -0
- data/lib/slack.rb +1 -0
- data/lib/slack/config.rb +21 -0
- data/lib/slack/messages/formatting.rb +30 -0
- data/lib/slack/real_time/api/message.rb +20 -0
- data/lib/slack/real_time/api/message_id.rb +14 -0
- data/lib/slack/real_time/api/ping.rb +16 -0
- data/lib/slack/real_time/api/typing.rb +17 -0
- data/lib/slack/real_time/client.rb +152 -0
- data/lib/slack/real_time/concurrency.rb +8 -0
- data/lib/slack/real_time/concurrency/celluloid.rb +93 -0
- data/lib/slack/real_time/concurrency/eventmachine.rb +39 -0
- data/lib/slack/real_time/config.rb +55 -0
- data/lib/slack/real_time/socket.rb +81 -0
- data/lib/slack/version.rb +3 -0
- data/lib/slack/web/api/endpoints.rb +53 -0
- data/lib/slack/web/api/endpoints/api.rb +24 -0
- data/lib/slack/web/api/endpoints/auth.rb +20 -0
- data/lib/slack/web/api/endpoints/channels.rb +220 -0
- data/lib/slack/web/api/endpoints/chat.rb +91 -0
- data/lib/slack/web/api/endpoints/dnd.rb +64 -0
- data/lib/slack/web/api/endpoints/emoji.rb +20 -0
- data/lib/slack/web/api/endpoints/files.rb +108 -0
- data/lib/slack/web/api/endpoints/groups.rb +247 -0
- data/lib/slack/web/api/endpoints/im.rb +85 -0
- data/lib/slack/web/api/endpoints/mpim.rb +84 -0
- data/lib/slack/web/api/endpoints/oauth.rb +32 -0
- data/lib/slack/web/api/endpoints/pins.rb +64 -0
- data/lib/slack/web/api/endpoints/presence.rb +23 -0
- data/lib/slack/web/api/endpoints/reactions.rb +89 -0
- data/lib/slack/web/api/endpoints/rtm.rb +27 -0
- data/lib/slack/web/api/endpoints/search.rb +65 -0
- data/lib/slack/web/api/endpoints/stars.rb +61 -0
- data/lib/slack/web/api/endpoints/team.rb +47 -0
- data/lib/slack/web/api/endpoints/usergroups.rb +113 -0
- data/lib/slack/web/api/endpoints/users.rb +73 -0
- data/lib/slack/web/api/error.rb +14 -0
- data/lib/slack/web/api/mixins.rb +3 -0
- data/lib/slack/web/api/mixins/channels.id.json +20 -0
- data/lib/slack/web/api/mixins/channels.id.rb +26 -0
- data/lib/slack/web/api/mixins/groups.id.json +20 -0
- data/lib/slack/web/api/mixins/groups.id.rb +26 -0
- data/lib/slack/web/api/mixins/users.id.json +20 -0
- data/lib/slack/web/api/mixins/users.id.rb +26 -0
- data/lib/slack/web/api/patches/chat.1.text-attachments-required.patch +13 -0
- data/lib/slack/web/api/patches/chat.2.attachments-json.patch +17 -0
- data/lib/slack/web/api/schema/group.json +14 -0
- data/lib/slack/web/api/schema/method.json +45 -0
- data/lib/slack/web/api/tasks/generate.rake +61 -0
- data/lib/slack/web/api/templates/command.erb +34 -0
- data/lib/slack/web/api/templates/commands.erb +5 -0
- data/lib/slack/web/api/templates/endpoints.erb +21 -0
- data/lib/slack/web/api/templates/method.erb +49 -0
- data/lib/slack/web/client.rb +28 -0
- data/lib/slack/web/config.rb +41 -0
- data/lib/slack/web/faraday/connection.rb +29 -0
- data/lib/slack/web/faraday/request.rb +39 -0
- data/lib/slack/web/faraday/response/raise_error.rb +15 -0
- data/lib/slack_ruby_client.rb +1 -0
- data/screenshots/register-bot.png +0 -0
- data/slack-ruby-client.gemspec +31 -0
- data/slack.png +0 -0
- data/spec/fixtures/slack/web/429_error.yml +83 -0
- data/spec/fixtures/slack/web/auth_test_error.yml +48 -0
- data/spec/fixtures/slack/web/auth_test_success.yml +57 -0
- data/spec/fixtures/slack/web/channels_info.yml +46 -0
- data/spec/fixtures/slack/web/groups_info.yml +43 -0
- data/spec/fixtures/slack/web/rtm_start.yml +73 -0
- data/spec/fixtures/slack/web/users_info.yml +130 -0
- data/spec/fixtures/slack/web/users_list.yml +72 -0
- data/spec/integration/integration_spec.rb +107 -0
- data/spec/slack/config_spec.rb +14 -0
- data/spec/slack/messages/formatting_spec.rb +43 -0
- data/spec/slack/real_time/api/message_spec.rb +15 -0
- data/spec/slack/real_time/api/ping_spec.rb +15 -0
- data/spec/slack/real_time/api/typing_spec.rb +15 -0
- data/spec/slack/real_time/client_spec.rb +198 -0
- data/spec/slack/real_time/concurrency/celluloid_spec.rb +58 -0
- data/spec/slack/real_time/concurrency/eventmachine_spec.rb +49 -0
- data/spec/slack/slack_spec.rb +58 -0
- data/spec/slack/version_spec.rb +7 -0
- data/spec/slack/web/api/endpoints/auth_spec.rb +20 -0
- data/spec/slack/web/api/endpoints/channels_spec.rb +11 -0
- data/spec/slack/web/api/endpoints/chat_spec.rb +33 -0
- data/spec/slack/web/api/endpoints/groups_spec.rb +11 -0
- data/spec/slack/web/api/endpoints/users_spec.rb +17 -0
- data/spec/slack/web/api/error_spec.rb +14 -0
- data/spec/slack/web/api/mixins/channels_spec.rb +31 -0
- data/spec/slack/web/api/mixins/groups_spec.rb +31 -0
- data/spec/slack/web/api/mixins/users_spec.rb +31 -0
- data/spec/slack/web/client_spec.rb +134 -0
- data/spec/spec_helper.rb +14 -0
- data/spec/support/queue_with_timeout.rb +34 -0
- data/spec/support/real_time/concurrency/mock.rb +31 -0
- data/spec/support/real_time/connected_client.rb +16 -0
- data/spec/support/token.rb +10 -0
- data/spec/support/vcr.rb +8 -0
- metadata +392 -0
@@ -0,0 +1,107 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
RSpec.describe 'integration test', skip: !ENV['SLACK_API_TOKEN'] && 'missing SLACK_API_TOKEN' do
|
4
|
+
around do |ex|
|
5
|
+
WebMock.allow_net_connect!
|
6
|
+
VCR.turned_off { ex.run }
|
7
|
+
WebMock.disable_net_connect!
|
8
|
+
end
|
9
|
+
|
10
|
+
before do
|
11
|
+
Thread.abort_on_exception = true
|
12
|
+
end
|
13
|
+
|
14
|
+
let(:logger) { Logger.new(STDOUT) }
|
15
|
+
|
16
|
+
let(:queue) { QueueWithTimeout.new }
|
17
|
+
|
18
|
+
let(:client) { Slack::RealTime::Client.new(token: ENV['SLACK_API_TOKEN']) }
|
19
|
+
|
20
|
+
let(:connection) do
|
21
|
+
# starts the client and pushes an item on a queue when connected
|
22
|
+
client.start_async do |driver|
|
23
|
+
driver.on :open do |data|
|
24
|
+
logger.debug "connection.on :open, data=#{data}"
|
25
|
+
queue.push nil
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
before do
|
31
|
+
client.on :hello do
|
32
|
+
logger.info "Successfully connected, welcome '#{client.self['name']}' to the '#{client.team['name']}' team at https://#{client.team['domain']}.slack.com."
|
33
|
+
end
|
34
|
+
|
35
|
+
client.on :close do
|
36
|
+
# pushes another item to the queue when disconnected
|
37
|
+
queue.push nil
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def start_server
|
42
|
+
dt = rand(5) + 1
|
43
|
+
logger.debug "#start_server, waiting #{dt} second(s)"
|
44
|
+
sleep dt # prevent Slack 429 rate limit errors
|
45
|
+
# start server and wait for on :open
|
46
|
+
c = connection
|
47
|
+
logger.debug "connection is #{c}"
|
48
|
+
queue.pop_with_timeout(5)
|
49
|
+
end
|
50
|
+
|
51
|
+
def wait_for_server
|
52
|
+
logger.debug '#wait_for_server'
|
53
|
+
queue.pop_with_timeout(5)
|
54
|
+
logger.debug '#wait_for_server, joined'
|
55
|
+
end
|
56
|
+
|
57
|
+
def stop_server
|
58
|
+
logger.debug '#stop_server'
|
59
|
+
client.stop!
|
60
|
+
logger.debug '#stop_server, stopped'
|
61
|
+
end
|
62
|
+
|
63
|
+
after do
|
64
|
+
wait_for_server
|
65
|
+
end
|
66
|
+
|
67
|
+
context 'client connected' do
|
68
|
+
before do
|
69
|
+
start_server
|
70
|
+
end
|
71
|
+
|
72
|
+
let(:channel) { "@#{client.self['name']}" }
|
73
|
+
|
74
|
+
it 'responds to message' do
|
75
|
+
message = SecureRandom.hex
|
76
|
+
|
77
|
+
client.on :message do |data|
|
78
|
+
logger.debug data
|
79
|
+
# concurrent execution of tests causes messages to arrive in any order
|
80
|
+
next unless data['text'] == message
|
81
|
+
expect(data['text']).to eq message
|
82
|
+
expect(data['subtype']).to eq 'bot_message'
|
83
|
+
logger.debug 'client.stop!'
|
84
|
+
expect(client.started?).to be true
|
85
|
+
client.stop!
|
86
|
+
end
|
87
|
+
|
88
|
+
logger.debug "chat_postMessage, channel=#{channel}, message=#{message}"
|
89
|
+
client.web_client.chat_postMessage channel: channel, text: message
|
90
|
+
end
|
91
|
+
|
92
|
+
it 'sends message' do
|
93
|
+
client.message(channel: channel, text: 'Hello world!')
|
94
|
+
client.stop!
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
it 'gets hello' do
|
99
|
+
client.on :hello do |data|
|
100
|
+
logger.debug "client.on :hello, data=#{data}"
|
101
|
+
expect(client.started?).to be true
|
102
|
+
client.stop!
|
103
|
+
end
|
104
|
+
|
105
|
+
start_server
|
106
|
+
end
|
107
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require 'spec_helper'
|
3
|
+
|
4
|
+
describe Slack::Messages::Formatting do
|
5
|
+
subject do
|
6
|
+
Slack::Messages::Formatting
|
7
|
+
end
|
8
|
+
context '#unescape' do
|
9
|
+
it 'plain text' do
|
10
|
+
expect(subject.unescape('plain text')).to eq 'plain text'
|
11
|
+
end
|
12
|
+
it 'decodes an HTML-encoded message' do
|
13
|
+
expect(subject.unescape('Hello & <world>')).to eq 'Hello & <world>'
|
14
|
+
end
|
15
|
+
it 'unescapes a user reference' do
|
16
|
+
expect(subject.unescape('Hey <@U024BE7LH|bob>, did you see my file?')).to eq 'Hey @bob, did you see my file?'
|
17
|
+
end
|
18
|
+
it 'unescapes a user reference without a name' do
|
19
|
+
expect(subject.unescape('<@U02BEFY4U> ^^^')).to eq '@U02BEFY4U ^^^'
|
20
|
+
end
|
21
|
+
it 'unescapes a URL without text' do
|
22
|
+
expect(subject.unescape('This message contains a URL <http://foo.com/>')).to eq 'This message contains a URL http://foo.com/'
|
23
|
+
end
|
24
|
+
it 'unescapes a URL with text' do
|
25
|
+
expect(subject.unescape('So does this one: <http://www.foo.com|www.foo.com>')).to eq 'So does this one: www.foo.com'
|
26
|
+
end
|
27
|
+
it 'removes mailto' do
|
28
|
+
expect(subject.unescape('<mailto:bob@example.com|Bob>')).to eq 'Bob'
|
29
|
+
end
|
30
|
+
it 'unlinkifies references' do
|
31
|
+
expect(subject.unescape('Hello <@U123|bob>, say hi to <!everyone> in <#C1234|general>')).to eq 'Hello @bob, say hi to @everyone in #general'
|
32
|
+
end
|
33
|
+
it 'can handle a lone >' do
|
34
|
+
expect(subject.unescape('Hello <@U123|bob> > file.txt')).to eq 'Hello @bob > file.txt'
|
35
|
+
end
|
36
|
+
it 'unescapes a double smart quote' do
|
37
|
+
expect(subject.unescape('“hello”')).to eq '"hello"'
|
38
|
+
end
|
39
|
+
it 'unescapes a single smart quote' do
|
40
|
+
expect(subject.unescape('‘hello’')).to eq "'hello'"
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
RSpec.describe Slack::RealTime::Client, vcr: { cassette_name: 'web/rtm_start' } do
|
4
|
+
include_context 'connected client'
|
5
|
+
|
6
|
+
describe '#message' do
|
7
|
+
before do
|
8
|
+
allow(client).to receive(:next_id).and_return(42)
|
9
|
+
end
|
10
|
+
it 'sends message' do
|
11
|
+
expect(socket).to receive(:send_data).with({ type: 'message', id: 42, text: 'hello world', channel: 'channel' }.to_json)
|
12
|
+
client.message(text: 'hello world', channel: 'channel')
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
RSpec.describe Slack::RealTime::Client, vcr: { cassette_name: 'web/rtm_start' } do
|
4
|
+
include_context 'connected client'
|
5
|
+
|
6
|
+
describe '#ping' do
|
7
|
+
before do
|
8
|
+
allow(client).to receive(:next_id).and_return(42)
|
9
|
+
end
|
10
|
+
it 'sends message' do
|
11
|
+
expect(socket).to receive(:send_data).with({ type: 'ping', id: 42 }.to_json)
|
12
|
+
client.ping
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
RSpec.describe Slack::RealTime::Client, vcr: { cassette_name: 'web/rtm_start' } do
|
4
|
+
include_context 'connected client'
|
5
|
+
|
6
|
+
describe '#typing' do
|
7
|
+
before do
|
8
|
+
allow(client).to receive(:next_id).and_return(42)
|
9
|
+
end
|
10
|
+
it 'sends a typing indicator' do
|
11
|
+
expect(socket).to receive(:send_data).with({ type: 'typing', id: 42, channel: 'channel' }.to_json)
|
12
|
+
client.typing(channel: 'channel')
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,198 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
RSpec.describe Slack::RealTime::Client, vcr: { cassette_name: 'web/rtm_start' } do
|
4
|
+
let(:ws) { double(Slack::RealTime::Concurrency::Mock::WebSocket, on: true) }
|
5
|
+
let(:url) { 'wss://ms173.slack-msgs.com/websocket/lqcUiAvrKTP-uuid=' }
|
6
|
+
before do
|
7
|
+
@token = ENV.delete('SLACK_API_TOKEN')
|
8
|
+
Slack::Config.reset
|
9
|
+
Slack::RealTime::Config.reset
|
10
|
+
Slack::RealTime.configure do |config|
|
11
|
+
config.concurrency = Slack::RealTime::Concurrency::Mock
|
12
|
+
end
|
13
|
+
end
|
14
|
+
after do
|
15
|
+
ENV['SLACK_API_TOKEN'] = @token if @token
|
16
|
+
end
|
17
|
+
context 'token' do
|
18
|
+
before do
|
19
|
+
Slack.configure do |config|
|
20
|
+
config.token = 'global default'
|
21
|
+
end
|
22
|
+
end
|
23
|
+
it 'defaults token to global default' do
|
24
|
+
client = Slack::RealTime::Client.new
|
25
|
+
expect(client.token).to eq 'global default'
|
26
|
+
expect(client.web_client.token).to eq 'global default'
|
27
|
+
end
|
28
|
+
context 'with real time config' do
|
29
|
+
before do
|
30
|
+
Slack::RealTime::Client.configure do |config|
|
31
|
+
config.token = 'custom real time token'
|
32
|
+
end
|
33
|
+
end
|
34
|
+
it 'overrides token to real time config' do
|
35
|
+
client = Slack::RealTime::Client.new
|
36
|
+
expect(client.token).to eq 'custom real time token'
|
37
|
+
expect(client.web_client.token).to eq 'custom real time token'
|
38
|
+
end
|
39
|
+
it 'overrides token to specific token' do
|
40
|
+
client = Slack::RealTime::Client.new(token: 'local token')
|
41
|
+
expect(client.token).to eq 'local token'
|
42
|
+
expect(client.web_client.token).to eq 'local token'
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
context 'client' do
|
47
|
+
let(:client) { Slack::RealTime::Client.new }
|
48
|
+
context 'started' do
|
49
|
+
describe '#start!' do
|
50
|
+
let(:socket) { double(Slack::RealTime::Socket, connected?: true) }
|
51
|
+
before do
|
52
|
+
allow(Slack::RealTime::Socket).to receive(:new).with(url, ping: 30).and_return(socket)
|
53
|
+
allow(socket).to receive(:connect!)
|
54
|
+
allow(socket).to receive(:start_sync).and_yield
|
55
|
+
client.start!
|
56
|
+
end
|
57
|
+
context 'properties provided upon connection' do
|
58
|
+
it 'sets url' do
|
59
|
+
expect(client.url).to eq url
|
60
|
+
end
|
61
|
+
it 'sets team' do
|
62
|
+
expect(client.team['domain']).to eq 'dblockdotorg'
|
63
|
+
end
|
64
|
+
it 'sets self' do
|
65
|
+
expect(client.self['id']).to eq 'U07518DTL'
|
66
|
+
end
|
67
|
+
it 'sets users' do
|
68
|
+
expect(client.users.count).to eq 7
|
69
|
+
expect(client.users.first['id']).to eq 'U07KECJ77'
|
70
|
+
end
|
71
|
+
it 'sets channels' do
|
72
|
+
expect(client.channels.count).to eq 8
|
73
|
+
expect(client.channels.first['name']).to eq 'demo'
|
74
|
+
end
|
75
|
+
it 'sets ims' do
|
76
|
+
expect(client.ims.count).to eq 2
|
77
|
+
expect(client.ims.first['user']).to eq 'USLACKBOT'
|
78
|
+
end
|
79
|
+
it 'sets bots' do
|
80
|
+
expect(client.bots.count).to eq 5
|
81
|
+
expect(client.bots.first['name']).to eq 'bot'
|
82
|
+
end
|
83
|
+
it 'sets groups' do
|
84
|
+
expect(client.groups.count).to eq 0
|
85
|
+
end
|
86
|
+
end
|
87
|
+
it 'uses web client to fetch url' do
|
88
|
+
expect(client.web_client).to be_a Slack::Web::Client
|
89
|
+
end
|
90
|
+
it 'remembers socket' do
|
91
|
+
expect(client.instance_variable_get('@socket')).to eq socket
|
92
|
+
end
|
93
|
+
it 'cannot be invoked twice' do
|
94
|
+
expect do
|
95
|
+
client.start!
|
96
|
+
end.to raise_error Slack::RealTime::Client::ClientAlreadyStartedError
|
97
|
+
end
|
98
|
+
describe '#stop!' do
|
99
|
+
before do
|
100
|
+
expect(socket).to receive(:disconnect!)
|
101
|
+
client.stop!
|
102
|
+
end
|
103
|
+
it 'cannot be invoked twice' do
|
104
|
+
client.instance_variable_set('@socket', nil) # caused by a :close callback
|
105
|
+
expect do
|
106
|
+
client.stop!
|
107
|
+
end.to raise_error Slack::RealTime::Client::ClientNotStartedError
|
108
|
+
end
|
109
|
+
end
|
110
|
+
describe '#next_id' do
|
111
|
+
it 'increments' do
|
112
|
+
previous_id = client.send(:next_id)
|
113
|
+
expect(client.send(:next_id)).to eq previous_id + 1
|
114
|
+
end
|
115
|
+
end
|
116
|
+
end
|
117
|
+
end
|
118
|
+
context 'with defaults' do
|
119
|
+
describe '#initialize' do
|
120
|
+
it 'sets ping' do
|
121
|
+
expect(client.websocket_ping).to eq 30
|
122
|
+
end
|
123
|
+
it "doesn't set proxy" do
|
124
|
+
expect(client.websocket_proxy).to be nil
|
125
|
+
end
|
126
|
+
Slack::RealTime::Config::ATTRIBUTES.each do |key|
|
127
|
+
it "sets #{key}" do
|
128
|
+
expect(client.send(key)).to eq Slack::RealTime::Config.send(key)
|
129
|
+
end
|
130
|
+
end
|
131
|
+
end
|
132
|
+
end
|
133
|
+
end
|
134
|
+
context 'with custom settings' do
|
135
|
+
describe '#initialize' do
|
136
|
+
Slack::RealTime::Config::ATTRIBUTES.each do |key|
|
137
|
+
context key do
|
138
|
+
let(:client) { Slack::RealTime::Client.new(key => 'custom') }
|
139
|
+
it "sets #{key}" do
|
140
|
+
expect(client.send(key)).to_not eq Slack::RealTime::Config.send(key)
|
141
|
+
expect(client.send(key)).to eq 'custom'
|
142
|
+
end
|
143
|
+
end
|
144
|
+
end
|
145
|
+
end
|
146
|
+
end
|
147
|
+
context 'global config' do
|
148
|
+
after do
|
149
|
+
Slack::RealTime::Client.config.reset
|
150
|
+
end
|
151
|
+
let(:client) { Slack::RealTime::Client.new }
|
152
|
+
context 'ping' do
|
153
|
+
before do
|
154
|
+
Slack::RealTime::Client.configure do |config|
|
155
|
+
config.websocket_ping = 15
|
156
|
+
end
|
157
|
+
end
|
158
|
+
describe '#initialize' do
|
159
|
+
it 'sets ping' do
|
160
|
+
expect(client.websocket_ping).to eq 15
|
161
|
+
end
|
162
|
+
it 'creates a connection with custom ping' do
|
163
|
+
expect(Slack::RealTime::Concurrency::Mock::WebSocket).to receive(:new).with(url, nil, ping: 15).and_return(ws)
|
164
|
+
client.start!
|
165
|
+
end
|
166
|
+
end
|
167
|
+
end
|
168
|
+
context 'proxy' do
|
169
|
+
before do
|
170
|
+
Slack::RealTime::Client.configure do |config|
|
171
|
+
config.websocket_proxy = {
|
172
|
+
origin: 'http://username:password@proxy.example.com',
|
173
|
+
headers: { 'User-Agent' => 'ruby' }
|
174
|
+
}
|
175
|
+
end
|
176
|
+
end
|
177
|
+
describe '#initialize' do
|
178
|
+
it 'sets proxy' do
|
179
|
+
expect(client.websocket_proxy).to eq(
|
180
|
+
origin: 'http://username:password@proxy.example.com',
|
181
|
+
headers: { 'User-Agent' => 'ruby' }
|
182
|
+
)
|
183
|
+
end
|
184
|
+
it 'creates a connection with custom proxy' do
|
185
|
+
expect(Slack::RealTime::Concurrency::Mock::WebSocket).to receive(:new).with(
|
186
|
+
url,
|
187
|
+
nil,
|
188
|
+
ping: 30,
|
189
|
+
proxy: {
|
190
|
+
origin: 'http://username:password@proxy.example.com',
|
191
|
+
headers: { 'User-Agent' => 'ruby' }
|
192
|
+
}).and_return(ws)
|
193
|
+
client.start!
|
194
|
+
end
|
195
|
+
end
|
196
|
+
end
|
197
|
+
end
|
198
|
+
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
begin
|
4
|
+
RSpec.describe Slack::RealTime::Concurrency::Celluloid::Socket do
|
5
|
+
context 'with url' do
|
6
|
+
let(:url) { 'wss://echo.websocket.org/websocket/xyz' }
|
7
|
+
subject(:socket) { described_class.new(url, ping: 42) }
|
8
|
+
let(:driver) { WebSocket::Driver::Client }
|
9
|
+
let(:ws) { double(driver) }
|
10
|
+
|
11
|
+
describe '#initialize' do
|
12
|
+
it 'sets url' do
|
13
|
+
expect(socket.url).to eq url
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
describe '#connect!' do
|
18
|
+
before do
|
19
|
+
allow(ws).to receive(:on).with(:close)
|
20
|
+
end
|
21
|
+
|
22
|
+
xit 'connects' do
|
23
|
+
expect(socket.driver).to receive(:start)
|
24
|
+
|
25
|
+
socket.connect!
|
26
|
+
end
|
27
|
+
|
28
|
+
xit 'pings every 30s' do
|
29
|
+
expect(driver).to receive(:client).with(socket).and_return(ws)
|
30
|
+
socket.connect!
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
describe '#disconnect!' do
|
35
|
+
it 'closes and nils the websocket' do
|
36
|
+
socket.instance_variable_set('@driver', ws)
|
37
|
+
expect(ws).to receive(:close)
|
38
|
+
socket.disconnect!
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
describe 'send_data' do
|
43
|
+
let(:driver) { socket.driver }
|
44
|
+
before do
|
45
|
+
allow(driver).to receive(:start)
|
46
|
+
allow(subject).to receive(:run_loop)
|
47
|
+
socket.connect!
|
48
|
+
end
|
49
|
+
|
50
|
+
it 'sends data' do
|
51
|
+
expect(driver).to receive(:text).with('data')
|
52
|
+
subject.send_data('data')
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
rescue LoadError
|
58
|
+
end
|