How to use addDefaultPort method in Cypress

Best JavaScript code snippet using cypress

Run Cypress automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

uri.js

Source: uri.js Github

copy
1"use strict";
2// useful links for describing the parts that make up a URL:
3// - https://nodejs.org/api/url.html#url_url_strings_and_url_objects
4// - https://en.wikipedia.org/wiki/Uniform_Resource_Identifier#Examples
5//
6// node's url formatting algorithm (which acts pretty unexpectedly)
7// - https://nodejs.org/api/url.html#url_url_format_urlobject
8var __importDefault = (this && this.__importDefault) || function (mod) {
9    return (mod && mod.__esModule) ? mod : { "default": mod };
10};
11Object.defineProperty(exports, "__esModule", { value: true });
12exports.getPath = exports.addDefaultPort = exports.removeDefaultPort = exports.removePort = exports.stripProtocolAndDefaultPorts = exports.parse = void 0;
13const lodash_1 = __importDefault(require("lodash"));
14const url_1 = __importDefault(require("url"));
15// yup, protocol contains a: ':' colon
16// at the end of it (-______________-)
17const DEFAULT_PROTOCOL_PORTS = {
18    'https:': '443',
19    'http:': '80',
20};
21const DEFAULT_PORTS = lodash_1.default.values(DEFAULT_PROTOCOL_PORTS);
22const portIsDefault = (port) => {
23    return port && DEFAULT_PORTS.includes(port);
24};
25const parseClone = (urlObject) => {
26    return url_1.default.parse(lodash_1.default.clone(urlObject));
27};
28exports.parse = url_1.default.parse;
29function stripProtocolAndDefaultPorts(urlToCheck) {
30    // grab host which is 'hostname:port' only
31    const { host, hostname, port } = url_1.default.parse(urlToCheck);
32    // if we have a default port for 80 or 443
33    // then just return the hostname
34    if (portIsDefault(port)) {
35        return hostname;
36    }
37    // else return the host
38    return host;
39}
40exports.stripProtocolAndDefaultPorts = stripProtocolAndDefaultPorts;
41function removePort(urlObject) {
42    const parsed = parseClone(urlObject);
43    // set host to undefined else url.format(...) will ignore the port property
44    // https://nodejs.org/api/url.html#url_url_format_urlobject
45    // Additionally, the types are incorrect (don't include undefined), so we add TS exceptions
46    /* @ts-ignore */
47    delete parsed.host;
48    /* @ts-ignore */
49    delete parsed.port;
50    return parsed;
51}
52exports.removePort = removePort;
53function removeDefaultPort(urlToCheck) {
54    let parsed = parseClone(urlToCheck);
55    if (portIsDefault(parsed.port)) {
56        parsed = removePort(parsed);
57    }
58    return parsed;
59}
60exports.removeDefaultPort = removeDefaultPort;
61function addDefaultPort(urlToCheck) {
62    const parsed = parseClone(urlToCheck);
63    if (!parsed.port) {
64        // unset host...
65        // see above for reasoning
66        /* @ts-ignore */
67        delete parsed.host;
68        if (parsed.protocol) {
69            parsed.port = DEFAULT_PROTOCOL_PORTS[parsed.protocol];
70        }
71        else {
72            /* @ts-ignore */
73            delete parsed.port;
74        }
75    }
76    return parsed;
77}
78exports.addDefaultPort = addDefaultPort;
79function getPath(urlToCheck) {
80    return url_1.default.parse(urlToCheck).path;
81}
82exports.getPath = getPath;
83
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Run JavaScript Tests on LambdaTest Cloud Grid

Execute automation tests with Cypress on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)