Skip to content

Commit

Permalink
Refactor ClientContext and ServerContext events
Browse files Browse the repository at this point in the history
  • Loading branch information
egreenmachine committed Feb 28, 2014
1 parent 5691379 commit e4cde35
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 76 deletions.
37 changes: 8 additions & 29 deletions src/ClientContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,58 +77,37 @@ ClientContext.prototype.cancel = function (options) {
};

ClientContext.prototype.receiveResponse = function (response) {
var cause;
var cause = SIP.C.REASON_PHRASE[response.status_code] || '';

switch(true) {
case /^1[0-9]{2}$/.test(response.status_code):
this.emit('progress', {
code: response.status_code,
response: response
});
this.emit('progress', response, cause);
break;

case /^2[0-9]{2}$/.test(response.status_code):
if(this.ua.applicants[this]) {
delete this.ua.applicants[this];
}
this.emit('accepted', {
code: response.status_code,
response: response
});
this.emit('accepted', response, cause);
break;

default:
if(this.ua.applicants[this]) {
delete this.ua.applicants[this];
}
cause = SIP.Utils.sipErrorCause(response.status_code);
this.emit('rejected', {
code: response && response.status_code,
response: response,
cause: cause
});
this.emit('failed', {
code: response && response.status_code,
response: response,
cause: cause
});
}
this.emit('rejected', response, cause);
this.emit('failed', response, cause);
break;
}

};

ClientContext.prototype.onRequestTimeout = function () {
this.emit('failed',
/* Status code */ 0,
/* Response */ null,
SIP.C.causes.REQUEST_TIMEOUT);
this.emit('failed', null, SIP.C.causes.REQUEST_TIMEOUT);
};

ClientContext.prototype.onTransportError = function () {
this.emit('failed',
/* Status code */ 0,
/* Response */ null,
SIP.C.causes.CONNECTION_ERROR);
this.emit('failed', null, SIP.C.causes.CONNECTION_ERROR);
};

SIP.ClientContext = ClientContext;
Expand Down
48 changes: 15 additions & 33 deletions src/ServerContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ ServerContext.prototype.progress = function (options) {
options = options || {};
var
statusCode = options.statusCode || 180,
reasonPhrase = options.reasonPhrase,
reasonPhrase = options.reasonPhrase || SIP.C.REASON_PHRASE[statusCode],
extraHeaders = options.extraHeaders || [],
body = options.body,
response;
Expand All @@ -48,10 +48,7 @@ ServerContext.prototype.progress = function (options) {
throw new TypeError('Invalid statusCode: ' + statusCode);
}
response = this.request.reply(statusCode, reasonPhrase, extraHeaders, body);
this.emit('progress', {
code: statusCode,
response: response
});
this.emit('progress', response, reasonPhrase);

return this;
};
Expand All @@ -60,18 +57,16 @@ ServerContext.prototype.accept = function (options) {
options = options || {};
var
statusCode = options.statusCode || 200,
reasonPhrase = options.reasonPhrase,
reasonPhrase = options.reasonPhrase || SIP.C.REASON_PHRASE[statusCode],
extraHeaders = options.extraHeaders || [],
body = options.body;
body = options.body,
response;

if (statusCode < 200 || statusCode > 299) {
throw new TypeError('Invalid statusCode: ' + statusCode);
}
this.request.reply(statusCode, reasonPhrase, extraHeaders, body);
this.emit('accepted', {
code: statusCode,
response: null
});
response = this.request.reply(statusCode, reasonPhrase, extraHeaders, body);
this.emit('accepted', response, reasonPhrase);

return this;
};
Expand All @@ -80,24 +75,17 @@ ServerContext.prototype.reject = function (options) {
options = options || {};
var
statusCode = options.statusCode || 480,
reasonPhrase = options.reasonPhrase,
reasonPhrase = options.reasonPhrase || SIP.C.REASON_PHRASE[statusCode],
extraHeaders = options.extraHeaders || [],
body = options.body;
body = options.body,
response;

if (statusCode < 300 || statusCode > 699) {
throw new TypeError('Invalid statusCode: ' + statusCode);
}
this.request.reply(statusCode, reasonPhrase, extraHeaders, body);
this.emit('rejected', {
code: statusCode,
response: null,
cause: reasonPhrase
});
this.emit('failed', {
code: statusCode,
response: null,
cause: reasonPhrase
});
response = this.request.reply(statusCode, reasonPhrase, extraHeaders, body);
this.emit('rejected', response, reasonPhrase);
this.emit('failed', response, reasonPhrase);

return this;
};
Expand All @@ -116,17 +104,11 @@ ServerContext.prototype.reply = function (options) {
};

ServerContext.prototype.onRequestTimeout = function () {
this.emit('failed',
/* Status code */ 0,
/* Response */ null,
SIP.C.causes.REQUEST_TIMEOUT);
this.emit('failed', null, SIP.C.causes.REQUEST_TIMEOUT);
};

ServerContext.prototype.onTransportError = function () {
this.emit('failed',
/* Status code */ 0,
/* Response */ null,
SIP.C.causes.CONNECTION_ERROR);
this.emit('failed', null, SIP.C.causes.CONNECTION_ERROR);
};

SIP.ServerContext = ServerContext;
Expand Down
12 changes: 6 additions & 6 deletions test/spec/SpecClientContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ describe('ClientContext', function() {
response.status_code = i;
ClientContext.receiveResponse(response);

expect(ClientContext.emit).toHaveBeenCalledWith('progress', {code: i, response: response});
expect(ClientContext.emit).toHaveBeenCalledWith('progress', response, SIP.C.REASON_PHRASE[response.status_code]|| '');
ClientContext.emit.reset();
}
});
Expand All @@ -132,7 +132,7 @@ describe('ClientContext', function() {
response.status_code = i;
ClientContext.receiveResponse(response);

expect(ClientContext.emit).toHaveBeenCalledWith('accepted', {code: i, response: response});
expect(ClientContext.emit).toHaveBeenCalledWith('accepted', response, SIP.C.REASON_PHRASE[response.status_code]|| '');
ClientContext.emit.reset();
}
});
Expand All @@ -144,8 +144,8 @@ describe('ClientContext', function() {
response.status_code = i;
ClientContext.receiveResponse(response);

expect(ClientContext.emit).toHaveBeenCalledWith('rejected', {code: i, response: response, cause: SIP.Utils.sipErrorCause(i)});
expect(ClientContext.emit).toHaveBeenCalledWith('failed', {code: i, response: response, cause: SIP.Utils.sipErrorCause(i)});
expect(ClientContext.emit).toHaveBeenCalledWith('rejected', response, SIP.C.REASON_PHRASE[response.status_code]|| '');
expect(ClientContext.emit).toHaveBeenCalledWith('failed', response, SIP.C.REASON_PHRASE[response.status_code]|| '');
ClientContext.emit.reset();
}
});
Expand All @@ -155,15 +155,15 @@ describe('ClientContext', function() {
it('emits failed with a status code 0, null response, and request timeout cause', function() {
spyOn(ClientContext, 'emit');
ClientContext.onRequestTimeout();
expect(ClientContext.emit).toHaveBeenCalledWith('failed', 0, null, SIP.C.causes.REQUEST_TIMEOUT);
expect(ClientContext.emit).toHaveBeenCalledWith('failed', null, SIP.C.causes.REQUEST_TIMEOUT);
});
});

describe('.onTransportError', function() {
it('emits failed with a status code 0, null response, and connection error cause', function() {
spyOn(ClientContext, 'emit');
ClientContext.onTransportError();
expect(ClientContext.emit).toHaveBeenCalledWith('failed',0,null,SIP.C.causes.CONNECTION_ERROR);
expect(ClientContext.emit).toHaveBeenCalledWith('failed',null,SIP.C.causes.CONNECTION_ERROR);
});
});

Expand Down
16 changes: 8 additions & 8 deletions test/spec/SpecServerContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ describe('ServerContext', function() {
for (var i = 100; i < 200; i++) {
var options = {statusCode : i};
ServerContext.progress(options);
expect(ServerContext.emit).toHaveBeenCalledWith('progress', {code: i, response: 'reply'});
expect(ServerContext.emit.mostRecentCall.args[0]).toBe('progress');
ServerContext.emit.reset();
}
});
Expand All @@ -117,7 +117,7 @@ describe('ServerContext', function() {

it('defaults to status code 200 if none is provided', function() {
ServerContext.accept(null);
expect(ServerContext.request.reply).toHaveBeenCalledWith(200, undefined, [], undefined);
expect(ServerContext.request.reply).toHaveBeenCalledWith(200, 'OK', [], undefined);
});

it('throws an error with an invalid status code', function() {
Expand Down Expand Up @@ -147,7 +147,7 @@ describe('ServerContext', function() {
for (var i = 200; i < 300; i++) {
var options = {statusCode : i};
ServerContext.accept(options);
expect(ServerContext.emit).toHaveBeenCalledWith('accepted', {code: options.statusCode, response: null});
expect(ServerContext.emit.mostRecentCall.args[0]).toBe('accepted');
ServerContext.emit.reset();
}
});
Expand All @@ -164,7 +164,7 @@ describe('ServerContext', function() {

it('defaults to status code 480 if none is provided', function() {
ServerContext.reject(null);
expect(ServerContext.request.reply).toHaveBeenCalledWith(480, undefined, [], undefined);
expect(ServerContext.request.reply).toHaveBeenCalledWith(480, 'Temporarily Unavailable', [], undefined);
});

it('throws an error with an invalid status code', function() {
Expand All @@ -191,8 +191,8 @@ describe('ServerContext', function() {
for (var i = 300; i < 700; i++) {
options.statusCode = i;
ServerContext.reject(options);
expect(ServerContext.emit).toHaveBeenCalledWith('rejected', {code: options.statusCode, response: null, cause: options.reasonPhrase});
expect(ServerContext.emit).toHaveBeenCalledWith('failed', {code: options.statusCode, response: null, cause: options.reasonPhrase});
expect(ServerContext.emit).toHaveBeenCalledWith('rejected', undefined, options.reasonPhrase);
expect(ServerContext.emit).toHaveBeenCalledWith('failed', undefined, options.reasonPhrase);
ServerContext.emit.reset();
}
});
Expand Down Expand Up @@ -233,7 +233,7 @@ describe('ServerContext', function() {

ServerContext.onRequestTimeout();

expect(ServerContext.emit).toHaveBeenCalledWith('failed', 0, null, SIP.C.causes.REQUEST_TIMEOUT);
expect(ServerContext.emit).toHaveBeenCalledWith('failed', null, SIP.C.causes.REQUEST_TIMEOUT);
});
});

Expand All @@ -243,7 +243,7 @@ describe('ServerContext', function() {

ServerContext.onTransportError();

expect(ServerContext.emit).toHaveBeenCalledWith('failed', 0, null, SIP.C.causes.CONNECTION_ERROR);
expect(ServerContext.emit).toHaveBeenCalledWith('failed', null, SIP.C.causes.CONNECTION_ERROR);
});
});
});

0 comments on commit e4cde35

Please sign in to comment.