From a8c1305a9b9a50d44a8353f4878788389d3b2044 Mon Sep 17 00:00:00 2001 From: Jonathan Budzenski Date: Fri, 18 Mar 2016 15:02:53 -0500 Subject: [PATCH] short url: ensure absolute path isn't persisted --- .../public/share/__tests__/url_shortener.js | 87 +++++++++++++++++++ src/ui/public/share/lib/url_shortener.js | 27 +++--- 2 files changed, 101 insertions(+), 13 deletions(-) create mode 100644 src/ui/public/share/__tests__/url_shortener.js diff --git a/src/ui/public/share/__tests__/url_shortener.js b/src/ui/public/share/__tests__/url_shortener.js new file mode 100644 index 00000000000000..55ca786575382d --- /dev/null +++ b/src/ui/public/share/__tests__/url_shortener.js @@ -0,0 +1,87 @@ +import _ from 'lodash'; +import sinon from 'sinon'; +import expect from 'expect.js'; +import ngMock from 'ng_mock'; +import chrome from 'ui/chrome'; +import LibUrlShortenerProvider from 'ui/share/lib/url_shortener'; + +describe('Url shortener', function () { + let $rootScope; + let $location; + let $http; + let urlShortener; + let $httpBackend; + const shareId = 'id123'; + + beforeEach(ngMock.module('kibana')); + beforeEach(ngMock.inject(function (_$rootScope_, _$location_, _$httpBackend_, Private) { + $location = _$location_; + $rootScope = _$rootScope_; + $httpBackend = _$httpBackend_; + urlShortener = Private(LibUrlShortenerProvider); + })); + + describe('Shorten without base path', function () { + it('should shorten urls with a port', function (done) { + $httpBackend.when('POST', '/shorten').respond(function (type, route, data) { + expect(JSON.parse(data).url).to.be('/app/kibana#123'); + return [200, shareId]; + }); + urlShortener.shortenUrl('http://localhost:5601/app/kibana#123').then(function (url) { + expect(url).to.be('http://localhost:5601/goto/id123'); + done(); + }); + $httpBackend.flush(); + }); + + it('should shorten urls without a port', function (done) { + $httpBackend.when('POST', '/shorten').respond(function (type, route, data) { + expect(JSON.parse(data).url).to.be('/app/kibana#123'); + return [200, shareId]; + }); + urlShortener.shortenUrl('http://localhost/app/kibana#123').then(function (url) { + expect(url).to.be('http://localhost/goto/id123'); + done(); + }); + $httpBackend.flush(); + }); + }); + + describe('Shorten with base path', function () { + const basePath = '/foo'; + + let getBasePath; + beforeEach(ngMock.inject(function (Private) { + getBasePath = sinon.stub(chrome, 'getBasePath', () => basePath); + urlShortener = Private(LibUrlShortenerProvider); + })); + + it('should shorten urls with a port', function (done) { + $httpBackend.when('POST', `${basePath}/shorten`).respond(function (type, route, data) { + expect(JSON.parse(data).url).to.be('/app/kibana#123'); + return [200, shareId]; + }); + urlShortener.shortenUrl(`http://localhost:5601${basePath}/app/kibana#123`).then(function (url) { + expect(url).to.be(`http://localhost:5601${basePath}/goto/id123`); + done(); + }); + $httpBackend.flush(); + }); + + it('should shorten urls without a port', function (done) { + $httpBackend.when('POST', `${basePath}/shorten`).respond(function (type, route, data) { + expect(JSON.parse(data).url).to.be('/app/kibana#123'); + return [200, shareId]; + }); + urlShortener.shortenUrl(`http://localhost${basePath}/app/kibana#123`).then(function (url) { + expect(url).to.be(`http://localhost${basePath}/goto/id123`); + done(); + }); + $httpBackend.flush(); + }); + + afterEach(function () { + getBasePath.restore(); + }); + }); +}); diff --git a/src/ui/public/share/lib/url_shortener.js b/src/ui/public/share/lib/url_shortener.js index d20afb2a9af9e6..60e6dd9902c3ee 100644 --- a/src/ui/public/share/lib/url_shortener.js +++ b/src/ui/public/share/lib/url_shortener.js @@ -1,24 +1,25 @@ import chrome from 'ui/chrome'; - +import url from 'url'; export default function createUrlShortener(Notifier, $http, $location) { const notify = new Notifier({ location: 'Url Shortener' }); - const basePath = chrome.getBasePath(); - const baseUrl = `${$location.protocol()}://${$location.host()}:${$location.port()}${basePath}`; - async function shortenUrl(url) { - const relativeUrl = url.replace(baseUrl, ''); - const formData = { url: relativeUrl }; + function shortenUrl(absoluteUrl) { + const basePath = chrome.getBasePath(); - try { - const result = await $http.post(`${basePath}/shorten`, formData); + const parsedUrl = url.parse(absoluteUrl); + const path = parsedUrl.path.replace(basePath, ''); + const hash = parsedUrl.hash ? parsedUrl.hash : ''; + const relativeUrl = path + hash; + + const formData = { url: relativeUrl }; - return `${baseUrl}/goto/${result.data}`; - } catch (err) { - notify.error(err); - throw err; - } + return $http.post(`${basePath}/shorten`, formData).then((result) => { + return `${parsedUrl.protocol}//${parsedUrl.host}${basePath}/goto/${result.data}`; + }).catch((response) => { + notify.error(response); + }); } return {