diff --git a/src/app/serverViewState.test.js b/src/app/serverViewState.test.js index 85d9a233..c5b11bef 100644 --- a/src/app/serverViewState.test.js +++ b/src/app/serverViewState.test.js @@ -428,6 +428,38 @@ describe('app/serverViewState', () => { expect(result.validatedURL).toBe('https://server.com/'); }); + it('should not update the URL if the user is typing https://', async () => { + let result = await serverViewState.handleServerURLValidation({}, 'h'); + expect(result.status).toBe(URLValidationStatus.Invalid); + result = await serverViewState.handleServerURLValidation({}, 'ht'); + expect(result.status).toBe(URLValidationStatus.Invalid); + result = await serverViewState.handleServerURLValidation({}, 'htt'); + expect(result.status).toBe(URLValidationStatus.Invalid); + result = await serverViewState.handleServerURLValidation({}, 'http'); + expect(result.status).toBe(URLValidationStatus.Invalid); + result = await serverViewState.handleServerURLValidation({}, 'HTTP'); + expect(result.status).toBe(URLValidationStatus.Invalid); + result = await serverViewState.handleServerURLValidation({}, 'https'); + expect(result.status).toBe(URLValidationStatus.Invalid); + result = await serverViewState.handleServerURLValidation({}, 'HTTPS'); + expect(result.status).toBe(URLValidationStatus.Invalid); + result = await serverViewState.handleServerURLValidation({}, 'https:'); + expect(result.status).toBe(URLValidationStatus.Invalid); + result = await serverViewState.handleServerURLValidation({}, 'https:/'); + expect(result.status).toBe(URLValidationStatus.Invalid); + result = await serverViewState.handleServerURLValidation({}, 'https://'); + expect(result.status).toBe(URLValidationStatus.Invalid); + result = await serverViewState.handleServerURLValidation({}, 'https://a'); + expect(result.status).toBe(URLValidationStatus.OK); + }); + + it('should update the URL if the user is typing something other than http', async () => { + let result = await serverViewState.handleServerURLValidation({}, 'abchttp'); + expect(result.status).toBe(URLValidationStatus.OK); + result = await serverViewState.handleServerURLValidation({}, 'abchttps'); + expect(result.status).toBe(URLValidationStatus.OK); + }); + it('should attempt HTTP when HTTPS fails, and generate a warning', async () => { ServerInfo.mockImplementation(({url}) => ({ fetchConfigData: jest.fn().mockImplementation(() => { @@ -477,7 +509,7 @@ describe('app/serverViewState', () => { const result = await serverViewState.handleServerURLValidation({}, 'https://not-server.com'); expect(result.status).toBe(URLValidationStatus.NotMattermost); - expect(result.validatedURL).toBe('https://not-server.com/'); + expect(result.validatedURL).toBe('https://not-server.com'); }); it('should update the users URL when the Site URL is different', async () => { diff --git a/src/app/serverViewState.ts b/src/app/serverViewState.ts index f0b52f9b..658f086f 100644 --- a/src/app/serverViewState.ts +++ b/src/app/serverViewState.ts @@ -238,11 +238,11 @@ export class ServerViewState { let httpUrl = url; if (!isValidURL(url)) { - // If it already includes the protocol, tell them it's invalid - if (isValidURI(url)) { + // If it already includes the protocol, force it to HTTPS + if (isValidURI(url) && !url.toLowerCase().startsWith('http')) { httpUrl = url.replace(/^((.+):\/\/)?/, 'https://'); - } else { - // Otherwise add HTTPS for them + } else if (!'https://'.startsWith(url.toLowerCase()) && !'http://'.startsWith(url.toLowerCase())) { + // Check if they're starting to type `http(s)`, otherwise add HTTPS for them httpUrl = `https://${url}`; } } @@ -279,8 +279,9 @@ export class ServerViewState { // If we can't get the remote info, warn the user that this might not be the right URL // If the original URL was invalid, don't replace that as they probably have a typo somewhere + // Also strip the trailing slash if it's there so that the user can keep typing if (!remoteInfo) { - return {status: URLValidationStatus.NotMattermost, validatedURL: parsedURL.toString()}; + return {status: URLValidationStatus.NotMattermost, validatedURL: parsedURL.toString().replace(/\/$/, '')}; } const remoteServerName = remoteInfo.siteName === 'Mattermost' ? remoteURL.host.split('.')[0] : remoteInfo.siteName;