From e88e032d6aaaa6e44f9e9926dfa4edfe8f4b5eaa Mon Sep 17 00:00:00 2001 From: Pine Wu Date: Wed, 6 Nov 2019 11:01:02 -0800 Subject: [PATCH] Refactor link protection test --- .../test/contrib/linkProtection.test.ts | 53 +++++++++++-------- 1 file changed, 30 insertions(+), 23 deletions(-) diff --git a/src/vs/workbench/test/contrib/linkProtection.test.ts b/src/vs/workbench/test/contrib/linkProtection.test.ts index de8b11bde16..f027009a141 100644 --- a/src/vs/workbench/test/contrib/linkProtection.test.ts +++ b/src/vs/workbench/test/contrib/linkProtection.test.ts @@ -8,42 +8,49 @@ import * as assert from 'assert'; import { isURLDomainTrusted } from 'vs/workbench/contrib/url/common/trustedDomainsValidator'; import { URI } from 'vs/base/common/uri'; -suite('Link protection domain matching', () => { +function linkProtectedByRules(link: string, rules: string[]) { + assert.ok(isURLDomainTrusted(URI.parse(link), rules)); +} +function linkNotProtectedByRules(link: string, rules: string[]) { + assert.ok(!isURLDomainTrusted(URI.parse(link), rules)); +} +suite('Link protection domain matching', () => { test('simple', () => { - assert.ok(!isURLDomainTrusted(URI.parse('https://x.org'), [])); - assert.ok(isURLDomainTrusted(URI.parse('https://x.org'), ['https://x.org'])); - assert.ok(isURLDomainTrusted(URI.parse('https://x.org/foo'), ['https://x.org'])); + linkNotProtectedByRules('https://x.org', []); + + linkProtectedByRules('https://x.org', ['https://x.org']); + linkProtectedByRules('https://x.org/foo', ['https://x.org']); - assert.ok(!isURLDomainTrusted(URI.parse('https://x.org'), ['http://x.org'])); - assert.ok(!isURLDomainTrusted(URI.parse('http://x.org'), ['https://x.org'])); + linkNotProtectedByRules('https://x.org', ['http://x.org']); + linkNotProtectedByRules('http://x.org', ['https://x.org']); - assert.ok(!isURLDomainTrusted(URI.parse('https://www.x.org'), ['https://x.org'])); + linkNotProtectedByRules('https://www.x.org', ['https://x.org']); - assert.ok(isURLDomainTrusted(URI.parse('https://www.x.org'), ['https://www.x.org', 'https://y.org'])); + linkProtectedByRules('https://www.x.org', ['https://www.x.org', 'https://y.org']); }); test('localhost', () => { - assert.ok(isURLDomainTrusted(URI.parse('https://127.0.0.1'), [])); - assert.ok(isURLDomainTrusted(URI.parse('https://127.0.0.1:3000'), [])); - assert.ok(isURLDomainTrusted(URI.parse('https://localhost'), [])); - assert.ok(isURLDomainTrusted(URI.parse('https://localhost:3000'), [])); + linkProtectedByRules('https://127.0.0.1', []); + linkProtectedByRules('https://127.0.0.1:3000', []); + linkProtectedByRules('https://localhost', []); + linkProtectedByRules('https://localhost:3000', []); }); test('* star', () => { - assert.ok(isURLDomainTrusted(URI.parse('https://a.x.org'), ['https://*.x.org'])); - assert.ok(isURLDomainTrusted(URI.parse('https://a.b.x.org'), ['https://*.x.org'])); - assert.ok(isURLDomainTrusted(URI.parse('https://a.x.org'), ['https://a.x.*'])); - assert.ok(isURLDomainTrusted(URI.parse('https://a.x.org'), ['https://a.*.org'])); - assert.ok(isURLDomainTrusted(URI.parse('https://a.x.org'), ['https://*.*.org'])); - assert.ok(isURLDomainTrusted(URI.parse('https://a.b.x.org'), ['https://*.b.*.org'])); - assert.ok(isURLDomainTrusted(URI.parse('https://a.a.b.x.org'), ['https://*.b.*.org'])); + linkProtectedByRules('https://a.x.org', ['https://*.x.org']); + linkProtectedByRules('https://a.b.x.org', ['https://*.x.org']); + linkProtectedByRules('https://a.x.org', ['https://a.x.*']); + linkProtectedByRules('https://a.x.org', ['https://a.*.org']); + linkProtectedByRules('https://a.x.org', ['https://*.*.org']); + linkProtectedByRules('https://a.b.x.org', ['https://*.b.*.org']); + linkProtectedByRules('https://a.a.b.x.org', ['https://*.b.*.org']); }); test('no scheme', () => { - assert.ok(isURLDomainTrusted(URI.parse('https://a.x.org'), ['a.x.org'])); - assert.ok(isURLDomainTrusted(URI.parse('https://a.x.org'), ['*.x.org'])); - assert.ok(isURLDomainTrusted(URI.parse('https://a.b.x.org'), ['*.x.org'])); - assert.ok(isURLDomainTrusted(URI.parse('https://x.org'), ['*.x.org'])); + linkProtectedByRules('https://a.x.org', ['a.x.org']); + linkProtectedByRules('https://a.x.org', ['*.x.org']); + linkProtectedByRules('https://a.b.x.org', ['*.x.org']); + linkProtectedByRules('https://x.org', ['*.x.org']); }); }); -- GitLab