diff options
author | Pratik Naik <pratiknaik@gmail.com> | 2015-01-15 22:58:02 +0530 |
---|---|---|
committer | Pratik Naik <pratiknaik@gmail.com> | 2015-01-15 22:58:02 +0530 |
commit | 843492ee6c2167115f2bbc9c1b3c82da0ad075f8 (patch) | |
tree | 3b25966b07e0f11f91db2e461413c06f7ad114d6 | |
parent | a5c3a8d3e346acce79899c04133ba6fa3c88f830 (diff) | |
download | rails-843492ee6c2167115f2bbc9c1b3c82da0ad075f8.tar.gz rails-843492ee6c2167115f2bbc9c1b3c82da0ad075f8.tar.bz2 rails-843492ee6c2167115f2bbc9c1b3c82da0ad075f8.zip |
Add some tests. Work in progress. Testing websockets is hard.
-rw-r--r-- | .gitignore | 1 | ||||
-rw-r--r-- | Rakefile | 11 | ||||
-rw-r--r-- | test/channel_test.rb | 23 | ||||
-rw-r--r-- | test/server_test.rb | 36 | ||||
-rw-r--r-- | test/test_helper.rb | 46 |
5 files changed, 117 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000000..1918a1b0ee --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +test/tests.log
\ No newline at end of file diff --git a/Rakefile b/Rakefile new file mode 100644 index 0000000000..c2ae16b7d9 --- /dev/null +++ b/Rakefile @@ -0,0 +1,11 @@ +require 'rake' +require 'rake/testtask' + +task :default => :test + +Rake::TestTask.new(:test) do |t| + t.libs << "test" + t.pattern = 'test/**/*_test.rb' + t.verbose = true +end +Rake::Task['test'].comment = "Run tests" diff --git a/test/channel_test.rb b/test/channel_test.rb new file mode 100644 index 0000000000..ad5fa04356 --- /dev/null +++ b/test/channel_test.rb @@ -0,0 +1,23 @@ +require 'test_helper' + +class ChannelTest < ActionCableTest + + class PingChannel < ActionCable::Channel::Base + def self.matches?(identifier) + identifier[:channel] == 'chat' && identifier[:user_id].to_i.nonzero? + end + end + + class PingServer < ActionCable::Server + register_channels PingChannel + end + + def app + PingServer + end + + test "channel callbacks" do + ws = Faye::WebSocket::Client.new(websocket_url) + end + +end diff --git a/test/server_test.rb b/test/server_test.rb new file mode 100644 index 0000000000..50a95b9d59 --- /dev/null +++ b/test/server_test.rb @@ -0,0 +1,36 @@ +require 'test_helper' + +class ServerTest < ActionCableTest + + class ChatChannel < ActionCable::Channel::Base + def self.matches?(identifier) + identifier[:channel] == 'chat' && identifier[:user_id].to_i.nonzero? + end + end + + class ChatServer < ActionCable::Server + register_channels ChatChannel + end + + def app + ChatServer + end + + test "channel registration" do + assert_equal ChatServer.registered_channels, Set.new([ ChatChannel ]) + end + + test "subscribing to a channel with valid params" do + ws = Faye::WebSocket::Client.new(websocket_url) + + ws.on(:message) do |message| + puts message.inspect + end + + ws.send action: 'subscribe', identifier: { channel: 'chat'}.to_json + end + + test "subscribing to a channel with invalid params" do + end + +end diff --git a/test/test_helper.rb b/test/test_helper.rb new file mode 100644 index 0000000000..5251e711b7 --- /dev/null +++ b/test/test_helper.rb @@ -0,0 +1,46 @@ +require "rubygems" +require "bundler" + +gem 'minitest' +require "minitest/autorun" + +Bundler.setup +Bundler.require :default, :test + +require 'puma' + +require 'action_cable' +ActiveSupport.test_order = :sorted + +require 'logger' +logger = Logger.new(File.join(File.dirname(__FILE__), "tests.log")) +logger.level = Logger::DEBUG +Cramp.logger = logger + +class ActionCableTest < Cramp::TestCase + PORT = 420420 + + setup :start_puma_server + teardown :stop_puma_server + + def start_puma_server + events = Puma::Events.new(StringIO.new, StringIO.new) + binder = Puma::Binder.new(events) + binder.parse(["tcp://0.0.0.0:#{PORT}"], self) + @server = Puma::Server.new(app, events) + @server.binder = binder + @server.run + end + + def stop_puma_server + @server.stop(true) + end + + def websocket_url + "ws://0.0.0.0:#{PORT}/" + end + + def log(*args) + end + +end |