Skip to content

Commit

Permalink
feat: support reset to not reuse connection (#438)
Browse files Browse the repository at this point in the history
Co-authored-by: fengmk2 <fengmk2@gmail.com>
  • Loading branch information
killagu and fengmk2 authored Mar 21, 2023
1 parent 9fec8e8 commit 3e12703
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/HttpClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,9 @@ export class HttpClient extends EventEmitter {
opaque: internalOpaque,
dispatcher: args.dispatcher ?? this.#dispatcher,
};
if (typeof args.reset === 'boolean') {
requestOptions.reset = args.reset;
}

if (args.followRedirect === false) {
requestOptions.maxRedirections = 0;
Expand Down
2 changes: 2 additions & 0 deletions src/Request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,4 +130,6 @@ export type RequestOptions = {
* unix domain socket file path
*/
socketPath?: string | null;
/** Whether the request should stablish a keep-alive or not. Default `undefined` */
reset?: boolean;
};
3 changes: 2 additions & 1 deletion test/keep-alive-header.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ describe('keep-alive-header.test.ts', () => {

it('should handle Keep-Alive header and not throw reset error', async () => {
let count = 0;
const max = process.env.CI ? 10 : 2;
// const max = process.env.CI && process.platform !== 'win32' ? 10 : 2;
const max = 2;
while (count < max) {
count++;
let response = await urllib.request(_url);
Expand Down
27 changes: 27 additions & 0 deletions test/options.reset.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { describe, it, beforeAll, afterAll } from 'vitest';
import { strict as assert } from 'assert';
import urllib from '../src';
import { startServer } from './fixtures/server';

describe('options.reset.test.ts', () => {
let close: any;
let _url: string;
beforeAll(async () => {
const { closeServer, url } = await startServer();
close = closeServer;
_url = url;
});

afterAll(async () => {
await close();
});

it('should opaque work', async () => {
const response = await urllib.request(_url, {
dataType: 'json',
reset: true,
});
assert.equal(response.status, 200);
assert(response.data.headers['connection'] === 'close');
});
});

0 comments on commit 3e12703

Please sign in to comment.