Skip to content

Commit

Permalink
Merge pull request #397 from ylecuyer/configure-headers-yle
Browse files Browse the repository at this point in the history
Better: allow to configure default headers.
  • Loading branch information
tylerlong authored Aug 8, 2024
2 parents d6feaf2 + 3beae21 commit 95e10ce
Show file tree
Hide file tree
Showing 12 changed files with 344 additions and 15 deletions.
12 changes: 10 additions & 2 deletions demo/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ $(() => {
}

function register(data) {
webPhone = new WebPhone(data, {
const webPhoneConfig: any = {
enableDscp: true,
clientId: localStorage.getItem('webPhoneclientId')!,
audioHelper: {
Expand All @@ -150,7 +150,15 @@ $(() => {
},
enableQos: true,
enableMediaReportLogging: true,
});
};

const urlParams = new URLSearchParams(window.location.search);
if (urlParams.has('customHeader')) {
webPhoneConfig.defaultHeaders = ['P-Custom-Header: CustomValue'];
}

webPhone = new WebPhone(data, webPhoneConfig);

global.webPhone = webPhone; // for debugging

webPhone.userAgent.audioHelper.setVolume(0.3);
Expand Down
4 changes: 2 additions & 2 deletions docs/index.9050f078.js → docs/index.5edd3872.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions docs/index.5edd3872.js.map

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion docs/index.9050f078.js.map

This file was deleted.

4 changes: 2 additions & 2 deletions docs/index.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<!doctype html><html><head><script type="module" src="index.ff75d985.js"></script><link rel="stylesheet" href="index.f01e0bc4.css"><script type="module" src="index.runtime.42757dfb.js"></script><link rel="icon shortcut" href="favicon.4a73512e.png"><title>RingCentral WebPhone Demo</title></head><body> <div class="container"> <h1>RingCentral WebPhone Demo</h1> <div id="app"></div> </div> <video id="remoteVideo" hidden></video> <video id="localVideo" hidden muted></video> <script type="text/html" id="template-incoming">
<!doctype html><html><head><script type="module" src="index.ff75d985.js"></script><link rel="stylesheet" href="index.f01e0bc4.css"><script type="module" src="index.runtime.aa0fdf85.js"></script><link rel="icon shortcut" href="favicon.4a73512e.png"><title>RingCentral WebPhone Demo</title></head><body> <div class="container"> <h1>RingCentral WebPhone Demo</h1> <div id="app"></div> </div> <video id="remoteVideo" hidden></video> <video id="localVideo" hidden muted></video> <script type="text/html" id="template-incoming">
<div class="modal fade" tabindex="-1" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
Expand Down Expand Up @@ -179,4 +179,4 @@ <h4 class="modal-title">
</div>
</div>
</div>
</script> <script type="module" src="index.9050f078.js"></script> </body></html>
</script> <script type="module" src="index.5edd3872.js"></script> </body></html>

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

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
"eslint-plugin-jest": "^28.6.0",
"eslint-plugin-prettier": "^5.1.3",
"events": "^3.3.0",
"fix-esm": "^1.0.1",
"http-server": "^14.1.1",
"jest": "^29.7.0",
"jquery": "^3.7.1",
Expand Down
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ export interface WebPhoneOptions {
builtinEnabled?: boolean;
/** A function which will be called every time a log is generated. [Reference](https://github.com/onsip/SIP.js/blob/master/docs/api/sip.js.logconnector.md) */
connector?: LogConnector;
/** Default headers to add to SIP messages */
defaultHeaders?: string[];
/** If `true` media will be sent prior to call being answered
*
* Set to `true` by default for firefox browser
Expand Down
3 changes: 3 additions & 0 deletions src/userAgent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,9 @@ export function createWebPhoneUserAgent(
userAgent.removeListener = eventEmitter.removeListener.bind(eventEmitter);
userAgent.removeAllListeners = eventEmitter.removeAllListeners.bind(eventEmitter);
userAgent.defaultHeaders = [`P-rc-endpoint-id: ${id}`, `Client-id: ${options.clientId}`];
if (options.defaultHeaders) {
userAgent.defaultHeaders.push(...options.defaultHeaders);
}
userAgent.regId = options.regId;
userAgent.media = {};
userAgent.enableQos = options.enableQos;
Expand Down
63 changes: 59 additions & 4 deletions test/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,45 @@
import type { BrowserContext } from '@playwright/test';
import { expect, test } from '@playwright/test';
import RingCentral from '@rc-ex/core';
const sip = require('fix-esm').require('sip.js'); // eslint-disable-line
const log = new sip.Core.LoggerFactory();
const logger = log.getLogger('test.parser');

const login = async (context: BrowserContext, jwtToken: string) => {
const waitFor = async (condition, pollInterval = 1000, timeout = 10000) => {
const startTime = Date.now();

while (true) {
if (Date.now() > startTime + timeout) {
throw 'timeout';
}

const result = await condition();

if (result) {
return result;
}

await new Promise((r) => setTimeout(r, pollInterval)); // eslint-disable-line
}
};

// eslint-disable-next-line max-params
const login = async (context: BrowserContext, jwtToken: string, ws: any, options: { customHeader?: boolean } = {}) => {
const page = await context.newPage();
await page.goto('/');

let path = '/';

if (options && options.customHeader) {
path += '?customHeader=true';
}

await page.goto(path);
const title = page.locator('h1');
await expect(title).toHaveText('RingCentral WebPhone Demo');
await page.screenshot({ path: 'screenshots/before-login.png' });

page.on('websocket', ws);

await page.fill('input[name="server"]', process.env.RC_WP_SERVER!);
await page.fill('input[name="clientId"]', process.env.RC_WP_CLIENT_ID!);
await page.fill('input[name="clientSecret"]', process.env.RC_WP_CLIENT_SECRET!);
Expand All @@ -27,9 +58,9 @@ const login = async (context: BrowserContext, jwtToken: string) => {

test('home page', async ({ context }) => {
// login
const callerPage = await login(context, process.env.RC_WP_CALLER_JWT_TOKEN!);
const callerPage = await login(context, process.env.RC_WP_CALLER_JWT_TOKEN!, () => {});
await callerPage.screenshot({ path: 'screenshots/caller-logged-in.png' });
const receiverPage = await login(context, process.env.RC_WP_RECEIVER_JWT_TOKEN!);
const receiverPage = await login(context, process.env.RC_WP_RECEIVER_JWT_TOKEN!, () => {});
await receiverPage.screenshot({ path: 'screenshots/receiver-logged-in.png' });

// make the call
Expand Down Expand Up @@ -64,3 +95,27 @@ test('home page', async ({ context }) => {
await expect(receiverPage.locator('text=Call In Progress')).toBeHidden();
await receiverPage.screenshot({ path: 'screenshots/receiver-hung-up.png' });
});

test('allow to configure default headers', async ({ context }) => {
let wsHandled = false;
await login(
context,
process.env.RC_WP_CALLER_JWT_TOKEN!,
(ws) => {
ws.on('framesent', async (frame) => {
const parsed = sip.Core.Parser.parseMessage(frame.payload, logger);

if (parsed!.method === 'REGISTER') {
expect(parsed!.headers['P-Custom-Header'].length).toEqual(1);
expect(parsed!.headers['P-Custom-Header'][0].raw).toEqual('CustomValue');
wsHandled = true;
}
});
},
{
customHeader: true,
},
);

await waitFor(() => wsHandled);
});
Loading

0 comments on commit 95e10ce

Please sign in to comment.