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 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
|
require 'helper'
# These integration tests are older and use a different testing style to the
# integration tests for newer drafts. They use EM::HttpRequest which happens
# to currently estabish a websocket connection using the draft75 protocol.
#
describe "WebSocket server draft75" do
include EM::SpecHelper
default_timeout 1
def start_client
client = Draft75WebSocketClient.new
yield client if block_given?
return client
end
it_behaves_like "a websocket server" do
let(:version) { 75 }
end
it "should automatically complete WebSocket handshake" do
em {
MSG = "Hello World!"
EventMachine.add_timer(0.1) do
ws = EventMachine::WebSocketClient.connect('ws://127.0.0.1:12345/')
ws.errback { fail }
ws.callback { }
ws.stream { |msg|
msg.data.should == MSG
EventMachine.stop
}
end
start_server { |ws|
ws.onopen {
ws.send MSG
}
}
}
end
it "should split multiple messages into separate callbacks" do
em {
messages = %w[1 2]
received = []
EventMachine.add_timer(0.1) do
ws = EventMachine::WebSocketClient.connect('ws://127.0.0.1:12345/')
ws.errback { fail }
ws.stream {|msg|}
ws.callback {
ws.send_msg messages[0]
ws.send_msg messages[1]
}
end
start_server { |ws|
ws.onopen {}
ws.onclose {}
ws.onmessage {|msg|
msg.should == messages[received.size]
received.push msg
EventMachine.stop if received.size == messages.size
}
}
}
end
it "should call onclose callback when client closes connection" do
em {
EventMachine.add_timer(0.1) do
ws = EventMachine::WebSocketClient.connect('ws://127.0.0.1:12345/')
ws.errback { fail }
ws.callback {
ws.close_connection
}
ws.stream{|msg|}
end
start_server { |ws|
ws.onopen {}
ws.onclose {
ws.state.should == :closed
EventMachine.stop
}
}
}
end
it "should call onerror callback with raised exception and close connection on bad handshake" do
em {
EventMachine.add_timer(0.1) do
http = EM::HttpRequest.new('http://127.0.0.1:12345/').get
http.errback { }
http.callback { fail }
end
start_server { |ws|
ws.onopen { fail }
ws.onclose { EventMachine.stop }
ws.onerror {|e|
e.should be_an_instance_of EventMachine::WebSocket::HandshakeError
e.message.should match('Not an upgrade request')
EventMachine.stop
}
}
}
end
it "should report that close codes are not supported" do
em {
start_server { |ws|
ws.onopen {
ws.supports_close_codes?.should == false
done
}
}
start_client
}
end
end
|