@@ -106,7 +106,7 @@ export function executeScenario(testCase: RetryTestCase) {
106106
107107 describe ( `${ storageMethodString } ` , async ( ) => {
108108 beforeEach ( async ( ) => {
109- const rawStorageTransport = new StorageTransport ( {
109+ const rawTransport = new StorageTransport ( {
110110 apiEndpoint : TESTBENCH_HOST ,
111111 authClient : authClient ,
112112 keyFilename : SERVICE_ACCOUNT ,
@@ -131,65 +131,14 @@ export function executeScenario(testCase: RetryTestCase) {
131131 creationResult = await createTestBenchRetryTest (
132132 instructionSet . instructions ,
133133 jsonMethod ?. name . toString ( ) ,
134- rawStorageTransport ,
134+ rawTransport ,
135135 ) ;
136- if ( ! creationResult || ! creationResult . id ) {
137- throw new Error ( 'Failed to get a valid test ID from test bench.' ) ;
138- }
139-
140- /* // eslint-disable-next-line @typescript-eslint/no-explicit-any
141- const internalGaxios = (storageTransport as any).authClient
142- ?.gaxiosInstance;
143-
144- if (internalGaxios) {
145- // eslint-disable-next-line @typescript -eslint/no-explicit-any
146- internalGaxios.interceptors.request.use((config: any) => {
147- config.headers = config.headers || {};
148- config.headers['x-retry-test-id'] = creationResult.id;
149- return config;
150- });
151- } */
152136
153137 // Create a Proxy around rawStorageTransport to intercept makeRequest
154- storageTransport = new Proxy ( rawStorageTransport , {
155- get ( target , prop , receiver ) {
156- if ( prop === 'makeRequest' ) {
157- return async < T > (
158- reqOpts : StorageRequestOptions ,
159- callback ?: StorageTransportCallback < T > ,
160- ) : Promise < void | T > => {
161- const config = reqOpts ;
162- config . headers = config . headers || { } ;
163-
164- if ( creationResult && creationResult . id ) {
165- const retryId = creationResult . id ;
166- if ( config . headers instanceof Headers ) {
167- config . headers . set ( 'x-retry-test-id' , retryId ) ;
168- } else if (
169- typeof config . headers === 'object' &&
170- config . headers !== null &&
171- ! Array . isArray ( config . headers )
172- ) {
173- config . headers = {
174- ...( config . headers as {
175- [ key : string ] : string | string [ ] ;
176- } ) ,
177- 'x-retry-test-id' : retryId ,
178- } ;
179- } else {
180- config . headers = { 'x-retry-test-id' : retryId } ;
181- }
182- }
183- return Reflect . apply (
184- rawStorageTransport . makeRequest ,
185- rawStorageTransport ,
186- [ config , callback ] ,
187- ) ;
188- } ;
189- }
190- return Reflect . get ( target , prop , receiver ) ;
191- } ,
192- } ) ;
138+ storageTransport = createRetryProxy (
139+ rawTransport ,
140+ creationResult . id ,
141+ ) ;
193142
194143 storage = new Storage ( {
195144 apiEndpoint : TESTBENCH_HOST ,
@@ -222,12 +171,12 @@ export function executeScenario(testCase: RetryTestCase) {
222171
223172 it ( `${ instructionNumber } ` , async ( ) => {
224173 const methodParameters : libraryMethods . ConformanceTestOptions = {
225- storage : storage ,
226- bucket : bucket ,
227- file : file ,
228- storageTransport : storageTransport ,
229- notification : notification ,
230- hmacKey : hmacKey ,
174+ storage,
175+ bucket,
176+ file,
177+ storageTransport,
178+ notification,
179+ hmacKey,
231180 projectId : CONF_TEST_PROJECT_ID ,
232181 preconditionRequired : testCase . preconditionProvided ,
233182 } ;
@@ -251,85 +200,104 @@ export function executeScenario(testCase: RetryTestCase) {
251200 }
252201}
253202
203+ /**
204+ * Creates a Proxy to automatically inject x-retry-test-id into all requests
205+ */
206+ function createRetryProxy (
207+ transport : StorageTransport ,
208+ retryId : string ,
209+ ) : StorageTransport {
210+ return new Proxy ( transport , {
211+ get ( target , prop , receiver ) {
212+ const original = Reflect . get ( target , prop , receiver ) ;
213+ if ( prop === 'makeRequest' && typeof original === 'function' ) {
214+ return async (
215+ reqOpts : StorageRequestOptions ,
216+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
217+ callback ?: StorageTransportCallback < any > ,
218+ ) => {
219+ reqOpts . headers = reqOpts . headers || { } ;
220+
221+ if ( reqOpts . headers instanceof Headers ) {
222+ reqOpts . headers . set ( 'x-retry-test-id' , retryId ) ;
223+ } else {
224+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
225+ ( reqOpts . headers as any ) [ 'x-retry-test-id' ] = retryId ;
226+ }
227+
228+ return original . apply ( target , [ reqOpts , callback ] ) ;
229+ } ;
230+ }
231+ return original ;
232+ } ,
233+ } ) ;
234+ }
235+
254236async function createBucketForTest (
255237 storage : Storage ,
256- preconditionShouldBeOnInstance : boolean ,
257- storageMethodString : String ,
238+ withPrecondition : boolean ,
239+ method : String ,
258240) {
259- const name = generateName ( storageMethodString , 'bucket' ) ;
260- const bucket = storage . bucket ( name ) ;
261- await bucket . create ( ) ;
241+ const bucket = storage . bucket ( generateName ( method , 'bucket' ) ) ;
242+ const [ metadata ] = await bucket . create ( ) ;
262243 await bucket . setRetentionPeriod ( DURATION_SECONDS ) ;
263-
264- if ( preconditionShouldBeOnInstance ) {
244+ if ( withPrecondition ) {
265245 return new Bucket ( storage , bucket . name , {
266246 preconditionOpts : {
267- ifMetagenerationMatch : 2 ,
247+ ifMetagenerationMatch : metadata . metageneration ,
268248 } ,
269249 } ) ;
270250 }
271251 return bucket ;
272252}
273253
274254async function createFileForTest (
275- preconditionShouldBeOnInstance : boolean ,
276- storageMethodString : String ,
255+ withPrecondition : boolean ,
256+ method : String ,
277257 bucket : Bucket ,
278258) {
279- const name = generateName ( storageMethodString , 'file' ) ;
280- const file = bucket . file ( name ) ;
281- await file . save ( name ) ;
282- if ( preconditionShouldBeOnInstance ) {
283- await file . getMetadata ( ) ;
259+ const file = bucket . file ( generateName ( method , 'file' ) ) ;
260+ await file . save ( 'test-content' ) ;
261+ if ( withPrecondition ) {
262+ const [ metadata ] = await file . getMetadata ( ) ;
284263 return new File ( bucket , file . name , {
285264 preconditionOpts : {
286- ifMetagenerationMatch : file . metadata . metageneration ,
287- ifGenerationMatch : file . metadata . generation ,
265+ ifMetagenerationMatch : metadata . metageneration ,
266+ ifGenerationMatch : metadata . generation ,
288267 } ,
289268 } ) ;
290269 }
291270 return file ;
292271}
293272
294- function generateName ( storageMethodString : String , bucketOrFile : string ) {
295- return `${ TESTS_PREFIX } ${ storageMethodString . toLowerCase ( ) } ${ bucketOrFile } .${ shortUUID ( ) } ` ;
296- }
297-
298273async function createTestBenchRetryTest (
299274 instructions : String [ ] ,
300275 methodName : string ,
301- storageTransport : StorageTransport ,
276+ transport : StorageTransport ,
302277) : Promise < ConformanceTestCreationResult > {
303- const requestBody = { instructions : { [ methodName ] : instructions } } ;
304-
305- const requestOptions : StorageRequestOptions = {
278+ return ( await transport . makeRequest ( {
306279 method : 'POST' ,
307280 url : 'retry_test' ,
308- body : JSON . stringify ( requestBody ) ,
281+ body : JSON . stringify ( { instructions : { [ methodName ] : instructions } } ) ,
309282 headers : { 'Content-Type' : 'application/json' } ,
310- timeout : 10000 ,
311- } ;
312-
313- const response = await storageTransport . makeRequest ( requestOptions ) ;
314- return response as unknown as ConformanceTestCreationResult ;
283+ } ) ) as ConformanceTestCreationResult ;
315284}
316285
317286async function getTestBenchRetryTest (
318287 testId : string ,
319- storageTransport : StorageTransport ,
288+ transport : StorageTransport ,
320289) : Promise < ConformanceTestResult > {
321- const requestOptions : StorageRequestOptions = {
290+ return ( await transport . makeRequest ( {
322291 url : `retry_test/${ testId } ` ,
323292 method : 'GET' ,
324- retry : true ,
325- headers : {
326- 'x-retry-test-id' : testId ,
327- } ,
328- } ;
329- const response = await storageTransport . makeRequest ( requestOptions ) ;
330- return response as unknown as ConformanceTestResult ;
293+ headers : { 'x-retry-test-id' : testId } ,
294+ } ) ) as ConformanceTestResult ;
295+ }
296+
297+ function generateName ( method : String , type : string ) {
298+ return `${ TESTS_PREFIX } ${ method . toLowerCase ( ) } ${ type } .${ shortUUID ( ) } ` ;
331299}
332300
333301function shortUUID ( ) {
334- return uuid . v1 ( ) . split ( '-' ) . shift ( ) ;
302+ return uuid . v4 ( ) . split ( '-' ) . shift ( ) ;
335303}
0 commit comments