Skip to content

Commit 773aacc

Browse files
committed
[rush-amazon-s3-build-cache-plugin] add tests to verify 400 and 401 responses are retriable
1 parent b0c5ebf commit 773aacc

File tree

2 files changed

+182
-0
lines changed

2 files changed

+182
-0
lines changed

rush-plugins/rush-amazon-s3-build-cache-plugin/src/test/AmazonS3Client.test.ts

+38
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,44 @@ describe(AmazonS3Client.name, () => {
362362
spy.mockReset();
363363
spy.mockRestore();
364364
});
365+
366+
it('Retries on 400 error', async () => {
367+
const spy = jest.spyOn(global, 'setTimeout');
368+
await runAndExpectErrorAsync(
369+
async () =>
370+
await makeGetRequestAsync(credentials, DUMMY_OPTIONS, 'abc123', {
371+
responseInit: {
372+
status: 400,
373+
statusText: 'Bad Request'
374+
}
375+
})
376+
);
377+
expect(setTimeout).toHaveBeenCalledTimes(3);
378+
expect(setTimeout).toHaveBeenNthCalledWith(1, expect.any(Function), 4000);
379+
expect(setTimeout).toHaveBeenNthCalledWith(2, expect.any(Function), 8000);
380+
expect(setTimeout).toHaveBeenNthCalledWith(3, expect.any(Function), 16000);
381+
spy.mockReset();
382+
spy.mockRestore();
383+
});
384+
385+
it('Retries on 401 error', async () => {
386+
const spy = jest.spyOn(global, 'setTimeout');
387+
await runAndExpectErrorAsync(
388+
async () =>
389+
await makeGetRequestAsync(credentials, DUMMY_OPTIONS, 'abc123', {
390+
responseInit: {
391+
status: 401,
392+
statusText: 'Unauthorized'
393+
}
394+
})
395+
);
396+
expect(setTimeout).toHaveBeenCalledTimes(3);
397+
expect(setTimeout).toHaveBeenNthCalledWith(1, expect.any(Function), 4000);
398+
expect(setTimeout).toHaveBeenNthCalledWith(2, expect.any(Function), 8000);
399+
expect(setTimeout).toHaveBeenNthCalledWith(3, expect.any(Function), 16000);
400+
spy.mockReset();
401+
spy.mockRestore();
402+
});
365403
}
366404

367405
describe('Without credentials', () => {

rush-plugins/rush-amazon-s3-build-cache-plugin/src/test/__snapshots__/AmazonS3Client.test.ts.snap

+144
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,54 @@ Array [
116116

117117
exports[`AmazonS3Client Making requests Getting an object With credentials Handles an unexpected error 2`] = `[Error: Amazon S3 responded with status code 500 (Server Error)]`;
118118

119+
exports[`AmazonS3Client Making requests Getting an object With credentials Retries on 400 error 1`] = `
120+
Array [
121+
"http://localhost:9000/abc123",
122+
Object {
123+
"headers": Headers {
124+
Symbol(map): Object {
125+
"Authorization": Array [
126+
"AWS4-HMAC-SHA256 Credential=accessKeyId/20200418/us-east-1/s3/aws4_request,SignedHeaders=host;x-amz-content-sha256;x-amz-date,Signature=194608e9e7ba6d8aa4a019b3b6fd237e6b09ef1f45ff7fa60cbb81c1875538be",
127+
],
128+
"x-amz-content-sha256": Array [
129+
"e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
130+
],
131+
"x-amz-date": Array [
132+
"20200418T123242Z",
133+
],
134+
},
135+
},
136+
"verb": "GET",
137+
},
138+
]
139+
`;
140+
141+
exports[`AmazonS3Client Making requests Getting an object With credentials Retries on 400 error 2`] = `[Error: Amazon S3 responded with status code 400 (Bad Request)]`;
142+
143+
exports[`AmazonS3Client Making requests Getting an object With credentials Retries on 401 error 1`] = `
144+
Array [
145+
"http://localhost:9000/abc123",
146+
Object {
147+
"headers": Headers {
148+
Symbol(map): Object {
149+
"Authorization": Array [
150+
"AWS4-HMAC-SHA256 Credential=accessKeyId/20200418/us-east-1/s3/aws4_request,SignedHeaders=host;x-amz-content-sha256;x-amz-date,Signature=194608e9e7ba6d8aa4a019b3b6fd237e6b09ef1f45ff7fa60cbb81c1875538be",
151+
],
152+
"x-amz-content-sha256": Array [
153+
"e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
154+
],
155+
"x-amz-date": Array [
156+
"20200418T123242Z",
157+
],
158+
},
159+
},
160+
"verb": "GET",
161+
},
162+
]
163+
`;
164+
165+
exports[`AmazonS3Client Making requests Getting an object With credentials Retries on 401 error 2`] = `[Error: Amazon S3 responded with status code 401 (Unauthorized)]`;
166+
119167
exports[`AmazonS3Client Making requests Getting an object With credentials including a session token Can get an object 1`] = `
120168
Array [
121169
"http://localhost:9000/abc123",
@@ -245,6 +293,60 @@ Array [
245293

246294
exports[`AmazonS3Client Making requests Getting an object With credentials including a session token Handles an unexpected error 2`] = `[Error: Amazon S3 responded with status code 500 (Server Error)]`;
247295

296+
exports[`AmazonS3Client Making requests Getting an object With credentials including a session token Retries on 400 error 1`] = `
297+
Array [
298+
"http://localhost:9000/abc123",
299+
Object {
300+
"headers": Headers {
301+
Symbol(map): Object {
302+
"Authorization": Array [
303+
"AWS4-HMAC-SHA256 Credential=accessKeyId/20200418/us-east-1/s3/aws4_request,SignedHeaders=host;x-amz-content-sha256;x-amz-date;x-amz-security-token,Signature=2a4b8f28b1bdd37af6bb0fd79212c223e2c28e4f94bfb5d1c94a16bb056d5624",
304+
],
305+
"X-Amz-Security-Token": Array [
306+
"sessionToken",
307+
],
308+
"x-amz-content-sha256": Array [
309+
"e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
310+
],
311+
"x-amz-date": Array [
312+
"20200418T123242Z",
313+
],
314+
},
315+
},
316+
"verb": "GET",
317+
},
318+
]
319+
`;
320+
321+
exports[`AmazonS3Client Making requests Getting an object With credentials including a session token Retries on 400 error 2`] = `[Error: Amazon S3 responded with status code 400 (Bad Request)]`;
322+
323+
exports[`AmazonS3Client Making requests Getting an object With credentials including a session token Retries on 401 error 1`] = `
324+
Array [
325+
"http://localhost:9000/abc123",
326+
Object {
327+
"headers": Headers {
328+
Symbol(map): Object {
329+
"Authorization": Array [
330+
"AWS4-HMAC-SHA256 Credential=accessKeyId/20200418/us-east-1/s3/aws4_request,SignedHeaders=host;x-amz-content-sha256;x-amz-date;x-amz-security-token,Signature=2a4b8f28b1bdd37af6bb0fd79212c223e2c28e4f94bfb5d1c94a16bb056d5624",
331+
],
332+
"X-Amz-Security-Token": Array [
333+
"sessionToken",
334+
],
335+
"x-amz-content-sha256": Array [
336+
"e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
337+
],
338+
"x-amz-date": Array [
339+
"20200418T123242Z",
340+
],
341+
},
342+
},
343+
"verb": "GET",
344+
},
345+
]
346+
`;
347+
348+
exports[`AmazonS3Client Making requests Getting an object With credentials including a session token Retries on 401 error 2`] = `[Error: Amazon S3 responded with status code 401 (Unauthorized)]`;
349+
248350
exports[`AmazonS3Client Making requests Getting an object Without credentials Can get an object 1`] = `
249351
Array [
250352
"http://localhost:9000/abc123",
@@ -342,6 +444,48 @@ Array [
342444
]
343445
`;
344446

447+
exports[`AmazonS3Client Making requests Getting an object Without credentials Retries on 400 error 1`] = `
448+
Array [
449+
"http://localhost:9000/abc123",
450+
Object {
451+
"headers": Headers {
452+
Symbol(map): Object {
453+
"x-amz-content-sha256": Array [
454+
"e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
455+
],
456+
"x-amz-date": Array [
457+
"20200418T123242Z",
458+
],
459+
},
460+
},
461+
"verb": "GET",
462+
},
463+
]
464+
`;
465+
466+
exports[`AmazonS3Client Making requests Getting an object Without credentials Retries on 400 error 2`] = `[Error: Amazon S3 responded with status code 400 (Bad Request)]`;
467+
468+
exports[`AmazonS3Client Making requests Getting an object Without credentials Retries on 401 error 1`] = `
469+
Array [
470+
"http://localhost:9000/abc123",
471+
Object {
472+
"headers": Headers {
473+
Symbol(map): Object {
474+
"x-amz-content-sha256": Array [
475+
"e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
476+
],
477+
"x-amz-date": Array [
478+
"20200418T123242Z",
479+
],
480+
},
481+
},
482+
"verb": "GET",
483+
},
484+
]
485+
`;
486+
487+
exports[`AmazonS3Client Making requests Getting an object Without credentials Retries on 401 error 2`] = `[Error: Amazon S3 responded with status code 401 (Unauthorized)]`;
488+
345489
exports[`AmazonS3Client Making requests Uploading an object Throws an error if credentials are not provided 1`] = `[TypeError: Cannot read properties of undefined (reading 'body')]`;
346490

347491
exports[`AmazonS3Client Making requests Uploading an object With credentials Handles an unexpected error code 1`] = `

0 commit comments

Comments
 (0)