File tree 9 files changed +67
-48
lines changed 9 files changed +67
-48
lines changed Original file line number Diff line number Diff line change 8
8
require 'em-websocket'
9
9
require 'em-websocket-client'
10
10
11
+ require 'integration/shared_examples'
12
+ require 'integration/gte_03_examples'
13
+
11
14
RSpec . configure do |c |
12
15
c . mock_with :rspec
13
16
end
@@ -128,6 +131,12 @@ def close_connection
128
131
end
129
132
end
130
133
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
+
131
140
def format_request ( r )
132
141
data = "#{ r [ :method ] } #{ r [ :path ] } HTTP/1.1\r \n "
133
142
header_lines = r [ :headers ] . map { |k , v | "#{ k } : #{ v } " }
Original file line number Diff line number Diff line change 6
6
include EM ::SpecHelper
7
7
default_timeout 1
8
8
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
-
15
9
it "should fail on non WebSocket requests" do
16
10
em {
17
11
EM . add_timer ( 0.1 ) do
Original file line number Diff line number Diff line change 1
1
require 'helper'
2
- require 'integration/shared_examples'
3
2
4
3
describe "draft03" do
5
4
include EM ::SpecHelper
35
34
}
36
35
end
37
36
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
-
44
37
def start_client
45
38
client = EM . connect ( '0.0.0.0' , 12345 , Draft03FakeWebSocketClient )
46
39
client . send_data ( format_request ( @request ) )
@@ -52,6 +45,10 @@ def start_client
52
45
let ( :version ) { 3 }
53
46
end
54
47
48
+ it_behaves_like "a WebSocket server drafts 3 and above" do
49
+ let ( :version ) { 3 }
50
+ end
51
+
55
52
# These examples are straight from the spec
56
53
# http://tools.ietf.org/html/draft-ietf-hybi-thewebsocketprotocol-03#section-4.6
57
54
describe "examples from the spec" do
Original file line number Diff line number Diff line change 1
1
require 'helper'
2
- require 'integration/shared_examples'
3
2
4
3
describe "draft05" do
5
4
include EM ::SpecHelper
21
20
}
22
21
}
23
22
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
30
23
31
24
def start_client
32
25
client = EM . connect ( '0.0.0.0' , 12345 , Draft05FakeWebSocketClient )
@@ -39,6 +32,10 @@ def start_client
39
32
let ( :version ) { 5 }
40
33
end
41
34
35
+ it_behaves_like "a WebSocket server drafts 3 and above" do
36
+ let ( :version ) { 5 }
37
+ end
38
+
42
39
it "should report that close codes are not supported" do
43
40
em {
44
41
start_server { |ws |
Original file line number Diff line number Diff line change 1
1
require 'helper'
2
- require 'integration/shared_examples'
3
2
4
3
describe "draft06" do
5
4
include EM ::SpecHelper
30
29
}
31
30
}
32
31
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
39
32
40
33
def start_client
41
34
client = EM . connect ( '0.0.0.0' , 12345 , Draft05FakeWebSocketClient )
@@ -48,6 +41,10 @@ def start_client
48
41
let ( :version ) { 6 }
49
42
end
50
43
44
+ it_behaves_like "a WebSocket server drafts 3 and above" do
45
+ let ( :version ) { 6 }
46
+ end
47
+
51
48
it "should open connection" do
52
49
em {
53
50
start_server { |server |
Original file line number Diff line number Diff line change 1
1
# encoding: BINARY
2
2
3
3
require 'helper'
4
- require 'integration/shared_examples'
5
4
6
5
describe "draft13" do
7
6
include EM ::SpecHelper
33
32
}
34
33
end
35
34
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
-
42
35
def start_client
43
36
client = EM . connect ( '0.0.0.0' , 12345 , Draft07FakeWebSocketClient )
44
37
client . send_data ( format_request ( @request ) )
@@ -50,6 +43,10 @@ def start_client
50
43
let ( :version ) { 13 }
51
44
end
52
45
46
+ it_behaves_like "a WebSocket server drafts 3 and above" do
47
+ let ( :version ) { 13 }
48
+ end
49
+
53
50
it "should send back the correct handshake response" do
54
51
em {
55
52
start_server
Original file line number Diff line number Diff line change 1
1
require 'helper'
2
- require 'integration/shared_examples'
3
2
4
3
# These integration tests are older and use a different testing style to the
5
4
# integration tests for newer drafts. They use EM::HttpRequest which happens
9
8
include EM ::SpecHelper
10
9
default_timeout 1
11
10
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
-
18
11
def start_client
19
12
client = Draft75WebSocketClient . new
20
13
yield client if block_given?
Original file line number Diff line number Diff line change 1
1
# encoding: BINARY
2
2
3
3
require 'helper'
4
- require 'integration/shared_examples'
5
4
6
5
describe "WebSocket server draft76" do
7
6
include EM ::SpecHelper
35
34
:body => "8jKS\' y:G*Co,Wxa-"
36
35
}
37
36
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
44
37
45
38
def start_client
46
39
client = EM . connect ( '0.0.0.0' , 12345 , FakeWebSocketClient )
Original file line number Diff line number Diff line change
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
You can’t perform that action at this time.
0 commit comments