Skip to content

Commit

Permalink
short url: ensure absolute path isn't persisted
Browse files Browse the repository at this point in the history
  • Loading branch information
jbudz committed Mar 21, 2016
1 parent 7061021 commit a8c1305
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 13 deletions.
87 changes: 87 additions & 0 deletions src/ui/public/share/__tests__/url_shortener.js
Original file line number Diff line number Diff line change
@@ -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();
});
});
});
27 changes: 14 additions & 13 deletions src/ui/public/share/lib/url_shortener.js
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down

0 comments on commit a8c1305

Please sign in to comment.