url.spec.ts 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. // Copyright (c) Jupyter Development Team.
  2. // Distributed under the terms of the Modified BSD License.
  3. import { URLExt } from '@jupyterlab/coreutils';
  4. describe('@jupyterlab/coreutils', () => {
  5. describe('URLExt', () => {
  6. describe('.parse()', () => {
  7. it('should parse a url into a URLExt object', () => {
  8. const obj = URLExt.parse('http://www.example.com');
  9. expect(obj.href).toBe('http://www.example.com/');
  10. expect(obj.protocol).toBe('http:');
  11. expect(obj.host).toBe('www.example.com');
  12. expect(obj.hostname).toBe('www.example.com');
  13. expect(obj.pathname).toBe('/');
  14. });
  15. it('should handle query and hash', () => {
  16. const url = "http://example.com/path?that's#all, folks";
  17. const obj = URLExt.parse(url);
  18. // Chrome has a different href
  19. expect([
  20. 'http://example.com/path?that%27s#all,%20folks',
  21. 'http://example.com/path?that%27s#all, folks'
  22. ]).toContain(obj.href);
  23. expect(obj.protocol).toBe('http:');
  24. expect(obj.host).toBe('example.com');
  25. expect(obj.hostname).toBe('example.com');
  26. expect(obj.search).toBe('?that%27s');
  27. expect(obj.pathname).toBe('/path');
  28. // Chrome has a different hash
  29. expect(['#all,%20folks', '#all, folks']).toContain(obj.hash);
  30. });
  31. });
  32. describe('.join()', () => {
  33. it('should join a sequence of url components', () => {
  34. expect(URLExt.join('/foo/', 'bar/')).toBe('/foo/bar/');
  35. });
  36. });
  37. describe('.encodeParts()', () => {
  38. it('should encode and join a sequence of url components', () => {
  39. expect(URLExt.encodeParts('>/>')).toBe('%3E/%3E');
  40. });
  41. });
  42. describe('.normalize()', () => {
  43. it('should handle leading slash', () => {
  44. expect(URLExt.normalize('/')).toBe(location.origin + '/');
  45. });
  46. it('should handle leading double slash', () => {
  47. expect(URLExt.normalize('//foo')).toBe(location.protocol + '//foo/');
  48. });
  49. it('should handle http', () => {
  50. expect(URLExt.normalize('http://foo')).toBe('http://foo/');
  51. });
  52. it('should handle other', () => {
  53. expect(URLExt.normalize('ftp://foo')).toBe('ftp://foo/');
  54. });
  55. });
  56. describe('objectToQueryString()', () => {
  57. it('should return a serialized object string suitable for a query', () => {
  58. const obj = {
  59. name: 'foo',
  60. id: 'baz'
  61. };
  62. expect(URLExt.objectToQueryString(obj)).toBe('?name=foo&id=baz');
  63. });
  64. });
  65. describe('.isLocal()', () => {
  66. it('should test whether the url is a local url', () => {
  67. expect(URLExt.isLocal('https://foo/bar.txt')).toBe(false);
  68. expect(URLExt.isLocal('http://foo/bar.txt')).toBe(false);
  69. expect(URLExt.isLocal('//foo/bar.txt')).toBe(false);
  70. expect(URLExt.isLocal('file://foo/bar.txt')).toBe(false);
  71. expect(URLExt.isLocal('data:text/plain,123ABC')).toBe(false);
  72. expect(URLExt.isLocal('/foo/bar.txt')).toBe(false);
  73. expect(URLExt.isLocal('httpserver/index.html')).toBe(true);
  74. expect(URLExt.isLocal('../foo/bar.txt')).toBe(true);
  75. expect(URLExt.isLocal('./foo/bar.txt')).toBe(true);
  76. expect(URLExt.isLocal('foo/bar.txt')).toBe(true);
  77. expect(URLExt.isLocal('bar.txt')).toBe(true);
  78. });
  79. });
  80. });
  81. });