2
0

index.test.ts 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. import { describe, expect, it } from 'vitest'
  2. import { defaultHost } from '../src/constant'
  3. import { formatHost } from '../src/utils'
  4. describe('formatHost Function Tests', () => {
  5. it('should return default URL for empty string', () => {
  6. expect(formatHost('')).toBe(defaultHost)
  7. })
  8. it('should parse plain IP address', () => {
  9. expect(formatHost('1.2.3.4')).toBe('http://1.2.3.4:11434')
  10. })
  11. it('should parse IP address with port', () => {
  12. expect(formatHost('1.2.3.4:56789')).toBe('http://1.2.3.4:56789')
  13. })
  14. it('should parse with only a port', () => {
  15. expect(formatHost(':56789')).toBe('http://127.0.0.1:56789')
  16. })
  17. it('should parse HTTP URL', () => {
  18. expect(formatHost('http://1.2.3.4')).toBe('http://1.2.3.4:80')
  19. })
  20. it('should parse HTTPS URL', () => {
  21. expect(formatHost('https://1.2.3.4')).toBe('https://1.2.3.4:443')
  22. })
  23. it('should parse HTTPS URL with port', () => {
  24. expect(formatHost('https://1.2.3.4:56789')).toBe('https://1.2.3.4:56789')
  25. })
  26. it('should parse domain name', () => {
  27. expect(formatHost('example.com')).toBe('http://example.com:11434')
  28. })
  29. it('should parse domain name with port', () => {
  30. expect(formatHost('example.com:56789')).toBe('http://example.com:56789')
  31. })
  32. it('should parse HTTP domain', () => {
  33. expect(formatHost('http://example.com')).toBe('http://example.com:80')
  34. })
  35. it('should parse HTTPS domain', () => {
  36. expect(formatHost('https://example.com')).toBe('https://example.com:443')
  37. })
  38. it('should parse HTTPS domain with port', () => {
  39. expect(formatHost('https://example.com:56789')).toBe('https://example.com:56789')
  40. })
  41. it('should handle trailing slash in domain', () => {
  42. expect(formatHost('example.com/')).toBe('http://example.com:11434')
  43. })
  44. it('should handle trailing slash in domain with port', () => {
  45. expect(formatHost('example.com:56789/')).toBe('http://example.com:56789')
  46. })
  47. it('should handle trailing slash with only a port', () => {
  48. expect(formatHost(':56789/')).toBe('http://127.0.0.1:56789')
  49. })
  50. // Basic Auth Tests
  51. it('should preserve username in URL', () => {
  52. expect(formatHost('http://user@localhost:1234')).toBe('http://user@localhost:1234')
  53. })
  54. it('should preserve username and password in URL', () => {
  55. expect(formatHost('http://user:pass@localhost:5678')).toBe('http://user:pass@localhost:5678')
  56. })
  57. it('should preserve username with default port', () => {
  58. expect(formatHost('http://user@localhost')).toBe('http://user@localhost:80')
  59. })
  60. it('should preserve username and password with default port', () => {
  61. expect(formatHost('http://user:pass@localhost')).toBe('http://user:pass@localhost:80')
  62. })
  63. it('should preserve basic auth with https', () => {
  64. expect(formatHost('https://user:secret@secure.com')).toBe('https://user:secret@secure.com:443')
  65. })
  66. it('should preserve basic auth with domain and custom port', () => {
  67. expect(formatHost('http://admin:1234@example.com:8080')).toBe('http://admin:1234@example.com:8080')
  68. })
  69. it('should preserve basic auth and remove trailing slash', () => {
  70. expect(formatHost('http://john:doe@site.com:3000/')).toBe('http://john:doe@site.com:3000')
  71. })
  72. })