From e4cde35fb7b2b7cc8a462686d1fa2f6248c7eb82 Mon Sep 17 00:00:00 2001 From: Eric Green Date: Fri, 28 Feb 2014 13:00:11 -0500 Subject: [PATCH] Refactor ClientContext and ServerContext events --- src/ClientContext.js | 37 ++++++-------------------- src/ServerContext.js | 48 +++++++++++----------------------- test/spec/SpecClientContext.js | 12 ++++----- test/spec/SpecServerContext.js | 16 ++++++------ 4 files changed, 37 insertions(+), 76 deletions(-) diff --git a/src/ClientContext.js b/src/ClientContext.js index 39767eb62..237d6c940 100644 --- a/src/ClientContext.js +++ b/src/ClientContext.js @@ -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; diff --git a/src/ServerContext.js b/src/ServerContext.js index d2baf8ad5..62675f292 100644 --- a/src/ServerContext.js +++ b/src/ServerContext.js @@ -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; @@ -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; }; @@ -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; }; @@ -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; }; @@ -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; diff --git a/test/spec/SpecClientContext.js b/test/spec/SpecClientContext.js index 5993015cf..81423cfb7 100644 --- a/test/spec/SpecClientContext.js +++ b/test/spec/SpecClientContext.js @@ -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(); } }); @@ -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(); } }); @@ -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(); } }); @@ -155,7 +155,7 @@ 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); }); }); @@ -163,7 +163,7 @@ describe('ClientContext', 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); }); }); diff --git a/test/spec/SpecServerContext.js b/test/spec/SpecServerContext.js index 96be7911c..06c7a0279 100644 --- a/test/spec/SpecServerContext.js +++ b/test/spec/SpecServerContext.js @@ -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(); } }); @@ -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() { @@ -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(); } }); @@ -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() { @@ -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(); } }); @@ -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); }); }); @@ -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); }); }); }); \ No newline at end of file