Bug 676025 - Websockets - failure to connect should generate onerror r=sicking
authorPatrick McManus <mcmanus@ducksong.com>
Wed, 03 Aug 2011 12:42:04 -0400
changeset 73774 36b1761da39dbd75e2fe83786e3ecf0a589af4d3
parent 73773 ccbd9a4df280622fe5701b054a555264553e2170
child 73775 144add433e72577f4f2824ede07b91f08d868382
push id2
push userbsmedberg@mozilla.com
push dateFri, 19 Aug 2011 14:38:13 +0000
reviewerssicking
bugs676025
milestone8.0a1
Bug 676025 - Websockets - failure to connect should generate onerror r=sicking
content/base/src/nsWebSocket.cpp
content/base/test/test_websocket.html
--- a/content/base/src/nsWebSocket.cpp
+++ b/content/base/src/nsWebSocket.cpp
@@ -526,17 +526,17 @@ nsWebSocketEstablishedConnection::OnStop
   if (aStatusCode == NS_BASE_STREAM_CLOSED && 
       mOwner->mReadyState >= nsIMozWebSocket::CLOSING) {
     // don't generate an error event just because of an unclean close
     aStatusCode = NS_OK;
   }
 
   if (NS_FAILED(aStatusCode)) {
     ConsoleError();
-    if (mOwner && mOwner->mReadyState != nsIMozWebSocket::CONNECTING) {
+    if (mOwner) {
       nsresult rv =
         mOwner->CreateAndDispatchSimpleEvent(NS_LITERAL_STRING("error"));
       if (NS_FAILED(rv))
         NS_WARNING("Failed to dispatch the error event");
     }
   }
 
   mStatus = CONN_CLOSED;
--- a/content/base/test/test_websocket.html
+++ b/content/base/test/test_websocket.html
@@ -99,16 +99,20 @@ function shouldCloseCleanly(e)
 }
 
 function shouldCloseNotCleanly(e)
 {
   var ws = e.target;
   ok(!e.wasClean, "the ws connection in test " + ws._testNumber + " shouldn't be closed cleanly");
 }
 
+function ignoreError(e)
+{
+}
+
 function CreateTestWS(ws_location, ws_protocol)
 {
   var ws;
 
   try {
     if (ws_protocol == undefined) {
       ws = new MozWebSocket(ws_location);
     } else {
@@ -215,21 +219,28 @@ function test2()
   {
     ok(ws2CanConnect, "shouldn't connect yet in test-2!");
     doTest(3);
   }
 }
 
 function test3()
 {
+  var hasError = false;
   var ws = CreateTestWS("ws://this.websocket.server.probably.does.not.exist");
   ws.onopen = shouldNotOpen;
+  ws.onerror = function (e)
+  {
+    hasError = true;
+  }
+
   ws.onclose = function(e)
   {
     shouldCloseNotCleanly(e);
+    ok(hasError, "rcvd onerror event");
     doTest(4);
   };
 }
 
 function test4()
 {
   try {
     var ws = CreateTestWS("file_websocket");
@@ -535,27 +546,29 @@ function test17()
 // The tests that expects that their websockets neither open nor close MUST
 // be in the end of the tests, i.e. HERE, in order to prevent blocking the other
 // tests.
 
 function test18()
 {
   var ws = CreateTestWS("ws://mochi.test:8888/tests/content/base/test/file_websocket_http_resource.txt");
   ws.onopen = shouldNotOpen;
+  ws.onerror = ignoreError;
   ws.onclose = function(e)
   {
     shouldCloseNotCleanly(e);
     doTest(19);
   };
 }
 
 function test19()
 {
   var ws = CreateTestWS("ws://mochi.test:8888/tests/content/base/test/file_websocket", "test-19");
   ws.onopen = shouldNotOpen;
+  ws.onerror = ignoreError;
   ws.onclose = function(e)
   {
     shouldCloseNotCleanly(e);
     doTest(20);
   };
 }
 
 window._test20 = function()
@@ -633,16 +646,17 @@ function test21()
 {
   window._test21();
 }
 
 function test22()
 {
   var ws = CreateTestWS("ws://mochi.test:8888/tests/content/base/test/file_websocket", "test-22");
   ws.onopen = shouldNotOpen;
+  ws.onerror = ignoreError;
   ws.onclose = function(e)
   {
     shouldCloseNotCleanly(e);
     doTest(23);
   };
 }
 
 function test23()
@@ -667,21 +681,22 @@ function test24()
   }
 }
 
 function test25()
 {
   var prots=[];
 
   var ws = CreateTestWS("ws://mochi.test:8888/tests/content/base/test/file_websocket", prots);
-  ws.onopen = function(e)
-  {
-    ok(true, "test 25 protocol array open");
-    ws.close();
-  };
+
+  // This test errors because the server requires a sub-protocol, but
+  // the test just wants to ensure that the ctor doesn't generate an
+  // exception
+  ws.onerror = ignoreError;
+  ws.onopen = shouldNotOpen;
 
   ws.onclose = function(e)
   {
     ok(ws.protocol == "", "test25 subprotocol selection");
     ok(true, "test 25 protocol array close");
     doTest(26);
   };
 }