From bd5798206295c95da01914fc20a44e751e1f9f58 Mon Sep 17 00:00:00 2001 From: Chuck MANCHUCK Reeves Date: Thu, 30 Nov 2023 00:26:00 +0000 Subject: [PATCH] fix: post encoding for client --- lib/client.js | 591 +++++++++++++++++-------------------------- package.json | 45 ++-- test/opentok-test.js | 60 ++--- 3 files changed, 281 insertions(+), 415 deletions(-) diff --git a/lib/client.js b/lib/client.js index e8dba09b..c4a61e61 100644 --- a/lib/client.js +++ b/lib/client.js @@ -1,10 +1,10 @@ -var request = require('needle'); -var _ = require('lodash'); -var generateJwt = require('./generateJwt'); -var pkg = require('../package.json'); -var Stream = require('./stream'); -var Broadcast = require('./broadcast'); -var defaultConfig = { +const _ = require('lodash'); +const generateJwt = require('./generateJwt'); +const pkg = require('../package.json'); +const Stream = require('./stream'); +const Broadcast = require('./broadcast'); +const fetch = require('node-fetch'); +const defaultConfig = { apiKey: null, apiSecret: null, apiUrl: 'https://api.opentok.com', @@ -39,7 +39,78 @@ var defaultConfig = { } }; -var Client = function (c) { +const api = ({ + method, + url, + callback, + body, + form, + headers = {}, +}) => { + let requestBody = null; + if (body && ['POST', 'PATCH', 'PUT'].includes(method)) { + headers['Content-Type'] = 'application/json'; + requestBody = JSON.stringify(body); + } + + if (form) { + requestBody = new URLSearchParams(form).toString(); + headers['Content-Type'] = 'application/x-www-form-urlencoded'; + } + + Promise.resolve(fetch( + url, + { + method: method, + body: requestBody, + headers: headers, + } + )) + .then(async (response) => { + const bodyText = await response.text(); + let body = bodyText; + + switch (response.headers.get('content-type')) { + case 'application/x-www-form-urlencoded': + body = response.body + ? new URLSearchParams(body) + : '' ; + break; + case 'application/json': + body = JSON.parse(bodyText); + } + + if (response.status === 400) { + if (body?.code === 15204) { + return callback(new Error('SIP Interconnect for Video is not enabled in this project')); + } + return callback(new Error('Bad session ID, token, SIP credentials, or SIP URI (sip:user@domain.tld)')); + } + + // handle client errors + if (response.status === 403) { + return callback(new Error('An authentication error occurred: (' + + response.status + + ') ' + + bodyText)); + } + + // handle server errors + if (response.status >= 500 && response.status <= 599) { + return callback(new Error('A server error occurred: (' + + response.status + + ') ' + + bodyText)); + } + + callback(null, body); + }) + .catch(async (error) => { + callback(error); + }); +}; + +const Client = function (c) { this.c = {}; this.config(_.defaults(c, defaultConfig)); }; @@ -52,51 +123,20 @@ Client.prototype.config = function (c) { this.c.apiKey ); } - if ('request' in this.c) { - request.defaults(this.c.request); - } return this.c; }; Client.prototype.createSession = function (opts, cb) { - request.post( - this.c.apiUrl + this.c.endpoints.createSession, - opts, - { - // TODO: only works while apiUrl is always up to the domain, without the ending slash - headers: this.generateHeaders() - - }, - function (err, resp, body) { - if (err) { - return cb(new Error('The request failed: ' + err)); - } - - // handle client errors - if (resp.statusCode === 403) { - return cb(new Error('An authentication error occurred: (' - + resp.statusCode - + ') ' - + JSON.stringify(body))); - } - - // handle server errors - if (resp.statusCode >= 500 && resp.statusCode <= 599) { - return cb(new Error('A server error occurred: (' - + resp.statusCode - + ') ' - + JSON.stringify(body))); - } - - // check if the returned object is valid JSON - if (typeof body !== 'object') { - return cb(new Error('Server returned invalid JSON')); - } - - return cb(null, body); - } - ); + const url = new URL(this.c.apiUrl + this.c.endpoints.createSession); + + api({ + url: url.toString(), + method: 'POST', + form: opts, + headers: this.generateHeaders(), + callback: cb, + }); }; Client.prototype.startArchive = function () {}; @@ -110,7 +150,7 @@ Client.prototype.listArchives = function () {}; Client.prototype.deleteArchive = function () {}; Client.prototype.playDTMF = function (opts, cb) { - var url; + let url; if (opts.sessionId) { url = this.c.apiUrl @@ -126,303 +166,194 @@ Client.prototype.playDTMF = function (opts, cb) { .replace(/<%connectionId%>/g, opts.connectionId); } - request.post( - url, - { + api({ + url:url, + method: 'POST', + body: { digits: opts.digits }, - { - headers: this.generateHeaders() - }, - function (err, resp, body) { - if (err) return cb(new Error('The request failed: ' + err)); - - if (resp.statusCode === 200) { - // Success - return cb(null); - } - - return cb(new Error('(' + resp.statusCode + ') ' + JSON.stringify(body))); - } - ); + headers: this.generateHeaders(), + callback: cb, + }); }; Client.prototype.forceMuteStream = function (opts, cb) { - var url = this.c.apiUrl + const url = this.c.apiUrl + this.c.endpoints.forceMuteStream .replace(/<%apiKey%>/g, this.c.apiKey) .replace(/<%sessionId%>/g, opts.sessionId) .replace(/<%streamId%>/g, opts.streamId); - request.post( - url, - {}, - { - headers: this.generateHeaders() - }, - function (err, resp, body) { - if (err) return cb(new Error('The request failed: ' + err)); - - if (resp.statusCode === 200) { - // Success - return cb(null); - } - - return cb(new Error('(' + resp.statusCode + ') ' + JSON.stringify(body))); - } - ); + api({ + url:url, + method: 'POST', + body: { }, + headers: this.generateHeaders(), + callback: cb, + }); }; Client.prototype.forceMuteAll = function (opts, cb) { - var url = this.c.apiUrl + const url = this.c.apiUrl + this.c.endpoints.forceMute .replace(/<%apiKey%>/g, this.c.apiKey) .replace(/<%sessionId%>/g, opts.sessionId); opts.options.active = true; - request.post( - url, - opts.options, - { - headers: this.generateHeaders() - }, - function (err, resp, body) { - if (err) return cb(new Error('The request failed: ' + err)); - - if (resp.statusCode === 200) { - // Success - return cb(null); - } - - return cb(new Error('(' + resp.statusCode + ') ' + JSON.stringify(body))); - } - ); + api({ + url:url, + method: 'POST', + body: opts.options, + headers: this.generateHeaders(), + callback: cb, + }); }; Client.prototype.disableForceMute = function (opts, cb) { - var url = this.c.apiUrl + const url = this.c.apiUrl + this.c.endpoints.forceMute .replace(/<%apiKey%>/g, this.c.apiKey) .replace(/<%sessionId%>/g, opts.sessionId); - var options = { + const options = { active: false }; - request.post( - url, - options, - { - headers: this.generateHeaders() - }, - function (err, resp, body) { - if (err) return cb(new Error('The request failed: ' + err)); - - if (resp.statusCode === 200) { - // Success - return cb(null); - } - - return cb(new Error('(' + resp.statusCode + ') ' + JSON.stringify(body))); - } - ); + api({ + url:url, + method: 'POST', + body: options, + headers: this.generateHeaders(), + callback: cb, + }); }; Client.prototype.setArchiveLayout = function setArchiveLayout(opts, cb) { - var url = this.c.apiUrl + const url = this.c.apiUrl + this.c.endpoints.setArchiveLayout .replace(/<%apiKey%>/g, this.c.apiKey) .replace(/<%archiveId%>/g, opts.archiveId); - request.put( - url, - { + api({ + url:url, + method: 'PUT', + body: { type: opts.type, stylesheet: opts.stylesheet || undefined, screenshareType: opts.screenshareType || undefined }, - { - headers: this.generateHeaders() - }, - function requestCallback(err, resp, body) { - if (err) { - return cb(new Error('The request failed: ' + err)); - } - if (resp.statusCode === 200) { - return cb(null, body); - } - return cb(new Error('(' + resp.statusCode + ') ' + body.message)); - } - ); + headers: this.generateHeaders(), + callback: cb, + }); }; Client.prototype.startBroadcast = function (opts, cb) { - var url = this.c.apiUrl + const url = this.c.apiUrl + this.c.endpoints.startBroadcast.replace(/<%apiKey%>/g, this.c.apiKey); - request.post( - url, - opts, - { - headers: this.generateHeaders() + api({ + url:url, + method: 'POST', + body: opts, + headers: this.generateHeaders(), + callback: (err, json) => { + const responseText = typeof json === 'object' ? JSON.stringify(json) : json; + cb(err, responseText); }, - function (err, resp, body) { - if (err) return cb(new Error('The request failed: ' + err)); - - if (resp.statusCode !== 200) { - return cb(new Error('(' + resp.statusCode + ') ' + JSON.stringify(body))); - } + }); - return cb(null, JSON.stringify(body)); - } - ); }; Client.prototype.patchBroadcast = function patchBroadcast(broadcastId, opts, cb) { - var url = this.c.apiUrl + this.c.endpoints.patchBroadcast.replace(/<%apiKey%>/g, this.c.apiKey) + const url = this.c.apiUrl + this.c.endpoints.patchBroadcast.replace(/<%apiKey%>/g, this.c.apiKey) .replace(/<%broadcastId%>/g, broadcastId); + api({ + url:url, + method: 'PATCH', + body: opts, + headers: this.generateHeaders(), + callback: cb, + }); - request.patch( - url, - opts, - { - - headers: this.generateHeaders() - }, - function (err, resp, body) { - if (err) return cb(new Error('The request failed')); - if (resp.statusCode === 204) { - return cb(null, JSON.stringify(body)); - } - return cb(new Error('(' + resp.statusCode + ') ' + JSON.stringify(body))); - } - ); }; Client.prototype.stopBroadcast = function (broadcastId, cb) { - var url = this.c.apiUrl + const url = this.c.apiUrl + this.c.endpoints.stopBroadcast .replace(/<%apiKey%>/g, this.c.apiKey) .replace(/<%broadcastId%>/g, broadcastId); - - request.post( - url, - {}, - { - headers: this.generateHeaders() - }, - function (err, resp, body) { - if (err) return cb(new Error('The request failed: ' + err)); - - if (resp.statusCode === 200) { - // Success - return cb(null, body); - } - - return cb(new Error('(' + resp.statusCode + ') ' + JSON.parse(body).message)); - } - ); + api({ + url:url, + method: 'POST', + body: { }, + headers: this.generateHeaders(), + callback: cb, + }); }; Client.prototype.getBroadcast = function getBroadcast(broadcastId, cb) { - var url = this.c.apiUrl + const url = this.c.apiUrl + this.c.endpoints.getBroadcast .replace(/<%apiKey%>/g, this.c.apiKey) .replace(/<%broadcastId%>/g, broadcastId); - request.get( - url, - { - headers: this.generateHeaders() - }, - function requestCallback(err, resp, body) { - if (err) { - return cb(new Error('The request failed: ' + err)); - } - if (resp.statusCode === 200) { - return cb(null, body); - } - return cb(new Error('(' + resp.statusCode + ') ' + body ? body.message : '')); - } - ); + api({ + url:url, + method: 'GET', + headers: this.generateHeaders(), + callback: cb, + }); }; Client.prototype.listBroadcasts = function listBroadcasts(queryString, cb) { - var baseUrl = this.c.apiUrl + const baseUrl = this.c.apiUrl + this.c.endpoints.listBroadcasts.replace(/<%apiKey%>/g, this.c.apiKey); - var url = queryString.length > 0 ? baseUrl + '?' + queryString : baseUrl; - var parsedBody; - request.get( - url, - { - headers: this.generateHeaders() + const url = queryString.length > 0 ? baseUrl + '?' + queryString : baseUrl; + api({ + url:url, + method: 'GET', + headers: this.generateHeaders(), + callback: (err, response ) => { + const items = response ? JSON.parse(response) : response; + + cb( + err, + items?.items.map((item) => new Broadcast(Client, JSON.stringify(item))), + items?.count, + ) }, - function requestCallback(err, resp, body) { - if (err) { - return cb(new Error('The request failed: ' + err)); - } - if (resp.statusCode === 200) { - parsedBody = JSON.parse(body); - return cb( - null, - parsedBody.items.map(function itemIterator(item) { - return new Broadcast(Client, JSON.stringify(item)); - }), - parsedBody.count - ); - } - return cb(new Error('(' + resp.statusCode + ') ' + body ? body.message : '')); - } - ); + }); }; Client.prototype.setBroadcastLayout = function setBroadcastLayout(opts, cb) { - var url = this.c.apiUrl + const url = this.c.apiUrl + this.c.endpoints.setBroadcastLayout .replace(/<%apiKey%>/g, this.c.apiKey) .replace(/<%broadcastId%>/g, opts.broadcastId); - request.put( - url, - { + api({ + url:url, + method: 'PUT', + body: { type: opts.type, stylesheet: opts.stylesheet || undefined, screenshareType: opts.screenshareType || undefined }, - { - headers: this.generateHeaders() - }, - function requestCallback(err, resp, body) { - if (err) { - return cb(new Error('The request failed: ' + err)); - } - if (resp.statusCode === 200) { - return cb(null, body); - } - return cb(new Error('(' + resp.statusCode + ') ' + body.message)); - } - ); + headers: this.generateHeaders(), + callback: cb, + }); + }; Client.prototype.websocketConnect = function websocketConnect(opts, cb) { - var url = this.c.apiUrl + this.c.endpoints.audioStreamer + const url = this.c.apiUrl + this.c.endpoints.audioStreamer .replace(/<%apiKey%>/g, this.c.apiKey); + api({ + url:url, + method: 'POST', + body: opts, + headers: this.generateHeaders(), + callback: cb, + }); - request.post( - url, - opts, - { - headers: this.generateHeaders(), - json: true - }, - function requestCallback(err, resp, body) { - if (err) { - return cb(new Error('The request failed: ' + err)); - } - if (resp.statusCode === 200) { - return cb(null, body); - } - return cb(new Error('(' + resp.statusCode + ') ' + body.message)); - } - ); }; Client.prototype.setStreamClassLists = function setStreamClassLists( @@ -430,130 +361,60 @@ Client.prototype.setStreamClassLists = function setStreamClassLists( classListArray, cb ) { - var url = this.c.apiUrl + const url = this.c.apiUrl + this.c.endpoints.setStreamClassLists .replace(/<%apiKey%>/, this.c.apiKey) .replace(/<%sessionId%>/g, sessionId); - request.put( - url, - { + api({ + url:url, + method: 'PUT', + body: { items: classListArray }, - { - headers: this.generateHeaders() - }, - function requestCallback(err, resp, body) { - if (err) return cb(new Error('The request failed: ' + err)); - // handle client errors - switch (resp.statusCode) { - case 200: - return cb(null, body); - case 400: - return cb(new Error('Invalid session ID or stream ID (400).')); - case 403: - return cb(new Error('Invalid API key or secret (403).')); - default: - if (resp.statusCode >= 500 && resp.statusCode <= 599) { - // handle server errors - return cb(new Error('A server error occurred: (' - + resp.statusCode - + ') ' - + JSON.stringify(body))); - } - } - return cb(new Error('An unexpected error occurred: (' - + resp.statusCode - + ') ' - + JSON.stringify(body))); - } - ); + headers: this.generateHeaders(), + callback: cb, + }); }; Client.prototype.dial = function (opts, cb) { - request.post( - this.c.apiUrl + this.c.endpoints.dial, - opts, - { - // TODO: only works while apiUrl is always up to the domain, without the ending slash - headers: this.generateHeaders() - }, - function (err, resp, body) { - if (err) return cb(new Error('The request failed: ' + err)); - // handle client errors - if (resp.statusCode === 400) { - if (body.code === 15204) { - return cb(new Error('SIP Interconnect for Video is not enabled in this project')); - } - return cb(new Error('Bad session ID, token, SIP credentials, or SIP URI (sip:user@domain.tld)')); - } - - if (resp.statusCode === 403) { - return cb(new Error('Invalid API key or secret')); - } - - if (resp.statusCode === 409) { - return cb(new Error('Only Routed Sessions are allowed to initiate SIP Calls.')); - } - - // handle server errors - if (resp.statusCode >= 500 && resp.statusCode <= 599) { - return cb(new Error('A server error occurred: (' + resp.statusCode + ') ' + body)); - } - - // Parse data from server - return cb(null, body); - } - ); + api({ + url: this.c.apiUrl + this.c.endpoints.dial, + method: 'POST', + body: opts, + headers: this.generateHeaders(), + callback: cb, + }); }; Client.prototype.getStream = function getStream(sessionId, streamId, cb) { - var url = this.c.apiUrl + const url = this.c.apiUrl + this.c.endpoints.getStream .replace(/<%apiKey%>/g, this.c.apiKey) .replace(/<%streamId%>/g, streamId) .replace(/<%sessionId%>/g, sessionId); - request.get( - url, - { - headers: this.generateHeaders() - }, - function requestCallback(err, resp, body) { - if (err) { - return cb(new Error('The request failed: ' + err)); - } - if (resp.statusCode === 200) { - return cb(null, body); - } - return cb(new Error('(' + resp.statusCode + ') ' + body.message)); - } - ); + api({ + url: url, + method: 'GET', + headers: this.generateHeaders(), + callback: cb, + }); + }; Client.prototype.listStreams = function listStreams(sessionId, cb) { - var url = this.c.apiUrl + const url = this.c.apiUrl + this.c.endpoints.listStreams .replace(/<%apiKey%>/g, this.c.apiKey) .replace(/<%sessionId%>/g, sessionId); - request.get( - url, - { - headers: this.generateHeaders() + api({ + url: url, + method: 'GET', + headers: this.generateHeaders(), + callback: (err, response) => { + const body = response ? JSON.parse(response) : response; + cb(err, body?.items.map((stream) => new Stream(JSON.stringify(stream)))) }, - function requestCallback(err, resp, body) { - if (err) { - return cb(new Error('The request failed: ' + err)); - } - if (resp.statusCode === 200) { - return cb( - null, - JSON.parse(body).items.map(function itemIterator(item) { - return new Stream(JSON.stringify(item)); - }) - ); - } - return cb(new Error('(' + resp.statusCode + ') ' + body.message)); - } - ); + }); }; Client.prototype.generateHeaders = function () { diff --git a/package.json b/package.json index 4030e399..3560a9c7 100644 --- a/package.json +++ b/package.json @@ -1,16 +1,17 @@ { + "$schema": "https://json.schemastore.org/package.json", "name": "opentok", - "description": "OpenTok server-side SDK", "version": "2.17.0", + "description": "OpenTok server-side SDK", "homepage": "https://github.com/opentok/opentok-node", - "repository": { - "type": "git", - "url": "https://github.com/opentok/opentok-node.git" - }, "bugs": { "url": "https://github.com/opentok/opentok-node/issues", "email": "support@tokbox.com" }, + "repository": { + "type": "git", + "url": "https://github.com/opentok/opentok-node.git" + }, "license": "MIT", "contributors": [ { @@ -55,6 +56,11 @@ { "name": "Mofi Rahman", "url": "https://twitter.com/moficodes" + }, + { + "name": "Chuck \"MANCHUCK\" Reeves", + "email": "chuck@manchuck.com", + "url": "https://github.com/manchuck" } ], "main": "lib/opentok.js", @@ -62,19 +68,22 @@ "lib/" ], "scripts": { - "test": "npm run test-no-lint && npm run jasmine_node", - "test-coverage": "cross-env NODE_ENV=test nyc mocha", - "test-coverage-html": "cross-env NODE_ENV=test nyc --reporter html mocha", - "test-no-lint": "mocha ./test/*-test.js", + "jasmine-coverage": "cross-env NODE_ENV=test nyc --reporter=text-lcov jasmine-node spec > jasmine.lcov", + "jasmine_node": "jasmine spec/opentok_spec.js", "lint": "./node_modules/.bin/eslint ./lib/ ./test/ ./sample/", "lint-fix": "eslint --fix lib test", - "jasmine_node": "jasmine spec/opentok_spec.js", - "report-coverage": "npm run mocha-coverage && npm run jasmine-coverage", "mocha-coverage": "cross-env NODE_ENV=test nyc --reporter=text-lcov mocha > mocha.lcov", - "jasmine-coverage": "cross-env NODE_ENV=test nyc --reporter=text-lcov jasmine-node spec > jasmine.lcov" + "report-coverage": "npm run mocha-coverage && npm run jasmine-coverage", + "test": "npm run test-no-lint && npm run jasmine_node", + "test-coverage": "cross-env NODE_ENV=test nyc mocha", + "test-coverage-html": "cross-env NODE_ENV=test nyc --reporter html mocha", + "test-no-lint": "mocha ./test/*-test.js" }, - "engines": { - "node": ">=4" + "dependencies": { + "jsonwebtoken": "9.0.2", + "lodash": "4.17.21", + "node-fetch": "2.7.0", + "opentok-token": "1.1.1" }, "devDependencies": { "chai": "4.3.10", @@ -88,11 +97,7 @@ "nock": "13.3.6", "nyc": "15.1.0" }, - "dependencies": { - "jsonwebtoken": "9.0.2", - "lodash": "4.17.21", - "needle": "3.2.0", - "node-fetch": "2.7.0", - "opentok-token": "1.1.1" + "engines": { + "node": ">=4" } } diff --git a/test/opentok-test.js b/test/opentok-test.js index b167a126..c24f367c 100644 --- a/test/opentok-test.js +++ b/test/opentok-test.js @@ -268,7 +268,7 @@ describe('when initialized with an apiUrl', function () { var scope = nock(apiUrl) .matchHeader('x-opentok-auth', function (value) { try { - jwt.verify(value, apiSecret, { issuer: apiKey }); + jwt.verify(value[0], apiSecret, { issuer: apiKey }); return true; } catch (error) { @@ -372,7 +372,7 @@ describe('when a user agent addendum is needed', function () { var scope = nock('https://api.opentok.com:443') .matchHeader('x-opentok-auth', function (value) { try { - jwt.verify(value, apiSecret, { issuer: apiKey }); + jwt.verify(value[0], apiSecret, { issuer: apiKey }); return true; } catch (error) { @@ -490,7 +490,7 @@ describe('#createSession', function () { var scope = nock('https://api.opentok.com:443') .matchHeader('x-opentok-auth', function (value) { try { - jwt.verify(value, apiSecret, { issuer: apiKey }); + jwt.verify(value[0], apiSecret, { issuer: apiKey }); return true; } catch (error) { @@ -527,7 +527,7 @@ describe('#createSession', function () { var scope = nock('https://api.opentok.com:443') .matchHeader('x-opentok-auth', function (value) { try { - jwt.verify(value, apiSecret, { issuer: apiKey }); + jwt.verify(value[0], apiSecret, { issuer: apiKey }); return true; } catch (error) { @@ -566,7 +566,7 @@ describe('#createSession', function () { var scope = nock('https://api.opentok.com:443') .matchHeader('x-opentok-auth', function (value) { try { - jwt.verify(value, apiSecret, { issuer: apiKey }); + jwt.verify(value[0], apiSecret, { issuer: apiKey }); return true; } catch (error) { @@ -602,7 +602,7 @@ describe('#createSession', function () { var scope = nock('https://api.opentok.com:443') .matchHeader('x-opentok-auth', function (value) { try { - jwt.verify(value, apiSecret, { issuer: apiKey }); + jwt.verify(value[0], apiSecret, { issuer: apiKey }); return true; } catch (error) { @@ -641,7 +641,7 @@ describe('#createSession', function () { var scope = nock('https://api.opentok.com:443') .matchHeader('x-opentok-auth', function (value) { try { - jwt.verify(value, apiSecret, { issuer: apiKey }); + jwt.verify(value[0], apiSecret, { issuer: apiKey }); return true; } catch (error) { @@ -680,7 +680,7 @@ describe('#createSession', function () { var scope = nock('https://api.opentok.com:443') .matchHeader('x-opentok-auth', function (value) { try { - jwt.verify(value, apiSecret, { issuer: apiKey }); + jwt.verify(value[0], apiSecret, { issuer: apiKey }); return true; } catch (error) { @@ -719,7 +719,7 @@ describe('#createSession', function () { var scope = nock('https://api.opentok.com:443') .matchHeader('x-opentok-auth', function (value) { try { - jwt.verify(value, apiSecret, { issuer: apiKey }); + jwt.verify(value[0], apiSecret, { issuer: apiKey }); return true; } catch (error) { @@ -789,7 +789,7 @@ describe('#createSession', function () { var scope = nock('https://api.opentok.com:443') .matchHeader('x-opentok-auth', function (value) { try { - jwt.verify(value, apiSecret, { issuer: apiKey }); + jwt.verify(value[0], apiSecret, { issuer: apiKey }); return true; } catch (error) { @@ -820,7 +820,7 @@ describe('#createSession', function () { var scope = nock('https://api.opentok.com:443') .matchHeader('x-opentok-auth', function (value) { try { - jwt.verify(value, apiSecret, { issuer: apiKey }); + jwt.verify(value[0], apiSecret, { issuer: apiKey }); return true; } catch (error) { @@ -833,12 +833,12 @@ describe('#createSession', function () { .reply( 200, '[{"session_id":"' - + sessionId - + '","project_id":"' - + apiKey - + '","partner_id":"' - + apiKey - + '","create_dt":"Fri Nov 18 15:50:36 PST 2016","media_server_url":""}]', + + sessionId + + '","project_id":"' + + apiKey + + '","partner_id":"' + + apiKey + + '","create_dt":"Fri Nov 18 15:50:36 PST 2016","media_server_url":""}]', { server: 'nginx', date: 'Thu, 20 Mar 2014 06:35:24 GMT', @@ -1094,7 +1094,7 @@ describe('#dial', function () { var scope = nock('https://api.opentok.com:443') .matchHeader('x-opentok-auth', function (value) { try { - jwt.verify(value, apiSecret, { issuer: apiKey }); + jwt.verify(value[0], apiSecret, { issuer: apiKey }); return true; } catch (error) { @@ -1138,7 +1138,7 @@ describe('#dial', function () { var scope = nock('https://api.opentok.com:443') .matchHeader('x-opentok-auth', function (value) { try { - jwt.verify(value, apiSecret, { issuer: apiKey }); + jwt.verify(value[0], apiSecret, { issuer: apiKey }); return true; } catch (error) { @@ -1186,7 +1186,7 @@ describe('#dial', function () { var scope = nock('https://api.opentok.com:443') .matchHeader('x-opentok-auth', function (value) { try { - jwt.verify(value, apiSecret, { issuer: apiKey }); + jwt.verify(value[0], apiSecret, { issuer: apiKey }); return true; } catch (error) { @@ -1237,7 +1237,7 @@ describe('#dial', function () { var scope = nock('https://api.opentok.com:443') .matchHeader('x-opentok-auth', function (value) { try { - jwt.verify(value, apiSecret, { issuer: apiKey }); + jwt.verify(value[0], apiSecret, { issuer: apiKey }); return true; } catch (error) { @@ -1283,7 +1283,7 @@ describe('#dial', function () { var scope = nock('https://api.opentok.com:443') .matchHeader('x-opentok-auth', function (value) { try { - jwt.verify(value, apiSecret, { issuer: apiKey }); + jwt.verify(value[0], apiSecret, { issuer: apiKey }); return true; } catch (error) { @@ -1329,7 +1329,7 @@ describe('#dial', function () { var scope = nock('https://api.opentok.com:443') .matchHeader('x-opentok-auth', function (value) { try { - jwt.verify(value, apiSecret, { issuer: apiKey }); + jwt.verify(value[0], apiSecret, { issuer: apiKey }); return true; } catch (error) { @@ -1356,7 +1356,7 @@ describe('#dial', function () { var dialDTMFScope = nock('https://api.opentok.com:443') .matchHeader('x-opentok-auth', function (value) { try { - jwt.verify(value, apiSecret, { issuer: apiKey }); + jwt.verify(value[0], apiSecret, { issuer: apiKey }); return true; } catch (error) { @@ -1403,7 +1403,7 @@ describe('#dial', function () { var scope = nock('https://api.opentok.com:443') .matchHeader('x-opentok-auth', function (value) { try { - jwt.verify(value, apiSecret, { issuer: apiKey }); + jwt.verify(value[0], apiSecret, { issuer: apiKey }); return true; } catch (error) { @@ -1430,7 +1430,7 @@ describe('#dial', function () { var dialDTMFScope = nock('https://api.opentok.com:443') .matchHeader('x-opentok-auth', function (value) { try { - jwt.verify(value, apiSecret, { issuer: apiKey }); + jwt.verify(value[0], apiSecret, { issuer: apiKey }); return true; } catch (error) { @@ -1548,7 +1548,7 @@ describe('#websocketconnect', function () { var scope = nock('https://api.opentok.com:443') .matchHeader('x-opentok-auth', function (value) { try { - jwt.verify(value, apiSecret, { issuer: apiKey }); + jwt.verify(value[0], apiSecret, { issuer: apiKey }); return true; } catch (error) { @@ -1589,7 +1589,7 @@ describe('#websocketconnect', function () { var scope = nock('https://api.opentok.com:443') .matchHeader('x-opentok-auth', function (value) { try { - jwt.verify(value, apiSecret, { issuer: apiKey }); + jwt.verify(value[0], apiSecret, { issuer: apiKey }); return true; } catch (error) { @@ -1634,7 +1634,7 @@ describe('#websocketconnect', function () { var scope = nock('https://api.opentok.com:443') .matchHeader('x-opentok-auth', function (value) { try { - jwt.verify(value, apiSecret, { issuer: apiKey }); + jwt.verify(value[0], apiSecret, { issuer: apiKey }); return true; } catch (error) { @@ -1816,7 +1816,7 @@ describe('#startBroadcast', function () { error: 'remote error message' }); opentok.startBroadcast(SESSIONID, options, function (err, broadcast) { - expect(err.message).to.equal('Failed to start broadcast. Error: (400) {"error":"remote error message"}'); + expect(err.message).to.equal('Failed to start broadcast. Error: Bad session ID, token, SIP credentials, or SIP URI (sip:user@domain.tld)'); expect(broadcast).to.be.undefined; done(); });