@@ -17,11 +17,16 @@ const {
1717 kMockAgentAddCallHistoryLog,
1818 kMockAgentMockCallHistoryInstance,
1919 kMockAgentAcceptsNonStandardSearchParameters,
20- kMockCallHistoryAddLog
20+ kMockCallHistoryAddLog,
21+ kIgnoreTrailingSlash
2122} = require ( './mock-symbols' )
2223const MockClient = require ( './mock-client' )
2324const MockPool = require ( './mock-pool' )
24- const { matchValue, normalizeSearchParams, buildAndValidateMockOptions } = require ( './mock-utils' )
25+ const {
26+ matchValue,
27+ normalizeSearchParams,
28+ buildAndValidateMockOptions
29+ } = require ( './mock-utils' )
2530const { InvalidArgumentError, UndiciError } = require ( '../core/errors' )
2631const Dispatcher = require ( '../dispatcher/dispatcher' )
2732const PendingInterceptorsFormatter = require ( './pending-interceptors-formatter' )
@@ -35,12 +40,17 @@ class MockAgent extends Dispatcher {
3540
3641 this [ kNetConnect ] = true
3742 this [ kIsMockActive ] = true
38- this [ kMockAgentIsCallHistoryEnabled ] = mockOptions ?. enableCallHistory ?? false
39- this [ kMockAgentAcceptsNonStandardSearchParameters ] = mockOptions ?. acceptNonStandardSearchParameters ?? false
43+ this [ kMockAgentIsCallHistoryEnabled ] =
44+ mockOptions ?. enableCallHistory ?? false
45+ this [ kMockAgentAcceptsNonStandardSearchParameters ] =
46+ mockOptions ?. acceptNonStandardSearchParameters ?? false
47+ this [ kIgnoreTrailingSlash ] = mockOptions ?. ignoreTrailingSlash ?? false
4048
4149 // Instantiate Agent and encapsulate
4250 if ( opts ?. agent && typeof opts . agent . dispatch !== 'function' ) {
43- throw new InvalidArgumentError ( 'Argument opts.agent must implement Agent' )
51+ throw new InvalidArgumentError (
52+ 'Argument opts.agent must implement Agent'
53+ )
4454 }
4555 const agent = opts ?. agent ? opts . agent : new Agent ( opts )
4656 this [ kAgent ] = agent
@@ -54,11 +64,15 @@ class MockAgent extends Dispatcher {
5464 }
5565
5666 get ( origin ) {
57- let dispatcher = this [ kMockAgentGet ] ( origin )
67+ const originKey = this [ kIgnoreTrailingSlash ]
68+ ? origin . replace ( / \/ $ / , '' )
69+ : origin
70+
71+ let dispatcher = this [ kMockAgentGet ] ( originKey )
5872
5973 if ( ! dispatcher ) {
60- dispatcher = this [ kFactory ] ( origin )
61- this [ kMockAgentSet ] ( origin , dispatcher )
74+ dispatcher = this [ kFactory ] ( originKey )
75+ this [ kMockAgentSet ] ( originKey , dispatcher )
6276 }
6377 return dispatcher
6478 }
@@ -69,13 +83,17 @@ class MockAgent extends Dispatcher {
6983
7084 this [ kMockAgentAddCallHistoryLog ] ( opts )
7185
72- const acceptNonStandardSearchParameters = this [ kMockAgentAcceptsNonStandardSearchParameters ]
86+ const acceptNonStandardSearchParameters =
87+ this [ kMockAgentAcceptsNonStandardSearchParameters ]
7388
7489 const dispatchOpts = { ...opts }
7590
7691 if ( acceptNonStandardSearchParameters && dispatchOpts . path ) {
7792 const [ path , searchParams ] = dispatchOpts . path . split ( '?' )
78- const normalizedSearchParams = normalizeSearchParams ( searchParams , acceptNonStandardSearchParameters )
93+ const normalizedSearchParams = normalizeSearchParams (
94+ searchParams ,
95+ acceptNonStandardSearchParameters
96+ )
7997 dispatchOpts . path = `${ path } ?${ normalizedSearchParams } `
8098 }
8199
@@ -97,7 +115,11 @@ class MockAgent extends Dispatcher {
97115 }
98116
99117 enableNetConnect ( matcher ) {
100- if ( typeof matcher === 'string' || typeof matcher === 'function' || matcher instanceof RegExp ) {
118+ if (
119+ typeof matcher === 'string' ||
120+ typeof matcher === 'function' ||
121+ matcher instanceof RegExp
122+ ) {
101123 if ( Array . isArray ( this [ kNetConnect ] ) ) {
102124 this [ kNetConnect ] . push ( matcher )
103125 } else {
@@ -106,7 +128,9 @@ class MockAgent extends Dispatcher {
106128 } else if ( typeof matcher === 'undefined' ) {
107129 this [ kNetConnect ] = true
108130 } else {
109- throw new InvalidArgumentError ( 'Unsupported matcher. Must be one of String|Function|RegExp.' )
131+ throw new InvalidArgumentError (
132+ 'Unsupported matcher. Must be one of String|Function|RegExp.'
133+ )
110134 }
111135 }
112136
@@ -185,7 +209,11 @@ class MockAgent extends Dispatcher {
185209
186210 // If we match, create a pool and assign the same dispatches
187211 for ( const [ keyMatcher , result ] of Array . from ( this [ kClients ] ) ) {
188- if ( result && typeof keyMatcher !== 'string' && matchValue ( keyMatcher , origin ) ) {
212+ if (
213+ result &&
214+ typeof keyMatcher !== 'string' &&
215+ matchValue ( keyMatcher , origin )
216+ ) {
189217 const dispatcher = this [ kFactory ] ( origin )
190218 this [ kMockAgentSet ] ( origin , dispatcher )
191219 dispatcher [ kDispatches ] = result . dispatcher [ kDispatches ]
@@ -202,11 +230,18 @@ class MockAgent extends Dispatcher {
202230 const mockAgentClients = this [ kClients ]
203231
204232 return Array . from ( mockAgentClients . entries ( ) )
205- . flatMap ( ( [ origin , result ] ) => result . dispatcher [ kDispatches ] . map ( dispatch => ( { ...dispatch , origin } ) ) )
233+ . flatMap ( ( [ origin , result ] ) =>
234+ result . dispatcher [ kDispatches ] . map ( ( dispatch ) => ( {
235+ ...dispatch ,
236+ origin
237+ } ) )
238+ )
206239 . filter ( ( { pending } ) => pending )
207240 }
208241
209- assertNoPendingInterceptors ( { pendingInterceptorsFormatter = new PendingInterceptorsFormatter ( ) } = { } ) {
242+ assertNoPendingInterceptors ( {
243+ pendingInterceptorsFormatter = new PendingInterceptorsFormatter ( )
244+ } = { } ) {
210245 const pending = this . pendingInterceptors ( )
211246
212247 if ( pending . length === 0 ) {
0 commit comments