@serenity-js/rest
Version:
Serenity/JS Screenplay Pattern library for interacting with REST and other HTTP-based services, supporting comprehensive API testing and blended testing scenarios
26 lines • 1.01 kB
JavaScript
import { ensure, isDefined, isNotBlank, isString } from 'tiny-types';
export function createUrl(options) {
const hostname = ensure('hostname', options?.hostname, isString(), isNotBlank()).trim();
const port = options?.port
? ':' + options?.port
: (options?.protocol ? undefined : ':80');
return new URL([
options?.protocol ? protocolFrom(options?.protocol) : 'http://',
(options?.username || options?.password) && credentialsFrom(options.username, options.password),
hostname,
port,
].filter(Boolean).join(''));
}
function protocolFrom(protocol) {
const protocolName = protocol.match(/([A-Za-z]+)[/:]*/)[1];
ensure('hostname', protocolName, isDefined());
return protocolName + '://';
}
function credentialsFrom(username, password) {
return [
username && encodeURIComponent(username),
password && ':' + encodeURIComponent(password),
'@'
].filter(Boolean).join('');
}
//# sourceMappingURL=createUrl.js.map