@@ -15,75 +15,7
6D47
1 @@
15
15
ServerExamples = Sus ::Shared ( 'a websocket server' ) do
16
16
let ( :websocket_client ) { Async ::WebSocket ::Client . open ( client_endpoint ) }
17
17
18
- with 'generic application' do
19
- let ( :message ) { "Hello World" }
18
+ it "can establish connection" do
19
+ connection = websocket_client . connect ( endpoint . authority , "/server" )
20
+
21
+ connection . send_text ( "Hello World!" )
22
+ message = connection . read
23
+ expect ( message . to_str ) . to be == "Hello World!"
24
+
25
+ connection . close
26
+ end
27
+
28
+ it "can establish connection with block" do
29
+ websocket_client . connect ( endpoint . authority , "/server" ) do |connection |
30
+ connection . send_text ( "Hello World!" )
31
+ message = connection . read
32
+ expect ( message . to_str ) . to be == "Hello World!"
33
+ end
34
+ end
35
+
36
+ with "headers" do
37
+ let ( :headers ) { { "foo" => "bar" } }
20
38
21
39
let ( :app ) do
22
40
Protocol ::HTTP ::Middleware . for do |request |
23
41
Async ::WebSocket ::Adapters ::HTTP . open ( request ) do |connection |
24
- connection . send_text ( message )
42
+ message = Protocol ::WebSocket ::JSONMessage . generate ( request . headers . fields )
43
+ message . send ( connection )
44
+
25
45
connection . close
26
46
end or Protocol ::HTTP ::Response [ 404 , { } , [ ] ]
27
47
end
28
48
end
29
49
30
- it "can establish connection " do
31
- connection = websocket_client . connect ( endpoint . authority , "/server" )
50
+ it "can send headers " do
51
+ connection = websocket_client . connect ( endpoint . authority , "/headers" , headers : headers )
32
52
33
53
begin
34
- expect ( connection . read ) . to be == message
54
+ json_message = Protocol ::WebSocket ::JSONMessage . wrap ( connection . read )
55
+
56
+ expect ( json_message . to_h ) . to have_keys ( *headers . keys )
35
57
expect ( connection . read ) . to be_nil
36
58
expect ( connection ) . to be ( :closed? )
37
59
ensure
38
60
connection . close
39
61
end
40
62
end
41
-
42
- with "headers" do
43
- let ( :headers ) { { "foo" => "bar" } }
44
-
45
- let ( :app ) do
46
- Protocol ::HTTP ::Middleware . for do |request |
47
- Async ::WebSocket ::Adapters ::HTTP . open ( request ) do |connection |
48
- message = Protocol ::WebSocket ::JSONMessage . generate ( request . headers . fields )
49
- message . send ( connection )
50
-
51
- connection . close
52
- end or Protocol ::HTTP ::Response [ 404 , { } , [ ] ]
53
- end
54
- end
55
-
56
- it "can send headers" do
57
- connection = websocket_client . connect ( endpoint . authority , "/headers" , headers : headers )
58
-
59
- begin
60
- json_message = Protocol ::WebSocket ::JSONMessage . wrap ( connection . read )
61
-
62
- expect ( json_message . to_h ) . to have_keys ( *headers . keys )
63
- expect ( connection . read ) . to be_nil
64
- expect ( connection ) . to be ( :closed? )
65
- ensure
66
- connection . close
67
- end
68
- end
69
- end
70
63
end
71
64
72
65
with 'server middleware' do
73
66
let ( :app ) do
74
67
Protocol ::HTTP ::Middleware . build do
75
- use Async ::WebSocket ::Server do |connection |
76
- connection . send_text ( "Hello World " )
68
+ use Async ::WebSocket ::Server , protocols : [ 'echo' , 'baz' ] do |connection |
69
+ connection . send_text ( "protocol: #{ connection . protocol } " )
77
70
connection . close
78
71
end
79
72
end
80
73
end
81
74
82
- it "can establish connection" do
83
- connection = websocket_client . connect ( endpoint . authority , "/server" )
75
+ it "can establish connection with explicit protocol" do
76
+ connection = websocket_client . connect ( endpoint . authority , "/server" , protocols : [ 'echo' , 'foo' , 'bar' ] )
77
+
78
+ # The negotiated protocol:
79
+ expect ( connection . protocol ) . to be == 'echo'
84
80
85
81
begin
86
- expect ( connection . read ) . to be == "Hello World "
82
+ expect ( connection . read ) . to be == "protocol: echo "
87
83
expect ( connection . read ) . to be_nil
88
84
expect ( connection ) . to be ( :closed? )
89
85
ensure
96
92
describe Async ::WebSocket ::Server do
97
93
include Sus ::Fixtures ::Async ::HTTP ::ServerContext
98
94
95
+ let ( :app ) do
96
+ Protocol ::HTTP ::Middleware . for do |request |
97
+ Async ::WebSocket ::Adapters ::HTTP . open ( request ) do |connection |
98
+ while message = connection . read
99
+ connection . write ( message )
100
+ end
101
+ end or Protocol ::HTTP ::Response [ 404 , { } , [ ] ]
102
+ end
103
+ end
104
+
99
105
with 'http/1' do
100
106
let ( :protocol ) { Async ::HTTP ::Protocol ::HTTP1 }
101
107
it_behaves_like ServerExamples
108
+
109
+ it "fails with bad request if missing nounce" do
110
+ request = Protocol ::HTTP ::Request [ "GET" , "/" , {
111
+ "upgrade" => "websocket" ,
112
+ "connection" => "upgrade" ,
113
+ } ]
114
+
115
+ response = client . call ( request )
116
+
117
+ expect ( response ) . to be ( :bad_request? )
118
+ end
102
119
end
103
120
104
121
with 'http/2' do
0 commit comments