File: common_spec.rb

package info (click to toggle)
ruby-em-websocket 0.5.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 416 kB
  • sloc: ruby: 3,137; makefile: 5
file content (138 lines) | stat: -rw-r--r-- 3,942 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
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