Skip to content

Commit 6a4bfe3

Browse files
authored
fix(formatHost): support basic auth in host URL (#219)
1 parent e4f9c8f commit 6a4bfe3

File tree

2 files changed

+44
-5
lines changed

2 files changed

+44
-5
lines changed

src/utils.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { version } from './version.js'
1+
import { defaultHost, defaultPort } from './constant.js'
22
import type { ErrorResponse, Fetch } from './interfaces.js'
3-
import { defaultPort, defaultHost } from './constant.js'
3+
import { version } from './version.js'
44

55
/**
66
* An error class for response errors.
@@ -331,7 +331,17 @@ export const formatHost = (host: string): string => {
331331
}
332332
}
333333

334-
let formattedHost = `${url.protocol}//${url.hostname}:${port}${url.pathname}`
334+
// Build basic auth part if present
335+
let auth = '';
336+
if (url.username) {
337+
auth = url.username;
338+
if (url.password) {
339+
auth += `:${url.password}`;
340+
}
341+
auth += '@';
342+
}
343+
344+
let formattedHost = `${url.protocol}//${auth}${url.hostname}:${port}${url.pathname}`;
335345
// remove trailing slashes
336346
if (formattedHost.endsWith('/')) {
337347
formattedHost = formattedHost.slice(0, -1)

test/index.test.ts

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { describe, it, expect } from 'vitest'
2-
import { formatHost } from '../src/utils'
1+
import { describe, expect, it } from 'vitest'
32
import { defaultHost } from '../src/constant'
3+
import { formatHost } from '../src/utils'
44

55
describe('formatHost Function Tests', () => {
66
it('should return default URL for empty string', () => {
@@ -62,4 +62,33 @@ describe('formatHost Function Tests', () => {
6262
it('should handle trailing slash with only a port', () => {
6363
expect(formatHost(':56789/')).toBe('http://127.0.0.1:56789')
6464
})
65+
66+
// Basic Auth Tests
67+
it('should preserve username in URL', () => {
68+
expect(formatHost('http://user@localhost:1234')).toBe('http://user@localhost:1234')
69+
})
70+
71+
it('should preserve username and password in URL', () => {
72+
expect(formatHost('http://user:pass@localhost:5678')).toBe('http://user:pass@localhost:5678')
73+
})
74+
75+
it('should preserve username with default port', () => {
76+
expect(formatHost('http://user@localhost')).toBe('http://user@localhost:80')
77+
})
78+
79+
it('should preserve username and password with default port', () => {
80+
expect(formatHost('http://user:pass@localhost')).toBe('http://user:pass@localhost:80')
81+
})
82+
83+
it('should preserve basic auth with https', () => {
84+
expect(formatHost('https://user:[email protected]')).toBe('https://user:[email protected]:443')
85+
})
86+
87+
it('should preserve basic auth with domain and custom port', () => {
88+
expect(formatHost('http://admin:[email protected]:8080')).toBe('http://admin:[email protected]:8080')
89+
})
90+
91+
it('should preserve basic auth and remove trailing slash', () => {
92+
expect(formatHost('http://john:[email protected]:3000/')).toBe('http://john:[email protected]:3000')
93+
})
6594
})

0 commit comments

Comments
 (0)