|
19 | 19 |
|
20 | 20 | import { shortUrlAssertValid } from './short_url_assert_valid';
|
21 | 21 |
|
| 22 | +const PROTOCOL_ERROR = /^Short url targets cannot have a protocol/; |
| 23 | +const HOSTNAME_ERROR = /^Short url targets cannot have a hostname/; |
| 24 | +const PATH_ERROR = /^Short url target path must be in the format/; |
| 25 | + |
22 | 26 | describe('shortUrlAssertValid()', () => {
|
23 | 27 | const invalid = [
|
24 |
| - ['protocol', 'http://localhost:5601/app/kibana'], |
25 |
| - ['protocol', 'https://localhost:5601/app/kibana'], |
26 |
| - ['protocol', 'mailto:foo@bar.net'], |
27 |
| - ['protocol', 'javascript:alert("hi")'], // eslint-disable-line no-script-url |
28 |
| - ['hostname', 'localhost/app/kibana'], |
29 |
| - ['hostname and port', 'local.host:5601/app/kibana'], |
30 |
| - ['hostname and auth', 'user:pass@localhost.net/app/kibana'], |
31 |
| - ['path traversal', '/app/../../not-kibana'], |
32 |
| - ['deep path', '/app/kibana/foo'], |
33 |
| - ['deep path', '/app/kibana/foo/bar'], |
34 |
| - ['base path', '/base/app/kibana'], |
| 28 | + ['protocol', 'http://localhost:5601/app/kibana', PROTOCOL_ERROR], |
| 29 | + ['protocol', 'https://localhost:5601/app/kibana', PROTOCOL_ERROR], |
| 30 | + ['protocol', 'mailto:foo@bar.net', PROTOCOL_ERROR], |
| 31 | + ['protocol', 'javascript:alert("hi")', PROTOCOL_ERROR], // eslint-disable-line no-script-url |
| 32 | + ['hostname', 'localhost/app/kibana', PATH_ERROR], // according to spec, this is not a valid URL -- you cannot specify a hostname without a protocol |
| 33 | + ['hostname and port', 'local.host:5601/app/kibana', PROTOCOL_ERROR], // parser detects 'local.host' as the protocol |
| 34 | + ['hostname and auth', 'user:pass@localhost.net/app/kibana', PROTOCOL_ERROR], // parser detects 'user' as the protocol |
| 35 | + ['path traversal', '/app/../../not-kibana', PATH_ERROR], // fails because there are >2 path parts |
| 36 | + ['path traversal', '/../not-kibana', PATH_ERROR], // fails because first path part is not 'app' |
| 37 | + ['deep path', '/app/kibana/foo', PATH_ERROR], // fails because there are >2 path parts |
| 38 | + ['deeper path', '/app/kibana/foo/bar', PATH_ERROR], // fails because there are >2 path parts |
| 39 | + ['base path', '/base/app/kibana', PATH_ERROR], // fails because there are >2 path parts |
| 40 | + ['path with an extra leading slash', '//foo/app/kibana', HOSTNAME_ERROR], // parser detects 'foo' as the hostname |
| 41 | + ['path with an extra leading slash', '///app/kibana', HOSTNAME_ERROR], // parser detects '' as the hostname |
| 42 | + ['path without app', '/foo/kibana', PATH_ERROR], // fails because first path part is not 'app' |
| 43 | + ['path without appId', '/app/', PATH_ERROR], // fails because there is only one path part (leading and trailing slashes are trimmed) |
35 | 44 | ];
|
36 | 45 |
|
37 |
| - invalid.forEach(([desc, url]) => { |
38 |
| - it(`fails when url has ${desc}`, () => { |
39 |
| - try { |
40 |
| - shortUrlAssertValid(url); |
41 |
| - throw new Error(`expected assertion to throw`); |
42 |
| - } catch (err) { |
43 |
| - if (!err || !err.isBoom) { |
44 |
| - throw err; |
45 |
| - } |
46 |
| - } |
| 46 | + invalid.forEach(([desc, url, error]) => { |
| 47 | + it(`fails when url has ${desc as string}`, () => { |
| 48 | + expect(() => shortUrlAssertValid(url as string)).toThrowError(error); |
47 | 49 | });
|
48 | 50 | });
|
49 | 51 |
|
50 | 52 | const valid = [
|
51 | 53 | '/app/kibana',
|
| 54 | + '/app/kibana/', // leading and trailing slashes are trimmed |
52 | 55 | '/app/monitoring#angular/route',
|
53 | 56 | '/app/text#document-id',
|
54 | 57 | '/app/some?with=query',
|
|
0 commit comments