|
| 1 | +import _ from 'lodash'; |
| 2 | +import sinon from 'sinon'; |
| 3 | +import expect from 'expect.js'; |
| 4 | +import ngMock from 'ngMock'; |
| 5 | +import chrome from 'ui/chrome'; |
| 6 | +import LibUrlShortenerProvider from 'ui/share/lib/url_shortener'; |
| 7 | + |
| 8 | +describe('Url shortener', () => { |
| 9 | + let $rootScope; |
| 10 | + let $location; |
| 11 | + let $http; |
| 12 | + let urlShortener; |
| 13 | + let $httpBackend; |
| 14 | + const shareId = 'id123'; |
| 15 | + |
| 16 | + beforeEach(ngMock.module('kibana')); |
| 17 | + beforeEach(ngMock.inject(function (_$rootScope_, _$location_, _$httpBackend_, Private) { |
| 18 | + $location = _$location_; |
| 19 | + $rootScope = _$rootScope_; |
| 20 | + $httpBackend = _$httpBackend_; |
| 21 | + urlShortener = Private(LibUrlShortenerProvider); |
| 22 | + })); |
| 23 | + |
| 24 | + describe('Shorten without base path', () => { |
| 25 | + it('should shorten urls with a port', function (done) { |
| 26 | + $httpBackend.when('POST', '/shorten').respond(function (type, route, data) { |
| 27 | + expect(JSON.parse(data).url).to.be('/app/kibana#123'); |
| 28 | + return [200, shareId]; |
| 29 | + }); |
| 30 | + urlShortener.shortenUrl('http://localhost:5601/app/kibana#123').then(function (url) { |
| 31 | + expect(url).to.be(`http://localhost:5601/goto/${shareId}`); |
| 32 | + done(); |
| 33 | + }); |
| 34 | + $httpBackend.flush(); |
| 35 | + }); |
| 36 | + |
| 37 | + it('should shorten urls without a port', function (done) { |
| 38 | + $httpBackend.when('POST', '/shorten').respond(function (type, route, data) { |
| 39 | + expect(JSON.parse(data).url).to.be('/app/kibana#123'); |
| 40 | + return [200, shareId]; |
| 41 | + }); |
| 42 | + urlShortener.shortenUrl('http://localhost/app/kibana#123').then(function (url) { |
| 43 | + expect(url).to.be(`http://localhost/goto/${shareId}`); |
| 44 | + done(); |
| 45 | + }); |
| 46 | + $httpBackend.flush(); |
| 47 | + }); |
| 48 | + }); |
| 49 | + |
| 50 | + describe('Shorten with base path', () => { |
| 51 | + const basePath = '/foo'; |
| 52 | + |
| 53 | + let getBasePath; |
| 54 | + beforeEach(ngMock.inject((Private) => { |
| 55 | + getBasePath = sinon.stub(chrome, 'getBasePath', () => basePath); |
| 56 | + urlShortener = Private(LibUrlShortenerProvider); |
| 57 | + })); |
| 58 | + |
| 59 | + it('should shorten urls with a port', (done) => { |
| 60 | + $httpBackend.when('POST', `${basePath}/shorten`).respond((type, route, data) => { |
| 61 | + expect(JSON.parse(data).url).to.be('/app/kibana#123'); |
| 62 | + return [200, shareId]; |
| 63 | + }); |
| 64 | + urlShortener.shortenUrl(`http://localhost:5601${basePath}/app/kibana#123`).then((url) => { |
| 65 | + expect(url).to.be(`http://localhost:5601${basePath}/goto/${shareId}`); |
| 66 | + done(); |
| 67 | + }); |
| 68 | + $httpBackend.flush(); |
| 69 | + }); |
| 70 | + |
| 71 | + it('should shorten urls without a port', (done) => { |
| 72 | + $httpBackend.when('POST', `${basePath}/shorten`).respond((type, route, data) => { |
| 73 | + expect(JSON.parse(data).url).to.be('/app/kibana#123'); |
| 74 | + return [200, shareId]; |
| 75 | + }); |
| 76 | + urlShortener.shortenUrl(`http://localhost${basePath}/app/kibana#123`).then((url) => { |
| 77 | + expect(url).to.be(`http://localhost${basePath}/goto/${shareId}`); |
| 78 | + done(); |
| 79 | + }); |
| 80 | + $httpBackend.flush(); |
| 81 | + }); |
| 82 | + |
| 83 | + it('should shorten urls with a query string', (done) => { |
| 84 | + $httpBackend.when('POST', `${basePath}/shorten`).respond((type, route, data) => { |
| 85 | + expect(JSON.parse(data).url).to.be('/app/kibana?foo#123'); |
| 86 | + return [200, shareId]; |
| 87 | + }); |
| 88 | + urlShortener.shortenUrl(`http://localhost${basePath}/app/kibana?foo#123`).then((url) => { |
| 89 | + expect(url).to.be(`http://localhost${basePath}/goto/${shareId}`); |
| 90 | + done(); |
| 91 | + }); |
| 92 | + $httpBackend.flush(); |
| 93 | + }); |
| 94 | + |
| 95 | + it('should shorten urls without a hash', (done) => { |
| 96 | + $httpBackend.when('POST', `${basePath}/shorten`).respond((type, route, data) => { |
| 97 | + expect(JSON.parse(data).url).to.be('/app/kibana'); |
| 98 | + return [200, shareId]; |
| 99 | + }); |
| 100 | + urlShortener.shortenUrl(`http://localhost${basePath}/app/kibana`).then((url) => { |
| 101 | + expect(url).to.be(`http://localhost${basePath}/goto/${shareId}`); |
| 102 | + done(); |
| 103 | + }); |
| 104 | + $httpBackend.flush(); |
| 105 | + }); |
| 106 | + |
| 107 | + it('should shorten urls with a query string in the hash', (done) => { |
| 108 | + const relativeUrl = "/app/kibana#/discover?_g=(refreshInterval:(display:Off,pause:!f,value:0),time:(from:now-15m,mode:quick,to:now))&_a=(columns:!(_source),index:%27logstash-*%27,interval:auto,query:(query_string:(analyze_wildcard:!t,query:%27*%27)),sort:!(%27@timestamp%27,desc))"; //eslint-disable-line max-len, quotes |
| 109 | + $httpBackend.when('POST', `${basePath}/shorten`).respond((type, route, data) => { |
| 110 | + expect(JSON.parse(data).url).to.be(relativeUrl); |
| 111 | + return [200, shareId]; |
| 112 | + }); |
| 113 | + urlShortener.shortenUrl(`http://localhost${basePath}${relativeUrl}`).then((url) => { |
| 114 | + expect(url).to.be(`http://localhost${basePath}/goto/${shareId}`); |
| 115 | + done(); |
| 116 | + }); |
| 117 | + $httpBackend.flush(); |
| 118 | + }); |
| 119 | + |
| 120 | + afterEach(() => { |
| 121 | + getBasePath.restore(); |
| 122 | + }); |
| 123 | + }); |
| 124 | +}); |
0 commit comments