-
Notifications
You must be signed in to change notification settings - Fork 123
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: should HEAD request keepalive by default (#566)
closes #565 <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit ## Release Notes - **New Features** - Enhanced request handling with the addition of a `reset` property for more control over HTTP requests. - Improved diagnostics capabilities for socket management and error handling. - **Bug Fixes** - Updated error handling to ensure retries are based on specific error types. - **Tests** - Introduced new test cases to validate keep-alive behavior for HTTP HEAD requests. - Enhanced existing tests to ensure accurate tracking of socket requests and responses. - **Documentation** - Updated documentation for the `reset` property in request options to clarify its default behavior. - **Chores** - Minor adjustments to logging namespaces for consistency. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
- Loading branch information
Showing
8 changed files
with
223 additions
and
16 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
import { strict as assert } from 'node:assert'; | ||
import { scheduler } from 'node:timers/promises'; | ||
import { describe, it, beforeAll, afterAll } from 'vitest'; | ||
import { fetch } from '../src/index.js'; | ||
import { startServer } from './fixtures/server.js'; | ||
|
||
describe.skip('fetch-head-request-should-keepalive.test.ts', () => { | ||
// https://github.com/node-modules/urllib/issues/565 | ||
// head request should keepalive | ||
let close: any; | ||
let _url: string; | ||
beforeAll(async () => { | ||
const { closeServer, urlWithDns } = await startServer(); | ||
close = closeServer; | ||
_url = urlWithDns; | ||
}); | ||
|
||
afterAll(async () => { | ||
await close(); | ||
}); | ||
|
||
it('should keepalive on GET => HEAD => HEAD Request', async () => { | ||
let res = await fetch(_url, { | ||
method: 'GET', | ||
}); | ||
assert.equal(res.status, 200); | ||
console.log(res.headers, res); | ||
assert.equal(res.headers.get('connection'), 'keep-alive'); | ||
|
||
await scheduler.wait(10); | ||
res = await fetch(_url, { | ||
method: 'HEAD', | ||
}); | ||
assert.equal(res.status, 200); | ||
console.log(res.headers, res); | ||
assert.equal(res.headers.get('connection'), 'keep-alive'); | ||
|
||
// await scheduler.wait(1); | ||
// res = await httpClient.request(_url, { | ||
// method: 'HEAD', | ||
// }); | ||
// assert.equal(res.status, 200); | ||
// // console.log(res.headers, res.res.socket); | ||
// assert.equal(res.headers.connection, 'keep-alive'); | ||
// assert.equal(res.res.socket.id, socketId); | ||
|
||
// res = await httpClient.request(_url, { | ||
// method: 'HEAD', | ||
// }); | ||
// assert.equal(res.status, 200); | ||
// // console.log(res.headers, res.res.socket); | ||
// assert.equal(res.headers.connection, 'keep-alive'); | ||
|
||
// await scheduler.wait(1); | ||
// res = await httpClient.request(_url, { | ||
// method: 'HEAD', | ||
// }); | ||
// assert.equal(res.status, 200); | ||
// // console.log(res.headers, res.res.socket); | ||
// assert.equal(res.headers.connection, 'keep-alive'); | ||
// assert.equal(res.res.socket.id, socketId); | ||
|
||
// await scheduler.wait(1); | ||
// res = await httpClient.request(_url, { | ||
// method: 'HEAD', | ||
// }); | ||
// assert.equal(res.status, 200); | ||
// // console.log(res.headers, res.res.socket); | ||
// assert.equal(res.headers.connection, 'keep-alive'); | ||
// assert.equal(res.res.socket.id, socketId); | ||
}); | ||
|
||
// it('should close connection when reset = true', async () => { | ||
// const httpClient = new HttpClient(); | ||
// let res = await httpClient.request(_url, { | ||
// method: 'GET', | ||
// reset: true, | ||
// }); | ||
// assert.equal(res.status, 200); | ||
// // console.log(res.headers, res.res.socket); | ||
// assert.equal(res.headers.connection, 'close'); | ||
// const socketId = res.res.socket.id; | ||
|
||
// await scheduler.wait(10); | ||
// res = await httpClient.request(_url, { | ||
// method: 'HEAD', | ||
// reset: true, | ||
// }); | ||
// assert.equal(res.status, 200); | ||
// // console.log(res.headers, res.res.socket); | ||
// assert.equal(res.headers.connection, 'close'); | ||
// assert.notEqual(res.res.socket.id, socketId); | ||
// }); | ||
}); |
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,97 @@ | ||
import { strict as assert } from 'node:assert'; | ||
import { scheduler } from 'node:timers/promises'; | ||
import { describe, it, beforeAll, afterAll } from 'vitest'; | ||
import { HttpClient } from '../src/index.js'; | ||
import { startServer } from './fixtures/server.js'; | ||
|
||
describe('head-request-should-keepalive.test.ts', () => { | ||
// https://github.com/node-modules/urllib/issues/565 | ||
// head request should keepalive | ||
let close: any; | ||
let _url: string; | ||
beforeAll(async () => { | ||
const { closeServer, urlWithDns } = await startServer(); | ||
close = closeServer; | ||
_url = urlWithDns; | ||
}); | ||
|
||
afterAll(async () => { | ||
await close(); | ||
}); | ||
|
||
it('should keepalive on GET => HEAD => HEAD Request', async () => { | ||
const httpClient = new HttpClient(); | ||
let res = await httpClient.request(_url, { | ||
method: 'GET', | ||
}); | ||
assert.equal(res.status, 200); | ||
// console.log(res.headers, res.res.socket); | ||
assert.equal(res.headers.connection, 'keep-alive'); | ||
const socketId = res.res.socket.id; | ||
|
||
await scheduler.wait(10); | ||
res = await httpClient.request(_url, { | ||
method: 'HEAD', | ||
}); | ||
assert.equal(res.status, 200); | ||
// console.log(res.headers, res.res.socket); | ||
assert.equal(res.headers.connection, 'keep-alive'); | ||
assert.equal(res.res.socket.id, socketId); | ||
|
||
await scheduler.wait(1); | ||
res = await httpClient.request(_url, { | ||
method: 'HEAD', | ||
}); | ||
assert.equal(res.status, 200); | ||
// console.log(res.headers, res.res.socket); | ||
assert.equal(res.headers.connection, 'keep-alive'); | ||
assert.equal(res.res.socket.id, socketId); | ||
|
||
res = await httpClient.request(_url, { | ||
method: 'HEAD', | ||
}); | ||
assert.equal(res.status, 200); | ||
// console.log(res.headers, res.res.socket); | ||
assert.equal(res.headers.connection, 'keep-alive'); | ||
|
||
await scheduler.wait(1); | ||
res = await httpClient.request(_url, { | ||
method: 'HEAD', | ||
}); | ||
assert.equal(res.status, 200); | ||
// console.log(res.headers, res.res.socket); | ||
assert.equal(res.headers.connection, 'keep-alive'); | ||
assert.equal(res.res.socket.id, socketId); | ||
|
||
await scheduler.wait(1); | ||
res = await httpClient.request(_url, { | ||
method: 'HEAD', | ||
}); | ||
assert.equal(res.status, 200); | ||
// console.log(res.headers, res.res.socket); | ||
assert.equal(res.headers.connection, 'keep-alive'); | ||
assert.equal(res.res.socket.id, socketId); | ||
}); | ||
|
||
it('should close connection when reset = true', async () => { | ||
const httpClient = new HttpClient(); | ||
let res = await httpClient.request(_url, { | ||
method: 'GET', | ||
reset: true, | ||
}); | ||
assert.equal(res.status, 200); | ||
// console.log(res.headers, res.res.socket); | ||
assert.equal(res.headers.connection, 'close'); | ||
const socketId = res.res.socket.id; | ||
|
||
await scheduler.wait(10); | ||
res = await httpClient.request(_url, { | ||
method: 'HEAD', | ||
reset: true, | ||
}); | ||
assert.equal(res.status, 200); | ||
// console.log(res.headers, res.res.socket); | ||
assert.equal(res.headers.connection, 'close'); | ||
assert.notEqual(res.res.socket.id, socketId); | ||
}); | ||
}); |