url-helper.test.ts 3.4 KB

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