diff options
author | Paul Slaughter <pslaughter@gitlab.com> | 2019-06-04 17:26:46 -0500 |
---|---|---|
committer | Paul Slaughter <pslaughter@gitlab.com> | 2019-06-07 09:12:11 -0500 |
commit | 908829089c2f739af47e58bac11c51bd423bb2d1 (patch) | |
tree | 443c4dce74f387f1a7a7e614c419230ae56474b1 | |
parent | f08abfcc3275c41e3e92e3504188359cc773730c (diff) | |
download | gitlab-ce-ce-5276-3-update-ide-diff-and-mirror-modules.tar.gz |
Update IDE file mirror service (ce utils)ce-5276-3-update-ide-diff-and-mirror-modules
**Notes:**
This module is a client to the webide-file-sync service.
https://gitlab.com/gitlab-org/webide-file-sync
-rw-r--r-- | app/assets/javascripts/lib/utils/url_utility.js | 12 | ||||
-rw-r--r-- | spec/frontend/lib/utils/url_utility_spec.js | 44 |
2 files changed, 49 insertions, 7 deletions
diff --git a/app/assets/javascripts/lib/utils/url_utility.js b/app/assets/javascripts/lib/utils/url_utility.js index b5474fc5c71..32fd0990374 100644 --- a/app/assets/javascripts/lib/utils/url_utility.js +++ b/app/assets/javascripts/lib/utils/url_utility.js @@ -1,3 +1,5 @@ +import { join as joinPaths } from 'path'; + // Returns an array containing the value(s) of the // of the key passed as an argument export function getParameterValues(sParam) { @@ -157,4 +159,12 @@ export function isSafeURL(url) { } } -export { join as joinPaths } from 'path'; +export function getWebSocketProtocol() { + return window.location.protocol.replace('http', 'ws'); +} + +export function getWebSocketUrl(path) { + return `${getWebSocketProtocol()}//${joinPaths(window.location.host, path)}`; +} + +export { joinPaths }; diff --git a/spec/frontend/lib/utils/url_utility_spec.js b/spec/frontend/lib/utils/url_utility_spec.js index eca240c9c18..c771984a137 100644 --- a/spec/frontend/lib/utils/url_utility_spec.js +++ b/spec/frontend/lib/utils/url_utility_spec.js @@ -1,5 +1,12 @@ import * as urlUtils from '~/lib/utils/url_utility'; +const setWindowLocation = value => { + Object.defineProperty(window, 'location', { + writable: true, + value, + }); +}; + describe('URL utility', () => { describe('webIDEUrl', () => { afterEach(() => { @@ -110,12 +117,9 @@ describe('URL utility', () => { describe('getBaseURL', () => { beforeEach(() => { - global.window = Object.create(window); - Object.defineProperty(window, 'location', { - value: { - host: 'gitlab.com', - protocol: 'https:', - }, + setWindowLocation({ + protocol: 'https:', + host: 'gitlab.com', }); }); @@ -191,4 +195,32 @@ describe('URL utility', () => { }); }); }); + + describe('getWebSocketProtocol', () => { + it.each` + protocol | expectation + ${'http:'} | ${'ws:'} + ${'https:'} | ${'wss:'} + `('returns "$expectation" with "$protocol" protocol', ({ protocol, expectation }) => { + setWindowLocation({ + protocol, + host: 'example.com', + }); + + expect(urlUtils.getWebSocketProtocol()).toEqual(expectation); + }); + }); + + describe('getWebSocketUrl', () => { + it('joins location host to path', () => { + setWindowLocation({ + protocol: 'http:', + host: 'example.com', + }); + + const path = '/lorem/ipsum?a=bc'; + + expect(urlUtils.getWebSocketUrl(path)).toEqual('ws://example.com/lorem/ipsum?a=bc'); + }); + }); }); |