diff --git a/JavaScript/JavaScriptSDK.Tests/CheckinTests/Util.tests.ts b/JavaScript/JavaScriptSDK.Tests/CheckinTests/Util.tests.ts index 3454beffb..7b5bb9574 100644 --- a/JavaScript/JavaScriptSDK.Tests/CheckinTests/Util.tests.ts +++ b/JavaScript/JavaScriptSDK.Tests/CheckinTests/Util.tests.ts @@ -12,6 +12,7 @@ class UtilTests extends TestClass { public registerTests() { var Util = Microsoft.ApplicationInsights.Util; + var UrlHelper = Microsoft.ApplicationInsights.UrlHelper; this.testCase({ name: "UtilTests: getStorage with available storage", @@ -52,7 +53,7 @@ class UtilTests extends TestClass { // disable storages Util.disableStorage(); - // can't read + // can't read Assert.ok(!Util.canUseLocalStorage(), "can use local storage after it was disabled"); Assert.ok(!Util.canUseSessionStorage(), "can use session storage after it was disabled"); @@ -472,6 +473,39 @@ class UtilTests extends TestClass { Assert.equal(false, Util.isInternalApplicationInsightsEndpoint("https://somethingelse.com/v2/track")); } }); + + this.testCase({ + name: "UrlHelper: parseUrl should contain host field even if document.createElement is not defined", + test: () => { + var origCreateElement = document.createElement; + document.createElement = null; + + let passed; + let match; + try { + const host = UrlHelper.parseUrl("https://portal.azure.com/some/endpoint").host.toLowerCase(); + passed = true; + match = host === "portal.azure.com"; + } catch (e) { + passed = false; + } + + // Need to reset createElement before doing any assertions, else qunit crashes + document.createElement = origCreateElement; + Assert.ok(passed); + Assert.ok(match, "host should be portal.azure.com"); + } + }); + + this.testCase({ + name: "UrlHelper: parseHost should return correct host name", + test: () => { + Assert.equal("portal.azure.com", UrlHelper.parseHost("https://portal.azure.com/some/endpoint")); + Assert.equal("bing.com", UrlHelper.parseHost("http://www.bing.com")); + Assert.equal("bing.com", UrlHelper.parseHost("https://www2.bing.com/")); + Assert.equal("p.r.e.f.i.x.bing.com", UrlHelper.parseHost("http://wwW2.p.r.e.f.i.x.bing.com/")); + } + }); } private getMockStorage() { @@ -482,4 +516,4 @@ class UtilTests extends TestClass { return storage; } } -new UtilTests().registerTests(); \ No newline at end of file +new UtilTests().registerTests(); \ No newline at end of file diff --git a/JavaScript/JavaScriptSDK/Util.ts b/JavaScript/JavaScriptSDK/Util.ts index f2864fb15..d2ce60d80 100644 --- a/JavaScript/JavaScriptSDK/Util.ts +++ b/JavaScript/JavaScriptSDK/Util.ts @@ -477,9 +477,9 @@ module Microsoft.ApplicationInsights { return (days > 0 ? days + "." : "") + hour + ":" + min + ":" + sec + "." + ms; } - /** - * Checks if error has no meaningful data inside. Ususally such errors are received by window.onerror when error - * happens in a script from other domain (cross origin, CORS). + /** + * Checks if error has no meaningful data inside. Ususally such errors are received by window.onerror when error + * happens in a script from other domain (cross origin, CORS). */ public static isCrossOriginError(message: string, url: string, lineNumber: number, columnNumber: number, error: Error): boolean { return (message === "Script error." || message === "Script error") && !error; @@ -512,7 +512,7 @@ module Microsoft.ApplicationInsights { /** * Adds an event handler for the specified event * @param eventName {string} - The name of the event - * @param callback {any} - The callback function that needs to be executed for the given event + * @param callback {any} - The callback function that needs to be executed for the given event * @return {boolean} - true if the handler was successfully added */ public static addEventHandler(eventName: string, callback: any): boolean { @@ -549,7 +549,7 @@ module Microsoft.ApplicationInsights { public static parseUrl(url): HTMLAnchorElement { if (!UrlHelper.htmlAnchorElement) { - UrlHelper.htmlAnchorElement = !!UrlHelper.document.createElement ? UrlHelper.document.createElement('a') : {}; + UrlHelper.htmlAnchorElement = !!UrlHelper.document.createElement ? UrlHelper.document.createElement('a') : { host: UrlHelper.parseHost(url) }; } UrlHelper.htmlAnchorElement.href = url; @@ -557,6 +557,16 @@ module Microsoft.ApplicationInsights { return UrlHelper.htmlAnchorElement; } + // Fallback method to grab host from url if document.createElement method is not available + public static parseHost(url: string) { + var match = url.match(/:\/\/(www[0-9]?\.)?(.[^/:]+)/i); + if (match != null && match.length > 2 && typeof match[2] === 'string' && match[2].length > 0) { + return match[2]; + } else { + return null; + } + } + public static getAbsoluteUrl(url): string { var result: string; var a = UrlHelper.parseUrl(url);