@@ -7,7 +7,7 @@ import { httpRequest } from "./httpRequest";
7
7
describe ( "httpRequest" , ( ) => {
8
8
const requestSpy = jest . spyOn ( http , "request" ) ;
9
9
let port : number ;
10
- const host = "localhost" ;
10
+ const hostname = "localhost" ;
11
11
const path = "/" ;
12
12
13
13
const getOpenPort = async ( candidatePort = 4321 ) : Promise < number > => {
@@ -34,9 +34,9 @@ describe("httpRequest", () => {
34
34
describe ( "returns response" , ( ) => {
35
35
it ( "defaults to method GET" , async ( ) => {
36
36
const expectedResponse = "expectedResponse" ;
37
- const scope = nock ( `http://${ host } :${ port } ` ) . get ( path ) . reply ( 200 , expectedResponse ) ;
37
+ const scope = nock ( `http://${ hostname } :${ port } ` ) . get ( path ) . reply ( 200 , expectedResponse ) ;
38
38
39
- const response = await httpRequest ( { host , path, port } ) ;
39
+ const response = await httpRequest ( { hostname , path, port } ) ;
40
40
expect ( response . toString ( ) ) . toStrictEqual ( expectedResponse ) ;
41
41
expect ( requestSpy . mock . results [ 0 ] . value . socket ) . toHaveProperty ( "destroyed" , true ) ;
42
42
@@ -46,9 +46,21 @@ describe("httpRequest", () => {
46
46
it ( "uses method passed in options" , async ( ) => {
47
47
const method = "POST" ;
48
48
const expectedResponse = "expectedResponse" ;
49
- const scope = nock ( `http://${ host } :${ port } ` ) . post ( path ) . reply ( 200 , expectedResponse ) ;
49
+ const scope = nock ( `http://${ hostname } :${ port } ` ) . post ( path ) . reply ( 200 , expectedResponse ) ;
50
50
51
- const response = await httpRequest ( { host, path, port, method } ) ;
51
+ const response = await httpRequest ( { hostname, path, port, method } ) ;
52
+ expect ( response . toString ( ) ) . toStrictEqual ( expectedResponse ) ;
53
+ expect ( requestSpy . mock . results [ 0 ] . value . socket ) . toHaveProperty ( "destroyed" , true ) ;
54
+
55
+ scope . done ( ) ;
56
+ } ) ;
57
+
58
+ it ( "works with IPv6 hostname with encapsulated brackets" , async ( ) => {
59
+ const expectedResponse = "expectedResponse" ;
60
+ const encapsulatedIPv6Hostname = "[::1]" ;
61
+ const scope = nock ( `http://${ encapsulatedIPv6Hostname } :${ port } ` ) . get ( path ) . reply ( 200 , expectedResponse ) ;
62
+
63
+ const response = await httpRequest ( { hostname : encapsulatedIPv6Hostname , path, port } ) ;
52
64
expect ( response . toString ( ) ) . toStrictEqual ( expectedResponse ) ;
53
65
expect ( requestSpy . mock . results [ 0 ] . value . socket ) . toHaveProperty ( "destroyed" , true ) ;
54
66
@@ -59,9 +71,9 @@ describe("httpRequest", () => {
59
71
describe ( "throws error" , ( ) => {
60
72
const errorOnStatusCode = async ( statusCode : number ) => {
61
73
it ( `statusCode: ${ statusCode } ` , async ( ) => {
62
- const scope = nock ( `http://${ host } :${ port } ` ) . get ( path ) . reply ( statusCode , "continue" ) ;
74
+ const scope = nock ( `http://${ hostname } :${ port } ` ) . get ( path ) . reply ( statusCode , "continue" ) ;
63
75
64
- await expect ( httpRequest ( { host , path, port } ) ) . rejects . toStrictEqual (
76
+ await expect ( httpRequest ( { hostname , path, port } ) ) . rejects . toStrictEqual (
65
77
Object . assign ( new ProviderError ( "Error response received from instance metadata service" ) , { statusCode } )
66
78
) ;
67
79
expect ( requestSpy . mock . results [ 0 ] . value . socket ) . toHaveProperty ( "destroyed" , true ) ;
@@ -71,9 +83,9 @@ describe("httpRequest", () => {
71
83
} ;
72
84
73
85
it ( "when request throws error" , async ( ) => {
74
- const scope = nock ( `http://${ host } :${ port } ` ) . get ( path ) . replyWithError ( "error" ) ;
86
+ const scope = nock ( `http://${ hostname } :${ port } ` ) . get ( path ) . replyWithError ( "error" ) ;
75
87
76
- await expect ( httpRequest ( { host , path, port } ) ) . rejects . toStrictEqual (
88
+ await expect ( httpRequest ( { hostname , path, port } ) ) . rejects . toStrictEqual (
77
89
new ProviderError ( "Unable to connect to instance metadata service" )
78
90
) ;
79
91
expect ( requestSpy . mock . results [ 0 ] . value . socket ) . toHaveProperty ( "destroyed" , true ) ;
@@ -92,12 +104,12 @@ describe("httpRequest", () => {
92
104
93
105
it ( "timeout" , async ( ) => {
94
106
const timeout = 1000 ;
95
- const scope = nock ( `http://${ host } :${ port } ` )
107
+ const scope = nock ( `http://${ hostname } :${ port } ` )
96
108
. get ( path )
97
109
. delay ( timeout * 2 )
98
110
. reply ( 200 , "expectedResponse" ) ;
99
111
100
- await expect ( httpRequest ( { host , path, port, timeout } ) ) . rejects . toStrictEqual (
112
+ await expect ( httpRequest ( { hostname , path, port, timeout } ) ) . rejects . toStrictEqual (
101
113
new ProviderError ( "TimeoutError from instance metadata service" )
102
114
) ;
103
115
expect ( requestSpy . mock . results [ 0 ] . value . socket ) . toHaveProperty ( "destroyed" , true ) ;
0 commit comments