Skip to content

Commit

Permalink
Add tests for draft 75 and 76
Browse files Browse the repository at this point in the history
  • Loading branch information
imanel committed Apr 1, 2011
1 parent 1f8f6da commit 8f3fde7
Show file tree
Hide file tree
Showing 5 changed files with 171 additions and 0 deletions.
10 changes: 10 additions & 0 deletions spec/spec_helper.rb
Expand Up @@ -2,3 +2,13 @@
require 'rspec'

require 'rack/websocket'
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}

RSpec.configure do |config|
config.mock_with :mocha
end

class TestApp < Rack::WebSocket::Application
end

TEST_PORT = 8081
25 changes: 25 additions & 0 deletions spec/support/all_drafts.rb
@@ -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
69 changes: 69 additions & 0 deletions spec/support/requests.rb
@@ -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
66 changes: 66 additions & 0 deletions spec/thin_spec.rb
@@ -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
1 change: 1 addition & 0 deletions websocket-rack.gemspec
Expand Up @@ -16,6 +16,7 @@ Gem::Specification.new do |s|
s.add_dependency 'em-websocket', '~> 0.2.1'
s.add_dependency 'thin' # Temporary until we support more servers
s.add_development_dependency 'rspec', '~> 2.4.0'
s.add_development_dependency 'mocha'

s.files = `git ls-files`.split("\n")
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
Expand Down

0 comments on commit 8f3fde7

Please sign in to comment.