File tree 2 files changed +43
-1
lines changed
packages/node-http-handler/src
2 files changed +43
-1
lines changed Original file line number Diff line number Diff line change
1
+ import EventEmitter from "events" ;
2
+
3
+ import { writeRequestBody } from "./write-request-body" ;
4
+
5
+ describe ( writeRequestBody . name , ( ) => {
6
+ it ( "should continue on the continue event" , async ( ) => {
7
+ const emitter = Object . assign ( new EventEmitter ( ) , { end ( ) { } } ) as any ;
8
+ const request = {
9
+ headers : { expect : "100-continue" } ,
10
+ body : Buffer . from ( "" ) ,
11
+ end ( ) { } ,
12
+ } as any ;
13
+
14
+ const promise = writeRequestBody ( emitter , request , 10_000 ) ;
15
+ emitter . emit ( "continue" , "ok" ) ;
16
+ await promise ;
17
+ } ) ;
18
+
19
+ it ( "should continue on the error event" , async ( ) => {
20
+ const emitter = Object . assign ( new EventEmitter ( ) , { end ( ) { } } ) as any ;
21
+ const request = {
22
+ headers : { expect : "100-continue" } ,
23
+ body : Buffer . from ( "" ) ,
24
+ } as any ;
25
+
26
+ const promise = writeRequestBody ( emitter , request , 10_000 ) ;
27
+ emitter . emit ( "error" , "uh oh" ) ;
28
+ await promise ;
29
+ } ) ;
30
+ } ) ;
Original file line number Diff line number Diff line change @@ -21,6 +21,7 @@ export async function writeRequestBody(
21
21
const expect = headers [ "Expect" ] || headers [ "expect" ] ;
22
22
23
23
let timeoutId = - 1 ;
24
+ let hasError = false ;
24
25
25
26
if ( expect === "100-continue" ) {
26
27
await Promise . race < void > ( [
@@ -32,11 +33,22 @@ export async function writeRequestBody(
32
33
clearTimeout ( timeoutId ) ;
33
34
resolve ( ) ;
34
35
} ) ;
36
+ httpRequest . on ( "error" , ( ) => {
37
+ hasError = true ;
38
+ clearTimeout ( timeoutId ) ;
39
+ // this handler does not reject with the error
40
+ // because there is already an error listener
41
+ // on the request in node-http-handler
42
+ // and node-http2-handler.
43
+ resolve ( ) ;
44
+ } ) ;
35
45
} ) ,
36
46
] ) ;
37
47
}
38
48
39
- writeBody ( httpRequest , request . body ) ;
49
+ if ( ! hasError ) {
50
+ writeBody ( httpRequest , request . body ) ;
51
+ }
40
52
}
41
53
42
54
function writeBody (
You can’t perform that action at this time.
0 commit comments