diff options
author | Grégoire Aubert <gregoire.aubert@sonarsource.com> | 2018-02-09 15:44:52 +0100 |
---|---|---|
committer | Guillaume Jambet <guillaume.jambet@gmail.com> | 2018-03-01 15:21:05 +0100 |
commit | 4bf292bac1d4dae9b8338c464e88e9dac6ca4b03 (patch) | |
tree | cd3b55c1a527196333e8cf7e9d6bf207679a0c18 /server/sonar-web/src/main/js/helpers | |
parent | 91fe807305e89ecd9df3b6f4f221540fd451659e (diff) | |
download | sonarqube-4bf292bac1d4dae9b8338c464e88e9dac6ca4b03.tar.gz sonarqube-4bf292bac1d4dae9b8338c464e88e9dac6ca4b03.zip |
SONAR-10347 Add ability to browse webhook deliveries payloads
Diffstat (limited to 'server/sonar-web/src/main/js/helpers')
-rw-r--r-- | server/sonar-web/src/main/js/helpers/__tests__/urls-test.ts | 55 | ||||
-rw-r--r-- | server/sonar-web/src/main/js/helpers/urls.ts | 14 |
2 files changed, 68 insertions, 1 deletions
diff --git a/server/sonar-web/src/main/js/helpers/__tests__/urls-test.ts b/server/sonar-web/src/main/js/helpers/__tests__/urls-test.ts index 5b457e11305..ecd54ab1adb 100644 --- a/server/sonar-web/src/main/js/helpers/__tests__/urls-test.ts +++ b/server/sonar-web/src/main/js/helpers/__tests__/urls-test.ts @@ -23,7 +23,8 @@ import { getPathUrlAsString, getProjectUrl, getQualityGatesUrl, - getQualityGateUrl + getQualityGateUrl, + isUrl } from '../urls'; const SIMPLE_COMPONENT_KEY = 'sonarqube'; @@ -113,3 +114,55 @@ describe('#getQualityGate(s)Url', () => { }); }); }); + +describe('#isUrl', () => { + it('should be valid', () => { + expect(isUrl('https://localhost')).toBeTruthy(); + expect(isUrl('https://localhost/')).toBeTruthy(); + expect(isUrl('https://localhost:9000')).toBeTruthy(); + expect(isUrl('https://localhost:9000/')).toBeTruthy(); + expect(isUrl('https://foo:bar@localhost:9000')).toBeTruthy(); + expect(isUrl('https://foo@localhost')).toBeTruthy(); + expect(isUrl('http://foo.com/blah_blah')).toBeTruthy(); + expect(isUrl('http://foo.com/blah_blah/')).toBeTruthy(); + expect(isUrl('http://www.example.com/wpstyle/?p=364')).toBeTruthy(); + expect(isUrl('https://www.example.com/foo/?bar=baz&inga=42&quux')).toBeTruthy(); + expect(isUrl('http://userid@example.com')).toBeTruthy(); + expect(isUrl('http://userid@example.com/')).toBeTruthy(); + expect(isUrl('http://userid:password@example.com:8080')).toBeTruthy(); + expect(isUrl('http://userid:password@example.com:8080/')).toBeTruthy(); + expect(isUrl('http://userid@example.com:8080')).toBeTruthy(); + expect(isUrl('http://userid@example.com:8080/')).toBeTruthy(); + expect(isUrl('http://userid:password@example.com')).toBeTruthy(); + expect(isUrl('http://userid:password@example.com/')).toBeTruthy(); + expect(isUrl('http://142.42.1.1/')).toBeTruthy(); + expect(isUrl('http://142.42.1.1:8080/')).toBeTruthy(); + expect(isUrl('http://foo.com/blah_(wikipedia)#cite-1')).toBeTruthy(); + expect(isUrl('http://foo.com/blah_(wikipedia)_blah#cite-1')).toBeTruthy(); + expect(isUrl('http://foo.com/(something)?after=parens')).toBeTruthy(); + expect(isUrl('http://code.google.com/events/#&product=browser')).toBeTruthy(); + expect(isUrl('http://j.mp')).toBeTruthy(); + expect(isUrl('http://foo.bar/?q=Test%20URL-encoded%20stuff')).toBeTruthy(); + expect(isUrl('http://1337.net')).toBeTruthy(); + expect(isUrl('http://a.b-c.de')).toBeTruthy(); + expect(isUrl('http://223.255.255.254')).toBeTruthy(); + expect(isUrl('https://foo_bar.example.com/')).toBeTruthy(); + }); + + it('should not be valid', () => { + expect(isUrl('http://')).toBeFalsy(); + expect(isUrl('http://?')).toBeFalsy(); + expect(isUrl('http://??')).toBeFalsy(); + expect(isUrl('http://??/')).toBeFalsy(); + expect(isUrl('http://#')).toBeFalsy(); + expect(isUrl('http://##')).toBeFalsy(); + expect(isUrl('http://##/')).toBeFalsy(); + expect(isUrl('//')).toBeFalsy(); + expect(isUrl('//a')).toBeFalsy(); + expect(isUrl('///a')).toBeFalsy(); + expect(isUrl('///')).toBeFalsy(); + expect(isUrl('foo.com')).toBeFalsy(); + expect(isUrl('http:// shouldfail.com')).toBeFalsy(); + expect(isUrl(':// should fail')).toBeFalsy(); + }); +}); diff --git a/server/sonar-web/src/main/js/helpers/urls.ts b/server/sonar-web/src/main/js/helpers/urls.ts index 732c0b6a6be..2bde3a1184e 100644 --- a/server/sonar-web/src/main/js/helpers/urls.ts +++ b/server/sonar-web/src/main/js/helpers/urls.ts @@ -191,3 +191,17 @@ export function getHomePageUrl(homepage: HomePage) { // should never happen, but just in case... return '/projects'; } + +export function isUrl(url: string) { + if (!URL) { + const elem = document.createElement('a'); + elem.href = url; + return !!(elem.host && elem.hostname && elem.protocol); + } + try { + const parsedUrl = new URL(url); + return url.includes(parsedUrl.host); + } catch (error) { + return false; + } +} |