This repository has been archived by the owner on Jan 26, 2022. It is now read-only.
/
websocket_tunnel_spec.rb
71 lines (59 loc) · 2.02 KB
/
websocket_tunnel_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
# Copyright (c) 2009-2011 VMware, Inc.
require File.join(File.dirname(__FILE__), 'spec_helper')
require 'caldecott/client/websocket_tunnel.rb'
describe 'Client WebSocket Tunnel' do
before do
@log = Logger.new StringIO.new
@host = 'foo'
@port = 12345
@base_url = 'ws://tunnel/'
@ws_request = mock(EM::HttpRequest)
@ws_request.should_receive(:get).once.and_return(@ws_request)
EM::HttpRequest.should_receive(:new).once.with("#{@base_url}/websocket/#{@host}/#{@port}").and_return(@ws_request)
@tunnel = Caldecott::Client::WebSocketTunnel.new(@log, @base_url, @host, @port)
end
describe '#onopen' do
it 'should register the onopen handler' do
@ws_request.should_receive(:callback) { |*args, &blk| @ws_callback = blk }
times_called = 0
@tunnel.onopen { times_called += 1 }
@ws_callback.call
times_called.should == 1
end
end
describe '#onclose' do
it 'should register the onclose handler' do
@ws_request.should_receive(:errback) { |*args, &blk| @ws_errback = blk }
@ws_request.should_receive(:disconnect) { |*args, &blk| @ws_disconnect = blk }
times_called = 0
@tunnel.onclose { times_called += 1 }
@ws_errback.call
times_called.should == 1
@ws_disconnect.call
times_called.should == 2
end
end
describe '#onreceive' do
it 'should receive data from the websocket' do
msg = "hi there for receive"
@ws_request.should_receive(:stream) { |*args, &blk| @ws_stream = blk }
received = nil
@tunnel.onreceive { |data| received = data }
@ws_stream.call Base64.encode64(msg)
received.should == msg
end
end
describe '#send_data' do
it 'should send data to the websocket' do
msg = "hi there for send"
@ws_request.should_receive(:send).with(Base64.encode64(msg))
@tunnel.send_data(msg)
end
end
describe '#close' do
it 'should close the websocket' do
@ws_request.should_receive(:close_connection_after_writing)
@tunnel.close
end
end
end