url-helper.test.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import {jest, describe, it, expect, beforeEach, afterAll} from '@jest/globals'
  2. import * as urlHelper from '../src/url-helper.js'
  3. describe('getServerUrl tests', () => {
  4. it('basics', async () => {
  5. // Note that URL::toString will append a trailing / when passed just a domain name ...
  6. expect(urlHelper.getServerUrl().toString()).toBe('https://github.com/')
  7. expect(urlHelper.getServerUrl(' ').toString()).toBe('https://github.com/')
  8. expect(urlHelper.getServerUrl(' ').toString()).toBe('https://github.com/')
  9. expect(urlHelper.getServerUrl('http://contoso.com').toString()).toBe(
  10. 'http://contoso.com/'
  11. )
  12. expect(urlHelper.getServerUrl('https://contoso.com').toString()).toBe(
  13. 'https://contoso.com/'
  14. )
  15. expect(urlHelper.getServerUrl('https://contoso.com/').toString()).toBe(
  16. 'https://contoso.com/'
  17. )
  18. // ... but can't make that same assumption when passed an URL that includes some deeper path.
  19. expect(urlHelper.getServerUrl('https://contoso.com/a/b').toString()).toBe(
  20. 'https://contoso.com/a/b'
  21. )
  22. })
  23. })
  24. describe('isGhes tests', () => {
  25. const pristineEnv = process.env
  26. beforeEach(() => {
  27. jest.resetModules()
  28. process.env = {...pristineEnv}
  29. })
  30. afterAll(() => {
  31. process.env = pristineEnv
  32. })
  33. it('basics', async () => {
  34. delete process.env['GITHUB_SERVER_URL']
  35. expect(urlHelper.isGhes()).toBeFalsy()
  36. expect(urlHelper.isGhes('https://github.com')).toBeFalsy()
  37. expect(urlHelper.isGhes('https://contoso.ghe.com')).toBeFalsy()
  38. expect(urlHelper.isGhes('https://test.github.localhost')).toBeFalsy()
  39. expect(urlHelper.isGhes('https://src.onpremise.fabrikam.com')).toBeTruthy()
  40. })
  41. it('returns false when the GITHUB_SERVER_URL environment variable is not defined', async () => {
  42. delete process.env['GITHUB_SERVER_URL']
  43. expect(urlHelper.isGhes()).toBeFalsy()
  44. })
  45. it('returns false when the GITHUB_SERVER_URL environment variable is set to github.com', async () => {
  46. process.env['GITHUB_SERVER_URL'] = 'https://github.com'
  47. expect(urlHelper.isGhes()).toBeFalsy()
  48. })
  49. it('returns false when the GITHUB_SERVER_URL environment variable is set to a GitHub Enterprise Cloud-style URL', async () => {
  50. process.env['GITHUB_SERVER_URL'] = 'https://contoso.ghe.com'
  51. expect(urlHelper.isGhes()).toBeFalsy()
  52. })
  53. it('returns false when the GITHUB_SERVER_URL environment variable has a .localhost suffix', async () => {
  54. process.env['GITHUB_SERVER_URL'] = 'https://mock-github.localhost'
  55. expect(urlHelper.isGhes()).toBeFalsy()
  56. })
  57. it('returns true when the GITHUB_SERVER_URL environment variable is set to some other URL', async () => {
  58. process.env['GITHUB_SERVER_URL'] = 'https://src.onpremise.fabrikam.com'
  59. expect(urlHelper.isGhes()).toBeTruthy()
  60. })
  61. })
  62. describe('getServerApiUrl tests', () => {
  63. it('basics', async () => {
  64. expect(urlHelper.getServerApiUrl()).toBe('https://api.github.com')
  65. expect(urlHelper.getServerApiUrl('https://github.com')).toBe(
  66. 'https://api.github.com'
  67. )
  68. expect(urlHelper.getServerApiUrl('https://GitHub.com')).toBe(
  69. 'https://api.github.com'
  70. )
  71. expect(urlHelper.getServerApiUrl('https://contoso.ghe.com')).toBe(
  72. 'https://api.contoso.ghe.com'
  73. )
  74. expect(urlHelper.getServerApiUrl('https://fabrikam.GHE.COM')).toBe(
  75. 'https://api.fabrikam.ghe.com'
  76. )
  77. expect(
  78. urlHelper.getServerApiUrl('https://src.onpremise.fabrikam.com')
  79. ).toBe('https://src.onpremise.fabrikam.com/api/v3')
  80. })
  81. })