Skip to content

Commit

Permalink
Merge pull request #24 from Bandwidth/feature/EDGE-630
Browse files Browse the repository at this point in the history
Feature/edge 630
  • Loading branch information
acopeland-bw authored Jul 22, 2021
2 parents d0466df + 986edb4 commit a133553
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 11 deletions.
27 changes: 18 additions & 9 deletions src/controllers/apiController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -337,28 +337,37 @@ export class ApiController extends BaseController {
* header.
*
* @param {string} deviceToken Token returned from createParticipant call
* @param {string} voiceCallId The callId returned by the Voice platform
* @param {string} [sipUri=sip:sipx.webrtc.bandwidth.com:5060] SIP URL to transfer to
*
* @returns BXML string
*/
static generateTransferBxml(deviceToken: string, sipUri='sip:sipx.webrtc.bandwidth.com:5060'): string {
return '<?xml version="1.0" encoding="UTF-8" ?>\n'
+ '<Response>'
+ this.generateTransferBxmlVerb(deviceToken, sipUri)
+ '</Response>';
static generateTransferBxml(deviceToken: string, voiceCallId: string, sipUri='sip:sipx.webrtc.bandwidth.com:5060'): string {
return '<?xml version="1.0" encoding="UTF-8" ?>\n'
+ '<Response>'
+ this.generateTransferBxmlVerb(deviceToken, voiceCallId, sipUri)
+ '</Response>';
}

/**
* Generates the BXML verb to transfer a call into a WebRTC session (not wrapped in a Response element).
*
* @param {string} deviceToken Token returned from createParticipant call
* @param {string} voiceCallId The callId returned by the Voice platform
* @param {string} [sipUri=sip:sipx.webrtc.bandwidth.com:5060] SIP URL to transfer to
*
* @returns BXML string
*/
static generateTransferBxmlVerb(deviceToken: string, sipUri = 'sip:sipx.webrtc.bandwidth.com:5060'): string {
return '<Transfer>\n'
+ `\t<SipUri uui="${deviceToken};encoding=jwt">${sipUri}</SipUri>\n`
+ '</Transfer>';
static generateTransferBxmlVerb(deviceToken: string, voiceCallId: string, sipUri = 'sip:sipx.webrtc.bandwidth.com:5060'): string {
if (voiceCallId) {
voiceCallId = voiceCallId.substring(1).replace(/-/g,'');
return '<Transfer>\n'
+ `\t<SipUri uui="${voiceCallId};encoding=base64,${deviceToken};encoding=jwt">${sipUri}</SipUri>\n`
+ '</Transfer>';
} else {
return '<Transfer>\n'
+ `\t<SipUri uui="${deviceToken};encoding=jwt">${sipUri}</SipUri>\n`
+ '</Transfer>';
}
}
}
37 changes: 35 additions & 2 deletions tests/api.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,43 @@ describe('api', () => {
});

it('should generate transfer bxml', () => {
var bxmlString = ApiController.generateTransferBxml("test-string");
var bxmlString = ApiController.generateTransferBxml("test-string", "c-93d6f3c0-be584596-0b74-4fa2-8015-d8ede84bd1a4");
bxmlString = bxmlString.replace(/\s/g, "");

var expectedString = '<?xmlversion="1.0"encoding="UTF-8"?><Response><Transfer><SipUriuui="test-string;encoding=jwt">sip:sipx.webrtc.bandwidth.com:5060</SipUri></Transfer></Response>';
var expectedString = '<?xmlversion="1.0"encoding="UTF-8"?><Response><Transfer><SipUriuui="93d6f3c0be5845960b744fa28015d8ede84bd1a4;encoding=base64,test-string;encoding=jwt">sip:sipx.webrtc.bandwidth.com:5060</SipUri></Transfer></Response>';
expect(bxmlString).toEqual(expectedString);
});
});

describe('api', () => {
it('should handle webrtc participant and session management', async () => {
const accountId = process.env.BW_ACCOUNT_ID;

const createSessionBody = {
tag: "new-session"
};

const createSessionResponse = await controller.createSession(accountId, createSessionBody);
const sessionId = createSessionResponse.result.id;

const createParticipantBody = {
callbackUrl: "https://sample.com",
publishPermissions: ["AUDIO", "VIDEO"],
deviceApiVersion: "V3"
};

const createParticipantResponse = await controller.createParticipant(accountId, createParticipantBody);
const participantId = createParticipantResponse.result.participant.id;

controller.addParticipantToSession(accountId, sessionId, participantId);
});

it('should generate transfer bxml', () => {
var bxmlString = ApiController.generateTransferBxml("test-string", "c-93d6f3c0-be584596-0b74-4fa2-8015-d8ede84bd1a4", "sip:sipx-rtc.edge.bandwidth.com:5060");
bxmlString = bxmlString.replace(/\s/g, "");

var expectedString = '<?xmlversion="1.0"encoding="UTF-8"?><Response><Transfer><SipUriuui="93d6f3c0be5845960b744fa28015d8ede84bd1a4;encoding=base64,test-string;encoding=jwt">sip:sipx-rtc.edge.bandwidth.com:5060</SipUri></Transfer></Response>';
expect(bxmlString).toEqual(expectedString);
});
});

0 comments on commit a133553

Please sign in to comment.