Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
171 additions
and
0 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
shared_examples_for 'all drafts' do | ||
it "- should accept incoming connection" do | ||
conn = new_server_connection | ||
conn.write(handshake_request) | ||
conn.read(handshake_response.length).should eql(handshake_response) | ||
end | ||
it "- should call 'on_open' on new connection" do | ||
TestApp.any_instance.expects(:on_open) | ||
conn = new_server_connection | ||
conn.write(handshake_request) | ||
end | ||
it "- should call 'on_close' on connection close" do | ||
TestApp.any_instance.expects(:on_close) | ||
conn = new_server_connection | ||
conn.write(handshake_request) | ||
conn.close | ||
end | ||
it "- should call 'on_message' on connection sending data" do | ||
TestApp.any_instance.expects(:on_message).once.with { |env, message| message == 'some message' } | ||
conn = new_server_connection | ||
conn.write(handshake_request) | ||
conn.read(handshake_response.length) | ||
conn.write(message) | ||
end | ||
end |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
def new_server_connection | ||
TCPSocket.new('localhost', TEST_PORT) | ||
end | ||
|
||
def flash_policy_request | ||
"<policy-file-request/>\000" | ||
end | ||
|
||
def flash_policy_response | ||
'<?xml version="1.0"?><cross-domain-policy><allow-access-from domain="*" to-ports="*"/></cross-domain-policy>' | ||
end | ||
|
||
def spec75_handshake_request | ||
<<-EOF | ||
GET / HTTP/1.1\r | ||
Upgrade: WebSocket\r | ||
Connection: Upgrade\r | ||
Host: localhost:#{TEST_PORT}\r | ||
Origin: http://localhost:#{TEST_PORT}\r | ||
\r | ||
EOF | ||
end | ||
|
||
def spec75_handshake_response | ||
<<-EOF | ||
HTTP/1.1 101 Web Socket Protocol Handshake\r | ||
Upgrade: WebSocket\r | ||
Connection: Upgrade\r | ||
WebSocket-Origin: http://localhost:#{TEST_PORT}\r | ||
WebSocket-Location: ws://localhost:#{TEST_PORT}/\r | ||
\r | ||
EOF | ||
end | ||
|
||
def spec75_message | ||
"\x00some message\xff" | ||
end | ||
|
||
def spec76_handshake_request | ||
request = <<-EOF | ||
GET / HTTP/1.1\r | ||
Upgrade: WebSocket\r | ||
Connection: Upgrade\r | ||
Host: localhost:#{TEST_PORT}\r | ||
Origin: http://localhost:#{TEST_PORT}\r | ||
Sec-WebSocket-Key1: 18x 6]8vM;54 *(5: { U1]8 z [ 8\r | ||
Sec-WebSocket-Key2: 1_ tx7X d < nw 334J702) 7]o}` 0\r | ||
\r | ||
Tm[K T2u | ||
EOF | ||
request.rstrip | ||
end | ||
|
||
def spec76_handshake_response | ||
response = <<-EOF | ||
HTTP/1.1 101 WebSocket Protocol Handshake\r | ||
Upgrade: WebSocket\r | ||
Connection: Upgrade\r | ||
Sec-WebSocket-Location: ws://localhost:#{TEST_PORT}/\r | ||
Sec-WebSocket-Origin: http://localhost:#{TEST_PORT}\r | ||
\r | ||
fQJ,fN/4F4!~K~MH | ||
EOF | ||
response.rstrip | ||
end | ||
|
||
def spec76_message | ||
"\x00some message\xff" | ||
end |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
require 'thin' | ||
require 'spec_helper' | ||
|
||
describe 'Thin handler' do | ||
let(:app) { TestApp.new } | ||
|
||
before(:all) { silent_thin } | ||
before { start_thin_server(app) } | ||
after { stop_thin_server } | ||
|
||
it "should include extensions" do | ||
::Thin::Connection.include?(::Rack::WebSocket::Extensions::Common).should be_true | ||
::Thin::Connection.include?(::Rack::WebSocket::Extensions::Thin::Connection).should be_true | ||
end | ||
|
||
it "should return flash policy file" do | ||
conn = new_server_connection | ||
conn.write(flash_policy_request) | ||
conn.read(flash_policy_response.length).should eql(flash_policy_response) | ||
end | ||
|
||
context 'draft75:' do | ||
let(:handshake_request) { spec75_handshake_request } | ||
let(:handshake_response) { spec75_handshake_response } | ||
let(:message) { spec75_message } | ||
|
||
it_should_behave_like 'all drafts' | ||
end | ||
|
||
context 'draft76:' do | ||
let(:handshake_request) { spec76_handshake_request } | ||
let(:handshake_response) { spec76_handshake_response } | ||
let(:message) { spec76_message } | ||
|
||
it_should_behave_like 'all drafts' | ||
end | ||
end | ||
|
||
def start_thin_server(app, options = {}) | ||
@server = Thin::Server.new('0.0.0.0', TEST_PORT, options, app) | ||
@server.ssl = options[:ssl] | ||
# @server.threaded = options[:threaded] | ||
# @server.timeout = 3 | ||
|
||
@thread = Thread.new { @server.start } | ||
sleep 1 until @server.running? | ||
end | ||
|
||
def stop_thin_server | ||
sleep 0.1 | ||
@server.stop! | ||
@thread.kill | ||
raise "Reactor still running, wtf?" if EventMachine.reactor_running? | ||
end | ||
|
||
def silent_thin | ||
::Thin::Logging.silent = true | ||
if EM::VERSION < "1.0.0" | ||
begin | ||
old_verbose, $VERBOSE = $VERBOSE, nil | ||
::Thin::Server.const_set 'DEFAULT_TIMEOUT', 0 | ||
ensure | ||
$VERBOSE = old_verbose | ||
end | ||
end | ||
end |
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