• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

geosolutions-it / MapStore2 / 15182851401

22 May 2025 09:21AM UTC coverage: 76.604% (-0.01%) from 76.614%
15182851401

push

github

web-flow
updating tests (#11131)

30807 of 48266 branches covered (63.83%)

38541 of 50312 relevant lines covered (76.6%)

32.87 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

88.33
/web/client/utils/URLUtils.js
1
/*
2
* Copyright 2017, GeoSolutions Sas.
3
* All rights reserved.
4
*
5
* This source code is licensed under the BSD-style license found in the
6
* LICENSE file in the root directory of this source tree.
7
*/
8

9
import Url from "url";
10
import { isArray, isString, isEqual, sortBy, find } from 'lodash';
11
import queryString from 'query-string';
12

13
export const urlParts = (url) => {
1✔
14
    if (!url) return {};
56✔
15
    let isRelativeUrl = !(url.indexOf("http") === 0);
53✔
16
    let urlPartsArray = isRelativeUrl ? [] : url.match(/([^:]*:)\/\/([^:]*:?[^@]*@)?([^:\/\?]*):?([^\/\?]*)/);
53✔
17
    if (isRelativeUrl) {
53✔
18
        let location = window.location;
28✔
19
        urlPartsArray[1] = location.protocol;
28✔
20
        urlPartsArray[3] = location.hostname;
28✔
21
        urlPartsArray[4] = location.port;
28✔
22
        urlPartsArray[5] = url;
28✔
23

24
    }
25
    urlPartsArray[4] = urlPartsArray[4] === "" || !urlPartsArray[4] ? (urlPartsArray[1] === "https:" ? "443" : "80") : urlPartsArray[4];
53!
26
    urlPartsArray[5] = urlPartsArray[5] ? urlPartsArray[5] : url.slice(urlPartsArray[0].length);
53✔
27
    const [, protocol, , domain, port, rootPath] = urlPartsArray;
53✔
28
    const applicationRootPath = rootPath.indexOf('/') === 0 ? rootPath.split('/')[1] : '';
53✔
29
    return {protocol, domain, port, rootPath, applicationRootPath};
53✔
30
};
31

32
export const sameQueryParams = ( q1 = "", q2 = "") => {
1!
33
    if (q1 === q2) {
27✔
34
        return true;
11✔
35
    }
36
    // if both "", false or undefined, means they are both empty query strings
37
    if (!q1 && !q2) {
16✔
38
        return true;
4✔
39
    }
40
    const params1 = q1 ? q1.split('&').filter(v => !!v) : [];
14✔
41
    const params2 = q2 ? q2.split('&').filter(v => !!v) : [];
29!
42
    return isEqual(sortBy(params1), sortBy(params2));
12✔
43
};
44

45
/**
46
 * Compares two url to check if are the same. In case of multi-URL (array of URLs)
47
 * passed as parameter, the function will compare the first element of the array with the other URL.
48
 * @function
49
 * @memberof utils.URLUtils
50
 * @param  {string|string[]} u1 the first URL to compare (or an array of URLs)
51
 * @param  {string!string[]} u2 the second URL to compare with (or an array of URLs)
52
 * @return {boolean} true when urls are the same else false
53
 */
54
export const isSameUrl = (u1, u2) => {
1✔
55
    // if array takes the first
56
    const originalUrl = isArray(u1) ? u1[0] : u1;
39!
57
    const otherUrl = isArray(u2) ? u2[0] : u2;
39✔
58
    if (originalUrl === otherUrl) return true;
39✔
59
    if (!originalUrl || !otherUrl) return false; // if one is undefined they are not the same
19✔
60
    // check type before parsing to avoid parse exceptions
61
    if (!isString(originalUrl) || !isString(otherUrl)) return false;
16✔
62
    const urlParsed = Url.parse(originalUrl);
15✔
63
    const otherUrlParsed = Url.parse(otherUrl);
15✔
64

65
    const originalUrlParts = urlParts(originalUrl);
15✔
66
    const otherUrlParts = urlParts(otherUrl);
15✔
67

68
    const isSameProtocol = originalUrlParts.protocol === otherUrlParts.protocol;
15✔
69
    const isSameDomain = originalUrlParts.domain === otherUrlParts.domain;
15✔
70
    const isSamePort = originalUrlParts.port === otherUrlParts.port;
15✔
71
    const isSamePathname = urlParsed.pathname === otherUrlParsed.pathname;
15✔
72
    const isSameQueryParams = sameQueryParams(urlParsed.query, otherUrlParsed.query);
15✔
73
    return isSameProtocol && isSamePort && isSameDomain && isSamePathname && isSameQueryParams;
15✔
74
};
75

76
/**
77
 * Method parse query string into object
78
 * @param {string} url - any url
79
 * @return {object}
80
 */
81
export const getQueryParams = (url) => {
1✔
82
    return queryString.parse(url);
11✔
83
};
84

85
/**
86
 * Validator of URL
87
 * @param {string} url - url to validate
88
 * @param {RegExp} regexp - optional custom regexp
89
 */
90
export const isValidURL = (url, regexp = /^(http(s{0,1}):\/\/)+?[\w.-]+(?:\.[\w\.-]+)+[\w\-\._~:/?#[\]@!\$&'\(\)\*\+,;=.]+$/) => {
1✔
91
    const regex = new RegExp(regexp);
49✔
92
    return regex.test(url);
49✔
93
};
94

95
/**
96
 * Check url templates. It accepts URL in this format
97
 */
98
export const isValidURLTemplate = (url, params, regexp = /^(http(s{0,1}):\/\/)+?[\w.\-{}]+(?:\.[\w\.-]+)+[\w\-\._~\/\;\.\%\:\&\=\?{}]+$/) => {
1✔
99
    const regex = new RegExp(regexp);
46✔
100
    const match = regex.test(url);
46✔
101
    if (!match) {
46✔
102
        return false;
4✔
103
    }
104
    if (match && !params) {
42!
105
        return true;
42✔
106
    }
107
    if (match && params) {
×
108
        const foundParams = /\{(.*?)\}/.test(url);
×
109
        return params.filter(p => find(foundParams, p)).length === 0;
×
110
    }
111
    return false;
×
112

113
};
114

115
/**
116
 * Helper for working with a single string url.
117
 * Use when calling implementations that do not know about array of urls, such as the `urlUtil` library,
118
 * while still supporting our implementation of domain aliases and domain sharding.
119
 *
120
 * @param {string || array} url - Either a string representing a valid url or an array of strings which are all valid urls.
121
 * @returns {string} Returns the argument if the argument is string, otherwise if the argument is an array, returns the first element.
122
 */
123
export const getDefaultUrl = (url) => {
1✔
124
    return isArray(url) ? url[0] : url;
139✔
125
};
126

127
/**
128
 * Updates the given URL by adding or updating query parameters.
129
 *
130
 * @param {string} url - The source URL.
131
 * @param {Object} params - The parameters to add or update.
132
 * @returns {string} - The updated URL with new query parameters.
133
 */
134
export function updateUrlParams(url, params) {
135
    const parsedUrl = queryString.parseUrl(url);
×
136
    const updatedQuery = { ...parsedUrl?.query, ...params };
×
137
    // TODO: use stringifyUrl instead after updating `query-string`, not supported in current version
138
    return parsedUrl?.url + '?' + queryString.stringify(updatedQuery);
×
139
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc