diff --git a/dist/mock-socket.js b/dist/mock-socket.js index bc9fc910..9ef525d8 100644 --- a/dist/mock-socket.js +++ b/dist/mock-socket.js @@ -291,9 +291,17 @@ function MockServer(url) { globalContext.MockSocket.services[this.url] = service; this.service = service; - service.server = this; + // ignore possible query parameters + if(url.indexOf(MockServer.unresolvableURL) === -1) { + service.server = this; + } } +/* +* This URL can be used to emulate server that does not establish connection +*/ +MockServer.unresolvableURL = "ws://unresolvable_url"; + MockServer.prototype = { service: null, @@ -526,8 +534,8 @@ SocketService.prototype = { // if the server has not been set then we notify the onclose method of this client if(!this.server) { - this.notify(client, 'updateReadyState', globalContext.MockSocket.CLOSED); - this.notifyOnlyFor(client, 'clientOnError'); + this.notifyOnlyFor(client, 'updateReadyState', globalContext.MockSocket.CLOSED); + this.notifyOnlyFor(client, 'clientOnError', socketMessageEvent('error', null, client.url)); return false; } diff --git a/dist/mock-socket.min.js b/dist/mock-socket.min.js index 7b4c4fd8..317995d9 100644 --- a/dist/mock-socket.min.js +++ b/dist/mock-socket.min.js @@ -1 +1 @@ -!function e(t,n,r){function s(o,l){if(!n[o]){if(!t[o]){var c="function"==typeof require&&require;if(!l&&c)return c(o,!0);if(i)return i(o,!0);var a=new Error("Cannot find module '"+o+"'");throw a.code="MODULE_NOT_FOUND",a}var u=n[o]={exports:{}};t[o][0].call(u.exports,function(e){var n=t[o][1][e];return s(n?n:e)},u,u.exports,e,t,n,r)}return n[o].exports}for(var i="function"==typeof require&&require,o=0;o3?"/":"")+t.slice(3,t.length).join("/").split("?")[0].split("#")[0];var o=r.pathname;"/"===o.charAt(o.length-1)&&(o=o.substring(0,o.length-1));var l=r.hostname,c=l.split("."),a=o.split("/");if("hostname"===e)return l;if("domain"===e)return/^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$/.test(l)?l:c.slice(-2).join(".");if("sub"===e)return c.slice(0,c.length-2).join(".");if("port"===e)return r.port;if("protocol"===e)return r.protocol.split(":")[0];if("auth"===e)return r.auth;if("user"===e)return r.auth.split(":")[0];if("pass"===e)return r.auth.split(":")[1]||"";if("path"===e)return r.pathname;if("."===e.charAt(0)){if(e=e.substring(1),s(e))return e=parseInt(e,10),c[0>e?c.length+e:e-1]||""}else{if(s(e))return e=parseInt(e,10),a[0>e?a.length+e:e]||"";if("file"===e)return a.slice(-1)[0];if("filename"===e)return a.slice(-1)[0].split(".")[0];if("fileext"===e)return a.slice(-1)[0].split(".")[1]||"";if("?"===e.charAt(0)||"#"===e.charAt(0)){var u=n,h=null;if("?"===e.charAt(0)?u=(u.split("?")[1]||"").split("#")[0]:"#"===e.charAt(0)&&(u=u.split("#")[1]||""),!e.charAt(1))return u;e=e.substring(1),u=u.split("&");for(var f=0,p=u.length;p>f;f++)if(h=u[f].split("="),h[0]===e)return h[1]||"";return null}}return""}t.exports=r},{}],5:[function(e,t,n){function r(e){var t=function(t){return function(n){n.target=e,t.apply(e,arguments)}};Object.defineProperties(e,{onopen:{enumerable:!0,get:function(){return this._onopen},set:function(n){this._onopen=t(n),this.service.setCallbackObserver("clientOnOpen",this._onopen,e)}},onmessage:{enumerable:!0,get:function(){return this._onmessage},set:function(n){this._onmessage=t(n),this.service.setCallbackObserver("clientOnMessage",this._onmessage,e)}},onclose:{enumerable:!0,get:function(){return this._onclose},set:function(n){this._onclose=t(n),this.service.setCallbackObserver("clientOnclose",this._onclose,e)}},onerror:{enumerable:!0,get:function(){return this._onerror},set:function(n){this._onerror=t(n),this.service.setCallbackObserver("clientOnError",this._onerror,e)}}})}t.exports=r},{}],6:[function(e,t,n){var r=e("./service"),s=e("./mock-server"),i=e("./mock-socket"),o=e("./helpers/global-context");o.SocketService=r,o.MockSocket=i,o.MockServer=s},{"./helpers/global-context":2,"./mock-server":7,"./mock-socket":8,"./service":9}],7:[function(e,t,n){function r(e){var t=new s;this.url=o(e),c.MockSocket.services[this.url]=t,this.service=t,t.server=this}var s=e("./service"),i=e("./helpers/delay"),o=e("./helpers/url-transform"),l=e("./helpers/message-event"),c=e("./helpers/global-context");r.prototype={service:null,on:function(e,t){var n;if("function"!=typeof t||"string"!=typeof e)return!1;switch(e){case"connection":n="clientHasJoined";break;case"message":n="clientHasSentMessage";break;case"close":n="clientHasLeft"}"string"==typeof n&&(this.service.clearAll(n),this.service.setCallbackObserver(n,t,this))},send:function(e){i(function(){this.service.sendMessageToClients(l("message",e,this.url))},this)},close:function(){i(function(){this.service.closeConnectionFromServer(l("close",null,this.url))},this)}},t.exports=r},{"./helpers/delay":1,"./helpers/global-context":2,"./helpers/message-event":3,"./helpers/url-transform":4,"./service":9}],8:[function(e,t,n){function r(e){this.binaryType="blob",this.url=i(e),this.readyState=l.MockSocket.CONNECTING,this.service=l.MockSocket.services[this.url],c(this),s(function(){this.service.clientIsConnecting(this,this._updateReadyState)},this)}var s=e("./helpers/delay"),i=e("./helpers/url-transform"),o=e("./helpers/message-event"),l=e("./helpers/global-context"),c=e("./helpers/websocket-properties");r.CONNECTING=0,r.OPEN=1,r.CLOSING=2,r.CLOSED=3,r.services={},r.prototype={_onopen:null,_onmessage:null,_onerror:null,_onclose:null,service:null,_eventHandlers:{},addEventListener:function(e,t){if(!this._eventHandlers[e]){this._eventHandlers[e]=[];var n=this;this["on"+e]=function(e){n.dispatchEvent(e)}}this._eventHandlers[e].push(t)},removeEventListener:function(e,t){if(this._eventHandlers[e]){var n=this._eventHandlers[e];n.splice(n.indexOf(t),1),n.length||(delete this._eventHandlers[e],delete this["on"+e])}},dispatchEvent:function(e){var t=this._eventHandlers[e.type];if(t)for(var n=0;n=0&&4>=e&&(this.readyState=e)}},t.exports=r},{"./helpers/delay":1,"./helpers/global-context":2,"./helpers/message-event":3,"./helpers/url-transform":4,"./helpers/websocket-properties":5}],9:[function(e,t,n){function r(){this.list={}}var s=e("./helpers/message-event"),i=e("./helpers/global-context");r.prototype={server:null,clientIsConnecting:function(e,t){return this.observe("updateReadyState",t,e),this.server?(this.notifyOnlyFor(e,"updateReadyState",i.MockSocket.OPEN),this.notify("clientHasJoined",this.server),void this.notifyOnlyFor(e,"clientOnOpen",s("open",null,this.server.url))):(this.notify(e,"updateReadyState",i.MockSocket.CLOSED),this.notifyOnlyFor(e,"clientOnError"),!1)},closeConnectionFromServer:function(e){this.notify("updateReadyState",i.MockSocket.CLOSING),this.notify("clientOnclose",e),this.notify("updateReadyState",i.MockSocket.CLOSED),this.notify("clientHasLeft")},closeConnectionFromClient:function(e,t){t.readyState===i.MockSocket.OPEN&&(this.notifyOnlyFor(t,"updateReadyState",i.MockSocket.CLOSING),this.notifyOnlyFor(t,"clientOnclose",e),this.notifyOnlyFor(t,"updateReadyState",i.MockSocket.CLOSED),this.notify("clientHasLeft"))},sendMessageToServer:function(e){this.notify("clientHasSentMessage",e.data,e)},sendMessageToClients:function(e){this.notify("clientOnMessage",e)},setCallbackObserver:function(e,t,n){this.observe(e,t,n)},observe:function(e,t,n){return"string"!=typeof e||"function"!=typeof t||n&&"object"!=typeof n?!1:(this.list[e]||(this.list[e]=[]),void this.list[e].push({callback:t,context:n}))},clearAll:function(e){return this.verifyNamespaceArg(e)?void(this.list[e]=[]):!1},notify:function(e){var t=Array.prototype.slice.call(arguments,1);if(!this.verifyNamespaceArg(e))return!1;for(var n=0,r=this.list[e].length;r>n;n++)this.list[e][n].callback.apply(this.list[e][n].context,t)},notifyOnlyFor:function(e,t){var n=Array.prototype.slice.call(arguments,2);if(!this.verifyNamespaceArg(t))return!1;for(var r=0,s=this.list[t].length;s>r;r++)this.list[t][r].context===e&&this.list[t][r].callback.apply(this.list[t][r].context,n)},verifyNamespaceArg:function(e){return"string"==typeof e&&this.list[e]?!0:!1}},t.exports=r},{"./helpers/global-context":2,"./helpers/message-event":3}]},{},[6]); \ No newline at end of file +!function e(t,n,r){function s(o,l){if(!n[o]){if(!t[o]){var c="function"==typeof require&&require;if(!l&&c)return c(o,!0);if(i)return i(o,!0);var a=new Error("Cannot find module '"+o+"'");throw a.code="MODULE_NOT_FOUND",a}var u=n[o]={exports:{}};t[o][0].call(u.exports,function(e){var n=t[o][1][e];return s(n?n:e)},u,u.exports,e,t,n,r)}return n[o].exports}for(var i="function"==typeof require&&require,o=0;o3?"/":"")+t.slice(3,t.length).join("/").split("?")[0].split("#")[0];var o=r.pathname;"/"===o.charAt(o.length-1)&&(o=o.substring(0,o.length-1));var l=r.hostname,c=l.split("."),a=o.split("/");if("hostname"===e)return l;if("domain"===e)return/^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$/.test(l)?l:c.slice(-2).join(".");if("sub"===e)return c.slice(0,c.length-2).join(".");if("port"===e)return r.port;if("protocol"===e)return r.protocol.split(":")[0];if("auth"===e)return r.auth;if("user"===e)return r.auth.split(":")[0];if("pass"===e)return r.auth.split(":")[1]||"";if("path"===e)return r.pathname;if("."===e.charAt(0)){if(e=e.substring(1),s(e))return e=parseInt(e,10),c[0>e?c.length+e:e-1]||""}else{if(s(e))return e=parseInt(e,10),a[0>e?a.length+e:e]||"";if("file"===e)return a.slice(-1)[0];if("filename"===e)return a.slice(-1)[0].split(".")[0];if("fileext"===e)return a.slice(-1)[0].split(".")[1]||"";if("?"===e.charAt(0)||"#"===e.charAt(0)){var u=n,h=null;if("?"===e.charAt(0)?u=(u.split("?")[1]||"").split("#")[0]:"#"===e.charAt(0)&&(u=u.split("#")[1]||""),!e.charAt(1))return u;e=e.substring(1),u=u.split("&");for(var f=0,p=u.length;p>f;f++)if(h=u[f].split("="),h[0]===e)return h[1]||"";return null}}return""}t.exports=r},{}],5:[function(e,t,n){function r(e){var t=function(t){return function(n){n.target=e,t.apply(e,arguments)}};Object.defineProperties(e,{onopen:{enumerable:!0,get:function(){return this._onopen},set:function(n){this._onopen=t(n),this.service.setCallbackObserver("clientOnOpen",this._onopen,e)}},onmessage:{enumerable:!0,get:function(){return this._onmessage},set:function(n){this._onmessage=t(n),this.service.setCallbackObserver("clientOnMessage",this._onmessage,e)}},onclose:{enumerable:!0,get:function(){return this._onclose},set:function(n){this._onclose=t(n),this.service.setCallbackObserver("clientOnclose",this._onclose,e)}},onerror:{enumerable:!0,get:function(){return this._onerror},set:function(n){this._onerror=t(n),this.service.setCallbackObserver("clientOnError",this._onerror,e)}}})}t.exports=r},{}],6:[function(e,t,n){var r=e("./service"),s=e("./mock-server"),i=e("./mock-socket"),o=e("./helpers/global-context");o.SocketService=r,o.MockSocket=i,o.MockServer=s},{"./helpers/global-context":2,"./mock-server":7,"./mock-socket":8,"./service":9}],7:[function(e,t,n){function r(e){var t=new s;this.url=o(e),c.MockSocket.services[this.url]=t,this.service=t,-1===e.indexOf(r.unresolvableURL)&&(t.server=this)}var s=e("./service"),i=e("./helpers/delay"),o=e("./helpers/url-transform"),l=e("./helpers/message-event"),c=e("./helpers/global-context");r.unresolvableURL="ws://unresolvable_url",r.prototype={service:null,on:function(e,t){var n;if("function"!=typeof t||"string"!=typeof e)return!1;switch(e){case"connection":n="clientHasJoined";break;case"message":n="clientHasSentMessage";break;case"close":n="clientHasLeft"}"string"==typeof n&&(this.service.clearAll(n),this.service.setCallbackObserver(n,t,this))},send:function(e){i(function(){this.service.sendMessageToClients(l("message",e,this.url))},this)},close:function(){i(function(){this.service.closeConnectionFromServer(l("close",null,this.url))},this)}},t.exports=r},{"./helpers/delay":1,"./helpers/global-context":2,"./helpers/message-event":3,"./helpers/url-transform":4,"./service":9}],8:[function(e,t,n){function r(e){this.binaryType="blob",this.url=i(e),this.readyState=l.MockSocket.CONNECTING,this.service=l.MockSocket.services[this.url],c(this),s(function(){this.service.clientIsConnecting(this,this._updateReadyState)},this)}var s=e("./helpers/delay"),i=e("./helpers/url-transform"),o=e("./helpers/message-event"),l=e("./helpers/global-context"),c=e("./helpers/websocket-properties");r.CONNECTING=0,r.OPEN=1,r.CLOSING=2,r.CLOSED=3,r.services={},r.prototype={_onopen:null,_onmessage:null,_onerror:null,_onclose:null,service:null,_eventHandlers:{},addEventListener:function(e,t){if(!this._eventHandlers[e]){this._eventHandlers[e]=[];var n=this;this["on"+e]=function(e){n.dispatchEvent(e)}}this._eventHandlers[e].push(t)},removeEventListener:function(e,t){if(this._eventHandlers[e]){var n=this._eventHandlers[e];n.splice(n.indexOf(t),1),n.length||(delete this._eventHandlers[e],delete this["on"+e])}},dispatchEvent:function(e){var t=this._eventHandlers[e.type];if(t)for(var n=0;n=0&&4>=e&&(this.readyState=e)}},t.exports=r},{"./helpers/delay":1,"./helpers/global-context":2,"./helpers/message-event":3,"./helpers/url-transform":4,"./helpers/websocket-properties":5}],9:[function(e,t,n){function r(){this.list={}}var s=e("./helpers/message-event"),i=e("./helpers/global-context");r.prototype={server:null,clientIsConnecting:function(e,t){return this.observe("updateReadyState",t,e),this.server?(this.notifyOnlyFor(e,"updateReadyState",i.MockSocket.OPEN),this.notify("clientHasJoined",this.server),void this.notifyOnlyFor(e,"clientOnOpen",s("open",null,this.server.url))):(this.notifyOnlyFor(e,"updateReadyState",i.MockSocket.CLOSED),this.notifyOnlyFor(e,"clientOnError",s("error",null,e.url)),!1)},closeConnectionFromServer:function(e){this.notify("updateReadyState",i.MockSocket.CLOSING),this.notify("clientOnclose",e),this.notify("updateReadyState",i.MockSocket.CLOSED),this.notify("clientHasLeft")},closeConnectionFromClient:function(e,t){t.readyState===i.MockSocket.OPEN&&(this.notifyOnlyFor(t,"updateReadyState",i.MockSocket.CLOSING),this.notifyOnlyFor(t,"clientOnclose",e),this.notifyOnlyFor(t,"updateReadyState",i.MockSocket.CLOSED),this.notify("clientHasLeft"))},sendMessageToServer:function(e){this.notify("clientHasSentMessage",e.data,e)},sendMessageToClients:function(e){this.notify("clientOnMessage",e)},setCallbackObserver:function(e,t,n){this.observe(e,t,n)},observe:function(e,t,n){return"string"!=typeof e||"function"!=typeof t||n&&"object"!=typeof n?!1:(this.list[e]||(this.list[e]=[]),void this.list[e].push({callback:t,context:n}))},clearAll:function(e){return this.verifyNamespaceArg(e)?void(this.list[e]=[]):!1},notify:function(e){var t=Array.prototype.slice.call(arguments,1);if(!this.verifyNamespaceArg(e))return!1;for(var n=0,r=this.list[e].length;r>n;n++)this.list[e][n].callback.apply(this.list[e][n].context,t)},notifyOnlyFor:function(e,t){var n=Array.prototype.slice.call(arguments,2);if(!this.verifyNamespaceArg(t))return!1;for(var r=0,s=this.list[t].length;s>r;r++)this.list[t][r].context===e&&this.list[t][r].callback.apply(this.list[t][r].context,n)},verifyNamespaceArg:function(e){return"string"==typeof e&&this.list[e]?!0:!1}},t.exports=r},{"./helpers/global-context":2,"./helpers/message-event":3}]},{},[6]); \ No newline at end of file diff --git a/src/mock-server.js b/src/mock-server.js index 1922ae0b..d614d09c 100644 --- a/src/mock-server.js +++ b/src/mock-server.js @@ -11,9 +11,17 @@ function MockServer(url) { globalContext.MockSocket.services[this.url] = service; this.service = service; - service.server = this; + // ignore possible query parameters + if(url.indexOf(MockServer.unresolvableURL) === -1) { + service.server = this; + } } +/* +* This URL can be used to emulate server that does not establish connection +*/ +MockServer.unresolvableURL = "ws://unresolvable_url"; + MockServer.prototype = { service: null, diff --git a/src/service.js b/src/service.js index c044d919..7b952cc9 100644 --- a/src/service.js +++ b/src/service.js @@ -20,8 +20,8 @@ SocketService.prototype = { // if the server has not been set then we notify the onclose method of this client if(!this.server) { - this.notify(client, 'updateReadyState', globalContext.MockSocket.CLOSED); - this.notifyOnlyFor(client, 'clientOnError'); + this.notifyOnlyFor(client, 'updateReadyState', globalContext.MockSocket.CLOSED); + this.notifyOnlyFor(client, 'clientOnError', socketMessageEvent('error', null, client.url)); return false; } diff --git a/tests/on-error-test.js b/tests/on-error-test.js new file mode 100644 index 00000000..017b658f --- /dev/null +++ b/tests/on-error-test.js @@ -0,0 +1,14 @@ +module('Mocksocket onerror tests'); + +asyncTest('that server rejects connection to unresolvable URL', function() { + var mockServer = new MockServer(MockServer.unresolvableURL); + var mockSocket = new MockSocket(MockServer.unresolvableURL); + + expect(2); + + mockSocket.onerror = function(event) { + ok(true, 'mocksocket onerror fires as expected'); + equal(this.readyState, MockSocket.CLOSED, 'the readystate is correctly set to CLOSED'); + start(); + }; +});