Skip to content

Commit

Permalink
updates unit tests
Browse files Browse the repository at this point in the history
Signed-off-by: Diana Barsan <[email protected]>
  • Loading branch information
dianabarsan committed Jan 15, 2025
1 parent c10ac9d commit ee374cb
Show file tree
Hide file tree
Showing 10 changed files with 460 additions and 1,638 deletions.
7 changes: 5 additions & 2 deletions package-lock.json

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

2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -214,8 +214,6 @@
"prometheus-api-metrics": "^3.2.2",
"properties": "^1.2.1",
"rate-limiter-flexible": "^3.0.2",
"request": "^2.88.2",
"request-promise-native": "^1.0.9",
"sanitize-html": "^2.12.1",
"semver": "^7.5.4",
"simple-password-tester": "^1.0.0",
Expand Down
197 changes: 0 additions & 197 deletions sentinel/package-lock.json

This file was deleted.

2 changes: 1 addition & 1 deletion shared-libs/couch-request/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "Provides a declarative global options interface for http requests",
"main": "src/couch-request.js",
"scripts": {
"test": "mocha ./test"
"test": "nyc --nycrcPath='../nyc.config.js' mocha ./test"
},
"author": "",
"license": "Apache-2.0",
Expand Down
47 changes: 26 additions & 21 deletions shared-libs/couch-request/src/couch-request.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
const environment = require('@medic/environment');
const servername = environment.host;
let asyncLocalStorage;
let requestIdHeader;


const isString = value => typeof value === 'string' || value instanceof String;
const isTrue = value => isString(value) ? value.toLowerCase() === 'true' : value === true;

Expand Down Expand Up @@ -37,25 +35,27 @@ const setRequestUri = (options) => {
uri = `${uri}?${new URLSearchParams(options.qs).toString()}`;
}

delete options.url;
delete options.baseUrl;
delete options.qs;

options.uri = uri;
};

const setRequestAuth = (options) => {
let auth;

if (options.auth) {
auth = options.auth;
} else {
const url = new URL(options.uri);
if (url.username) {
auth = { username: url.username, password: url.password };
url.username = '';
url.password = '';
options.uri = url.toString();
}
let auth = options.auth;

const url = new URL(options.uri);
if (url.username) {
auth = auth || { username: url.username, password: url.password };
url.username = '';
url.password = '';
options.uri = url.toString();
}

if (!auth) {
delete options.auth;

if (!auth || options.headers.Authorization) {
return;
}

Expand All @@ -74,20 +74,25 @@ const setRequestContentType = (options) => {
if (sendJson) {
options.headers.Accept = 'application/json';
options.headers['Content-Type'] = 'application/json';
options.body = JSON.stringify(options.body);
options.body && (options.body = JSON.stringify(options.body));
}

if (!sendJson && options.form) {
const formData = new FormData();
Object.keys(options.form).forEach(key => formData.append(key, options.form[key]));
options.headers['Content-Type'] = 'multipart/form-data';
options.body = formData;
delete options.headers.Accept;

options.body = new URLSearchParams(formData).toString();
}

delete options.json;
delete options.form;

return sendJson;
};

const getRequestOptions = (options, servername) => {
const getRequestOptions = (options) => {
options.headers = options.headers || {};

const requestId = asyncLocalStorage?.getRequestId();
Expand All @@ -99,7 +104,7 @@ const getRequestOptions = (options, servername) => {
setRequestAuth(options);
const sendJson = setRequestContentType(options);
if (addServername) {
options.servername = servername;
options.servername = environment.host;
}

return { options, sendJson };
Expand All @@ -112,9 +117,9 @@ const getResponseBody = async (response, sendJson) => {
};

const request = async (options = {}) => {
const { options: requestInit, sendJson } = getRequestOptions(options, servername);
const { options: requestInit, sendJson } = getRequestOptions(options);

const response = await fetch(requestInit.uri, requestInit);
const response = await global.fetch(requestInit.uri, requestInit);
const responseObj = {
...response,
body: await getResponseBody(response, sendJson),
Expand Down
Loading

0 comments on commit ee374cb

Please sign in to comment.