Skip to content
This repository
Browse code

Add basic websocket test.

It performs the handshake, then sends one text message and checks
that it is echoed back correctly.
  • Loading branch information...
commit 940ea3a4fa89a233bdaf822478e359c6f84dbbc7 1 parent 2f3a4bf
JD Bothma authored July 31, 2012

Showing 1 changed file with 35 additions and 0 deletions. Show diff stats Hide diff stats

  1. 35  test/t2/app_test.erl
35  test/t2/app_test.erl
@@ -33,6 +33,7 @@ start() ->
33 33
     throw_test(),
34 34
     too_many_headers_test(),
35 35
     index_files_test(),
  36
+    websocket_test(),
36 37
     ibrowse:stop().
37 38
 
38 39
 
@@ -812,6 +813,40 @@ index_files_test() ->
812 813
     ?line {ok, "200", _, Content} = ibrowse:send_req(Uri2, [], get),
813 814
     ok.
814 815
 
  816
+%% Do handshake, then send "hello" in a text frame
  817
+%% and check that "hello" is echoed back.
  818
+websocket_test() ->
  819
+    OpenHeads = "GET /websockets_example_endpoint.yaws HTTP/1.1\r\n"
  820
+        "Host: localhost\r\n"
  821
+        "Upgrade: websocket\r\n"
  822
+        "Connection: Upgrade\r\n"
  823
+        "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"
  824
+        "Origin: http://localhost\r\n"
  825
+        "Sec-WebSocket-Version: 13\r\n\r\n",
  826
+    Opts = [{send_timeout, 2000},
  827
+            binary,
  828
+            {packet, 0},
  829
+            {active, false}],
  830
+    {ok, Sock} = gen_tcp:connect(localhost, 8000, Opts),
  831
+    ok = gen_tcp:send(Sock, OpenHeads),
  832
+    {ok, Packet} = gen_tcp:recv(Sock, 0, 2000),
  833
+    AcceptLines = re:split(Packet, "[\r\n]+", [{return, list}, trim]),
  834
+    %% Check that exactly the expected lines are there, in any order
  835
+    4 = length(AcceptLines),
  836
+    true = lists:member("HTTP/1.1 101 Switching Protocols", AcceptLines),
  837
+    true = lists:member("Upgrade: websocket", AcceptLines),
  838
+    true = lists:member("Connection: Upgrade", AcceptLines),
  839
+    true = lists:member("Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=",
  840
+                        AcceptLines),
  841
+
  842
+    %% Text frame "hello" with mask "aaaa"
  843
+    Frame =
  844
+        <<129, 133, "aaaa", 38722669838:5/integer-unit:8>>,
  845
+    ok = gen_tcp:send(Sock, Frame),
  846
+    ExpectFrame = <<129, 5, "hello">>,
  847
+    {ok, ExpectFrame} = gen_tcp:recv(Sock, 0, 2000).
  848
+
  849
+
815 850
 %% used for appmod tests
816 851
 %%
817 852
 out(_A) ->

0 notes on commit 940ea3a

Please sign in to comment.
Something went wrong with that request. Please try again.