Skip to content

Commit

Permalink
Merge pull request #30 from vlizard/error-unresolvable-url
Browse files Browse the repository at this point in the history
Allow to create mock server that is unreachable
  • Loading branch information
thoov committed Jun 25, 2015
2 parents 9490c8a + 9a2b7f8 commit d7df971
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 7 deletions.
14 changes: 11 additions & 3 deletions dist/mock-socket.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,

Expand Down Expand Up @@ -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;
}

Expand Down
2 changes: 1 addition & 1 deletion dist/mock-socket.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 9 additions & 1 deletion src/mock-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,

Expand Down
4 changes: 2 additions & 2 deletions src/service.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
14 changes: 14 additions & 0 deletions tests/on-error-test.js
Original file line number Diff line number Diff line change
@@ -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();
};
});

0 comments on commit d7df971

Please sign in to comment.