Skip to content

Commit

Permalink
test: improving code coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
manchuck committed Oct 20, 2023
1 parent 3c4d674 commit 8cdc8d0
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 45 deletions.
84 changes: 46 additions & 38 deletions lib/callbacks.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,26 +65,29 @@ exports.listCallbacks = function (config, options, callback) {
throw (new errors.ArgumentError('No callback given to listCallbacks'));
}
api(config, 'GET', '/callback', null, function (err, response, body) {
if (!err && body && body.constructor === String) {
try {
body = JSON.parse(body);
}
catch (_err) {
err = _err;
}
if (err) {
callback(err);
}
if (err || Math.floor(response.statusCode / 100) !== 2) {
if (response && response.statusCode === 403) {

try {
body = JSON.parse(body);
}
catch (_err) {
err = _err;
}

switch (response.statusCode) {
case 200:
callback(null, body.map(function (item) {
return new Callback(config, item);
}));
return;
case 403:
callback(new errors.AuthError('Invalid API key or secret'));
}
else {
return;
default:
callback(new errors.RequestError('Unexpected response from OpenTok: ' + JSON.stringify(body || { statusCode: response.statusCode, statusMessage: response.statusMessage })));
}
}
else {
callback(null, body.map(function (item) {
return new Callback(config, item);
}));
return;
}
});
};
Expand All @@ -104,20 +107,21 @@ exports.registerCallback = function (config, options, callback) {
}, function apiCallback(err, response, body) {
if (err) {
callback(err);
return;
}
else if (Math.floor(response.statusCode / 100) !== 2) {
if (response && response.statusCode === 404) {

switch(response.statusCode) {
case 200:
callback(null, new Callback(config, body));
return;
case 404:
callback(new errors.CallbackError('Callback event not found'));
}
else if (response && response.statusCode === 403) {
return;
case 403:
callback(new errors.AuthError('Invalid API key or secret'));
}
else {
return;
default:
callback(new errors.RequestError('Unexpected response from OpenTok: ' + JSON.stringify(body || { statusCode: response.statusCode, statusMessage: response.statusMessage })));
}
}
else {
callback(null, new Callback(config, body));
}
});
};
Expand All @@ -138,20 +142,24 @@ exports.unregisterCallback = function (config, callbackId, callback) {
function (err, response, body) {
if (err) {
callback(err);
return;
}
else if (Math.floor(response.statusCode / 100) !== 2) {
if (response && response.statusCode === 404) {

switch (response.statusCode) {
case 204:
callback(null);
return;
case 404:
callback(new errors.CallbackError('Callback not found'));
}
else if (response && response.statusCode === 403) {
return;
case 403:
callback(new errors.AuthError('Invalid API key or secret'));
}
else {
callback(new errors.RequestError('Unexpected response from OpenTok: ' + JSON.stringify(body || { statusCode: response.statusCode, statusMessage: response.statusMessage })));
}
}
else {
callback(null);
return;
default:
callback(new errors.RequestError('Unexpected response from OpenTok: ' + JSON.stringify(body || {
statusCode: response.statusCode,
statusMessage: response.statusMessage
})));
}
}
);
Expand Down
3 changes: 2 additions & 1 deletion lib/moderation.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ var api = function (config, method, session, connection, body, callback) {
(config.uaAddendum ? ' ' + config.uaAddendum : '')
};

if (body && ['POST', 'PATCH', 'PUT'].includes(method)) {
if (body) {
headers['Content-Type'] = 'application/json';
}

Expand All @@ -37,6 +37,7 @@ var api = function (config, method, session, connection, body, callback) {
callback(null, otResponse, body);
})
.catch(async (error) => {
/* istanbul ignore next */
callback(error);
});
};
Expand Down
10 changes: 4 additions & 6 deletions lib/opentok.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,7 @@ function decodeSessionId(sessionId) {
if (typeof Buffer.from === 'function') {
sessionId = Buffer.from(sessionId, 'base64').toString('ascii');
}
else {
sessionId = Buffer.from(sessionId, 'base64').toString('ascii');
}

// separate fields
fields = sessionId.split('~');
return {
Expand Down Expand Up @@ -1158,7 +1156,7 @@ OpenTok = function (apiKey, apiSecret, env) {
* an <code>error</code> object is passed in as the first parameter of the function.
* Upon success, the function is called with no error object passed in (as the first parameter)
* and the second parameter is an object with the following properties:
*
*
* <ul>
* <li>
* <code>id</code> -- A unique ID identifying the Audio Streamer WebSocket connection.
Expand All @@ -1168,7 +1166,7 @@ OpenTok = function (apiKey, apiSecret, env) {
* WebSocket connection in the OpenTok session.
* </li>
* </ul>
*
*
* @method #websocketConnect
* @memberof OpenTok
*/
Expand Down Expand Up @@ -1825,7 +1823,7 @@ OpenTok.prototype.dial = function (sessionId, token, sipUri, options, callback)
* OpenTok network. If you do not set a location hint, the OpenTok servers will be based on
* the first client connecting to the session.
* </li>
*
*
* <li><code>e2ee</code> (Boolean) &mdash;
* Determines whether to enable <a href="https://tokbox.com/developer/guides/end-to-end-encryption/">
`end-to-end encryption</a>
Expand Down

0 comments on commit 8cdc8d0

Please sign in to comment.