1
0
mirror of https://github.com/bitwarden/help synced 2025-12-27 21:53:15 +00:00

Promote to Master (#748)

* initial commit

* adding quotes for the array error

* Create Gemfile

* Create Gemfile.lock

* add .nvmrc and .node-version

* removed /article from URL

* update links to work with netlify

* more fixed links

* link fixes

* update bad links

* Update netlify.toml

toml test for redirects

* article redirect

* link fixes

* Update index.html

* Update netlify.toml

* Update _config.yml

* Update netlify.toml

* Update netlify.toml

* Update netlify.toml

* Update netlify.toml

* Update netlify.toml

* add article back into URL for launch

* Update netlify.toml

* Update netlify.toml

* add order to categories front matter

* Update netlify.toml

* update

* sidemenu update

* Revert "sidemenu update"

This reverts commit 5441c3d35c.

* update order prop

* Navbar updates per Gary and compiler warnings

* font/style tweaks

* Update sidebar.html

* Stage Release Documentation (#739)

* initial drafts

* rewrite Custom Fields article to prioritize new context-menu option & better organize ancillary information

* edit

* edit

* Custom Field Context Menu & CAPTCHA item in release notes

* SSO relink event

* update rn

* small edits

* improve release notes titles

* fix side menu

* Edits courtest of mportune!

* update order

* link fixes

* link cleanup

* image updates and a link

* fix trailing slash

Co-authored-by: DanHillesheim <79476558+DanHillesheim@users.noreply.github.com>
This commit is contained in:
fred_the_tech_writer
2021-09-21 13:21:11 -04:00
committed by GitHub
parent 63f78e8979
commit 906e2ca0dd
3304 changed files with 386714 additions and 8864 deletions

View File

@@ -0,0 +1,138 @@
require 'helper'
# These tests are not specific to any particular draft of the specification
#
describe "WebSocket server" do
include EM::SpecHelper
default_timeout 1
it "should fail on non WebSocket requests" do
em {
EM.add_timer(0.1) do
http = EM::HttpRequest.new('http://127.0.0.1:12345/').get :timeout => 0
http.errback { done }
http.callback { fail }
end
start_server
}
end
it "should expose the WebSocket request headers, path and query params" do
em {
EM.add_timer(0.1) do
ws = EventMachine::WebSocketClient.connect('ws://127.0.0.1:12345/',
:origin => 'http://example.com')
ws.errback { fail }
ws.callback { ws.close_connection }
ws.stream { |msg| }
end
start_server do |ws|
ws.onopen { |handshake|
headers = handshake.headers
headers["Connection"].should == "Upgrade"
headers["Upgrade"].should == "websocket"
headers["Host"].to_s.should == "127.0.0.1:12345"
handshake.path.should == "/"
handshake.query.should == {}
handshake.origin.should == 'http://example.com'
}
ws.onclose {
ws.state.should == :closed
done
}
end
}
end
it "should expose the WebSocket path and query params when nonempty" do
em {
EM.add_timer(0.1) do
ws = EventMachine::WebSocketClient.connect('ws://127.0.0.1:12345/hello?foo=bar&baz=qux')
ws.errback { fail }
ws.callback {
ws.close_connection
}
ws.stream { |msg| }
end
start_server do |ws|
ws.onopen { |handshake|
handshake.path.should == '/hello'
handshake.query_string.split('&').sort.
should == ["baz=qux", "foo=bar"]
handshake.query.should == {"foo"=>"bar", "baz"=>"qux"}
}
ws.onclose {
ws.state.should == :closed
done
}
end
}
end
it "should raise an exception if frame sent before handshake complete" do
em {
# 1. Start WebSocket server
start_server { |ws|
# 3. Try to send a message to the socket
lambda {
ws.send('early message')
}.should raise_error('Cannot send data before onopen callback')
done
}
# 2. Connect a dumb TCP connection (will not send handshake)
EM.connect('0.0.0.0', 12345, EM::Connection)
}
end
it "should allow the server to be started inside an existing EM" do
em {
EM.add_timer(0.1) do
http = EM::HttpRequest.new('http://127.0.0.1:12345/').get :timeout => 0
http.errback { |e| done }
http.callback { fail }
end
start_server do |ws|
ws.onopen { |handshake|
headers = handshake.headers
headers["Host"].to_s.should == "127.0.0.1:12345"
}
ws.onclose {
ws.state.should == :closed
done
}
end
}
end
context "outbound limit set" do
it "should close the connection if the limit is reached" do
em {
start_server(:outbound_limit => 150) do |ws|
# Increase the message size by one on each loop
ws.onmessage{|msg| ws.send(msg + "x") }
ws.onclose{|status|
status[:code].should == 1006 # Unclean
status[:was_clean].should be false
}
end
EM.add_timer(0.1) do
ws = EventMachine::WebSocketClient.connect('ws://127.0.0.1:12345/')
ws.callback { ws.send_msg "hello" }
ws.disconnect { done } # Server closed the connection
ws.stream { |msg|
# minus frame size ? (getting 146 max here)
msg.data.size.should <= 150
# Return back the message
ws.send_msg(msg.data)
}
end
}
end
end
end

View File

@@ -0,0 +1,298 @@
require 'helper'
describe "draft03" do
include EM::SpecHelper
default_timeout 1
before :each do
@request = {
:port => 80,
:method => "GET",
:path => "/demo",
:headers => {
'Host' => 'example.com',
'Connection' => 'Upgrade',
'Sec-WebSocket-Key2' => '12998 5 Y3 1 .P00',
'Sec-WebSocket-Protocol' => 'sample',
'Upgrade' => 'WebSocket',
'Sec-WebSocket-Key1' => '4 @1 46546xW%0l 1 5',
'Origin' => 'http://example.com',
'Sec-WebSocket-Draft' => '3'
},
:body => '^n:ds[4U'
}
@response = {
:headers => {
"Upgrade" => "WebSocket",
"Connection" => "Upgrade",
"Sec-WebSocket-Location" => "ws://example.com/demo",
"Sec-WebSocket-Origin" => "http://example.com",
"Sec-WebSocket-Protocol" => "sample"
},
:body => "8jKS\'y:G*Co,Wxa-"
}
end
def start_client
client = EM.connect('0.0.0.0', 12345, Draft03FakeWebSocketClient)
client.send_data(format_request(@request))
yield client if block_given?
return client
end
it_behaves_like "a websocket server" do
let(:version) { 3 }
end
it_behaves_like "a WebSocket server drafts 3 and above" do
let(:version) { 3 }
end
# These examples are straight from the spec
# http://tools.ietf.org/html/draft-ietf-hybi-thewebsocketprotocol-03#section-4.6
describe "examples from the spec" do
it "should accept a single-frame text message" do
em {
start_server { |ws|
ws.onmessage { |msg|
msg.should == 'Hello'
done
}
}
start_client { |client|
client.onopen {
client.send_data("\x04\x05Hello")
}
}
}
end
it "should accept a fragmented text message" do
em {
start_server { |ws|
ws.onmessage { |msg|
msg.should == 'Hello'
done
}
}
connection = start_client
# Send frame
connection.onopen {
connection.send_data("\x84\x03Hel")
connection.send_data("\x00\x02lo")
}
}
end
it "should accept a ping request and respond with the same body" do
em {
start_server
connection = start_client
# Send frame
connection.onopen {
connection.send_data("\x02\x05Hello")
}
connection.onmessage { |frame|
next if frame.nil?
frame.should == "\x03\x05Hello"
done
}
}
end
it "should accept a 256 bytes binary message in a single frame" do
em {
data = "a" * 256
start_server { |ws|
ws.onbinary { |msg|
msg.encoding.should == Encoding.find("BINARY") if defined?(Encoding)
msg.should == data
done
}
}
connection = start_client
# Send frame
connection.onopen {
connection.send_data("\x05\x7E\x01\x00" + data)
}
}
end
it "should accept a 64KiB binary message in a single frame" do
em {
data = "a" * 65536
start_server { |ws|
ws.onbinary { |msg|
msg.encoding.should == Encoding.find("BINARY") if defined?(Encoding)
msg.should == data
done
}
}
connection = start_client
# Send frame
connection.onopen {
connection.send_data("\x05\x7F\x00\x00\x00\x00\x00\x01\x00\x00" + data)
}
}
end
end
describe "close handling" do
it "should respond to a new close frame with a close frame" do
em {
start_server
connection = start_client
# Send close frame
connection.onopen {
connection.send_data("\x01\x00")
}
# Check that close ack received
connection.onmessage { |frame|
frame.should == "\x01\x00"
done
}
}
end
it "should close the connection on receiving a close acknowlegement and call onclose with close code 1005 and was_clean=true (initiated by server)" do
em {
ack_received = false
start_server { |ws|
ws.onopen {
# 2. Send a close frame
EM.next_tick {
ws.close
}
}
# 5. Onclose event on server
ws.onclose { |event|
event.should == {
:code => 1005,
:reason => "",
:was_clean => true,
}
done
}
}
# 1. Create a fake client which sends draft 76 handshake
connection = start_client
# 3. Check that close frame recieved and acknowlege it
connection.onmessage { |frame|
frame.should == "\x01\x00"
ack_received = true
connection.send_data("\x01\x00")
}
# 4. Check that connection is closed _after_ the ack
connection.onclose {
ack_received.should == true
}
}
end
# it "should repur"
#
it "should return close code 1005 and was_clean=true after closing handshake (initiated by client)" do
em {
start_server { |ws|
ws.onclose { |event|
event.should == {
:code => 1005,
:reason => "",
:was_clean => true,
}
done
}
}
start_client { |client|
client.onopen {
client.send_data("\x01\x00")
}
}
}
end
it "should not allow data frame to be sent after close frame sent" do
em {
start_server { |ws|
ws.onopen {
# 2. Send a close frame
EM.next_tick {
ws.close
}
# 3. Check that exception raised if I attempt to send more data
EM.add_timer(0.1) {
lambda {
ws.send('hello world')
}.should raise_error(EM::WebSocket::WebSocketError, 'Cannot send data frame since connection is closing')
done
}
}
}
# 1. Create a fake client which sends draft 76 handshake
start_client
}
end
it "should still respond to control frames after close frame sent" do
em {
start_server { |ws|
ws.onopen {
# 2. Send a close frame
EM.next_tick {
ws.close
}
}
}
# 1. Create a fake client which sends draft 76 handshake
connection = start_client
connection.onmessage { |frame|
if frame == "\x01\x00"
# 3. After the close frame is received send a ping frame, but
# don't respond with a close ack
connection.send_data("\x02\x05Hello")
else
# 4. Check that the pong is received
frame.should == "\x03\x05Hello"
done
end
}
}
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
end

View File

@@ -0,0 +1,50 @@
require 'helper'
describe "draft05" do
include EM::SpecHelper
default_timeout 1
before :each do
@request = {
:port => 80,
:method => "GET",
:path => "/demo",
:headers => {
'Host' => 'example.com',
'Upgrade' => 'websocket',
'Connection' => 'Upgrade',
'Sec-WebSocket-Key' => 'dGhlIHNhbXBsZSBub25jZQ==',
'Sec-WebSocket-Protocol' => 'sample',
'Sec-WebSocket-Origin' => 'http://example.com',
'Sec-WebSocket-Version' => '5'
}
}
end
def start_client
client = EM.connect('0.0.0.0', 12345, Draft05FakeWebSocketClient)
client.send_data(format_request(@request))
yield client if block_given?
return client
end
it_behaves_like "a websocket server" do
let(:version) { 5 }
end
it_behaves_like "a WebSocket server drafts 3 and above" do
let(:version) { 5 }
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

View File

@@ -0,0 +1,145 @@
require 'helper'
describe "draft06" do
include EM::SpecHelper
default_timeout 1
before :each do
@request = {
:port => 80,
:method => "GET",
:path => "/demo",
:headers => {
'Host' => 'example.com',
'Upgrade' => 'websocket',
'Connection' => 'Upgrade',
'Sec-WebSocket-Key' => 'dGhlIHNhbXBsZSBub25jZQ==',
'Sec-WebSocket-Protocol' => 'sample',
'Sec-WebSocket-Origin' => 'http://example.com',
'Sec-WebSocket-Version' => '6'
}
}
@response = {
:protocol => "HTTP/1.1 101 Switching Protocols\r\n",
:headers => {
"Upgrade" => "websocket",
"Connection" => "Upgrade",
"Sec-WebSocket-Accept" => "s3pPLMBiTxaQ9kYGzzhZRbK+xOo=",
"Sec-WebSocket-Protocol" => "sample",
}
}
end
def start_client
client = EM.connect('0.0.0.0', 12345, Draft05FakeWebSocketClient)
client.send_data(format_request(@request))
yield client if block_given?
return client
end
it_behaves_like "a websocket server" do
let(:version) { 6 }
end
it_behaves_like "a WebSocket server drafts 3 and above" do
let(:version) { 6 }
end
it "should open connection" do
em {
start_server { |server|
server.onopen {
server.instance_variable_get(:@handler).class.should == EventMachine::WebSocket::Handler06
}
}
start_client { |client|
client.onopen {
client.handshake_response.lines.sort.
should == format_response(@response).lines.sort
done
}
}
}
end
it "should accept a single-frame text message (masked)" do
em {
start_server { |server|
server.onmessage { |msg|
msg.should == 'Hello'
if msg.respond_to?(:encoding)
msg.encoding.should == Encoding.find("UTF-8")
end
done
}
server.onerror {
fail
}
}
start_client { |client|
client.onopen {
client.send_data("\x00\x00\x01\x00\x84\x05Ielln")
}
}
}
end
it "should return close code and reason if closed via handshake" do
em {
start_server { |ws|
ws.onclose { |event|
# 2. Receive close event in server
event.should == {
:code => 4004,
:reason => "close reason",
:was_clean => true,
}
done
}
}
start_client { |client|
client.onopen {
# 1: Send close handshake
close_data = [4004].pack('n')
close_data << "close reason"
client.send_frame(:close, close_data)
}
}
}
end
it "should return close code 1005 if no code was specified" do
em {
start_server { |ws|
ws.onclose { |event|
event.should == {
:code => 1005,
:reason => "",
:was_clean => true,
}
done
}
}
start_client { |client|
client.onopen {
client.send_frame(:close, '')
}
}
}
end
it "should report that close codes are supported" do
em {
start_server { |ws|
ws.onopen {
ws.supports_close_codes?.should == true
done
}
}
start_client
}
end
end

View File

@@ -0,0 +1,105 @@
# encoding: BINARY
require 'helper'
describe "draft13" do
include EM::SpecHelper
default_timeout 1
before :each do
@request = {
:port => 80,
:method => "GET",
:path => "/demo",
:headers => {
'Host' => 'example.com',
'Upgrade' => 'websocket',
'Connection' => 'Upgrade',
'Sec-WebSocket-Key' => 'dGhlIHNhbXBsZSBub25jZQ==',
'Sec-WebSocket-Protocol' => 'sample',
'Sec-WebSocket-Origin' => 'http://example.com',
'Sec-WebSocket-Version' => '13'
}
}
@response = {
:protocol => "HTTP/1.1 101 Switching Protocols\r\n",
:headers => {
"Upgrade" => "websocket",
"Connection" => "Upgrade",
"Sec-WebSocket-Accept" => "s3pPLMBiTxaQ9kYGzzhZRbK+xOo=",
"Sec-WebSocket-Protocol" => "sample",
}
}
end
def start_client
client = EM.connect('0.0.0.0', 12345, Draft07FakeWebSocketClient)
client.send_data(format_request(@request))
yield client if block_given?
return client
end
it_behaves_like "a websocket server" do
let(:version) { 13 }
end
it_behaves_like "a WebSocket server drafts 3 and above" do
let(:version) { 13 }
end
it "should send back the correct handshake response" do
em {
start_server
connection = start_client
connection.onopen {
connection.handshake_response.lines.sort.
should == format_response(@response).lines.sort
done
}
}
end
# TODO: This test would be much nicer with a real websocket client...
it "should support sending pings and binding to onpong" do
em {
start_server { |ws|
ws.onopen {
ws.should be_pingable
EM.next_tick {
ws.ping('hello').should == true
}
}
ws.onpong { |data|
data.should == 'hello'
done
}
}
connection = start_client
# Confusing, fake onmessage means any data after the handshake
connection.onmessage { |data|
# This is what a ping looks like
data.should == "\x89\x05hello"
# This is what a pong looks like
connection.send_data("\x8a\x05hello")
}
}
end
it "should report that close codes are supported" do
em {
start_server { |ws|
ws.onopen {
ws.supports_close_codes?.should == true
done
}
}
start_client
}
end
end

View File

@@ -0,0 +1,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

View File

@@ -0,0 +1,234 @@
# encoding: BINARY
require 'helper'
describe "WebSocket server draft76" do
include EM::SpecHelper
default_timeout 1
before :each do
@request = {
:port => 80,
:method => "GET",
:path => "/demo",
:headers => {
'Host' => 'example.com',
'Connection' => 'Upgrade',
'Sec-WebSocket-Key2' => '12998 5 Y3 1 .P00',
'Sec-WebSocket-Protocol' => 'sample',
'Upgrade' => 'WebSocket',
'Sec-WebSocket-Key1' => '4 @1 46546xW%0l 1 5',
'Origin' => 'http://example.com'
},
:body => '^n:ds[4U'
}
@response = {
:headers => {
"Upgrade" => "WebSocket",
"Connection" => "Upgrade",
"Sec-WebSocket-Location" => "ws://example.com/demo",
"Sec-WebSocket-Origin" => "http://example.com",
"Sec-WebSocket-Protocol" => "sample"
},
:body => "8jKS\'y:G*Co,Wxa-"
}
end
def start_client
client = EM.connect('0.0.0.0', 12345, FakeWebSocketClient)
client.send_data(format_request(@request))
yield client if block_given?
return client
end
it_behaves_like "a websocket server" do
let(:version) { 76 }
end
it "should send back the correct handshake response" do
em {
start_server
start_client { |connection|
connection.onopen {
connection.handshake_response.lines.sort.
should == format_response(@response).lines.sort
done
}
}
}
end
it "should send closing frame back and close the connection after recieving closing frame" do
em {
start_server
connection = start_client
# Send closing frame after handshake complete
connection.onopen {
connection.send_data(EM::WebSocket::Handler76::TERMINATE_STRING)
}
# Check that this causes a termination string to be returned and the
# connection close
connection.onclose {
connection.packets[0].should ==
EM::WebSocket::Handler76::TERMINATE_STRING
done
}
}
end
it "should ignore any data received after the closing frame" do
em {
start_server { |ws|
# Fail if foobar message is received
ws.onmessage { |msg|
fail
}
}
connection = start_client
# Send closing frame after handshake complete, followed by another msg
connection.onopen {
connection.send_data(EM::WebSocket::Handler76::TERMINATE_STRING)
connection.send('foobar')
}
connection.onclose {
done
}
}
end
it "should accept null bytes within the frame after a line return" do
em {
start_server { |ws|
ws.onmessage { |msg|
msg.should == "\n\000"
}
}
connection = start_client
# Send closing frame after handshake complete
connection.onopen {
connection.send_data("\000\n\000\377")
connection.send_data(EM::WebSocket::Handler76::TERMINATE_STRING)
}
connection.onclose {
done
}
}
end
it "should handle unreasonable frame lengths by calling onerror callback" do
em {
start_server { |server|
server.onerror { |error|
error.should be_an_instance_of EM::WebSocket::WSMessageTooBigError
error.message.should == "Frame length too long (1180591620717411303296 bytes)"
done
}
}
client = start_client
# This particular frame indicates a message length of
# 1180591620717411303296 bytes. Such a message would previously cause
# a "bignum too big to convert into `long'" error.
# However it is clearly unreasonable and should be rejected.
client.onopen {
client.send_data("\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\x00")
}
}
end
it "should handle impossible frames by calling onerror callback" do
em {
start_server { |server|
server.onerror { |error|
error.should be_an_instance_of EM::WebSocket::WSProtocolError
error.message.should == "Invalid frame received"
done
}
}
client = start_client
client.onopen {
client.send_data("foobar") # Does not start with \x00 or \xff
}
}
end
it "should handle invalid http requests by raising HandshakeError passed to onerror callback" do
em {
start_server { |server|
server.onerror { |error|
error.should be_an_instance_of EM::WebSocket::HandshakeError
error.message.should == "Invalid HTTP header: Could not parse data entirely (1 != 29)"
done
}
}
client = EM.connect('0.0.0.0', 12345, FakeWebSocketClient)
client.send_data("This is not a HTTP header\r\n\r\n")
}
end
it "should handle handshake request split into two TCP packets" do
em {
start_server
# Create a fake client which sends draft 76 handshake
connection = EM.connect('0.0.0.0', 12345, FakeWebSocketClient)
data = format_request(@request)
# Sends first half of the request
connection.send_data(data[0...(data.length / 2)])
connection.onopen {
connection.handshake_response.lines.sort.
should == format_response(@response).lines.sort
done
}
EM.add_timer(0.1) do
# Sends second half of the request
connection.send_data(data[(data.length / 2)..-1])
end
}
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
it "should call onclose when the server closes the connection [antiregression]" do
em {
start_server { |ws|
ws.onopen {
EM.add_timer(0.1) {
ws.close()
}
}
ws.onclose {
done
}
}
start_client
}
end
end

View File

@@ -0,0 +1,42 @@
shared_examples_for "a WebSocket server drafts 3 and above" do
it "should force close connections after a timeout if close handshake is not sent by the client" do
em {
server_onerror_fired = false
server_onclose_fired = false
client_got_close_handshake = false
start_server(:close_timeout => 0.1) { |ws|
ws.onopen {
# 1: Send close handshake to client
EM.next_tick { ws.close(4999, "Close message") }
}
ws.onerror { |e|
# 3: Client should receive onerror
e.class.should == EM::WebSocket::WSProtocolError
e.message.should == "Close handshake un-acked after 0.1s, closing tcp connection"
server_onerror_fired = true
}
ws.onclose {
server_onclose_fired = true
}
}
start_client { |client|
client.onmessage { |msg|
# 2: Client does not respond to close handshake (the fake client
# doesn't understand them at all hence this is in onmessage)
msg.should =~ /Close message/ if version >= 6
client_got_close_handshake = true
}
client.onclose {
server_onerror_fired.should == true
server_onclose_fired.should == true
client_got_close_handshake.should == true
done
}
}
}
end
end

View File

@@ -0,0 +1,265 @@
# encoding: UTF-8
# These tests are run against all draft versions
#
shared_examples_for "a websocket server" do
it "should expose the protocol version" do
em {
start_server { |ws|
ws.onopen { |handshake|
handshake.protocol_version.should == version
done
}
}
start_client
}
end
it "should expose the origin header" do
em {
start_server { |ws|
ws.onopen { |handshake|
handshake.origin.should == 'http://example.com'
done
}
}
start_client
}
end
it "should expose the remote IP address" do
em {
start_server { |ws|
ws.onopen {
ws.remote_ip.should == "127.0.0.1"
done
}
}
start_client
}
end
it "should send messages successfully" do
em {
start_server { |ws|
ws.onmessage { |message|
message.should == "hello server"
done
}
}
start_client { |client|
client.onopen {
client.send("hello server")
}
}
}
end
it "should allow connection to be closed with valid close code" do
em {
start_server { |ws|
ws.onopen {
ws.close(4004, "Bye bye")
done
}
}
start_client
# TODO: Use a real client which understands how to respond to closing
# handshakes, sending the handshake currently untested
}
end
it "should raise error if if invalid close code is used" do
em {
start_server { |ws|
ws.onopen {
lambda {
ws.close(2000)
}.should raise_error("Application code may only use codes from 1000, 3000-4999")
done
}
}
start_client
}
end
it "should call onclose with was_clean set to false if connection closed without closing handshake by server" do
em {
start_server { |ws|
ws.onopen {
# Close tcp connection (no close handshake)
ws.close_connection
}
ws.onclose { |event|
event.should == {:code => 1006, :was_clean => false}
done
}
}
start_client
}
end
it "should call onclose with was_clean set to false if connection closed without closing handshake by client" do
em {
start_server { |ws|
ws.onclose { |event|
event.should == {:code => 1006, :was_clean => false}
done
}
}
start_client { |client|
client.onopen {
# Close tcp connection (no close handshake)
client.close_connection
}
}
}
end
it "should call onerror if an application error raised in onopen" do
em {
start_server { |ws|
ws.onopen {
raise "application error"
}
ws.onerror { |e|
e.message.should == "application error"
done
}
}
start_client
}
end
it "should call onerror if an application error raised in onmessage" do
em {
start_server { |server|
server.onmessage {
raise "application error"
}
server.onerror { |e|
e.message.should == "application error"
done
}
}
start_client { |client|
client.onopen {
client.send('a message')
}
}
}
end
it "should call onerror in an application error raised in onclose" do
em {
start_server { |server|
server.onclose {
raise "application error"
}
server.onerror { |e|
e.message.should == "application error"
done
}
}
start_client { |client|
client.onopen {
EM.add_timer(0.1) {
client.close_connection
}
}
}
}
end
it "should close the connection when a too long frame is sent" do
em {
start_server { |server|
server.max_frame_size = 20
server.onerror { |e|
# 3: Error should be reported to server
e.class.should == EventMachine::WebSocket::WSMessageTooBigError
e.message.should =~ /Frame length too long/
}
}
start_client { |client|
client.onopen {
EM.next_tick {
client.send("This message is longer than 20 characters")
}
}
client.onmessage { |msg|
# 4: This is actually the close message. Really need to use a real
# WebSocket client in these tests...
done
}
client.onclose {
# 4: Drafts 75 & 76 don't send a close message, they just close the
# connection
done
}
}
}
end
# Only run these tests on ruby 1.9
if "a".respond_to?(:force_encoding)
it "should raise error if you try to send non utf8 text data to ws" do
em {
start_server { |server|
server.onopen {
# Create a string which claims to be UTF-8 but which is not
s = "ê" # utf-8 string
s.encode!("ISO-8859-1")
s.force_encoding("UTF-8")
s.valid_encoding?.should == false # now invalid utf8
# Send non utf8 encoded data
server.send(s)
}
server.onerror { |error|
error.class.should == EventMachine::WebSocket::WebSocketError
error.message.should == "Data sent to WebSocket must be valid UTF-8 but was UTF-8 (valid: false)"
done
}
}
start_client { }
}
end
it "should not change the encoding of strings sent to send [antiregression]" do
em {
start_server { |server|
server.onopen {
s = "example string"
s.force_encoding("UTF-8")
server.send(s)
s.encoding.should == Encoding.find("UTF-8")
done
}
}
start_client { }
}
end
end
end