From d3f223c331e7dc05b1c43bf681ee73cbfd675018 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Burak=20Yi=C4=9Fit=20Kaya?= Date: Fri, 8 Sep 2017 13:31:13 +0300 Subject: [PATCH] Fix: fix support for registry URLs without trailing slash (#4350) **Summary** Fixes #4339. Also fixes handling of upper-case registry names. **Test plan** Added unit tests for `NpmRegistry.prototype.getRequestUrl()`. --- __tests__/registries/npm-registry.js | 21 +++++++++++++++++++++ src/registries/npm-registry.js | 4 ++-- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/__tests__/registries/npm-registry.js b/__tests__/registries/npm-registry.js index ac0ac310b0..db1df9f3a0 100644 --- a/__tests__/registries/npm-registry.js +++ b/__tests__/registries/npm-registry.js @@ -281,6 +281,27 @@ describe('isScopedPackage functional test', () => { }); }); +describe('getRequestUrl functional test', () => { + test('returns pathname when it is a full URL', () => { + const testCwd = '.'; + const {mockRequestManager, mockRegistries, mockReporter} = createMocks(); + const npmRegistry = new NpmRegistry(testCwd, mockRegistries, mockRequestManager, mockReporter); + const fullURL = 'HTTP://xn--xample-hva.com:80/foo/bar/baz'; + + expect(npmRegistry.getRequestUrl('https://my.registry.co', fullURL)).toEqual(fullURL); + }); + + test('correctly handles registries lacking a trailing slash', () => { + const testCwd = '.'; + const {mockRequestManager, mockRegistries, mockReporter} = createMocks(); + const npmRegistry = new NpmRegistry(testCwd, mockRegistries, mockRequestManager, mockReporter); + const registry = 'https://my.registry.co/registry'; + const pathname = 'foo/bar/baz'; + + expect(npmRegistry.getRequestUrl(registry, pathname)).toEqual('https://my.registry.co/registry/foo/bar/baz'); + }); +}); + describe('getScope functional test', () => { describe('matches scope correctly', () => { const testCwd = '.'; diff --git a/src/registries/npm-registry.js b/src/registries/npm-registry.js index 1933e7e195..fdd7c19d64 100644 --- a/src/registries/npm-registry.js +++ b/src/registries/npm-registry.js @@ -85,12 +85,12 @@ export default class NpmRegistry extends Registry { } getRequestUrl(registry: string, pathname: string): string { - const isUrl = /^https?:/.test(pathname); + const isUrl = /^https?:/i.test(pathname); if (isUrl) { return pathname; } else { - return url.resolve(registry, pathname); + return url.resolve(addSuffix(registry, '/'), pathname); } }