@@ -35,14 +35,13 @@ export function unpatchHttp() {
35
35
function patchMethod ( mod : typeof http | typeof https , method : "get" | "request" , contextService : TraceContextService ) {
36
36
shimmer . wrap ( mod , method , ( original ) => {
37
37
const fn = ( arg1 : any , arg2 : any , arg3 : any ) => {
38
- const { options, callback } = normalizeArgs ( arg1 , arg2 , arg3 ) ;
39
- const requestOpts = getRequestOptionsWithTraceContext ( options , contextService ) ;
38
+ [ arg1 , arg2 , arg3 ] = addTraceContextToArgs ( contextService , arg1 , arg2 , arg3 ) ;
40
39
41
- if ( isIntegrationTest ( ) ) {
42
- _logHttpRequest ( requestOpts ) ;
40
+ if ( arg3 === undefined || arg3 === null ) {
41
+ return original ( arg1 , arg2 ) ;
42
+ } else {
43
+ return original ( arg1 , arg2 , arg3 ) ;
43
44
}
44
-
45
- return original ( requestOpts , callback ) ;
46
45
} ;
47
46
return fn as any ;
48
47
} ) ;
@@ -54,23 +53,37 @@ function unpatchMethod(mod: typeof http | typeof https, method: "get" | "request
54
53
}
55
54
56
55
/**
57
- * The input into the http.request function has 6 different overloads. This method normalized the inputs
58
- * into a consistent format.
56
+ * Finds the RequestOptions in the args and injects context into headers
59
57
*/
60
- function normalizeArgs (
58
+ function addTraceContextToArgs (
59
+ contextService : TraceContextService ,
61
60
arg1 : string | URL | http . RequestOptions ,
62
61
arg2 ?: RequestCallback | http . RequestOptions ,
63
62
arg3 ?: RequestCallback ,
64
63
) {
65
- let options : http . RequestOptions = typeof arg1 === "string" ? parse ( arg1 ) : { ...arg1 } ;
66
- options . headers = options . headers || { } ;
67
- let callback = arg3 ;
68
- if ( typeof arg2 === "function" ) {
69
- callback = arg2 ;
70
- } else if ( typeof arg2 === "object" ) {
71
- options = { ...options , ...arg2 } ;
64
+ let requestOpts : http . RequestOptions | undefined ;
65
+ if ( typeof arg1 === "string" || arg1 instanceof URL ) {
66
+ if ( arg2 === undefined || arg2 === null ) {
67
+ requestOpts = {
68
+ method : "GET" ,
69
+ } ;
70
+ requestOpts = getRequestOptionsWithTraceContext ( requestOpts , contextService ) ;
71
+ return [ arg1 , requestOpts , arg3 ] ;
72
+ } else if ( typeof arg2 === "function" ) {
73
+ requestOpts = {
74
+ method : "GET" ,
75
+ } ;
76
+ requestOpts = getRequestOptionsWithTraceContext ( requestOpts , contextService ) ;
77
+ return [ arg1 , requestOpts , arg2 ] ;
78
+ } else {
79
+ requestOpts = arg2 as http . RequestOptions ;
80
+ requestOpts = getRequestOptionsWithTraceContext ( requestOpts , contextService ) ;
81
+ return [ arg1 , requestOpts , arg3 ] ;
82
+ }
83
+ } else {
84
+ requestOpts = getRequestOptionsWithTraceContext ( arg1 , contextService ) ;
85
+ return [ requestOpts , arg2 , arg3 ] ;
72
86
}
73
- return { options, callback } ;
74
87
}
75
88
76
89
function getRequestOptionsWithTraceContext (
@@ -86,10 +99,16 @@ function getRequestOptionsWithTraceContext(
86
99
...headers ,
87
100
...traceHeaders ,
88
101
} ;
89
- return {
102
+ const requestOpts = {
90
103
...options ,
91
104
headers,
92
105
} ;
106
+ // Logging all http requests during integration tests let's
107
+ // us track traffic in our test snapshots
108
+ if ( isIntegrationTest ( ) ) {
109
+ _logHttpRequest ( requestOpts ) ;
110
+ }
111
+ return requestOpts ;
93
112
}
94
113
95
114
function isIntegrationTest ( ) {
0 commit comments