parse-common-connection-url-options.test.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. import { parseCommonConnectionUrlOptions } from '@sequelize/core/_non-semver-use-at-your-own-risk_/utils/connection-options.js';
  2. import { expect } from 'chai';
  3. describe('parseCommonConnectionUrlOptions', () => {
  4. it('maps url parts to specified options', () => {
  5. const options = parseCommonConnectionUrlOptions<any>({
  6. stringSearchParams: ['search1', 'search2'],
  7. booleanSearchParams: ['search3'],
  8. numberSearchParams: ['search4'],
  9. allowedProtocols: ['mariadb'],
  10. hostname: 'theHost',
  11. password: 'thePassword',
  12. pathname: 'theDb',
  13. port: 'thePort',
  14. url: 'mariadb://a:b@c:1234/d?search1=1&search2=2&search3=true&search4=4',
  15. username: 'theUser',
  16. });
  17. expect(options).to.deep.eq({
  18. theDb: 'd',
  19. theHost: 'c',
  20. thePassword: 'b',
  21. thePort: 1234,
  22. theUser: 'a',
  23. search1: '1',
  24. search2: '2',
  25. search3: true,
  26. search4: 4,
  27. });
  28. });
  29. it('throws when specifying an unknown search param', () => {
  30. expect(() =>
  31. parseCommonConnectionUrlOptions<any>({
  32. url: 'mariadb://localhost?connectTimeout=1000',
  33. allowedProtocols: ['mariadb'],
  34. hostname: 'host',
  35. port: 'port',
  36. pathname: 'database',
  37. }),
  38. ).to.throw('Option "connectTimeout" cannot be set as a connection URL search parameter.');
  39. });
  40. it('throws when specifying an invalid value for boolean search param', () => {
  41. expect(() =>
  42. parseCommonConnectionUrlOptions<any>({
  43. booleanSearchParams: ['search'],
  44. url: 'mariadb://localhost?search=invalid',
  45. allowedProtocols: ['mariadb'],
  46. hostname: 'host',
  47. port: 'port',
  48. pathname: 'database',
  49. }),
  50. ).to.throwWithCause(
  51. 'Cannot convert "invalid" to a boolean. It must be either "true" or "false".',
  52. );
  53. });
  54. it('throws when specifying an invalid value for number search param', () => {
  55. expect(() =>
  56. parseCommonConnectionUrlOptions<any>({
  57. numberSearchParams: ['search'],
  58. url: 'mariadb://localhost?search=invalid',
  59. allowedProtocols: ['mariadb'],
  60. hostname: 'host',
  61. port: 'port',
  62. pathname: 'database',
  63. }),
  64. ).to.throwWithCause('Cannot convert "invalid" to a finite number.');
  65. });
  66. it('throws an error if the protocol is not supported', () => {
  67. expect(() =>
  68. parseCommonConnectionUrlOptions<any>({
  69. url: 'mysql://localhost',
  70. allowedProtocols: ['mariadb'],
  71. hostname: 'host',
  72. port: 'port',
  73. pathname: 'database',
  74. }),
  75. ).to.throw(
  76. `URL "mysql://localhost" is not a valid connection URL. Expected the protocol to be one of "mariadb", but it's "mysql"`,
  77. );
  78. });
  79. it('supports not providing username, password, port, or database name', () => {
  80. const options = parseCommonConnectionUrlOptions<any>({
  81. allowedProtocols: ['mariadb'],
  82. hostname: 'host',
  83. pathname: 'database',
  84. url: 'mariadb://localhost',
  85. port: 'port',
  86. });
  87. expect(options).to.deep.eq({
  88. host: 'localhost',
  89. });
  90. });
  91. it('supports URL-encoded username, password, hostname, pathname, and search parameters keys and values', () => {
  92. const options = parseCommonConnectionUrlOptions<any>({
  93. stringSearchParams: ['search1', 'search2', '1', '2'],
  94. allowedProtocols: ['mariadb'],
  95. hostname: 'theHost',
  96. password: 'thePassword',
  97. pathname: 'theDb',
  98. port: 'thePort',
  99. url: 'mariadb://%61:%62@%63:1234/%64?search1=%31&search2=%32&%31=%31&%32=%32',
  100. username: 'theUser',
  101. });
  102. expect(options).to.deep.eq({
  103. theDb: 'd',
  104. theHost: 'c',
  105. thePassword: 'b',
  106. thePort: 1234,
  107. theUser: 'a',
  108. search1: '1',
  109. search2: '2',
  110. 1: '1',
  111. 2: '2',
  112. });
  113. });
  114. it('supports using a socket path as an encoded hostname', () => {
  115. const options = parseCommonConnectionUrlOptions<any>({
  116. allowedProtocols: ['postgres'],
  117. hostname: 'host',
  118. pathname: 'database',
  119. url: 'postgres://%2Ftmp%2Fmysocket:9821/dbname',
  120. port: 'port',
  121. });
  122. expect(options).to.deep.eq({
  123. host: '/tmp/mysocket',
  124. port: 9821,
  125. database: 'dbname',
  126. });
  127. });
  128. });