@@ -7,8 +7,6 @@ import { expect, fetch, ignoreNetworkError, nodeOnly } from "./test-utils";
7
7
8
8
import { CA , generateCACertificate } from '../src/util/tls' ;
9
9
10
- const nodeMajorVersion = parseInt ( process . versions . node . split ( '.' ) [ 0 ] , 10 ) ;
11
-
12
10
nodeOnly ( ( ) => {
13
11
describe ( "Certificate generation" , ( ) => {
14
12
const caKey = fs . readFile ( path . join ( __dirname , 'fixtures' , 'test-ca.key' ) , 'utf8' ) ;
@@ -35,6 +33,21 @@ nodeOnly(() => {
35
33
let constrainedCA : CA ;
36
34
let constrainedCaCert : string ;
37
35
36
+ function localhostRequest ( { hostname, port } : { hostname : string ; port : number } ) {
37
+ return https . request ( {
38
+ hostname,
39
+ port,
40
+ ca : [ constrainedCaCert ] ,
41
+ lookup : ( _ , options , callback ) => {
42
+ if ( options . all ) {
43
+ callback ( null , [ { address : "127.0.0.1" , family : 4 } ] ) ;
44
+ } else {
45
+ callback ( null , "127.0.0.1" , 4 ) ;
46
+ }
47
+ } ,
48
+ } ) ;
49
+ }
50
+
38
51
beforeEach ( async ( ) => {
39
52
const rootCa = await generateCACertificate ( {
40
53
nameConstraints : { permitted : [ "example.com" ] } ,
@@ -53,28 +66,18 @@ nodeOnly(() => {
53
66
} ) ;
54
67
await new Promise < void > ( ( resolve ) => server . listen ( 4430 , resolve ) ) ;
55
68
56
- await new Promise < void > ( ( resolve ) => {
57
- const req = https . request (
58
- {
59
- hostname : "hello.example.com" ,
60
- port : 4430 ,
61
- ca : [ constrainedCaCert ] ,
62
- lookup : ( hostname , options , callback ) => {
63
- if ( nodeMajorVersion <= 18 ) {
64
- callback ( null , "127.0.0.1" , 4 ) ;
65
- } else {
66
- callback ( null , [ { address : "127.0.0.1" , family : 4 } ] ) ;
67
- }
68
- } ,
69
- } ,
70
- ( res ) => {
71
- expect ( res . statusCode ) . to . equal ( 200 ) ;
72
- res . on ( "data" , ( data ) => {
73
- expect ( data . toString ( ) ) . to . equal ( "signed response!" ) ;
74
- resolve ( ) ;
75
- } ) ;
76
- }
77
- ) ;
69
+ const req = localhostRequest ( { hostname : "hello.example.com" , port : 4430 } ) ;
70
+ return new Promise < void > ( ( resolve , reject ) => {
71
+ req . on ( "response" , ( res ) => {
72
+ expect ( res . statusCode ) . to . equal ( 200 ) ;
73
+ res . on ( "data" , ( data ) => {
74
+ expect ( data . toString ( ) ) . to . equal ( "signed response!" ) ;
75
+ resolve ( ) ;
76
+ } ) ;
77
+ } ) ;
78
+ req . on ( "error" , ( err ) => {
79
+ reject ( err ) ;
80
+ } ) ;
78
81
req . end ( ) ;
79
82
} ) ;
80
83
@@ -89,25 +92,15 @@ nodeOnly(() => {
89
92
} ) ;
90
93
await new Promise < void > ( ( resolve ) => server . listen ( 4430 , resolve ) ) ;
91
94
92
- await new Promise < void > ( ( resolve ) => {
93
- const req = https . request (
94
- {
95
- hostname : "hello.other.com" ,
96
- port : 4430 ,
97
- ca : [ constrainedCaCert ] ,
98
- lookup : ( hostname , options , callback ) => {
99
- if ( nodeMajorVersion <= 18 ) {
100
- callback ( null , "127.0.0.1" , 4 ) ;
101
- } else {
102
- callback ( null , [ { address : "127.0.0.1" , family : 4 } ] ) ;
103
- }
104
- } ,
105
- } ,
106
- ) ;
95
+ const req = localhostRequest ( { hostname : "hello.other.com" , port : 4430 } ) ;
96
+ return new Promise < void > ( ( resolve , reject ) => {
107
97
req . on ( "error" , ( err ) => {
108
98
expect ( err . message ) . to . equal ( "permitted subtree violation" ) ;
109
99
resolve ( ) ;
110
- } )
100
+ } ) ;
101
+ req . on ( "response" , ( res ) => {
102
+ expect . fail ( "Unexpected response received" ) ;
103
+ } ) ;
111
104
req . end ( ) ;
112
105
} ) ;
113
106
} ) ;
0 commit comments