1515 */
1616import * as jsonToNodeApiMapping from './test-data/retryInvocationMap.json' ;
1717import * as libraryMethods from './libraryMethods' ;
18- import { Bucket , File , HmacKey , Notification , Storage } from '../src/' ;
18+ import {
19+ Bucket ,
20+ File ,
21+ GaxiosOptions ,
22+ HmacKey ,
23+ Notification ,
24+ Storage ,
25+ } from '../src' ;
1926import * as uuid from 'uuid' ;
2027import * as assert from 'assert' ;
21- import { DecorateRequestOptions } from '../src/nodejs-common' ;
22- import fetch from 'node-fetch' ;
23-
28+ import {
29+ StorageRequestOptions ,
30+ StorageTransport ,
31+ } from '../src/storage-transport' ;
2432interface RetryCase {
2533 instructions : String [ ] ;
2634}
@@ -50,7 +58,7 @@ interface ConformanceTestResult {
5058
5159type LibraryMethodsModuleType = typeof import ( './libraryMethods' ) ;
5260const methodMap : Map < String , String [ ] > = new Map (
53- Object . entries ( jsonToNodeApiMapping ) ,
61+ Object . entries ( { } ) , // TODO: replace with Object.entries(jsonToNodeApiMapping)
5462) ;
5563
5664const DURATION_SECONDS = 600 ; // 10 mins.
@@ -82,19 +90,43 @@ export function executeScenario(testCase: RetryTestCase) {
8290 let creationResult : { id : string } ;
8391 let storage : Storage ;
8492 let hmacKey : HmacKey ;
93+ let storageTransport : StorageTransport ;
8594
8695 describe ( `${ storageMethodString } ` , async ( ) => {
8796 beforeEach ( async ( ) => {
97+ storageTransport = new StorageTransport ( {
98+ apiEndpoint : TESTBENCH_HOST ,
99+ authClient : undefined ,
100+ baseUrl : TESTBENCH_HOST ,
101+ packageJson : { name : 'test-package' , version : '1.0.0' } ,
102+ retryOptions : {
103+ retryDelayMultiplier : RETRY_MULTIPLIER_FOR_CONFORMANCE_TESTS ,
104+ maxRetries : 3 ,
105+ maxRetryDelay : 32 ,
106+ totalTimeout : TIMEOUT_FOR_INDIVIDUAL_TEST ,
107+ } ,
108+ scopes : [
109+ 'http://www.googleapis.com/auth/devstorage.full_control' ,
110+ ] ,
111+ projectId : CONF_TEST_PROJECT_ID ,
112+ userAgent : 'retry-test' ,
113+ useAuthWithCustomEndpoint : true ,
114+ customEndpoint : true ,
115+ timeout : DURATION_SECONDS ,
116+ } ) ;
117+
88118 storage = new Storage ( {
89119 apiEndpoint : TESTBENCH_HOST ,
90120 projectId : CONF_TEST_PROJECT_ID ,
91121 retryOptions : {
92122 retryDelayMultiplier : RETRY_MULTIPLIER_FOR_CONFORMANCE_TESTS ,
93123 } ,
94124 } ) ;
125+
95126 creationResult = await createTestBenchRetryTest (
96127 instructionSet . instructions ,
97128 jsonMethod ?. name . toString ( ) ,
129+ storageTransport ,
98130 ) ;
99131 if ( storageMethodString . includes ( 'InstancePrecondition' ) ) {
100132 bucket = await createBucketForTest (
@@ -119,42 +151,51 @@ export function executeScenario(testCase: RetryTestCase) {
119151 bucket ,
120152 ) ;
121153 }
122- notification = bucket . notification ( ` ${ TESTS_PREFIX } ` ) ;
154+ notification = bucket . notification ( TESTS_PREFIX ) ;
123155 await notification . create ( ) ;
124156
125157 [ hmacKey ] = await storage . createHmacKey (
126158 `${ TESTS_PREFIX } @email.com` ,
127159 ) ;
128160
129161 storage . interceptors . push ( {
130- request : requestConfig => {
162+ resolved : requestConfig => {
131163 requestConfig . headers = requestConfig . headers || { } ;
132164 Object . assign ( requestConfig . headers , {
133165 'x-retry-test-id' : creationResult . id ,
134166 } ) ;
135- return requestConfig as DecorateRequestOptions ;
167+ return Promise . resolve ( requestConfig as GaxiosOptions ) ;
168+ } ,
169+ rejected : error => {
170+ return Promise . reject ( error ) ;
136171 } ,
137172 } ) ;
138173 } ) ;
139174
140175 it ( `${ instructionNumber } ` , async ( ) => {
141176 const methodParameters : libraryMethods . ConformanceTestOptions = {
177+ storage : storage ,
142178 bucket : bucket ,
143179 file : file ,
180+ storageTransport : storageTransport ,
144181 notification : notification ,
145- storage : storage ,
146182 hmacKey : hmacKey ,
147183 } ;
148184 if ( testCase . preconditionProvided ) {
149185 methodParameters . preconditionRequired = true ;
150186 }
187+
151188 if ( testCase . expectSuccess ) {
152189 assert . ifError ( await storageMethodObject ( methodParameters ) ) ;
153190 } else {
154- await assert . rejects ( storageMethodObject ( methodParameters ) ) ;
191+ await assert . rejects ( async ( ) => {
192+ await storageMethodObject ( methodParameters ) ;
193+ } , undefined ) ;
155194 }
195+
156196 const testBenchResult = await getTestBenchRetryTest (
157197 creationResult . id ,
198+ storageTransport ,
158199 ) ;
159200 assert . strictEqual ( testBenchResult . completed , true ) ;
160201 } ) . timeout ( TIMEOUT_FOR_INDIVIDUAL_TEST ) ;
@@ -210,24 +251,34 @@ function generateName(storageMethodString: String, bucketOrFile: string) {
210251async function createTestBenchRetryTest (
211252 instructions : String [ ] ,
212253 methodName : string ,
254+ storageTransport : StorageTransport ,
213255) : Promise < ConformanceTestCreationResult > {
214256 const requestBody = { instructions : { [ methodName ] : instructions } } ;
215- const response = await fetch ( `${ TESTBENCH_HOST } retry_test` , {
257+
258+ const requestOptions : StorageRequestOptions = {
216259 method : 'POST' ,
260+ url : 'retry_test' ,
217261 body : JSON . stringify ( requestBody ) ,
218262 headers : { 'Content-Type' : 'application/json' } ,
219- } ) ;
220- return response . json ( ) as Promise < ConformanceTestCreationResult > ;
263+ } ;
264+
265+ const response = await storageTransport . makeRequest ( requestOptions ) ;
266+ return response as unknown as ConformanceTestCreationResult ;
221267}
222268
223269async function getTestBenchRetryTest (
224270 testId : string ,
271+ storageTransport : StorageTransport ,
225272) : Promise < ConformanceTestResult > {
226- const response = await fetch ( `${ TESTBENCH_HOST } retry_test/${ testId } ` , {
273+ const response = await storageTransport . makeRequest ( {
274+ url : `retry_test/${ testId } ` ,
227275 method : 'GET' ,
276+ retry : true ,
277+ headers : {
278+ 'x-retry-test-id' : testId ,
279+ } ,
228280 } ) ;
229-
230- return response . json ( ) as Promise < ConformanceTestResult > ;
281+ return response as unknown as ConformanceTestResult ;
231282}
232283
233284function shortUUID ( ) {
0 commit comments