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
  
inherit "../pike_test_common"; 
 
class Connection { 
  inherit Protocols.WebSocket.Connection; 
 
  Concurrent.Promise p; 
  array(string) tosend = ({}); 
  array received = ({}); 
 
  void send_next() 
  { 
    if (sizeof(tosend)) { 
      send_text(tosend[0]); 
      tosend = tosend[1..]; 
      return; 
    } 
    close(); 
  } 
 
  void got_open(mixed id) 
  { 
    received += ({ -1 }); 
    send_next(); 
  } 
 
  void got_close(int reason, mixed id) 
  { 
    received += ({ reason }); 
    p->success(received); 
  } 
 
  void got_message(Protocols.WebSocket.Frame frame, mixed id) 
  { 
    if (frame->opcode == Protocols.WebSocket.FRAME_TEXT) { 
      received += ({ frame->text }); 
      send_next(); 
    } 
  } 
 
  protected void create(Concurrent.Promise p) 
  { 
    this::p = p; 
    onclose = got_close; 
    onopen = got_open; 
    onmessage = got_message; 
 
    ::create(); 
  } 
} 
 
void run_tests(Configuration c) 
{ 
  foreach(c->query("URLs"), string url) { 
    if (!has_prefix(url, "http://") && !has_prefix(url, "https://")) { 
      continue; 
    } 
    url = (url/"#")[0]; 
 
    Standards.URI uri = Standards.URI("websocket_example/", url); 
    if (uri->host == "*") uri->host = "127.0.0.1"; 
 
    Concurrent.Promise p = Concurrent.Promise(); 
    Connection ws = Connection(p); 
 
    array expect = ({ -1 }); 
    for (int i = 0; i < 5; i++) { 
      ws->tosend += ({ sprintf("%d %d", 17, i+1) }); 
      expect += ({ sprintf("%d %d", i+1, 17) }); 
    } 
    expect += ({ Protocols.WebSocket.CLOSE_NORMAL }); 
    if (test_true(ws->connect, uri)) { 
      test_equal(expect, p->future()->get); 
    } 
 
    p = Concurrent.Promise(); 
    ws = Connection(p); 
 
    expect = ({ -1 }); 
    for (int i = 0; i < 5; i++) { 
      ws->tosend += ({ sprintf("%d %d", 18, i+1) }); 
      expect += ({ sprintf("%d %d", i+1, 18) }); 
    } 
    // Sending a message with the wrong id should trigger 
    // a remote close. 
    ws->tosend += ({ sprintf("%d %d", 19, 7) }); 
    expect += ({ Protocols.WebSocket.CLOSE_NORMAL }); 
    // NB: The following two should not generate any messages 
    //     (or even be sent). 
    ws->tosend += ({ sprintf("%d %d", 18, 8) }); 
    ws->tosend += ({ sprintf("%d %d", 18, 9) }); 
    array expected_trailers = ws->tosend[sizeof(ws->tosend)-2..]; 
    if (test_true(ws->connect, uri)) { 
      test_equal(expect, p->future()->get); 
      test_equal(expected_trailers, `->, ws, "tosend"); 
    } 
  } 
}