-
Notifications
You must be signed in to change notification settings - Fork 121
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: allow set rejectUnauthorized = true on urllib.request options (#561
) closes #531 <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit ## Release Notes - **New Features** - Enhanced HTTP client configuration with options for handling HTTP/2 and unauthorized requests. - Introduced new tests for validating behavior with `rejectUnauthorized` set to false. - **Bug Fixes** - Improved error handling and address validation in HTTP client tests. - **Documentation** - Updated comments for clarity and consistency in the `HttpClient` class. - **Chores** - Updated dependency management for improved security and functionality. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
- Loading branch information
Showing
8 changed files
with
167 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { strict as assert } from 'node:assert'; | ||
import { describe, it } from 'vitest'; | ||
import urllib from '../src/index.js'; | ||
|
||
describe('urllib.options.allowH2.test.ts', () => { | ||
it('should 200 on options.allowH2 = true', async () => { | ||
let response = await urllib.request('https://registry.npmmirror.com', { | ||
allowH2: true, | ||
dataType: 'json', | ||
}); | ||
assert.equal(response.status, 200); | ||
|
||
response = await urllib.curl('https://registry.npmmirror.com', { | ||
allowH2: true, | ||
dataType: 'json', | ||
}); | ||
assert.equal(response.status, 200); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import { strict as assert } from 'node:assert'; | ||
import { once } from 'node:events'; | ||
import { createSecureServer } from 'node:http2'; | ||
import { describe, it, beforeAll, afterAll } from 'vitest'; | ||
import selfsigned from 'selfsigned'; | ||
import urllib, { HttpClient } from '../src/index.js'; | ||
import { startServer } from './fixtures/server.js'; | ||
|
||
describe('urllib.options.rejectUnauthorized-false.test.ts', () => { | ||
let close: any; | ||
let _url: string; | ||
beforeAll(async () => { | ||
const { closeServer, url } = await startServer({ https: true }); | ||
close = closeServer; | ||
_url = url; | ||
}); | ||
|
||
afterAll(async () => { | ||
await close(); | ||
}); | ||
|
||
it('should 200 on options.rejectUnauthorized = false', async () => { | ||
const response = await urllib.request(_url, { | ||
rejectUnauthorized: false, | ||
dataType: 'json', | ||
}); | ||
assert.equal(response.status, 200); | ||
assert.equal(response.data.method, 'GET'); | ||
}); | ||
|
||
it('should 200 with H2 on options.rejectUnauthorized = false', async () => { | ||
const pem = selfsigned.generate(); | ||
const server = createSecureServer({ | ||
key: pem.private, | ||
cert: pem.cert, | ||
}); | ||
|
||
server.on('stream', (stream, headers) => { | ||
assert.equal(headers[':method'], 'GET'); | ||
stream.respond({ | ||
'content-type': 'text/plain; charset=utf-8', | ||
'x-custom-h2': 'hello', | ||
':status': 200, | ||
}); | ||
stream.end('hello h2!'); | ||
}); | ||
|
||
server.listen(0); | ||
await once(server, 'listening'); | ||
|
||
const httpClient = new HttpClient({ | ||
allowH2: true, | ||
connect: { | ||
rejectUnauthorized: false, | ||
}, | ||
}); | ||
|
||
const url = `https://localhost:${server.address()!.port}`; | ||
const response1 = await httpClient.request(url, {}); | ||
assert.equal(response1.status, 200); | ||
assert.equal(response1.data.toString(), 'hello h2!'); | ||
|
||
const response2 = await urllib.request(url, { | ||
rejectUnauthorized: false, | ||
allowH2: true, | ||
dataType: 'text', | ||
}); | ||
assert.equal(response2.status, 200); | ||
assert.equal(response2.data, 'hello h2!'); | ||
}); | ||
}); |