Skip to content
This repository has been archived by the owner on May 3, 2022. It is now read-only.

Allow overriding user-agent via headers. #65

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 15 additions & 0 deletions __tests__/basics.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,21 @@ describe('basics', () => {
done()
})

it('does basic http get request with overridden user agent', async done => {
let res: httpm.HttpClientResponse = await _http.get(
'http://httpbin.org/get',
{
'user-agent': 'custom-user-agent'
}
)
expect(res.message.statusCode).toBe(200)
let body: string = await res.readBody()
let obj: any = JSON.parse(body)
expect(obj.url).toBe('http://httpbin.org/get')
expect(obj.headers['User-Agent']).toBe('custom-user-agent')
done()
})

it('does basic https get request', async done => {
let res: httpm.HttpClientResponse = await _http.get(
'https://httpbin.org/get'
Expand Down
2 changes: 1 addition & 1 deletion index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -562,7 +562,7 @@ export class HttpClient {
(info.parsedUrl.pathname || '') + (info.parsedUrl.search || '')
info.options.method = method
info.options.headers = this._mergeHeaders(headers)
if (this.userAgent != null) {
if (this.userAgent != null && !('user-agent' in info.options.headers)) {
info.options.headers['user-agent'] = this.userAgent
}

Expand Down