| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176 |
- import { describe, test, it, expect } from 'vitest'
- import UrlUtils from '~/services/utils/urlUtils'
- describe('join', () => {
- test('simple cases', () => {
- expect(UrlUtils.join('https://mydomain.com', 'foo')).toEqual(
- 'https://mydomain.com/foo',
- )
- expect(UrlUtils.join('https://mydomain.com/', '/foo')).toEqual(
- 'https://mydomain.com/foo',
- )
- expect(UrlUtils.join('https://mydomain.com/', 'foo/')).toEqual(
- 'https://mydomain.com/foo/',
- )
- })
- test('more parts and various types', () => {
- expect(UrlUtils.join('https://hoy.com', 'foo', 'bar', 10)).toEqual(
- 'https://hoy.com/foo/bar/10',
- )
- })
- test('tail is empty', () => {
- expect(UrlUtils.join('https://hoy.com')).toEqual('https://hoy.com')
- })
- test('relative uri', () => {
- expect(UrlUtils.join('foo', 'bar')).toEqual('foo/bar')
- expect(UrlUtils.join('/foo/', '/bar/')).toEqual('/foo/bar/')
- })
- test('empty base', () => {
- expect(UrlUtils.join('', 'foo')).toEqual('/foo')
- expect(UrlUtils.join('')).toEqual('')
- })
- })
- describe('prependHttps', () => {
- test('uri with no prefix', () => {
- expect(UrlUtils.prependHttps('opentalent.fr')).toEqual(
- 'https://opentalent.fr',
- )
- })
- test('uri with http prefix', () => {
- expect(UrlUtils.prependHttps('http://opentalent.fr')).toEqual(
- 'http://opentalent.fr',
- )
- })
- test('uri with https prefix', () => {
- expect(UrlUtils.prependHttps('https://opentalent.fr')).toEqual(
- 'https://opentalent.fr',
- )
- })
- test('uri with empty string', () => {
- expect(UrlUtils.prependHttps('')).toEqual('https://')
- })
- })
- describe('getParameter', () => {
- test('get param from a simple url', () => {
- expect(
- UrlUtils.getParameter('https://domain.fr/hello?foo=abc', 'foo'),
- ).toEqual('abc')
- })
- test('get a numeric param (no default value)', () => {
- expect(
- UrlUtils.getParameter('https://domain.fr/hello?foo=123', 'foo'),
- ).toEqual(123)
- })
- test('get a numeric param (default value is also numeric)', () => {
- expect(
- UrlUtils.getParameter('https://domain.fr/hello?foo=123', 'foo', 1),
- ).toEqual(123)
- })
- test('get a numeric param (default value is not numeric)', () => {
- expect(
- UrlUtils.getParameter('https://domain.fr/hello?foo=123', 'foo', 'hello'),
- ).toEqual('123')
- })
- test('try to get non-existing param (no default value)', () => {
- expect(
- UrlUtils.getParameter('https://domain.fr/hello?foo=abc', 'bar'),
- ).toEqual(null)
- })
- test('try to get non-existing param, providing a default value', () => {
- expect(
- UrlUtils.getParameter('https://domain.fr/hello?foo=abc', 'bar', 'def'),
- ).toEqual('def')
- })
- test('get param from a relative url', () => {
- expect(UrlUtils.getParameter('/hello?foo=123', 'foo')).toEqual(123)
- })
- })
- describe('extractIdFromUri', () => {
- test('extract id from uri', () => {
- expect(UrlUtils.extractIdFromUri('/api/organizations/1000')).toEqual(1000)
- })
- test('try to literal id', () => {
- expect(UrlUtils.extractIdFromUri('/api/organizations/abc', true)).toEqual(
- 'abc',
- )
- })
- test('try to extract missing id', () => {
- expect(() =>
- UrlUtils.extractIdFromUri('/api/organizations/first'),
- ).toThrowError('no id found')
- })
- test('try to extract id from empty string', () => {
- expect(() => UrlUtils.extractIdFromUri('')).toThrowError('no id found')
- })
- })
- describe('split', () => {
- test('split relative url', () => {
- expect(UrlUtils.split('foo/bar/1')).toEqual(['foo', 'bar', '1'])
- })
- test('split relative url with prepended slash', () => {
- expect(UrlUtils.split('/foo/bar/1')).toEqual(['foo', 'bar', '1'])
- })
- test('split complete url', () => {
- expect(UrlUtils.split('https://test.com/api/access/123')).toEqual([
- 'https:',
- 'test.com',
- 'api',
- 'access',
- '123',
- ])
- })
- test('split empty string', () => {
- expect(UrlUtils.split('')).toEqual([])
- })
- })
- describe('addQuery', () => {
- test('with empty query', () => {
- expect(UrlUtils.addQuery('foo/bar', {})).toEqual('/foo/bar')
- })
- test('with simple query', () => {
- expect(UrlUtils.addQuery('foo/bar', { a: 1 })).toEqual('/foo/bar?a=1')
- })
- test('with longer query', () => {
- expect(UrlUtils.addQuery('foo/bar', { a: 1, b: 2 })).toEqual(
- '/foo/bar?a=1&b=2',
- )
- })
- test('with existing query', () => {
- expect(UrlUtils.addQuery('foo/bar?a=1', { b: 2 })).toEqual(
- '/foo/bar?a=1&b=2',
- )
- })
- test('with empty url', () => {
- expect(UrlUtils.addQuery('', { a: 1 })).toEqual('/?a=1')
- })
- test('with empty url and empty query', () => {
- expect(UrlUtils.addQuery('', {})).toEqual('/')
- })
- test('with absolute url', () => {
- expect(UrlUtils.addQuery('https://foo.com/bar/', { a: 1 })).toEqual(
- 'https://foo.com/bar/?a=1',
- )
- })
- test('with number sign in path name', () => {
- expect(UrlUtils.addQuery('https://foo.com/#/bar/', { a: 1 })).toEqual(
- 'https://foo.com/#/bar/?a=1',
- )
- })
- })
- describe('makeIRI', () => {
- test('valid parameters', () => {
- expect(UrlUtils.makeIRI('someEntity', 123)).toEqual('/api/someEntity/123')
- })
- test('invalid id', () => {
- // @ts-ignore
- expect(() => UrlUtils.makeIRI('someEntity', 'abc')).toThrowError(
- 'Invalid id : abc',
- )
- })
- })
|