|
| 1 | +import nock from "nock"; |
| 2 | +import { IsomorphicFetchHttpLibrary } from "../../packages/datadog-api-client-common/http/isomorphic-fetch"; |
| 3 | +import { |
| 4 | + RequestContext, |
| 5 | + HttpMethod, |
| 6 | + } from "../../packages/datadog-api-client-common/"; |
| 7 | + |
| 8 | + |
| 9 | +describe("IsomorphicFetchHttpLibrary Retry Test", () => { |
| 10 | + |
| 11 | + const fakeRequestContext = new RequestContext("https://retry.test.com",HttpMethod.GET); |
| 12 | + |
| 13 | + beforeAll(() => { |
| 14 | + nock.disableNetConnect(); // Prevent actual network requests |
| 15 | + }); |
| 16 | + |
| 17 | + afterAll(() => { |
| 18 | + nock.cleanAll(); |
| 19 | + nock.enableNetConnect(); // Re-enable network connections |
| 20 | + }); |
| 21 | + |
| 22 | + it("should retry based on response code and retry settings", async () => { |
| 23 | + // Set up your mock responses |
| 24 | + nock("https://retry.test.com") |
| 25 | + .get("/") |
| 26 | + .reply(429, {}, { 'x-ratelimit-reset': '1' }) |
| 27 | + |
| 28 | + .get("/") |
| 29 | + .reply(429, {}, { 'x-ratelimit-reset': '1' }) |
| 30 | + |
| 31 | + .get("/") |
| 32 | + .reply(429, {}, { 'x-ratelimit-reset': '1' }) |
| 33 | + |
| 34 | + .get("/") |
| 35 | + .reply(200, { data: "success" }); // Simulate successful response |
| 36 | + |
| 37 | + const httpLibrary = new IsomorphicFetchHttpLibrary(); |
| 38 | + httpLibrary['sleep'] = jest.fn(() => Promise.resolve()); |
| 39 | + |
| 40 | + httpLibrary.enableRetry = true; |
| 41 | + httpLibrary.maxRetries = 3; |
| 42 | + httpLibrary.backoffBase = 2; |
| 43 | + httpLibrary.backoffMultiplier = 2; |
| 44 | + |
| 45 | + const response = await httpLibrary.send(fakeRequestContext); |
| 46 | + |
| 47 | + expect(response.httpStatusCode).toBe(200); |
| 48 | + |
| 49 | + expect(nock.isDone()).toBe(true); // Ensure all expected requests were made |
| 50 | + }); |
| 51 | +}); |
0 commit comments