1 <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
4 <script src="../../../../js-test-resources/js-test-pre.js"></script>
7 <div id="description"></div>
8 <div id="console"></div>
9 <script type="text/javascript">
10 description("Web Socket bufferedAmount after closed");
12 window.jsTestIsAsync = true;
13 if (window.layoutTestController)
14 layoutTestController.overridePreference("WebKitHixie76WebSocketProtocolEnabled", 0);
16 function createStringWithLength(length)
19 while (string.length < length)
20 string = string + string;
21 return string.substring(0, length);
24 function createBlobWithLength(length)
26 return new Blob([new ArrayBuffer(length)]);
29 var ws = new WebSocket("ws://localhost:8880/websocket/tests/hybi/simple");
31 ws.onopen = function()
37 ws.onclose = function()
40 shouldBe("ws.readyState", "3");
41 shouldBe("ws.bufferedAmount", "0");
43 var baseOverhead = 2 + 4; // Base header size and masking key size.
44 debug("Testing send(string)...");
45 testBufferedAmount('send to closed socket', 21 + baseOverhead);
46 testBufferedAmount('', baseOverhead);
47 testBufferedAmount('a', 1 + baseOverhead);
48 testBufferedAmount(createStringWithLength(125), 125 + baseOverhead);
49 testBufferedAmount(createStringWithLength(126), 126 + baseOverhead + 2); // With 16-bit extended payload length field.
50 testBufferedAmount(createStringWithLength(0xFFFF), 0xFFFF + baseOverhead + 2);
51 testBufferedAmount(createStringWithLength(0x10000), 0x10000 + baseOverhead + 8); // With 64-bit extended payload length field.
53 debug("Testing send(ArrayBuffer)...");
54 testBufferedAmount(new ArrayBuffer(0), baseOverhead);
55 testBufferedAmount(new ArrayBuffer(1), 1 + baseOverhead);
56 testBufferedAmount(new ArrayBuffer(125), 125 + baseOverhead);
57 testBufferedAmount(new ArrayBuffer(126), 126 + baseOverhead + 2);
58 testBufferedAmount(new ArrayBuffer(0xFFFF), 0xFFFF + baseOverhead + 2);
59 testBufferedAmount(new ArrayBuffer(0x10000), 0x10000 + baseOverhead + 8);
61 debug("Testing send(Blob)...");
62 testBufferedAmount(createBlobWithLength(0), baseOverhead);
63 testBufferedAmount(createBlobWithLength(1), 1 + baseOverhead);
64 testBufferedAmount(createBlobWithLength(125), 125 + baseOverhead);
65 testBufferedAmount(createBlobWithLength(126), 126 + baseOverhead + 2);
66 testBufferedAmount(createBlobWithLength(0xFFFF), 0xFFFF + baseOverhead + 2);
67 testBufferedAmount(createBlobWithLength(0x10000), 0x10000 + baseOverhead + 8);
73 var bufferedAmountDifference;
75 function testBufferedAmount(message, expectedBufferedAmountDifference)
77 // If the connection is closed, bufferedAmount attribute's value will only
78 // increase with each call to the send() method.
79 // (the number does not reset to zero once the connection closes).
80 messageToSend = message;
81 var bufferedAmountBeforeSend = ws.bufferedAmount;
82 shouldBeFalse("ws.send(messageToSend)");
83 var bufferedAmountAfterSend = ws.bufferedAmount;
84 bufferedAmountDifference = bufferedAmountAfterSend - bufferedAmountBeforeSend;
85 shouldEvaluateTo("bufferedAmountDifference", expectedBufferedAmountDifference);
89 <script src="../../../../js-test-resources/js-test-post.js"></script>