short url: ensure absolute path isn't persisted
This commit is contained in:
parent
7061021822
commit
a8c1305a9b
87
src/ui/public/share/__tests__/url_shortener.js
Normal file
87
src/ui/public/share/__tests__/url_shortener.js
Normal file
|
@ -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();
|
||||
});
|
||||
});
|
||||
});
|
|
@ -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, '');
|
||||
function shortenUrl(absoluteUrl) {
|
||||
const basePath = chrome.getBasePath();
|
||||
|
||||
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 };
|
||||
|
||||
try {
|
||||
const result = await $http.post(`${basePath}/shorten`, formData);
|
||||
|
||||
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 {
|
||||
|
|
Loading…
Reference in a new issue