@@ -4,6 +4,8 @@ import https from "https";
4
4
import { afterEach , beforeEach , describe , expect , test as it , vi } from "vitest" ;
5
5
6
6
import { NodeHttpHandler } from "./node-http-handler" ;
7
+ import * as setConnectionTimeoutModule from "./set-connection-timeout" ;
8
+ import * as setSocketTimeoutModule from "./set-socket-timeout" ;
7
9
import { timing } from "./timing" ;
8
10
9
11
vi . mock ( "http" , async ( ) => {
@@ -54,6 +56,8 @@ describe("NodeHttpHandler", () => {
54
56
describe ( "constructor and #handle" , ( ) => {
55
57
const randomMaxSocket = Math . round ( Math . random ( ) * 50 ) + 1 ;
56
58
const randomSocketAcquisitionWarningTimeout = Math . round ( Math . random ( ) * 10000 ) + 1 ;
59
+ const randomConnectionTimeout = Math . round ( Math . random ( ) * 10000 ) + 1 ;
60
+ const randomRequestTimeout = Math . round ( Math . random ( ) * 10000 ) + 1 ;
57
61
58
62
beforeEach ( ( ) => { } ) ;
59
63
@@ -110,6 +114,38 @@ describe("NodeHttpHandler", () => {
110
114
expect ( vi . mocked ( timing . setTimeout ) . mock . calls [ 0 ] [ 1 ] ) . toBe ( randomSocketAcquisitionWarningTimeout ) ;
111
115
} ) ;
112
116
117
+ it . each ( [
118
+ [ "an options hash" , { connectionTimeout : randomConnectionTimeout } ] ,
119
+ [
120
+ "a provider" ,
121
+ async ( ) => ( {
122
+ connectionTimeout : randomConnectionTimeout ,
123
+ } ) ,
124
+ ] ,
125
+ ] ) ( "sets connectionTimeout correctly when input is %s" , async ( _ , option ) => {
126
+ vi . spyOn ( setConnectionTimeoutModule , "setConnectionTimeout" ) ;
127
+ const nodeHttpHandler = new NodeHttpHandler ( option ) ;
128
+ await nodeHttpHandler . handle ( { } as any ) ;
129
+ expect ( vi . mocked ( setConnectionTimeoutModule . setConnectionTimeout ) . mock . calls [ 0 ] [ 2 ] ) . toBe (
130
+ randomConnectionTimeout
131
+ ) ;
132
+ } ) ;
133
+
134
+ it . each ( [
135
+ [ "an options hash" , { requestTimeout : randomRequestTimeout } ] ,
136
+ [
137
+ "a provider" ,
138
+ async ( ) => ( {
139
+ requestTimeout : randomRequestTimeout ,
140
+ } ) ,
141
+ ] ,
142
+ ] ) ( "sets requestTimeout correctly when input is %s" , async ( _ , option ) => {
143
+ vi . spyOn ( setSocketTimeoutModule , "setSocketTimeout" ) ;
144
+ const nodeHttpHandler = new NodeHttpHandler ( option ) ;
145
+ await nodeHttpHandler . handle ( { } as any ) ;
146
+ expect ( vi . mocked ( setSocketTimeoutModule . setSocketTimeout ) . mock . calls [ 0 ] [ 2 ] ) . toBe ( randomRequestTimeout ) ;
147
+ } ) ;
148
+
113
149
it . each ( [
114
150
[ "an options hash" , { httpAgent : new http . Agent ( { keepAlive : false , maxSockets : randomMaxSocket } ) } ] ,
115
151
[
@@ -211,6 +247,45 @@ describe("NodeHttpHandler", () => {
211
247
} ) ;
212
248
} ) ;
213
249
250
+ describe ( "create" , ( ) => {
251
+ const randomRequestTimeout = Math . round ( Math . random ( ) * 10000 ) + 1 ;
252
+
253
+ it . each ( [
254
+ [ "existing handler instance" , new NodeHttpHandler ( ) ] ,
255
+ [
256
+ "custom HttpHandler object" ,
257
+ {
258
+ handle : vi . fn ( ) ,
259
+ } as any ,
260
+ ] ,
261
+ ] ) ( "returns the input handler when passed %s" , ( _ , handler ) => {
262
+ const result = NodeHttpHandler . create ( handler ) ;
263
+ expect ( result ) . toBe ( handler ) ;
264
+ } ) ;
265
+
266
+ it . each ( [
267
+ [ "undefined" , undefined ] ,
268
+ [ "an empty options hash" , { } ] ,
269
+ [ "empty provider" , async ( ) => undefined ] ,
270
+ ] ) ( "creates new handler instance when input is %s" , async ( _ , input ) => {
271
+ const result = NodeHttpHandler . create ( input ) ;
272
+ expect ( result ) . toBeInstanceOf ( NodeHttpHandler ) ;
273
+ } ) ;
274
+
275
+ it . each ( [
276
+ [ "an options hash" , { requestTimeout : randomRequestTimeout } ] ,
277
+ [ "a provider" , async ( ) => ( { requestTimeout : randomRequestTimeout } ) ] ,
278
+ ] ) ( "creates new handler instance with config when input is %s" , async ( _ , input ) => {
279
+ const result = NodeHttpHandler . create ( input ) ;
280
+ expect ( result ) . toBeInstanceOf ( NodeHttpHandler ) ;
281
+
282
+ // Verify configuration by calling handle
283
+ await result . handle ( { } as any ) ;
284
+
285
+ expect ( result . httpHandlerConfigs ( ) . requestTimeout ) . toBe ( randomRequestTimeout ) ;
286
+ } ) ;
287
+ } ) ;
288
+
214
289
describe ( "#destroy" , ( ) => {
215
290
it ( "should be callable and return nothing" , ( ) => {
216
291
const nodeHttpHandler = new NodeHttpHandler ( ) ;
0 commit comments