Skip to content

Commit 7fc345c

Browse files
committed
Specs for behaviour on un-ack'd close connections
1 parent 65374e7 commit 7fc345c

9 files changed

+67
-48
lines changed

spec/helper.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
require 'em-websocket'
99
require 'em-websocket-client'
1010

11+
require 'integration/shared_examples'
12+
require 'integration/gte_03_examples'
13+
1114
RSpec.configure do |c|
1215
c.mock_with :rspec
1316
end
@@ -128,6 +131,12 @@ def close_connection
128131
end
129132
end
130133

134+
def start_server(opts = {})
135+
EM::WebSocket.run({:host => "0.0.0.0", :port => 12345}.merge(opts)) { |ws|
136+
yield ws if block_given?
137+
}
138+
end
139+
131140
def format_request(r)
132141
data = "#{r[:method]} #{r[:path]} HTTP/1.1\r\n"
133142
header_lines = r[:headers].map { |k,v| "#{k}: #{v}" }

spec/integration/common_spec.rb

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,6 @@
66
include EM::SpecHelper
77
default_timeout 1
88

9-
def start_server
10-
EM::WebSocket.run(:host => "0.0.0.0", :port => 12345) { |ws|
11-
yield ws if block_given?
12-
}
13-
end
14-
159
it "should fail on non WebSocket requests" do
1610
em {
1711
EM.add_timer(0.1) do

spec/integration/draft03_spec.rb

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
require 'helper'
2-
require 'integration/shared_examples'
32

43
describe "draft03" do
54
include EM::SpecHelper
@@ -35,12 +34,6 @@
3534
}
3635
end
3736

38-
def start_server
39-
EM::WebSocket.run(:host => "0.0.0.0", :port => 12345) { |ws|
40-
yield ws if block_given?
41-
}
42-
end
43-
4437
def start_client
4538
client = EM.connect('0.0.0.0', 12345, Draft03FakeWebSocketClient)
4639
client.send_data(format_request(@request))
@@ -52,6 +45,10 @@ def start_client
5245
let(:version) { 3 }
5346
end
5447

48+
it_behaves_like "a WebSocket server drafts 3 and above" do
49+
let(:version) { 3 }
50+
end
51+
5552
# These examples are straight from the spec
5653
# http://tools.ietf.org/html/draft-ietf-hybi-thewebsocketprotocol-03#section-4.6
5754
describe "examples from the spec" do

spec/integration/draft05_spec.rb

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
require 'helper'
2-
require 'integration/shared_examples'
32

43
describe "draft05" do
54
include EM::SpecHelper
@@ -21,12 +20,6 @@
2120
}
2221
}
2322
end
24-
25-
def start_server
26-
EM::WebSocket.run(:host => "0.0.0.0", :port => 12345) { |ws|
27-
yield ws if block_given?
28-
}
29-
end
3023

3124
def start_client
3225
client = EM.connect('0.0.0.0', 12345, Draft05FakeWebSocketClient)
@@ -39,6 +32,10 @@ def start_client
3932
let(:version) { 5 }
4033
end
4134

35+
it_behaves_like "a WebSocket server drafts 3 and above" do
36+
let(:version) { 5 }
37+
end
38+
4239
it "should report that close codes are not supported" do
4340
em {
4441
start_server { |ws|

spec/integration/draft06_spec.rb

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
require 'helper'
2-
require 'integration/shared_examples'
32

43
describe "draft06" do
54
include EM::SpecHelper
@@ -30,12 +29,6 @@
3029
}
3130
}
3231
end
33-
34-
def start_server
35-
EM::WebSocket.run(:host => "0.0.0.0", :port => 12345) { |ws|
36-
yield ws if block_given?
37-
}
38-
end
3932

4033
def start_client
4134
client = EM.connect('0.0.0.0', 12345, Draft05FakeWebSocketClient)
@@ -48,6 +41,10 @@ def start_client
4841
let(:version) { 6 }
4942
end
5043

44+
it_behaves_like "a WebSocket server drafts 3 and above" do
45+
let(:version) { 6 }
46+
end
47+
5148
it "should open connection" do
5249
em {
5350
start_server { |server|

spec/integration/draft13_spec.rb

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
# encoding: BINARY
22

33
require 'helper'
4-
require 'integration/shared_examples'
54

65
describe "draft13" do
76
include EM::SpecHelper
@@ -33,12 +32,6 @@
3332
}
3433
end
3534

36-
def start_server
37-
EM::WebSocket.run(:host => "0.0.0.0", :port => 12345) { |ws|
38-
yield ws if block_given?
39-
}
40-
end
41-
4235
def start_client
4336
client = EM.connect('0.0.0.0', 12345, Draft07FakeWebSocketClient)
4437
client.send_data(format_request(@request))
@@ -50,6 +43,10 @@ def start_client
5043
let(:version) { 13 }
5144
end
5245

46+
it_behaves_like "a WebSocket server drafts 3 and above" do
47+
let(:version) { 13 }
48+
end
49+
5350
it "should send back the correct handshake response" do
5451
em {
5552
start_server

spec/integration/draft75_spec.rb

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
require 'helper'
2-
require 'integration/shared_examples'
32

43
# These integration tests are older and use a different testing style to the
54
# integration tests for newer drafts. They use EM::HttpRequest which happens
@@ -9,12 +8,6 @@
98
include EM::SpecHelper
109
default_timeout 1
1110

12-
def start_server
13-
EM::WebSocket.run(:host => "0.0.0.0", :port => 12345) { |ws|
14-
yield ws if block_given?
15-
}
16-
end
17-
1811
def start_client
1912
client = Draft75WebSocketClient.new
2013
yield client if block_given?

spec/integration/draft76_spec.rb

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
# encoding: BINARY
22

33
require 'helper'
4-
require 'integration/shared_examples'
54

65
describe "WebSocket server draft76" do
76
include EM::SpecHelper
@@ -35,12 +34,6 @@
3534
:body => "8jKS\'y:G*Co,Wxa-"
3635
}
3736
end
38-
39-
def start_server
40-
EM::WebSocket.run(:host => "0.0.0.0", :port => 12345) { |ws|
41-
yield ws if block_given?
42-
}
43-
end
4437

4538
def start_client
4639
client = EM.connect('0.0.0.0', 12345, FakeWebSocketClient)

spec/integration/gte_03_examples.rb

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
shared_examples_for "a WebSocket server drafts 3 and above" do
2+
it "should force close connections after a timeout if close handshake is not sent by the client" do
3+
em {
4+
server_onerror_fired = false
5+
server_onclose_fired = false
6+
client_got_close_handshake = false
7+
8+
start_server(:close_timeout => 0.1) { |ws|
9+
ws.onopen {
10+
# 1: Send close handshake to client
11+
EM.next_tick { ws.close(4999, "Close message") }
12+
}
13+
14+
ws.onerror { |e|
15+
# 3: Client should receive onerror
16+
e.class.should == EM::WebSocket::WSProtocolError
17+
e.message.should == "Close handshake un-acked after 0.1s, closing tcp connection"
18+
server_onerror_fired = true
19+
}
20+
21+
ws.onclose {
22+
server_onclose_fired = true
23+
}
24+
}
25+
start_client { |client|
26+
client.onmessage { |msg|
27+
# 2: Client does not respond to close handshake (the fake client
28+
# doesn't understand them at all hence this is in onmessage)
29+
msg.should =~ /Close message/ if version >= 6
30+
client_got_close_handshake = true
31+
}
32+
33+
client.onclose {
34+
server_onerror_fired.should == true
35+
server_onclose_fired.should == true
36+
client_got_close_handshake.should == true
37+
done
38+
}
39+
}
40+
}
41+
end
42+
end

0 commit comments

Comments
 (0)