uri-handler-registry-spec.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /** @babel */
  2. import url from 'url';
  3. import URIHandlerRegistry from '../src/uri-handler-registry';
  4. describe('URIHandlerRegistry', () => {
  5. let registry;
  6. beforeEach(() => {
  7. registry = new URIHandlerRegistry(5);
  8. });
  9. it('handles URIs on a per-host basis', async () => {
  10. const testPackageSpy = jasmine.createSpy();
  11. const otherPackageSpy = jasmine.createSpy();
  12. registry.registerHostHandler('test-package', testPackageSpy);
  13. registry.registerHostHandler('other-package', otherPackageSpy);
  14. await registry.handleURI('atom://yet-another-package/path');
  15. expect(testPackageSpy).not.toHaveBeenCalled();
  16. expect(otherPackageSpy).not.toHaveBeenCalled();
  17. await registry.handleURI('atom://test-package/path');
  18. expect(testPackageSpy).toHaveBeenCalledWith(
  19. url.parse('atom://test-package/path', true),
  20. 'atom://test-package/path'
  21. );
  22. expect(otherPackageSpy).not.toHaveBeenCalled();
  23. await registry.handleURI('atom://other-package/path');
  24. expect(otherPackageSpy).toHaveBeenCalledWith(
  25. url.parse('atom://other-package/path', true),
  26. 'atom://other-package/path'
  27. );
  28. });
  29. it('keeps track of the most recent URIs', async () => {
  30. const spy1 = jasmine.createSpy();
  31. const spy2 = jasmine.createSpy();
  32. const changeSpy = jasmine.createSpy();
  33. registry.registerHostHandler('one', spy1);
  34. registry.registerHostHandler('two', spy2);
  35. registry.onHistoryChange(changeSpy);
  36. const uris = [
  37. 'atom://one/something?asdf=1',
  38. 'atom://fake/nothing',
  39. 'atom://two/other/stuff',
  40. 'atom://one/more/thing',
  41. 'atom://two/more/stuff'
  42. ];
  43. for (const u of uris) {
  44. await registry.handleURI(u);
  45. }
  46. expect(changeSpy.callCount).toBe(5);
  47. expect(registry.getRecentlyHandledURIs()).toEqual(
  48. uris
  49. .map((u, idx) => {
  50. return {
  51. id: idx + 1,
  52. uri: u,
  53. handled: !u.match(/fake/),
  54. host: url.parse(u).host
  55. };
  56. })
  57. .reverse()
  58. );
  59. await registry.handleURI('atom://another/url');
  60. expect(changeSpy.callCount).toBe(6);
  61. const history = registry.getRecentlyHandledURIs();
  62. expect(history.length).toBe(5);
  63. expect(history[0].uri).toBe('atom://another/url');
  64. expect(history[4].uri).toBe(uris[1]);
  65. });
  66. it('refuses to handle bad URLs', async () => {
  67. const invalidUris = [
  68. 'atom:package/path',
  69. 'atom:8080://package/path',
  70. 'user:pass@atom://package/path',
  71. 'smth://package/path'
  72. ];
  73. let numErrors = 0;
  74. for (const uri of invalidUris) {
  75. try {
  76. await registry.handleURI(uri);
  77. expect(uri).toBe('throwing an error');
  78. } catch (ex) {
  79. numErrors++;
  80. }
  81. }
  82. expect(numErrors).toBe(invalidUris.length);
  83. });
  84. });