9
9
kContentLength,
10
10
kMockDispatch
11
11
} = require ( './mock-symbols' )
12
- const { InvalidArgumentError, InvalidReturnValueError } = require ( '../core/errors' )
12
+ const { InvalidArgumentError } = require ( '../core/errors' )
13
13
14
14
/**
15
15
* Defines the scope API for a interceptor reply
@@ -66,6 +66,14 @@ class MockInterceptor {
66
66
if ( typeof opts . method === 'undefined' ) {
67
67
throw new InvalidArgumentError ( 'opts.method must be defined' )
68
68
}
69
+ // See https://github.com/nodejs/undici/issues/1245
70
+ // As per RFC 3986, clients are not supposed to send URI
71
+ // fragments to servers when they retrieve a document,
72
+ if ( typeof opts . path === 'string' ) {
73
+ // Matches https://github.com/nodejs/undici/blob/main/lib/fetch/index.js#L1811
74
+ const parsedURL = new URL ( opts . path , 'data://' )
75
+ opts . path = parsedURL . pathname + parsedURL . search
76
+ }
69
77
70
78
this [ kDispatchKey ] = buildKey ( opts )
71
79
this [ kDispatches ] = mockDispatches
@@ -74,16 +82,16 @@ class MockInterceptor {
74
82
this [ kContentLength ] = false
75
83
}
76
84
77
- createMockScopeDispatchData ( statusCode , data , responseOptions = { } ) {
85
+ createMockScopeDispatchData ( statusCode , data , responseOptions = { } ) {
78
86
const responseData = getResponseData ( data )
79
87
const contentLength = this [ kContentLength ] ? { 'content-length' : responseData . length } : { }
80
88
const headers = { ...this [ kDefaultHeaders ] , ...contentLength , ...responseOptions . headers }
81
89
const trailers = { ...this [ kDefaultTrailers ] , ...responseOptions . trailers }
82
90
83
- return { statusCode, data, headers, trailers } ;
91
+ return { statusCode, data, headers, trailers }
84
92
}
85
93
86
- validateReplyParameters ( statusCode , data , responseOptions ) {
94
+ validateReplyParameters ( statusCode , data , responseOptions ) {
87
95
if ( typeof statusCode === 'undefined' ) {
88
96
throw new InvalidArgumentError ( 'statusCode must be defined' )
89
97
}
@@ -107,39 +115,38 @@ class MockInterceptor {
107
115
// when invoked.
108
116
const wrappedDefaultsCallback = ( opts ) => {
109
117
// Our reply options callback contains the parameter for statusCode, data and options.
110
- const resolvedData = replyData ( opts ) ;
118
+ const resolvedData = replyData ( opts )
111
119
112
120
// Check if it is in the right format
113
121
if ( typeof resolvedData !== 'object' ) {
114
122
throw new InvalidArgumentError ( 'reply options callback must return an object' )
115
123
}
116
124
117
- const { statusCode, data, responseOptions = { } } = resolvedData ;
118
- this . validateReplyParameters ( statusCode , data , responseOptions ) ;
125
+ const { statusCode, data, responseOptions = { } } = resolvedData
126
+ this . validateReplyParameters ( statusCode , data , responseOptions )
119
127
// Since the values can be obtained immediately we return them
120
128
// from this higher order function that will be resolved later.
121
- return {
129
+ return {
122
130
...this . createMockScopeDispatchData ( statusCode , data , responseOptions )
123
131
}
124
132
}
125
133
126
134
// Add usual dispatch data, but this time set the data parameter to function that will eventually provide data.
127
135
const newMockDispatch = addMockDispatch ( this [ kDispatches ] , this [ kDispatchKey ] , wrappedDefaultsCallback )
128
- return new MockScope ( newMockDispatch ) ;
136
+ return new MockScope ( newMockDispatch )
129
137
}
130
138
131
139
// We can have either one or three parameters, if we get here,
132
140
// we should have 2-3 parameters. So we spread the arguments of
133
141
// this function to obtain the parameters, since replyData will always
134
- // just be the statusCode.
135
- const [ statusCode , data , responseOptions = { } ] = [ ...arguments ] ;
136
- this . validateReplyParameters ( statusCode , data , responseOptions ) ;
142
+ // just be the statusCode.
143
+ const [ statusCode , data , responseOptions = { } ] = [ ...arguments ]
144
+ this . validateReplyParameters ( statusCode , data , responseOptions )
137
145
138
146
// Send in-already provided data like usual
139
- const dispatchData = this . createMockScopeDispatchData ( statusCode , data , responseOptions ) ;
147
+ const dispatchData = this . createMockScopeDispatchData ( statusCode , data , responseOptions )
140
148
const newMockDispatch = addMockDispatch ( this [ kDispatches ] , this [ kDispatchKey ] , dispatchData )
141
149
return new MockScope ( newMockDispatch )
142
-
143
150
}
144
151
145
152
/**
0 commit comments