1
1
import { ProviderError } from "@aws-sdk/property-provider" ;
2
- import { createServer } from "http" ;
2
+ import http , { createServer } from "http" ;
3
3
import nock from "nock" ;
4
4
5
5
import { httpRequest } from "./httpRequest" ;
6
6
7
7
describe ( "httpRequest" , ( ) => {
8
+ const requestSpy = jest . spyOn ( http , "request" ) ;
8
9
let port : number ;
9
10
const host = "localhost" ;
10
11
const path = "/" ;
@@ -26,13 +27,18 @@ describe("httpRequest", () => {
26
27
port = await getOpenPort ( ) ;
27
28
} ) ;
28
29
30
+ afterEach ( ( ) => {
31
+ jest . clearAllMocks ( ) ;
32
+ } ) ;
33
+
29
34
describe ( "returns response" , ( ) => {
30
35
it ( "defaults to method GET" , async ( ) => {
31
36
const expectedResponse = "expectedResponse" ;
32
37
const scope = nock ( `http://${ host } :${ port } ` ) . get ( path ) . reply ( 200 , expectedResponse ) ;
33
38
34
39
const response = await httpRequest ( { host, path, port } ) ;
35
40
expect ( response . toString ( ) ) . toStrictEqual ( expectedResponse ) ;
41
+ expect ( requestSpy . mock . results [ 0 ] . value . socket ) . toHaveProperty ( "destroyed" , true ) ;
36
42
37
43
scope . done ( ) ;
38
44
} ) ;
@@ -44,6 +50,7 @@ describe("httpRequest", () => {
44
50
45
51
const response = await httpRequest ( { host, path, port, method } ) ;
46
52
expect ( response . toString ( ) ) . toStrictEqual ( expectedResponse ) ;
53
+ expect ( requestSpy . mock . results [ 0 ] . value . socket ) . toHaveProperty ( "destroyed" , true ) ;
47
54
48
55
scope . done ( ) ;
49
56
} ) ;
@@ -57,6 +64,7 @@ describe("httpRequest", () => {
57
64
await expect ( httpRequest ( { host, path, port } ) ) . rejects . toStrictEqual (
58
65
Object . assign ( new ProviderError ( "Error response received from instance metadata service" ) , { statusCode } )
59
66
) ;
67
+ expect ( requestSpy . mock . results [ 0 ] . value . socket ) . toHaveProperty ( "destroyed" , true ) ;
60
68
61
69
scope . done ( ) ;
62
70
} ) ;
@@ -68,6 +76,7 @@ describe("httpRequest", () => {
68
76
await expect ( httpRequest ( { host, path, port } ) ) . rejects . toStrictEqual (
69
77
new ProviderError ( "Unable to connect to instance metadata service" )
70
78
) ;
79
+ expect ( requestSpy . mock . results [ 0 ] . value . socket ) . toHaveProperty ( "destroyed" , true ) ;
71
80
72
81
scope . done ( ) ;
73
82
} ) ;
@@ -91,6 +100,7 @@ describe("httpRequest", () => {
91
100
await expect ( httpRequest ( { host, path, port, timeout } ) ) . rejects . toStrictEqual (
92
101
new ProviderError ( "TimeoutError from instance metadata service" )
93
102
) ;
103
+ expect ( requestSpy . mock . results [ 0 ] . value . socket ) . toHaveProperty ( "destroyed" , true ) ;
94
104
95
105
scope . done ( ) ;
96
106
} ) ;
0 commit comments