Browse Source

Fix host breaking when set to only port (#116)

Kim Hallberg 1 year ago
parent
commit
d4ec573bf5
2 changed files with 9 additions and 1 deletions
  1. 1 1
      src/utils.ts
  2. 8 0
      test/index.spec.ts

+ 1 - 1
src/utils.ts

@@ -266,7 +266,7 @@ export const formatHost = (host: string): string => {
   if (host.startsWith(':')) {
     // if host starts with ':', prepend the default hostname
     host = `http://127.0.0.1${host}`
-    isExplicitProtocol = false
+    isExplicitProtocol = true
   }
 
   if (!isExplicitProtocol) {

+ 8 - 0
test/index.spec.ts

@@ -13,6 +13,10 @@ describe('formatHost Function Tests', () => {
     expect(formatHost('1.2.3.4:56789')).toBe('http://1.2.3.4:56789')
   })
 
+  it('should parse with only a port', () => {
+    expect(formatHost(':56789')).toBe('http://127.0.0.1:56789')
+  })
+
   it('should parse HTTP URL', () => {
     expect(formatHost('http://1.2.3.4')).toBe('http://1.2.3.4:80')
   })
@@ -52,4 +56,8 @@ describe('formatHost Function Tests', () => {
   it('should handle trailing slash in domain with port', () => {
     expect(formatHost('example.com:56789/')).toBe('http://example.com:56789')
   })
+
+  it('should handle traling slash with only a port', () => {
+    expect(formatHost(':56789/')).toBe('http://127.0.0.1:56789')
+  })
 })