-
Notifications
You must be signed in to change notification settings - Fork 8.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
short url: ensure absolute path isn't persisted #6581
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
a8c1305
short url: ensure absolute path isn't persisted
jbudz 2a71673
[short url] use url.format when creating /goto link
jbudz 690a140
[short url] Add tests for query strings, no hashes
jbudz 49d297f
[short url] Cleanup
jbudz a612a0e
[short url] Add server tests
jbudz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,87 @@ | ||
import expect from 'expect.js'; | ||
import * as kbnTestServer from '../../../../test/utils/kbn_server'; | ||
import fromRoot from '../../../utils/from_root'; | ||
|
||
describe('routes', function () { | ||
this.slow(10000); | ||
this.timeout(60000); | ||
|
||
describe('cookie validation', function () { | ||
let kbnServer; | ||
beforeEach(function () { | ||
kbnServer = kbnTestServer.createServer(); | ||
kbnServer = kbnTestServer.createServer({ | ||
plugins: { | ||
scanDirs: [ | ||
fromRoot('src/plugins') | ||
] | ||
} | ||
}); | ||
return kbnServer.ready(); | ||
}); | ||
afterEach(function () { | ||
return kbnServer.close(); | ||
}); | ||
|
||
it('allows non-strict cookies', function (done) { | ||
const options = { | ||
method: 'GET', | ||
url: '/', | ||
headers: { | ||
cookie: 'test:80=value;test_80=value' | ||
} | ||
}; | ||
kbnTestServer.makeRequest(kbnServer, options, (res) => { | ||
expect(res.payload).not.to.contain('Invalid cookie header'); | ||
done(); | ||
describe('cookie validation', function () { | ||
it('allows non-strict cookies', function (done) { | ||
const options = { | ||
method: 'GET', | ||
url: '/', | ||
headers: { | ||
cookie: 'test:80=value;test_80=value' | ||
} | ||
}; | ||
kbnTestServer.makeRequest(kbnServer, options, (res) => { | ||
expect(res.payload).not.to.contain('Invalid cookie header'); | ||
done(); | ||
}); | ||
}); | ||
|
||
it('returns an error if the cookie can\'t be parsed', function (done) { | ||
const options = { | ||
method: 'GET', | ||
url: '/', | ||
headers: { | ||
cookie: 'a' | ||
} | ||
}; | ||
kbnTestServer.makeRequest(kbnServer, options, (res) => { | ||
expect(res.payload).to.contain('Invalid cookie header'); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
|
||
it('returns an error if the cookie can\'t be parsed', function (done) { | ||
const options = { | ||
method: 'GET', | ||
url: '/', | ||
headers: { | ||
cookie: 'a' | ||
describe('url shortener', () => { | ||
const shortenOptions = { | ||
method: 'POST', | ||
url: '/shorten', | ||
payload: { | ||
url: '/app/kibana#/visualize/create' | ||
} | ||
}; | ||
kbnTestServer.makeRequest(kbnServer, options, (res) => { | ||
expect(res.payload).to.contain('Invalid cookie header'); | ||
done(); | ||
|
||
it('generates shortened urls', (done) => { | ||
kbnTestServer.makeRequest(kbnServer, shortenOptions, (res) => { | ||
expect(typeof res.payload).to.be('string'); | ||
expect(res.payload.length > 0).to.be(true); | ||
done(); | ||
}); | ||
}); | ||
|
||
it('redirects shortened urls', (done) => { | ||
kbnTestServer.makeRequest(kbnServer, shortenOptions, (res) => { | ||
const gotoOptions = { | ||
method: 'GET', | ||
url: '/goto/' + res.payload | ||
}; | ||
kbnTestServer.makeRequest(kbnServer, gotoOptions, (res) => { | ||
expect(res.statusCode).to.be(302); | ||
expect(res.headers.location).to.be(shortenOptions.payload.url); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
|
||
}); | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
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', () => { | ||
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', () => { | ||
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/${shareId}`); | ||
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/${shareId}`); | ||
done(); | ||
}); | ||
$httpBackend.flush(); | ||
}); | ||
}); | ||
|
||
describe('Shorten with base path', () => { | ||
const basePath = '/foo'; | ||
|
||
let getBasePath; | ||
beforeEach(ngMock.inject((Private) => { | ||
getBasePath = sinon.stub(chrome, 'getBasePath', () => basePath); | ||
urlShortener = Private(LibUrlShortenerProvider); | ||
})); | ||
|
||
it('should shorten urls with a port', (done) => { | ||
$httpBackend.when('POST', `${basePath}/shorten`).respond((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((url) => { | ||
expect(url).to.be(`http://localhost:5601${basePath}/goto/${shareId}`); | ||
done(); | ||
}); | ||
$httpBackend.flush(); | ||
}); | ||
|
||
it('should shorten urls without a port', (done) => { | ||
$httpBackend.when('POST', `${basePath}/shorten`).respond((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((url) => { | ||
expect(url).to.be(`http://localhost${basePath}/goto/${shareId}`); | ||
done(); | ||
}); | ||
$httpBackend.flush(); | ||
}); | ||
|
||
it('should shorten urls with a query string', (done) => { | ||
$httpBackend.when('POST', `${basePath}/shorten`).respond((type, route, data) => { | ||
expect(JSON.parse(data).url).to.be('/app/kibana?foo#123'); | ||
return [200, shareId]; | ||
}); | ||
urlShortener.shortenUrl(`http://localhost${basePath}/app/kibana?foo#123`).then((url) => { | ||
expect(url).to.be(`http://localhost${basePath}/goto/${shareId}`); | ||
done(); | ||
}); | ||
$httpBackend.flush(); | ||
}); | ||
|
||
it('should shorten urls without a hash', (done) => { | ||
$httpBackend.when('POST', `${basePath}/shorten`).respond((type, route, data) => { | ||
expect(JSON.parse(data).url).to.be('/app/kibana'); | ||
return [200, shareId]; | ||
}); | ||
urlShortener.shortenUrl(`http://localhost${basePath}/app/kibana`).then((url) => { | ||
expect(url).to.be(`http://localhost${basePath}/goto/${shareId}`); | ||
done(); | ||
}); | ||
$httpBackend.flush(); | ||
}); | ||
|
||
it('should shorten urls with a query string in the hash', (done) => { | ||
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 | ||
$httpBackend.when('POST', `${basePath}/shorten`).respond((type, route, data) => { | ||
expect(JSON.parse(data).url).to.be(relativeUrl); | ||
return [200, shareId]; | ||
}); | ||
urlShortener.shortenUrl(`http://localhost${basePath}${relativeUrl}`).then((url) => { | ||
expect(url).to.be(`http://localhost${basePath}/goto/${shareId}`); | ||
done(); | ||
}); | ||
$httpBackend.flush(); | ||
}); | ||
|
||
afterEach(() => { | ||
getBasePath.restore(); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
would like to see a test with urls that have basepaths, query strings, no hash, etc.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍