@@ -73,31 +73,49 @@ describe("Multipart", () => {
73
73
} ) ;
74
74
75
75
describe ( "when a file does not exist" , ( ) => {
76
- it ( "should emit an error" , done => {
76
+ it ( "should fail the request with an error" , done => {
77
77
const req = request . post ( `${ base } /echo` ) ;
78
78
79
79
req . attach ( "name" , "foo" ) ;
80
80
req . attach ( "name2" , "bar" ) ;
81
81
req . attach ( "name3" , "baz" ) ;
82
82
83
- req . on ( "error" , err => {
83
+ req . end ( ( err , res ) => {
84
+ assert . ok ( ! ! err , "Request should have failed." ) ;
85
+ err . code . should . equal ( "ENOENT" ) ;
84
86
err . message . should . containEql ( "ENOENT" ) ;
85
87
err . path . should . equal ( "foo" ) ;
86
88
done ( ) ;
87
89
} ) ;
88
-
89
- req . end ( ( err , res ) => {
90
- if ( err ) return done ( err ) ;
91
- assert ( 0 , "end() was called" ) ;
92
- } ) ;
93
90
} ) ;
94
91
95
92
it ( "promise should fail" , ( ) => {
96
- request . post ( 'nevermind' )
97
- . field ( { a :1 , b :2 } )
98
- . attach ( 'c' , 'does-not-exist.txt' )
99
- . then ( ( ) => assert . fail ( "It should not allow this" ) )
100
- . catch ( ( ) => true ) ;
93
+ return request . post ( `${ base } /echo` )
94
+ . field ( { a :1 , b :2 } )
95
+ . attach ( 'c' , 'does-not-exist.txt' )
96
+ . then (
97
+ res => assert . fail ( "It should not allow this" ) ,
98
+ err => {
99
+ err . code . should . equal ( "ENOENT" ) ;
100
+ err . path . should . equal ( "does-not-exist.txt" ) ;
101
+ } ) ;
102
+ } ) ;
103
+
104
+ it ( "should report ECONNREFUSED via the callback" , done => {
105
+ request . post ( 'http://127.0.0.1:1' ) // nobody is listening there
106
+ . attach ( "name" , "file-does-not-exist" )
107
+ . end ( ( err , res ) => {
108
+ assert . ok ( ! ! err , "Request should have failed" ) ;
109
+ err . code . should . equal ( "ECONNREFUSED" ) ;
110
+ done ( ) ;
111
+ } ) ;
112
+ } ) ;
113
+ it ( "should report ECONNREFUSED via Promise" , ( ) => {
114
+ return request . post ( 'http://127.0.0.1:1' ) // nobody is listening there
115
+ . attach ( "name" , "file-does-not-exist" )
116
+ . then (
117
+ res => assert . fail ( "Request should have failed" ) ,
118
+ err => err . code . should . equal ( "ECONNREFUSED" ) ) ;
101
119
} ) ;
102
120
} ) ;
103
121
} ) ;
@@ -143,13 +161,11 @@ describe("Multipart", () => {
143
161
request
144
162
. post ( `${ base } /echo` )
145
163
. attach ( "filedata" , "test/node/fixtures/non-existent-file.ext" )
146
- . on ( "error" , err => {
164
+ . end ( ( err , res ) => {
165
+ assert . ok ( ! ! err , "Request should have failed." ) ;
147
166
err . code . should . equal ( "ENOENT" ) ;
148
167
err . path . should . equal ( "test/node/fixtures/non-existent-file.ext" ) ;
149
168
done ( ) ;
150
- } )
151
- . end ( ( err , res ) => {
152
- done ( new Error ( "Request should have been aborted earlier!" ) ) ;
153
169
} ) ;
154
170
} ) ;
155
171
} ) ;
0 commit comments