UNPKG

cumulocity-cypress

Version:
173 lines (172 loc) 5.47 kB
import _ from "lodash"; export function isURL(obj) { return obj instanceof URL; } export function relativeURL(url) { try { const u = isURL(url) ? url : new URL(url); return u.pathname + u.search; } catch { return undefined; } } export function urlForBaseUrl(baseUrl, relativeOrAbsoluteUrl) { if (relativeOrAbsoluteUrl) { try { const url = new URL(relativeOrAbsoluteUrl, baseUrl); return url.toString(); } catch { // no-op } } else { return baseUrl; } return relativeOrAbsoluteUrl; } export function removeBaseUrlFromString(url, baseUrl) { if (!url || !baseUrl) { return url; } let normalizedBaseUrl = _.clone(baseUrl); while (normalizedBaseUrl.endsWith("/")) { normalizedBaseUrl = normalizedBaseUrl.slice(0, -1); } let result = url.replace(normalizedBaseUrl, ""); if (_.isEmpty(result)) { result = "/"; } return result; } export function removeBaseUrlFromRequestUrl(record, baseUrl) { if (!record?.request?.url || !baseUrl || !_.isString(baseUrl)) { return; } record.request.url = removeBaseUrlFromString(record.request.url, baseUrl); } export function normalizeUrl(url) { return url.replace(/\/+$/, ""); } export function tenantUrl(baseUrl, tenant) { if (!baseUrl || !tenant) return undefined; try { const url = new URL(baseUrl); const hostComponents = url.host.split("."); if (hostComponents.length <= 2) { url.host = `${tenant}.${hostComponents.join(".")}`; } else { const instance = url.host.split(".")?.slice(1)?.join("."); url.host = `${tenant}.${instance}`; } return normalizeUrl(url.toString()); } catch { // no-op } return undefined; } export function updateURLs(value, from, to) { if (!value || !from || !to) return value; let result = value; const fromTenantUrl = tenantUrl(from.baseUrl, from.tenant); const toTenantUrl = tenantUrl(to.baseUrl, to.tenant); if (fromTenantUrl && toTenantUrl) { result = result.replace(new RegExp(fromTenantUrl, "g"), toTenantUrl); } if (from.baseUrl && to.baseUrl) { const fromBaseUrl = normalizeUrl(from.baseUrl); const toBaseUrl = normalizeUrl(to.baseUrl); if (fromBaseUrl && toBaseUrl) { result = result.replace(new RegExp(fromBaseUrl, "g"), toBaseUrl); } result = result.replace(new RegExp(from.baseUrl.replace(/https?:\/\//i, ""), "g"), to.baseUrl.replace(/https?:\/\//i, "")); if (fromTenantUrl) { result = result.replace(new RegExp(fromTenantUrl, "g"), toTenantUrl || toBaseUrl); } } return result; } /** * Checks if the given URL is an absolute URL. * @param url The URL to check. * @returns True if the URL is an absolute URL, false otherwise. */ export function isAbsoluteURL(url) { if (!url || !_.isString(url) || _.isEmpty(url)) return false; return /^https?:\/\//i.test(url); } /** * Validates the base URL and throws an error if the base URL is not an absolute URL. This * is required as commands expect an absolute URL as baseUrl. Will not fail for undefined values. * `Cypress.config().baseUrl` is validated by Cypress itself and throw an error. * * @param baseUrl The url to validate. */ export function validateBaseUrl(baseUrl) { if (baseUrl != null && !isAbsoluteURL(baseUrl)) { const error = new Error(`Invalid value for base url. '${baseUrl}' must be an absolute URL or undefined.`); error.name = "C8yPactError"; throw error; } } /** * Normalizes a URL to ensure it has a protocol and proper trailing slash. * If no protocol is present, HTTPS is added by default. * If the URL has no path component, a trailing slash is appended. * * @param url - The URL string to normalize * @returns The normalized URL with HTTPS protocol and trailing slash if appropriate, or undefined for invalid input */ export function normalizeBaseUrl(url) { if (!url || !_.isString(url)) { return undefined; } const trimmedUrl = url.trim(); if (!trimmedUrl) { return undefined; } let normalizedUrl; // Check if URL already has a protocol if (/^https?:\/\//i.test(trimmedUrl)) { normalizedUrl = trimmedUrl; } else { // Add https:// if no protocol is present normalizedUrl = `https://${trimmedUrl}`; } try { const urlObj = new URL(normalizedUrl); // remove all components other than protocol, host normalizedUrl = `${urlObj.protocol}//${urlObj.host}`; } catch (error) { const errorMessage = error instanceof Error ? error.message : String(error); throw new Error(`Failed to normalize base url ${url}. ${errorMessage}`); } return normalizedUrl; } /** * Converts the given URL to a string. * @param url The URL or RequestInfo to convert. * @returns The URL as a string. */ export function toUrlString(url) { if (_.isString(url)) { return url; } else if (url instanceof URL) { return url.toString(); } else if (url instanceof Request) { return url.url; } else { throw new Error(`Type for URL not supported. Expected URL, string or Request, but found $'{typeof url}}'.`); } }