Last active
December 12, 2015 01:58
-
-
Save impact11/4695116 to your computer and use it in GitHub Desktop.
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
require 'spec_helper' | |
describe StreamingListener do | |
let(:err) { Proc.new { |c| fail "API request failed: #{c.inspect}" } } | |
let(:chat_group) { FactoryGirl.create :chat_group, :chat_group_size => 3 } | |
let(:user) { FactoryGirl.create :user } | |
let(:user2) { FactoryGirl.create :user } | |
describe 'chat_group listener' do | |
it 'should allow a user to become a listener to a chat_group room she has joined' do | |
chat_group.add_identity(user.identity) | |
with_api(RealtimeAPIs,{:log_file => 'spec/rspec_log.log'}) do | |
get_request({:path => '/chat_groups/request', | |
:query => {:action => 'ENTERED_CHAT_GROUP_ROOM', | |
:chat_group_id => chat_group.token, | |
:user_id => user.token} | |
}, err) do |c| | |
b = Yajl::Parser.parse(c.response) | |
b['chat_group_id'].should == chat_group.token | |
b['action'].should == 'ENTERED_CHAT_GROUP_ROOM' | |
options = {:query => {:action => 'LISTENING', | |
:chat_group_id => chat_group.token, | |
:user_id => user.token}} | |
stream = EventMachine::HttpRequest.new("http://#{c.req.uri.host}:#{c.req.uri.port}/chat_groups/listener").get options | |
# stream.errback { failed(stream) } | |
stream.callback { | |
stream.response.should == '' | |
stream.state.should == :chunk_header | |
stream.response_header["TRANSFER_ENCODING"].should == "chunked" | |
} | |
body = '' | |
stream.stream do |chunk| | |
body << chunk | |
puts body | |
end | |
end | |
end | |
end | |
it 'should not allow a user to become a listener to a chat_group room that no one has joined' do | |
with_api(RealtimeAPIs,{:log_file => 'spec/rspec_log.log'}) do | |
# Add two listeners to channel | |
get_request({:path => '/chat_groups/listener', | |
:query => {:action => 'LISTENING', | |
:chat_group_id => chat_group.token, | |
:user_id => user.token} | |
}, err) do |c| | |
b = Yajl::Parser.parse(c.response) | |
b['error'].should == 'Cannot listen to a chat_group room that no one has entered.' | |
end | |
end | |
end | |
it 'should not allow a user to become a listener to a chat_group room that the user has not joined' do | |
chat_group.add_identity(user2.identity) # Mock process for adding a user's identity to a chat_group. This is done by Rails | |
# Add first user to activate EM channel -- this keeps us from getting an error that is not the one we are testing for | |
with_api(RealtimeAPIs,{:log_file => 'spec/rspec_log.log'}) do | |
get_request({:path => "/chat_groups/request", | |
:query => {:action => 'ENTERED_CHAT_GROUP_ROOM', | |
:chat_group_id => chat_group.token, | |
:user_id => user2.token}}, err) do |page| | |
b = Yajl::Parser.parse(page.response) | |
b['chat_group_id'].should == chat_group.token | |
b['action'].should == 'ENTERED_CHAT_GROUP_ROOM' | |
get_request({:path => '/chat_groups/listener', | |
:query => {:action => 'LISTENING', | |
:chat_group_id => chat_group.token, | |
:user_id => user.token} | |
}, err) do |c| | |
b2 = Yajl::Parser.parse(c.response) | |
b2['error'].should == 'Cannot listen to a chat_group you have not joined.' | |
end | |
end | |
end | |
end | |
it 'can receive a message on a channel', :tag => 'listener' do | |
chat_group.add_identity(user.identity) | |
chat_group.add_identity(user2.identity) | |
uri = "http://localhost:3000" | |
first_run = true # We need this because jquery stream sends out a 1kb block of garbage data to put the browser in "listen to me" mode | |
EventMachine.run do | |
sender_enter_request = EventMachine::HttpRequest.new("#{uri}/chat_groups/request").aget({ | |
:query => {:action => 'ENTERED_CHAT_GROUP_ROOM', :chat_group_id => chat_group.token, :user_id => user2.token}}) | |
listener_enter_request = EventMachine::HttpRequest.new("#{uri}/chat_groups/request").aget({ | |
:query => {:action => 'ENTERED_CHAT_GROUP_ROOM', :chat_group_id => chat_group.token, :user_id => user.token}}) | |
listener_stream = EventMachine::HttpRequest.new("#{uri}/chat_groups/listener").aget({ | |
:query => {:action => 'LISTENING', :chat_group_id => chat_group.token, :user_id => user.token}}) | |
sender_enter_request.callback do | |
options2 = {:query => {:action => 'CHAT_MESSAGE', | |
:chat_group_id => chat_group.token, | |
:user_id => user2.token, | |
:chat_message => "A Chat Message"}} | |
newrequest = EventMachine::HttpRequest.new("#{uri}/chat_groups/request").aget options2 | |
end | |
listener_stream.stream do |chunk| | |
if first_run then | |
first_run = false # Now we're getting real data | |
else | |
chunk.should match(/(ENTERED_CHAT_GROUP_ROOM)/) | |
EventMachine.stop | |
end | |
end | |
end | |
end | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment