From f87ecc237bfdbfb059d2e00228802f93d9281c67 Mon Sep 17 00:00:00 2001 From: Brian Donovan Date: Fri, 8 Jun 2018 10:08:10 -0700 Subject: [PATCH] fix(astexplorer): accept non-SSL URLs Since astexplorer.net doesn't force SSL, users might be using it without SSL. When they copy the URL, it'll be `http` instead of `https`. We now accept such URLs, but turn them into `https` when normalizing them. --- src/resolvers/AstExplorerResolver.ts | 9 ++++++--- test/unit/resolvers/AstExplorerResolverTest.ts | 12 ++++++++++++ 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/src/resolvers/AstExplorerResolver.ts b/src/resolvers/AstExplorerResolver.ts index 6406d239..13a6404f 100644 --- a/src/resolvers/AstExplorerResolver.ts +++ b/src/resolvers/AstExplorerResolver.ts @@ -84,8 +84,11 @@ export default class AstExplorerResolver extends NetworkResolver { } private matchesHost(url: URL): boolean { - return ( - url.protocol === this.baseURL.protocol && url.host === this.baseURL.host - ); + if (url.host !== this.baseURL.host) { + return false; + } + + // use SSL even if the URL doesn't use it + return url.protocol === this.baseURL.protocol || url.protocol === 'http:'; } } diff --git a/test/unit/resolvers/AstExplorerResolverTest.ts b/test/unit/resolvers/AstExplorerResolverTest.ts index dfe9f300..7df16137 100644 --- a/test/unit/resolvers/AstExplorerResolverTest.ts +++ b/test/unit/resolvers/AstExplorerResolverTest.ts @@ -17,6 +17,18 @@ describe('AstExplorerResolver', function() { ); }); + it('normalizes http gist+commit editor URL to an https API URL', async function() { + let resolver = new AstExplorerResolver(); + let normalized = await resolver.normalize( + 'http://astexplorer.net/#/gist/b5b33c/f9ae8a' + ); + + strictEqual( + normalized, + 'https://astexplorer.net/api/v1/gist/b5b33c/f9ae8a' + ); + }); + it('normalizes a gist-only editor URL into an API URL', async function() { let resolver = new AstExplorerResolver(); let normalized = await resolver.normalize(