@@ -25,32 +25,36 @@ const http = require('http');
25
25
const net = require ( 'net' ) ;
26
26
const assert = require ( 'assert' ) ;
27
27
28
+ function commonHttpGet ( fn ) {
29
+ if ( typeof fn === 'function' ) {
30
+ fn = common . mustCall ( fn ) ;
31
+ }
32
+ return new Promise ( ( resolve , reject ) => {
33
+ http . get ( { createConnection : fn } , ( res ) => {
34
+ resolve ( res ) ;
35
+ } ) . on ( 'error' , ( err ) => {
36
+ reject ( err ) ;
37
+ } ) ;
38
+ } ) ;
39
+ }
40
+
28
41
const server = http . createServer ( common . mustCall ( function ( req , res ) {
29
42
res . end ( ) ;
30
- } , 4 ) ) . listen ( 0 , '127.0.0.1' , function ( ) {
31
- let fn = common . mustCall ( createConnection ) ;
32
- http . get ( { createConnection : fn } , function ( res ) {
33
- res . resume ( ) ;
34
- fn = common . mustCall ( createConnectionAsync ) ;
35
- http . get ( { createConnection : fn } , function ( res ) {
36
- res . resume ( ) ;
37
- fn = common . mustCall ( createConnectionBoth1 ) ;
38
- http . get ( { createConnection : fn } , function ( res ) {
39
- res . resume ( ) ;
40
- fn = common . mustCall ( createConnectionBoth2 ) ;
41
- http . get ( { createConnection : fn } , function ( res ) {
42
- res . resume ( ) ;
43
- fn = common . mustCall ( createConnectionError ) ;
44
- http . get ( { createConnection : fn } , function ( res ) {
45
- assert . fail ( 'Unexpected response callback' ) ;
46
- } ) . on ( 'error' , common . mustCall ( function ( err ) {
47
- assert . strictEqual ( err . message , 'Could not create socket' ) ;
48
- server . close ( ) ;
49
- } ) ) ;
50
- } ) ;
51
- } ) ;
52
- } ) ;
43
+ } , 4 ) ) . listen ( 0 , '127.0.0.1' , async ( ) => {
44
+ await commonHttpGet ( createConnection ) ;
45
+ await commonHttpGet ( createConnectionAsync ) ;
46
+ await commonHttpGet ( createConnectionBoth1 ) ;
47
+ await commonHttpGet ( createConnectionBoth2 ) ;
48
+
49
+ // Errors
50
+ await assert . rejects ( ( ) => commonHttpGet ( createConnectionError ) , {
51
+ message : 'sync'
52
+ } ) ;
53
+ await assert . rejects ( ( ) => commonHttpGet ( createConnectionAsyncError ) , {
54
+ message : 'async'
53
55
} ) ;
56
+
57
+ server . close ( ) ;
54
58
} ) ;
55
59
56
60
function createConnection ( ) {
@@ -78,5 +82,9 @@ function createConnectionBoth2(options, cb) {
78
82
}
79
83
80
84
function createConnectionError ( options , cb ) {
81
- process . nextTick ( cb , new Error ( 'Could not create socket' ) ) ;
85
+ throw new Error ( 'sync' ) ;
86
+ }
87
+
88
+ function createConnectionAsyncError ( options , cb ) {
89
+ process . nextTick ( cb , new Error ( 'async' ) ) ;
82
90
}
0 commit comments