urlUtils.test.ts 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. import { describe, test, it, expect } from 'vitest'
  2. import UrlUtils from '~/services/utils/urlUtils'
  3. describe('join', () => {
  4. test('simple cases', () => {
  5. expect(UrlUtils.join('https://mydomain.com', 'foo')).toEqual(
  6. 'https://mydomain.com/foo',
  7. )
  8. expect(UrlUtils.join('https://mydomain.com/', '/foo')).toEqual(
  9. 'https://mydomain.com/foo',
  10. )
  11. expect(UrlUtils.join('https://mydomain.com/', 'foo/')).toEqual(
  12. 'https://mydomain.com/foo/',
  13. )
  14. })
  15. test('more parts and various types', () => {
  16. expect(UrlUtils.join('https://hoy.com', 'foo', 'bar', 10)).toEqual(
  17. 'https://hoy.com/foo/bar/10',
  18. )
  19. })
  20. test('tail is empty', () => {
  21. expect(UrlUtils.join('https://hoy.com')).toEqual('https://hoy.com')
  22. })
  23. test('relative uri', () => {
  24. expect(UrlUtils.join('foo', 'bar')).toEqual('foo/bar')
  25. expect(UrlUtils.join('/foo/', '/bar/')).toEqual('/foo/bar/')
  26. })
  27. test('empty base', () => {
  28. expect(UrlUtils.join('', 'foo')).toEqual('/foo')
  29. expect(UrlUtils.join('')).toEqual('')
  30. })
  31. })
  32. describe('prependHttps', () => {
  33. test('uri with no prefix', () => {
  34. expect(UrlUtils.prependHttps('opentalent.fr')).toEqual(
  35. 'https://opentalent.fr',
  36. )
  37. })
  38. test('uri with http prefix', () => {
  39. expect(UrlUtils.prependHttps('http://opentalent.fr')).toEqual(
  40. 'http://opentalent.fr',
  41. )
  42. })
  43. test('uri with https prefix', () => {
  44. expect(UrlUtils.prependHttps('https://opentalent.fr')).toEqual(
  45. 'https://opentalent.fr',
  46. )
  47. })
  48. test('uri with empty string', () => {
  49. expect(UrlUtils.prependHttps('')).toEqual('https://')
  50. })
  51. })
  52. describe('getParameter', () => {
  53. test('get param from a simple url', () => {
  54. expect(
  55. UrlUtils.getParameter('https://domain.fr/hello?foo=abc', 'foo'),
  56. ).toEqual('abc')
  57. })
  58. test('get a numeric param (no default value)', () => {
  59. expect(
  60. UrlUtils.getParameter('https://domain.fr/hello?foo=123', 'foo'),
  61. ).toEqual(123)
  62. })
  63. test('get a numeric param (default value is also numeric)', () => {
  64. expect(
  65. UrlUtils.getParameter('https://domain.fr/hello?foo=123', 'foo', 1),
  66. ).toEqual(123)
  67. })
  68. test('get a numeric param (default value is not numeric)', () => {
  69. expect(
  70. UrlUtils.getParameter('https://domain.fr/hello?foo=123', 'foo', 'hello'),
  71. ).toEqual('123')
  72. })
  73. test('try to get non-existing param (no default value)', () => {
  74. expect(
  75. UrlUtils.getParameter('https://domain.fr/hello?foo=abc', 'bar'),
  76. ).toEqual(null)
  77. })
  78. test('try to get non-existing param, providing a default value', () => {
  79. expect(
  80. UrlUtils.getParameter('https://domain.fr/hello?foo=abc', 'bar', 'def'),
  81. ).toEqual('def')
  82. })
  83. test('get param from a relative url', () => {
  84. expect(UrlUtils.getParameter('/hello?foo=123', 'foo')).toEqual(123)
  85. })
  86. })
  87. describe('extractIdFromUri', () => {
  88. test('extract id from uri', () => {
  89. expect(UrlUtils.extractIdFromUri('/api/organizations/1000')).toEqual(1000)
  90. })
  91. test('try to literal id', () => {
  92. expect(UrlUtils.extractIdFromUri('/api/organizations/abc', true)).toEqual(
  93. 'abc',
  94. )
  95. })
  96. test('try to extract missing id', () => {
  97. expect(() =>
  98. UrlUtils.extractIdFromUri('/api/organizations/first'),
  99. ).toThrowError('no id found')
  100. })
  101. test('try to extract id from empty string', () => {
  102. expect(() => UrlUtils.extractIdFromUri('')).toThrowError('no id found')
  103. })
  104. })
  105. describe('split', () => {
  106. test('split relative url', () => {
  107. expect(UrlUtils.split('foo/bar/1')).toEqual(['foo', 'bar', '1'])
  108. })
  109. test('split relative url with prepended slash', () => {
  110. expect(UrlUtils.split('/foo/bar/1')).toEqual(['foo', 'bar', '1'])
  111. })
  112. test('split complete url', () => {
  113. expect(UrlUtils.split('https://test.com/api/access/123')).toEqual([
  114. 'https:',
  115. 'test.com',
  116. 'api',
  117. 'access',
  118. '123',
  119. ])
  120. })
  121. test('split empty string', () => {
  122. expect(UrlUtils.split('')).toEqual([])
  123. })
  124. })
  125. describe('addQuery', () => {
  126. test('with empty query', () => {
  127. expect(UrlUtils.addQuery('foo/bar', {})).toEqual('/foo/bar')
  128. })
  129. test('with simple query', () => {
  130. expect(UrlUtils.addQuery('foo/bar', { a: 1 })).toEqual('/foo/bar?a=1')
  131. })
  132. test('with longer query', () => {
  133. expect(UrlUtils.addQuery('foo/bar', { a: 1, b: 2 })).toEqual(
  134. '/foo/bar?a=1&b=2',
  135. )
  136. })
  137. test('with existing query', () => {
  138. expect(UrlUtils.addQuery('foo/bar?a=1', { b: 2 })).toEqual(
  139. '/foo/bar?a=1&b=2',
  140. )
  141. })
  142. test('with empty url', () => {
  143. expect(UrlUtils.addQuery('', { a: 1 })).toEqual('/?a=1')
  144. })
  145. test('with empty url and empty query', () => {
  146. expect(UrlUtils.addQuery('', {})).toEqual('/')
  147. })
  148. test('with absolute url', () => {
  149. expect(UrlUtils.addQuery('https://foo.com/bar/', { a: 1 })).toEqual(
  150. 'https://foo.com/bar/?a=1',
  151. )
  152. })
  153. test('with number sign in path name', () => {
  154. expect(UrlUtils.addQuery('https://foo.com/#/bar/', { a: 1 })).toEqual(
  155. 'https://foo.com/#/bar/?a=1',
  156. )
  157. })
  158. })
  159. describe('makeIRI', () => {
  160. test('valid parameters', () => {
  161. expect(UrlUtils.makeIRI('someEntity', 123)).toEqual('/api/someEntity/123')
  162. })
  163. test('invalid id', () => {
  164. // @ts-ignore
  165. expect(() => UrlUtils.makeIRI('someEntity', 'abc')).toThrowError(
  166. 'Invalid id : abc',
  167. )
  168. })
  169. })