@@ -25,32 +25,36 @@ const http = require('http');
2525const net = require ( 'net' ) ;
2626const assert = require ( 'assert' ) ;
2727
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+
2841const server = http . createServer ( common . mustCall ( function ( req , res ) {
2942 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'
5355 } ) ;
56+
57+ server . close ( ) ;
5458} ) ;
5559
5660function createConnection ( ) {
@@ -78,5 +82,9 @@ function createConnectionBoth2(options, cb) {
7882}
7983
8084function 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' ) ) ;
8290}
0 commit comments