1
1
import { AbortController } from "@aws-sdk/abort-controller" ;
2
2
import { HttpRequest } from "@aws-sdk/protocol-http" ;
3
- import { Server as HttpServer } from "http" ;
4
- import http from "http" ;
5
- import { Server as HttpsServer } from "https" ;
6
- import https from "https" ;
3
+ import http , { Server as HttpServer } from "http" ;
4
+ import https , { Server as HttpsServer } from "https" ;
7
5
import { AddressInfo } from "net" ;
8
6
9
7
import { NodeHttpHandler } from "./node-http-handler" ;
@@ -16,7 +14,7 @@ import {
16
14
} from "./server.mock" ;
17
15
18
16
describe ( "NodeHttpHandler" , ( ) => {
19
- describe ( "constructor" , ( ) => {
17
+ describe ( "constructor and #handle " , ( ) => {
20
18
let hRequestSpy : jest . SpyInstance ;
21
19
let hsRequestSpy : jest . SpyInstance ;
22
20
const randomMaxSocket = Math . round ( Math . random ( ) * 50 ) + 1 ;
@@ -38,55 +36,87 @@ describe("NodeHttpHandler", () => {
38
36
hRequestSpy . mockRestore ( ) ;
39
37
hsRequestSpy . mockRestore ( ) ;
40
38
} ) ;
39
+ describe ( "constructor" , ( ) => {
40
+ it . each ( [
41
+ [ "empty" , undefined ] ,
42
+ [ "a provider" , async ( ) => { } ] ,
43
+ ] ) ( "sets keepAlive=true by default when input is %s" , async ( _ , option ) => {
44
+ const nodeHttpHandler = new NodeHttpHandler ( option ) ;
45
+ await nodeHttpHandler . handle ( { } as any ) ;
46
+ expect ( hRequestSpy . mock . calls [ 0 ] [ 0 ] ?. agent . keepAlive ) . toEqual ( true ) ;
47
+ } ) ;
41
48
42
- it . each ( [
43
- [ "empty" , undefined ] ,
44
- [ "a provider" , async ( ) => { } ] ,
45
- ] ) ( "sets keepAlive=true by default when input is %s" , async ( _ , option ) => {
46
- const nodeHttpHandler = new NodeHttpHandler ( option ) ;
47
- await nodeHttpHandler . handle ( { } as any ) ;
48
- expect ( hRequestSpy . mock . calls [ 0 ] [ 0 ] ?. agent . keepAlive ) . toEqual ( true ) ;
49
- } ) ;
49
+ it . each ( [
50
+ [ "empty" , undefined ] ,
51
+ [ "a provider" , async ( ) => { } ] ,
52
+ ] ) ( "sets maxSockets=50 by default when input is %s" , async ( _ , option ) => {
53
+ const nodeHttpHandler = new NodeHttpHandler ( option ) ;
54
+ await nodeHttpHandler . handle ( { } as any ) ;
55
+ expect ( hRequestSpy . mock . calls [ 0 ] [ 0 ] ?. agent . maxSockets ) . toEqual ( 50 ) ;
56
+ } ) ;
50
57
51
- it . each ( [
52
- [ "empty" , undefined ] ,
53
- [ "a provider" , async ( ) => { } ] ,
54
- ] ) ( "sets maxSockets=50 by default when input is %s" , async ( _ , option ) => {
55
- const nodeHttpHandler = new NodeHttpHandler ( option ) ;
56
- await nodeHttpHandler . handle ( { } as any ) ;
57
- expect ( hRequestSpy . mock . calls [ 0 ] [ 0 ] ?. agent . maxSockets ) . toEqual ( 50 ) ;
58
- } ) ;
58
+ it . each ( [
59
+ [ "an options hash" , { httpAgent : new http . Agent ( { keepAlive : false , maxSockets : randomMaxSocket } ) } ] ,
60
+ [
61
+ "a provider" ,
62
+ async ( ) => ( {
63
+ httpAgent : new http . Agent ( { keepAlive : false , maxSockets : randomMaxSocket } ) ,
64
+ } ) ,
65
+ ] ,
66
+ ] ) ( "sets httpAgent when input is %s" , async ( _ , option ) => {
67
+ const nodeHttpHandler = new NodeHttpHandler ( option ) ;
68
+ await nodeHttpHandler . handle ( { protocol : "http:" , headers : { } , method : "GET" , hostname : "localhost" } as any ) ;
69
+ expect ( hRequestSpy . mock . calls [ 0 ] [ 0 ] ?. agent . keepAlive ) . toEqual ( false ) ;
70
+ expect ( hRequestSpy . mock . calls [ 0 ] [ 0 ] ?. agent . maxSockets ) . toEqual ( randomMaxSocket ) ;
71
+ } ) ;
59
72
60
- it . each ( [
61
- [ "an options hash" , { httpAgent : new http . Agent ( { keepAlive : false , maxSockets : randomMaxSocket } ) } ] ,
62
- [
63
- "a provider" ,
64
- async ( ) => ( {
65
- httpAgent : new http . Agent ( { keepAlive : false , maxSockets : randomMaxSocket } ) ,
66
- } ) ,
67
- ] ,
68
- ] ) ( "sets httpAgent when input is %s" , async ( _ , option ) => {
69
- const nodeHttpHandler = new NodeHttpHandler ( option ) ;
70
- await nodeHttpHandler . handle ( { protocol : "http:" , headers : { } , method : "GET" , hostname : "localhost" } as any ) ;
71
- expect ( hRequestSpy . mock . calls [ 0 ] [ 0 ] ?. agent . keepAlive ) . toEqual ( false ) ;
72
- expect ( hRequestSpy . mock . calls [ 0 ] [ 0 ] ?. agent . maxSockets ) . toEqual ( randomMaxSocket ) ;
73
+ it . each ( [
74
+ [ "an option hash" , { httpsAgent : new https . Agent ( { keepAlive : true , maxSockets : randomMaxSocket } ) } ] ,
75
+ [
76
+ "a provider" ,
77
+ async ( ) => ( {
78
+ httpsAgent : new https . Agent ( { keepAlive : true , maxSockets : randomMaxSocket } ) ,
79
+ } ) ,
80
+ ] ,
81
+ ] ) ( "sets httpsAgent when input is %s" , async ( _ , option ) => {
82
+ const nodeHttpHandler = new NodeHttpHandler ( option ) ;
83
+ await nodeHttpHandler . handle ( { protocol : "https:" } as any ) ;
84
+ expect ( hsRequestSpy . mock . calls [ 0 ] [ 0 ] ?. agent . keepAlive ) . toEqual ( true ) ;
85
+ expect ( hsRequestSpy . mock . calls [ 0 ] [ 0 ] ?. agent . maxSockets ) . toEqual ( randomMaxSocket ) ;
86
+ } ) ;
73
87
} ) ;
74
88
75
- it . each ( [
76
- [ "an option hash" , { httpsAgent : new https . Agent ( { keepAlive : true , maxSockets : randomMaxSocket } ) } ] ,
77
- [
78
- "a provider" ,
79
- async ( ) => ( {
80
- httpsAgent : new https . Agent ( { keepAlive : true , maxSockets : randomMaxSocket } ) ,
81
- } ) ,
82
- ] ,
83
- ] ) ( "sets httpsAgent when input is %s" , async ( _ , option ) => {
84
- const nodeHttpHandler = new NodeHttpHandler ( option ) ;
85
- await nodeHttpHandler . handle ( { protocol : "https:" } as any ) ;
86
- expect ( hsRequestSpy . mock . calls [ 0 ] [ 0 ] ?. agent . keepAlive ) . toEqual ( true ) ;
87
- expect ( hsRequestSpy . mock . calls [ 0 ] [ 0 ] ?. agent . maxSockets ) . toEqual ( randomMaxSocket ) ;
89
+ describe ( "#handle" , ( ) => {
90
+ it ( "should only generate a single config when the config provider is async and it is not ready yet" , async ( ) => {
91
+ let providerInvokedCount = 0 ;
92
+ let providerResolvedCount = 0 ;
93
+ const slowConfigProvider = async ( ) => {
94
+ providerInvokedCount += 1 ;
95
+ await new Promise ( ( r ) => setTimeout ( r , 15 ) ) ;
96
+ providerResolvedCount += 1 ;
97
+ return {
98
+ connectionTimeout : 12345 ,
99
+ socketTimeout : 12345 ,
100
+ httpAgent : null ,
101
+ httpsAgent : null ,
102
+ } ;
103
+ } ;
104
+
105
+ const nodeHttpHandler = new NodeHttpHandler ( slowConfigProvider ) ;
106
+
107
+ const promises = Promise . all (
108
+ Array . from ( { length : 20 } ) . map ( ( ) => nodeHttpHandler . handle ( { } as unknown as HttpRequest ) )
109
+ ) ;
110
+
111
+ expect ( providerInvokedCount ) . toBe ( 1 ) ;
112
+ expect ( providerResolvedCount ) . toBe ( 0 ) ;
113
+ await promises ;
114
+ expect ( providerInvokedCount ) . toBe ( 1 ) ;
115
+ expect ( providerResolvedCount ) . toBe ( 1 ) ;
116
+ } ) ;
88
117
} ) ;
89
118
} ) ;
119
+
90
120
describe ( "http" , ( ) => {
91
121
const mockHttpServer : HttpServer = createMockHttpServer ( ) . listen ( 54321 ) ;
92
122
0 commit comments